From 8847a025b334ea0d9814215291e8f546dd5f1e7e Mon Sep 17 00:00:00 2001 From: Ludovic Pouzenc Date: Sun, 14 Jun 2015 22:30:48 +0200 Subject: Various minor improovements * Log full stacktraces on exceptions * Compute correctly raid_size * Do all needed sanity checks on raid_disk_order --- myraid.py | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) (limited to 'myraid.py') diff --git a/myraid.py b/myraid.py index 60e8b28..785b514 100644 --- a/myraid.py +++ b/myraid.py @@ -26,17 +26,23 @@ class MyRaid(): def __init__(self, *args, **kwargs): self.raid_start = 0 self.raid_end = 0 - self.raid_size = 0 self.raid_sector_size = 512 self.raid_chunk_size = 65536 - self.raid_disk_order = range(15) + self.raid_disk_order = [] + self.raid_disk_count = 0 self.raid_types = [ '0', '1', '5', '5+0' ] - def _update_raid_size(self): - if self.raid_end > self.raid_start: - self.raid_size = self.raid_end - self.raid_start + def get_raid_size(self, raid_type): + size = self.raid_end - self.raid_start + if size <= 0: + return 0 else: - self.raid_size = 0 + return { + '0': size * self.raid_disk_count, + '1': size, + '5': size * (self.raid_disk_count - 1) if self.raid_disk_count >= 3 else 0, + '5+0': size * (self.raid_disk_count - 2) if self.raid_disk_count >= 6 and self.raid_disk_count % 2 == 0 else 0 , + }[raid_type] def get_raid_start(self): return self.raid_start @@ -53,12 +59,10 @@ class MyRaid(): def set_raid_start(self, new_raid_start): """Update the start offset of raid data on underlying disks""" self.raid_start = new_raid_start - self._update_raid_size() def set_raid_end(self, new_raid_end): """Update the end offset of raid data on underlying disks""" self.raid_end = new_raid_end - self._update_raid_size() def set_raid_chunk_size(self, new_raid_chunk_size): """Update the size of chucks of data (or slice size)""" @@ -66,7 +70,19 @@ class MyRaid(): def set_raid_disk_order(self, new_raid_disk_order): """Update the raid logical disk order""" + card=len(new_raid_disk_order) + check=[0]*card + for item in new_raid_disk_order: + d = int(item) + if not 0 <= d < card: + raise ValueError('Value out of range : %i [0,%i]'%(d,card-1)) + check[d]=check[d]+1 + + for d in range(card): + if check[d]!=1: + raise ValueError('Disk %i appears %i times (must be 1)'%(d,check[d])) self.raid_disk_order = new_raid_disk_order + self.raid_disk_count = len(new_raid_disk_order) def read_data(self,raid_type,disks,offset,size): -- cgit v1.2.3