Commit 3f209f29 authored by Grégory Wisniewski's avatar Grégory Wisniewski

Merge two dict definition from node.py and pt.py, to protocol.py.

Rename partition_cell_states to cell_states to match with other names.
Fix indent and unify imports.


git-svn-id: https://svn.erp5.org/repos/neo/branches/prototype3@1095 71dcc9de-d417-0410-9af5-da40c76e7ee4
parent fbbb07ea
...@@ -247,22 +247,10 @@ class NodeManager(object): ...@@ -247,22 +247,10 @@ class NodeManager(object):
logging.info('create node %s %s %s %s' % log_args) logging.info('create node %s %s %s %s' % log_args)
def log(self): def log(self):
node_state_dict = { RUNNING_STATE: 'R',
TEMPORARILY_DOWN_STATE: 'T',
DOWN_STATE: 'D',
BROKEN_STATE: 'B',
HIDDEN_STATE: 'H',
PENDING_STATE: 'P'}
node_type_dict = {
MASTER_NODE_TYPE: 'M',
STORAGE_NODE_TYPE: 'S',
CLIENT_NODE_TYPE: 'C',
ADMIN_NODE_TYPE: 'A',
}
for uuid, node in sorted(self.uuid_dict.items()): for uuid, node in sorted(self.uuid_dict.items()):
args = ( args = (
dump(uuid), dump(uuid),
node_type_dict[node.getType()], protocol.node_type_prefix_dict[node.getType()],
node_state_dict[node.getState()] protocol.node_state_prefix_dict[node.getState()]
) )
logging.debug('nm: %s : %s/%s' % args) logging.debug('nm: %s : %s/%s' % args)
...@@ -322,30 +322,48 @@ cluster_states = Enum({ ...@@ -322,30 +322,48 @@ cluster_states = Enum({
# Node types. # Node types.
node_types = Enum({ node_types = Enum({
'MASTER_NODE_TYPE' : 1, 'MASTER_NODE_TYPE' : 1,
'STORAGE_NODE_TYPE' : 2, 'STORAGE_NODE_TYPE' : 2,
'CLIENT_NODE_TYPE' : 3, 'CLIENT_NODE_TYPE' : 3,
'ADMIN_NODE_TYPE' : 4, 'ADMIN_NODE_TYPE' : 4,
}) })
# Node states. # Node states.
node_states = Enum({ node_states = Enum({
'RUNNING_STATE': 0, 'RUNNING_STATE': 0,
'TEMPORARILY_DOWN_STATE': 1, 'TEMPORARILY_DOWN_STATE': 1,
'DOWN_STATE': 2, 'DOWN_STATE': 2,
'BROKEN_STATE': 3, 'BROKEN_STATE': 3,
'HIDDEN_STATE' : 4, 'HIDDEN_STATE' : 4,
'PENDING_STATE': 5, 'PENDING_STATE': 5,
}) })
# used for logging
node_state_prefix_dict = {
RUNNING_STATE: 'R',
TEMPORARILY_DOWN_STATE: 'T',
DOWN_STATE: 'D',
BROKEN_STATE: 'B',
HIDDEN_STATE: 'H',
PENDING_STATE: 'P',
}
# Partition cell states. # Partition cell states.
partition_cell_states = Enum({ cell_states = Enum({
'UP_TO_DATE_STATE': 0, 'UP_TO_DATE_STATE': 0,
'OUT_OF_DATE_STATE': 1, 'OUT_OF_DATE_STATE': 1,
'FEEDING_STATE': 2, 'FEEDING_STATE': 2,
'DISCARDED_STATE': 3, 'DISCARDED_STATE': 3,
}) })
# used for logging
cell_state_prefix_dict = {
UP_TO_DATE_STATE: 'U',
OUT_OF_DATE_STATE: 'O',
FEEDING_STATE: 'F',
DISCARDED_STATE: 'D',
}
# Other constants. # Other constants.
INVALID_UUID = '\0' * 16 INVALID_UUID = '\0' * 16
INVALID_TID = '\0' * 8 INVALID_TID = '\0' * 8
...@@ -657,7 +675,7 @@ def _decodeAnswerPartitionTable(body): ...@@ -657,7 +675,7 @@ def _decodeAnswerPartitionTable(body):
for j in xrange(m): for j in xrange(m):
uuid, state = unpack('!16sH', body[index:index+18]) uuid, state = unpack('!16sH', body[index:index+18])
index += 18 index += 18
state = partition_cell_states.get(state) state = cell_states.get(state)
uuid = _decodeUUID(uuid) uuid = _decodeUUID(uuid)
cell_list.append((uuid, state)) cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list))) row_list.append((offset, tuple(cell_list)))
...@@ -678,7 +696,7 @@ def _decodeSendPartitionTable(body): ...@@ -678,7 +696,7 @@ def _decodeSendPartitionTable(body):
for j in xrange(m): for j in xrange(m):
uuid, state = unpack('!16sH', body[index:index+18]) uuid, state = unpack('!16sH', body[index:index+18])
index += 18 index += 18
state = partition_cell_states.get(state) state = cell_states.get(state)
uuid = _decodeUUID(uuid) uuid = _decodeUUID(uuid)
cell_list.append((uuid, state)) cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list))) row_list.append((offset, tuple(cell_list)))
...@@ -693,7 +711,7 @@ def _decodeNotifyPartitionChanges(body): ...@@ -693,7 +711,7 @@ def _decodeNotifyPartitionChanges(body):
cell_list = [] cell_list = []
for i in xrange(n): for i in xrange(n):
(offset, uuid, state) = unpack('!L16sH', body[12+i*22:34+i*22]) (offset, uuid, state) = unpack('!L16sH', body[12+i*22:34+i*22])
state = partition_cell_states.get(state) state = cell_states.get(state)
uuid = _decodeUUID(uuid) uuid = _decodeUUID(uuid)
cell_list.append((offset, uuid, state)) cell_list.append((offset, uuid, state))
return ptid, cell_list return ptid, cell_list
...@@ -976,7 +994,7 @@ def _decodeAnswerPartitionList(body): ...@@ -976,7 +994,7 @@ def _decodeAnswerPartitionList(body):
for j in xrange(m): for j in xrange(m):
uuid, state = unpack('!16sH', body[index:index+18]) uuid, state = unpack('!16sH', body[index:index+18])
index += 18 index += 18
state = partition_cell_states.get(state) state = cell_states.get(state)
uuid = _decodeUUID(uuid) uuid = _decodeUUID(uuid)
cell_list.append((uuid, state)) cell_list.append((uuid, state))
row_list.append((offset, tuple(cell_list))) row_list.append((offset, tuple(cell_list)))
......
...@@ -18,9 +18,6 @@ ...@@ -18,9 +18,6 @@
from neo import logging from neo import logging
from neo import protocol from neo import protocol
from neo.protocol import UP_TO_DATE_STATE, OUT_OF_DATE_STATE, FEEDING_STATE, \
DISCARDED_STATE, RUNNING_STATE, TEMPORARILY_DOWN_STATE, DOWN_STATE, \
BROKEN_STATE, HIDDEN_STATE, PENDING_STATE
from neo.util import dump, u64 from neo.util import dump, u64
from neo.locking import RLock from neo.locking import RLock
...@@ -28,7 +25,7 @@ from neo.locking import RLock ...@@ -28,7 +25,7 @@ from neo.locking import RLock
class Cell(object): class Cell(object):
"""This class represents a cell in a partition table.""" """This class represents a cell in a partition table."""
def __init__(self, node, state = UP_TO_DATE_STATE): def __init__(self, node, state = protocol.UP_TO_DATE_STATE):
self.node = node self.node = node
self.state = state self.state = state
...@@ -51,6 +48,7 @@ class Cell(object): ...@@ -51,6 +48,7 @@ class Cell(object):
def getServer(self): def getServer(self):
return self.node.getServer() return self.node.getServer()
class PartitionTable(object): class PartitionTable(object):
"""This class manages a partition table.""" """This class manages a partition table."""
...@@ -100,13 +98,13 @@ class PartitionTable(object): ...@@ -100,13 +98,13 @@ class PartitionTable(object):
def getCellList(self, offset, readable=False, writable=False): def getCellList(self, offset, readable=False, writable=False):
# allow all cell states # allow all cell states
state_set = set(protocol.partition_cell_states.values()) state_set = set(protocol.cell_states.values())
if readable or writable: if readable or writable:
# except non readables # except non readables
state_set.remove(DISCARDED_STATE) state_set.remove(protocol.DISCARDED_STATE)
if readable: if readable:
# except non writables # except non writables
state_set.remove(OUT_OF_DATE_STATE) state_set.remove(protocol.OUT_OF_DATE_STATE)
allowed_states = tuple(state_set) allowed_states = tuple(state_set)
try: try:
return [cell for cell in self.partition_list[offset] \ return [cell for cell in self.partition_list[offset] \
...@@ -126,16 +124,16 @@ class PartitionTable(object): ...@@ -126,16 +124,16 @@ class PartitionTable(object):
return index % self.np return index % self.np
def setCell(self, offset, node, state): def setCell(self, offset, node, state):
if state == DISCARDED_STATE: if state == protocol.DISCARDED_STATE:
return self.removeCell(offset, node) return self.removeCell(offset, node)
if node.getState() in (BROKEN_STATE, DOWN_STATE): if node.getState() in (protocol.BROKEN_STATE, protocol.DOWN_STATE):
return return
row = self.partition_list[offset] row = self.partition_list[offset]
if len(row) == 0: if len(row) == 0:
# Create a new row. # Create a new row.
row = [Cell(node, state), ] row = [Cell(node, state), ]
if state != FEEDING_STATE: if state != protocol.FEEDING_STATE:
self.count_dict[node] = self.count_dict.get(node, 0) + 1 self.count_dict[node] = self.count_dict.get(node, 0) + 1
self.partition_list[offset] = row self.partition_list[offset] = row
...@@ -146,11 +144,11 @@ class PartitionTable(object): ...@@ -146,11 +144,11 @@ 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() != FEEDING_STATE: if cell.getState() != protocol.FEEDING_STATE:
self.count_dict[node] = self.count_dict.get(node, 0) - 1 self.count_dict[node] = self.count_dict.get(node, 0) - 1
break break
row.append(Cell(node, state)) row.append(Cell(node, state))
if state != FEEDING_STATE: if state != protocol.FEEDING_STATE:
self.count_dict[node] = self.count_dict.get(node, 0) + 1 self.count_dict[node] = self.count_dict.get(node, 0) + 1
def removeCell(self, offset, node): def removeCell(self, offset, node):
...@@ -159,7 +157,7 @@ class PartitionTable(object): ...@@ -159,7 +157,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() != FEEDING_STATE: if cell.getState() != protocol.FEEDING_STATE:
self.count_dict[node] = self.count_dict.get(node, 0) - 1 self.count_dict[node] = self.count_dict.get(node, 0) - 1
break break
...@@ -219,16 +217,6 @@ class PartitionTable(object): ...@@ -219,16 +217,6 @@ class PartitionTable(object):
partition on the line (here, line length is 9 to keep the docstring partition on the line (here, line length is 9 to keep the docstring
width under 80 column). width under 80 column).
""" """
node_state_dict = { RUNNING_STATE: 'R',
TEMPORARILY_DOWN_STATE: 'T',
DOWN_STATE: 'D',
BROKEN_STATE: 'B',
HIDDEN_STATE: 'H',
PENDING_STATE: 'P'}
cell_state_dict = { UP_TO_DATE_STATE: 'U',
OUT_OF_DATE_STATE: 'O',
FEEDING_STATE: 'F',
DISCARDED_STATE: 'D'}
node_list = self.count_dict.keys() node_list = self.count_dict.keys()
node_list.sort() node_list.sort()
node_dict = {} node_dict = {}
...@@ -236,9 +224,10 @@ class PartitionTable(object): ...@@ -236,9 +224,10 @@ class PartitionTable(object):
uuid = node.getUUID() uuid = node.getUUID()
node_dict[uuid] = i node_dict[uuid] = i
logging.debug('pt: node %d: %s, %s', i, dump(uuid), logging.debug('pt: node %d: %s, %s', i, dump(uuid),
node_state_dict[node.getState()]) protocol.node_state_prefix_dict[node.getState()])
line = [] line = []
max_line_len = 20 # XXX: hardcoded number of partitions per line max_line_len = 20 # XXX: hardcoded number of partitions per line
cell_state_dict = protocol.cell_state_prefix_dict
for offset, row in enumerate(self.partition_list): for offset, row in enumerate(self.partition_list):
if len(line) == max_line_len: if len(line) == max_line_len:
logging.debug('pt: %08d: %s', offset - max_line_len, logging.debug('pt: %08d: %s', offset - max_line_len,
...@@ -269,8 +258,9 @@ class PartitionTable(object): ...@@ -269,8 +258,9 @@ class PartitionTable(object):
# a node state, and record which rows are ready. # a node state, and record which rows are ready.
for row in self.partition_list: for row in self.partition_list:
for cell in row: for cell in row:
if cell.getState() in (UP_TO_DATE_STATE, FEEDING_STATE) \ if cell.getState() in (protocol.UP_TO_DATE_STATE, \
and cell.getNodeState() == RUNNING_STATE: protocol.FEEDING_STATE) \
and cell.getNodeState() == protocol.RUNNING_STATE:
break break
else: else:
return False return False
......
...@@ -22,7 +22,7 @@ from collections import deque ...@@ -22,7 +22,7 @@ from collections import deque
from neo.config import ConfigurationManager from neo.config import ConfigurationManager
from neo import protocol from neo import protocol
from neo.protocol import TEMPORARILY_DOWN_STATE, \ from neo.protocol import TEMPORARILY_DOWN_STATE, \
partition_cell_states, HIDDEN_STATE cell_states, HIDDEN_STATE
from neo.node import NodeManager, MasterNode, StorageNode from neo.node import NodeManager, MasterNode, StorageNode
from neo.event import EventManager from neo.event import EventManager
from neo.storage.mysqldb import MySQLDatabaseManager from neo.storage.mysqldb import MySQLDatabaseManager
...@@ -116,7 +116,7 @@ class Application(object): ...@@ -116,7 +116,7 @@ class Application(object):
new_cell_list = [] new_cell_list = []
for offset, uuid, state in cell_list: for offset, uuid, state in cell_list:
# convert from int to Enum # convert from int to Enum
state = protocol.partition_cell_states[state] state = protocol.cell_states[state]
# register unknown nodes # register unknown nodes
if self.nm.getNodeByUUID(uuid) is None: if self.nm.getNodeByUUID(uuid) is None:
node = StorageNode(uuid=uuid) node = StorageNode(uuid=uuid)
......
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