Skip to content
This repository has been archived by the owner on Apr 15, 2021. It is now read-only.

Commit

Permalink
update partition plan
Browse files Browse the repository at this point in the history
  • Loading branch information
shaowen310 committed Oct 24, 2020
1 parent 5eda813 commit 20a5ebf
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions partitionplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,12 @@

module_logger = logging.getLogger('partitionplan')


class PartitionPlan:
NOALLOC = 0
MASTER = 1
SLAVE = 2

def __init__(self, ualloc, palloc, u2p):
self.logger = logging.getLogger('partitionplan.PartitionPlan')
self.ucap = len(ualloc)
self.pcap = len(palloc)
self.ualloc = ualloc
self.palloc = palloc
self.u2p = u2p

def __init__(self, n_partitions, user_capacity, partition_capacity):
self.logger = logging.getLogger('partitionplan.PartitionPlan')
# Assumption: user_id determines the user's storage location
Expand All @@ -33,7 +26,6 @@ def __init__(self, n_partitions, user_capacity, partition_capacity):
self.u2p = np.full((self.ucap, self.pcap), self.NOALLOC, dtype=np.int8)

def save(self, folder):

if not os.path.exists(folder):
os.makedirs(folder)

Expand All @@ -56,7 +48,7 @@ def load(self, folder):
self.ucap = len(self.ualloc)
self.pcap = len(self.palloc)
except FileNotFoundError:
print('file can not be found')
self.logger.warning('File not found')

def num_masters_per_partition(self):
au2app = self.u2p[np.ix_(self.ualloc, self.palloc)] == self.MASTER
Expand All @@ -82,17 +74,19 @@ def _expand_partition_capacity(self):
def partition_add_master(self, partition_id, user_id):
assert self.palloc[partition_id]
if self.u2p[user_id, partition_id] != self.NOALLOC:
self.logger.info('Add master ignored as user {0} partition {1} is allocated'.
format(user_id, partition_id))
self.logger.info(
'Add master ignored as user {0} partition {1} is allocated'.
format(user_id, partition_id))
return
self.u2p[user_id, partition_id] = self.MASTER
self.ualloc[user_id] = True

def partition_add_slave(self, partition_id, user_id):
assert self.palloc[partition_id]
if self.u2p[user_id, partition_id] != self.NOALLOC:
self.logger.info('Add slave ignored as user {0} partition {1} is allocated'.
format(user_id, partition_id))
self.logger.info(
'Add slave ignored as user {0} partition {1} is allocated'.
format(user_id, partition_id))
return
self.u2p[user_id, partition_id] = self.SLAVE
self.ualloc[user_id] = True
Expand All @@ -108,11 +102,13 @@ def partition_remove_slave(self, partition_id, user_id, k=2):
assert self.palloc[partition_id]
if self.u2p[user_id, partition_id] != self.SLAVE:
self.logger.info(
'Remove slave ignored as user {0} partition {1} is not slave'.format(
user_id, partition_id))
'Remove slave ignored as user {0} partition {1} is not slave'.
format(user_id, partition_id))
return
if self.num_slaves_by_user(user_id) <= k:
self.logger.debug('Remove slave ignored as at least {0} slave should be kept'.format(k))
self.logger.debug(
'Remove slave ignored as at least {0} slave should be kept'.
format(k))
return
self._partition_remove_replica(partition_id, user_id)
self.ualloc[user_id] = not np.alltrue(
Expand All @@ -131,7 +127,8 @@ def partition_ids_not_having_master(self, user_id):
return np.flatnonzero(self.palloc & (self.u2p[user_id] != self.MASTER))

def partition_ids_not_having_replica(self, user_id):
return np.flatnonzero(self.palloc & (self.u2p[user_id] == self.NOALLOC))
return np.flatnonzero(self.palloc
& (self.u2p[user_id] == self.NOALLOC))

def find_partition_having_master(self, user_id):
return np.flatnonzero(self.u2p[user_id] == self.MASTER)[0]
Expand Down

0 comments on commit 20a5ebf

Please sign in to comment.