From 52ffac04f162f9870d9db242554024ed0186c764 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Thu, 24 Aug 2023 11:48:41 +0200 Subject: [PATCH] rbd: pass a max-depth to `getCloneDepth()` The `getCloneDepth()` function does not need to traverse the whole chain of parents when a certain max-limit is configured. The traversing can be aborted once the hard-limit is reached. This makes the procedure a little more efficient, as unnecessary traversing is prevented. Signed-off-by: Niels de Vos --- internal/rbd/nodeserver.go | 2 +- internal/rbd/rbd_util.go | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/rbd/nodeserver.go b/internal/rbd/nodeserver.go index d5b46ad31882..591bcf93c696 100644 --- a/internal/rbd/nodeserver.go +++ b/internal/rbd/nodeserver.go @@ -598,7 +598,7 @@ func flattenImageBeforeMapping( if err != nil { return err } - depth, err = volOptions.getCloneDepth() + depth, err = volOptions.getCloneDepth(rbdHardMaxCloneDepth) if err != nil { return err } diff --git a/internal/rbd/rbd_util.go b/internal/rbd/rbd_util.go index 2076aace78f9..344aefebdb0b 100644 --- a/internal/rbd/rbd_util.go +++ b/internal/rbd/rbd_util.go @@ -698,11 +698,13 @@ func (ri *rbdImage) trashRemoveImage(ctx context.Context) error { } // getCloneDepth walks the parents of the image and returns the number of -// images in the chain. +// images in the chain. The `max` argument to the function is used to specify a +// limit of the depth to check. When `max` depth is reached, no further +// traversing of the depth is required. // // This function re-uses the ioctx of the image to open all images in the // chain. There is no need to open new ioctx's for every image. -func (ri *rbdImage) getCloneDepth() (uint, error) { +func (ri *rbdImage) getCloneDepth(max uint) (uint, error) { var ( depth uint parent librbd.ImageSpec @@ -733,6 +735,13 @@ func (ri *rbdImage) getCloneDepth() (uint, error) { // if there is a parent, count it to the depth depth++ + // if max (usually hard-limit) is reached, further traversing + // parents is not needed, some action (flattening) will be + // triggered regardless of deeper depth + if depth == max { + break + } + // open the parent image, so that the for-loop can continue // with checking for the parent of the parent parent = info.Image @@ -814,7 +823,7 @@ func (ri *rbdImage) flattenRbdImage( // skip clone depth check if request is for force flatten if !forceFlatten { - depth, err = ri.getCloneDepth() + depth, err = ri.getCloneDepth(hardlimit) if err != nil { return err }