From 2e18100d7e0efc8fe516e9314bb91113c14d496e Mon Sep 17 00:00:00 2001 From: Chin-Ya Huang Date: Mon, 9 Sep 2024 10:25:07 +0800 Subject: [PATCH] feat(v2/auto-salvage): construct replica during engine creation longhorn/longhorn-8430 Signed-off-by: Chin-Ya Huang --- pkg/spdk/replica.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/pkg/spdk/replica.go b/pkg/spdk/replica.go index 387f6770..1bc4170b 100644 --- a/pkg/spdk/replica.go +++ b/pkg/spdk/replica.go @@ -629,17 +629,39 @@ func (r *Replica) Create(spdkClient *spdkclient.Client, portCount int32, superio return nil, fmt.Errorf("found mismatching between the actual lvstore name %s with UUID %s and the recorded lvstore name %s with UUID %s during replica %s creation", lvsList[0].Name, lvsList[0].UUID, r.LvsName, r.LvsUUID, r.Name) } - r.log.Info("Creating a lvol bdev for the new replica") - if _, err := spdkClient.BdevLvolCreate("", r.LvsUUID, r.Name, util.BytesToMiB(r.SpecSize), "", true); err != nil { - return nil, err - } bdevLvolList, err := spdkClient.BdevLvolGet(r.Alias, 0) - if err != nil { - return nil, err + if err != nil && !jsonrpc.IsJSONRPCRespErrorNoSuchDevice(err) { + return nil, errors.Wrapf(err, "failed to check existence of lvol bdev for the new replica %v", r.Name) + } + + if len(bdevLvolList) == 0 { + r.log.Info("Creating a lvol bdev for the new replica") + if _, err := spdkClient.BdevLvolCreate("", r.LvsUUID, r.Name, util.BytesToMiB(r.SpecSize), "", true); err != nil { + return nil, err + } + bdevLvolList, err = spdkClient.BdevLvolGet(r.Alias, 0) + if err != nil { + return nil, err + } + } else { + r.log.Info("Skipping to create a lvol bdev for the new replica because it already exists") + + bdevLvolMap, err := GetBdevLvolMap(spdkClient) + if err != nil { + return nil, err + } + + r.log.Infof("Constructing replica %v with existing lvol during creation", r.Name) + err = r.construct(bdevLvolMap) + if err != nil { + return nil, err + } } + if len(bdevLvolList) < 1 { return nil, fmt.Errorf("cannot find lvol %v after creation", r.Alias) } + headSvcLvol.UUID = bdevLvolList[0].UUID headSvcLvol.CreationTime = bdevLvolList[0].CreationTime headSvcLvol.ActualSize = bdevLvolList[0].DriverSpecific.Lvol.NumAllocatedClusters * defaultClusterSize