Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fail seeing a backup refering to non-existent disk #70

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ydirson
Copy link
Contributor

@ydirson ydirson commented Oct 11, 2023

This fixes the various problems seen as part of #66, and then some.

This is a continuation of #67 which got closed when xs8 was destroyed and cannot be reopened (thx github).

diskutil.py Outdated
@@ -280,6 +280,9 @@ def getDiskDeviceSize(dev):
return int(__readOneLineFile__("/sys/block/%s/device/block/size" % dev))
elif os.path.exists("/sys/block/%s/size" % dev):
return int(__readOneLineFile__("/sys/block/%s/size" % dev))
else:
raise Exception("%s not found as %s or %s" % (dev, "/sys/block/%s/device/block/size",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will end up raising as e.g. sda not found as /sys/block/%s/device/block/size or /sys/block/%s/size, i.e. the %s characters will come out in the exception text, which doesn't seem quite what you'd expect?

If we don't need this for xs8 (i.e. we can rely on py3 functionality), could we use a format string to make it easier, at which point you'd just have e.g. raise Exception(f"{dev} not found as /sys/block/{dev}/device/block/size or /sys/block/{dev}/size")?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are trying to keep Python 2/3 compatibility, or at least have master and release/xs8 branches as similar as possible. Maybe a more generic and easier "Unable to get block size of %s device" % dev would be enough, also not requiring to be updated as new potential virtual files are needed to be checked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving the full paths inside the format string should be good for now. At least using str.format and {0} will allow to avoid repeating dev

product.py Outdated
except StopIteration:
pass
except Exception as ex:
logger.log("findXenSourceBackups caught exception for partition %s: %s" % (p, ex))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably want to use logger.logException(ex) here rather than trying to include the exception directly in the log message, as that will then give us a useful traceback etc, so e.g.:

logger.log("findXenSourceBackups caught exception for partition %s:" % (p,))
logger.logException(ex)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@freddy77
Copy link
Collaborator

freddy77 commented Jun 5, 2024

BTW... got the error too this morning. Pretty boring indeed.

ydirson added 4 commits June 12, 2024 16:39
- it is bad practice to "catch any"
- not logging anything just hides information, which can be especially
  important here as the reason for a try/catch is not obvious (exceptions
  thrown by XenServerBackup.__init__?)

Signed-off-by: Yann Dirson <[email protected]>
Previous code structure was that of 2 nested if's, with nominal code
path being in the positive branch of each if.  Adding another
condition will bring a need for logging the reason to ignore a backup,
but doing that by converting inner `if` to an `elif` chain (arguably
the simplest modification) would bring the nominal code path to switch
to the negative/last branch of the inner `elif` chain.  At the same
time, the outer `if` would not seem special enough to deserve it
special place (and the cyclomatic complexity).

This commit leverages the existing `try:except:` block to switch to an
"error out the loop" pattern, where the nominal code path is just
linear.

Using `StopIteration` may feel like an abuse, but:
- it is the only standard `Exception` that is not a `StandardError` or
  a `Warning`, and defining a new one could seem overkill
- the closest alternative would be a `while True` loop and breaking out
  in both exceptional code paths and at the end of nominal path, where
  the `break` statements would "stop the iteration" as well

Signed-off-by: Yann Dirson <[email protected]>
@ydirson ydirson force-pushed the inexistant-disks branch from 7a99335 to 44624db Compare June 12, 2024 14:41
@ydirson ydirson requested review from alexbrett and freddy77 June 12, 2024 14:41
backups.append(backup)
except:
if not os.path.exists(os.path.join(b.mount_point, '.xen-backup-partition')):
raise StopIteration()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found this raising StopIteration and catching it weird and hard to read. Why not using a try/finally doing the b.umount() in the finally and using continue here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants