From 89923d991e84cb6273b5fe6fbc5548c9fbd8a7a8 Mon Sep 17 00:00:00 2001 From: Shuo Wu Date: Tue, 22 Oct 2024 16:27:29 +0800 Subject: [PATCH] refactor: abstract replicaLvolFilter as a func Signed-off-by: Shuo Wu --- pkg/spdk/replica.go | 44 ++++++++++++-------------------------------- 1 file changed, 12 insertions(+), 32 deletions(-) diff --git a/pkg/spdk/replica.go b/pkg/spdk/replica.go index 521e371c..8e8fda0d 100644 --- a/pkg/spdk/replica.go +++ b/pkg/spdk/replica.go @@ -182,20 +182,21 @@ func NewReplica(ctx context.Context, replicaName, lvsName, lvsUUID string, specS } } +func (r *Replica) replicaLvolFilter(bdev *spdktypes.BdevInfo) bool { + var lvolName string + if len(bdev.Aliases) == 1 { + lvolName = spdktypes.GetLvolNameFromAlias(bdev.Aliases[0]) + } + return IsReplicaLvol(r.Name, lvolName) || (len(r.ActiveChain) > 0 && r.ActiveChain[0] != nil && r.ActiveChain[0].Name == lvolName) +} + func (r *Replica) Sync(spdkClient *spdkclient.Client) (err error) { r.Lock() defer r.Unlock() // It's better to let the server send the update signal // This lvol and nvmf subsystem fetch should be protected by replica lock, in case of snapshot operations happened during the sync-up. - replicaLvolFilter := func(bdev *spdktypes.BdevInfo) bool { - var lvolName string - if len(bdev.Aliases) == 1 { - lvolName = spdktypes.GetLvolNameFromAlias(bdev.Aliases[0]) - } - return IsReplicaLvol(r.Name, lvolName) || (r.ActiveChain[0] != nil && r.ActiveChain[0].Name == lvolName) - } - bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, replicaLvolFilter) + bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, r.replicaLvolFilter) if err != nil { return err } @@ -802,14 +803,7 @@ func (r *Replica) Delete(spdkClient *spdkclient.Client, cleanupRequired bool, su // Clean up the valid snapshot tree if len(r.ActiveChain) > 1 { - replicaLvolFilter := func(bdev *spdktypes.BdevInfo) bool { - var lvolName string - if len(bdev.Aliases) == 1 { - lvolName = spdktypes.GetLvolNameFromAlias(bdev.Aliases[0]) - } - return IsReplicaLvol(r.Name, lvolName) || (r.ActiveChain[0] != nil && r.ActiveChain[0].Name == lvolName) - } - bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, replicaLvolFilter) + bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, r.replicaLvolFilter) if err != nil { return err } @@ -1083,14 +1077,7 @@ func (r *Replica) SnapshotRevert(spdkClient *spdkclient.Client, snapshotName str return nil, err } - replicaLvolFilter := func(bdev *spdktypes.BdevInfo) bool { - var lvolName string - if len(bdev.Aliases) == 1 { - lvolName = spdktypes.GetLvolNameFromAlias(bdev.Aliases[0]) - } - return IsReplicaLvol(r.Name, lvolName) || (r.ActiveChain[0] != nil && r.ActiveChain[0].Name == lvolName) - } - bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, replicaLvolFilter) + bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, r.replicaLvolFilter) if err != nil { return nil, err } @@ -1546,14 +1533,7 @@ func (r *Replica) RebuildingDstFinish(spdkClient *spdkclient.Client) (err error) _ = r.doCleanupForRebuildingDst(spdkClient, r.rebuildingDstCache.rebuildingState == types.ProgressStateError) - replicaLvolFilter := func(bdev *spdktypes.BdevInfo) bool { - var lvolName string - if len(bdev.Aliases) == 1 { - lvolName = spdktypes.GetLvolNameFromAlias(bdev.Aliases[0]) - } - return IsReplicaLvol(r.Name, lvolName) || (r.ActiveChain[0] != nil && r.ActiveChain[0].Name == lvolName) - } - bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, replicaLvolFilter) + bdevLvolMap, err := GetBdevLvolMapWithFilter(spdkClient, r.replicaLvolFilter) if err != nil { return err }