Commit 5b2d787a authored by Grégory Wisniewski's avatar Grégory Wisniewski

Define is<State> methods on Cell class and use them.


git-svn-id: https://svn.erp5.org/repos/neo/trunk@1330 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent bc534916
...@@ -17,8 +17,7 @@ ...@@ -17,8 +17,7 @@
import neo.pt import neo.pt
from struct import pack, unpack from struct import pack, unpack
from neo.protocol import OUT_OF_DATE_STATE, FEEDING_STATE, \ from neo.protocol import OUT_OF_DATE_STATE, FEEDING_STATE, DISCARDED_STATE
DISCARDED_STATE, RUNNING_STATE, BROKEN_STATE
class PartitionTable(neo.pt.PartitionTable): class PartitionTable(neo.pt.PartitionTable):
"""This class manages a partition table for the primary master node""" """This class manages a partition table for the primary master node"""
...@@ -83,7 +82,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -83,7 +82,7 @@ class PartitionTable(neo.pt.PartitionTable):
if row is not None: if row is not None:
for cell in row: for cell in row:
if cell.getNode() is node: if cell.getNode() is node:
if cell.getState() != FEEDING_STATE: if not cell.isFeeding():
# If this cell is not feeding, find another node # If this cell is not feeding, find another node
# to be added. # to be added.
node_list = [c.getNode() for c in row] node_list = [c.getNode() for c in row]
...@@ -122,7 +121,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -122,7 +121,7 @@ class PartitionTable(neo.pt.PartitionTable):
if cell.getNode() == node: if cell.getNode() == node:
skip = True skip = True
break break
if cell.getState() == FEEDING_STATE: if cell.isFeeding():
feeding_cell = cell feeding_cell = cell
else: else:
num_cells += 1 num_cells += 1
...@@ -140,8 +139,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -140,8 +139,7 @@ class PartitionTable(neo.pt.PartitionTable):
node_count += 1 node_count += 1
else: else:
if max_count - node_count > 1: if max_count - node_count > 1:
if feeding_cell is not None \ if feeding_cell is not None or max_cell.isOutOfDate():
or max_cell.getState() == OUT_OF_DATE_STATE:
# If there is a feeding cell already or it is # If there is a feeding cell already or it is
# out-of-date, just drop the node. # out-of-date, just drop the node.
row.remove(max_cell) row.remove(max_cell)
...@@ -175,16 +173,16 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -175,16 +173,16 @@ class PartitionTable(neo.pt.PartitionTable):
out_of_date_cell_list = [] out_of_date_cell_list = []
up_to_date_cell_list = [] up_to_date_cell_list = []
for cell in row: for cell in row:
if cell.getNodeState() == BROKEN_STATE: if cell.getNode().isBroken():
# Remove a broken cell. # Remove a broken cell.
removed_cell_list.append(cell) removed_cell_list.append(cell)
elif cell.getState() == FEEDING_STATE: elif cell.isFeeding():
if feeding_cell is None: if feeding_cell is None:
feeding_cell = cell feeding_cell = cell
else: else:
# Remove an excessive feeding cell. # Remove an excessive feeding cell.
removed_cell_list.append(cell) removed_cell_list.append(cell)
elif cell.getState() == OUT_OF_DATE_STATE: elif cell.isOutOfDate():
out_of_date_cell_list.append(cell) out_of_date_cell_list.append(cell)
else: else:
up_to_date_cell_list.append(cell) up_to_date_cell_list.append(cell)
...@@ -220,7 +218,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -220,7 +218,7 @@ class PartitionTable(neo.pt.PartitionTable):
# Now remove cells really. # Now remove cells really.
for cell in removed_cell_list: for cell in removed_cell_list:
row.remove(cell) row.remove(cell)
if cell.getState() != FEEDING_STATE: if not cell.isFeeding():
self.count_dict[cell.getNode()] -= 1 self.count_dict[cell.getNode()] -= 1
changed_cell_list.append((offset, cell.getUUID(), DISCARDED_STATE)) changed_cell_list.append((offset, cell.getUUID(), DISCARDED_STATE))
...@@ -228,7 +226,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -228,7 +226,7 @@ class PartitionTable(neo.pt.PartitionTable):
for offset, row in enumerate(self.partition_list): for offset, row in enumerate(self.partition_list):
num_cells = 0 num_cells = 0
for cell in row: for cell in row:
if cell.getState() != FEEDING_STATE: if not cell.isFeeding():
num_cells += 1 num_cells += 1
while num_cells <= self.nr: while num_cells <= self.nr:
node = self.findLeastUsedNode([cell.getNode() for cell in row]) node = self.findLeastUsedNode([cell.getNode() for cell in row])
...@@ -251,8 +249,7 @@ class PartitionTable(neo.pt.PartitionTable): ...@@ -251,8 +249,7 @@ class PartitionTable(neo.pt.PartitionTable):
cell_list = [] cell_list = []
for offset, row in enumerate(self.partition_list): for offset, row in enumerate(self.partition_list):
for cell in row: for cell in row:
if cell.getNodeState() != RUNNING_STATE \ if not cell.getNode().isRunning() and not cell.isOutOfDate():
and cell.getState() != OUT_OF_DATE_STATE:
cell.setState(OUT_OF_DATE_STATE) cell.setState(OUT_OF_DATE_STATE)
cell_list.append((offset, cell.getUUID(), OUT_OF_DATE_STATE)) cell_list.append((offset, cell.getUUID(), OUT_OF_DATE_STATE))
return cell_list return cell_list
......
...@@ -35,6 +35,15 @@ class Cell(object): ...@@ -35,6 +35,15 @@ class Cell(object):
def setState(self, state): def setState(self, state):
self.state = state self.state = state
def isUpToDate(self):
return self.state == protocol.UP_TO_DATE_STATE
def isOutOfDate(self):
return self.state == protocol.OUT_OF_DATE_STATE
def isFeeding(self):
return self.state == protocol.FEEDING_STATE
def getNode(self): def getNode(self):
return self.node return self.node
...@@ -145,7 +154,7 @@ class PartitionTable(object): ...@@ -145,7 +154,7 @@ class PartitionTable(object):
for cell in row: for cell in row:
if cell.getNode() == node: if cell.getNode() == node:
row.remove(cell) row.remove(cell)
if cell.getState() != protocol.FEEDING_STATE: if not cell.isFeeding():
self.count_dict[node] -= 1 self.count_dict[node] -= 1
break break
row.append(Cell(node, state)) row.append(Cell(node, state))
...@@ -158,7 +167,7 @@ class PartitionTable(object): ...@@ -158,7 +167,7 @@ class PartitionTable(object):
for cell in row: for cell in row:
if cell.getNode() == node: if cell.getNode() == node:
row.remove(cell) row.remove(cell)
if cell.getState() != protocol.FEEDING_STATE: if not cell.isFeeding():
self.count_dict[node] -= 1 self.count_dict[node] -= 1
break break
...@@ -255,9 +264,8 @@ class PartitionTable(object): ...@@ -255,9 +264,8 @@ class PartitionTable(object):
return False return False
for row in self.partition_list: for row in self.partition_list:
for cell in row: for cell in row:
if cell.getState() in (protocol.UP_TO_DATE_STATE, \ if (cell.isUpToDate() or cell.isFeeding()) and \
protocol.FEEDING_STATE) \ cell.getNode().isRunning():
and cell.getNodeState() == protocol.RUNNING_STATE:
break break
else: else:
return False return False
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment