osd.py 2.17 KB
Newer Older
1
2
3
import sys


4
class OSD(object):
Felix Seibert's avatar
Felix Seibert committed
5
6
7
8
9
    """
    representation of an Object Storage device. the OSD is identified by its uuid.
    it keeps track of the folders saved on the OSD as well as the size of the folders.
    """

10
    def __init__(self, uuid: str, bandwidth=1, capacity=sys.maxsize):
Felix Seibert's avatar
Felix Seibert committed
11
12
        if not isinstance(uuid, str):
            raise ValueError("OSD uuid must be str!")
13
        self.uuid = uuid
14
15
        self.bandwidth = bandwidth
        self.capacity = capacity
16
        self.total_folder_size = 0
17
18
19
        self.folders = {}

    def add_folder(self, folder_id, folder_size):
20
21
        assert self.total_folder_size + folder_size <= self.capacity

22
23
        if folder_id not in self.folders:
            self.folders[folder_id] = folder_size
24
        else:
25
            self.folders[folder_id] += folder_size
26
        self.total_folder_size += folder_size
27
28
29

    def remove_folder(self, folder):
        if folder in self.folders.keys():
30
            self.total_folder_size -= self.folders[folder]
31
32
33
34
35
36
37
            del self.folders[folder]

    def update_folder(self, folder_id, size):
        assert folder_id in self.folders.keys()
        self.remove_folder(folder_id)
        self.add_folder(folder_id, size)

Felix Seibert's avatar
Felix Seibert committed
38
39
    def contains_folder(self, folder_id):
        return folder_id in self.folders
40

41
42
43
44
45
46
47
48
49
    def get_smallest_folder(self):
        smallest_id = None
        smallest_size = 0
        for id, size in self.folders.items():
            if smallest_id is None or size < smallest_size:
                smallest_id, smallest_size = id, size

        return smallest_id, smallest_size

50
51
52
    def get_load(self):
        return self.total_folder_size

53
54
55
    def get_free_capacity(self):
        return self.capacity - self.total_folder_size

56
57
58
    def get_processing_time(self):
        return self.total_folder_size / self.bandwidth

59
60
61
62
    def get_folder_size(self, folder_id):
        assert folder_id in self.folders
        return self.folders[folder_id]

63
64
    def __str__(self):
        return "osd: '" + self.uuid \
65
               + "' totalFolderSize: " + str(self.total_folder_size) \
66
               + " processing time: " + str(self.get_processing_time()) \
67
               + " number of folders: " + str(len(self.folders))