Commit d85b471d authored by Julien Muchembled's avatar Julien Muchembled

client: refactor code to connect to master

parent 1e0c5efc
...@@ -254,20 +254,19 @@ class Application(object): ...@@ -254,20 +254,19 @@ class Application(object):
Lookup for the current primary master node Lookup for the current primary master node
""" """
logging.debug('connecting to primary master...') logging.debug('connecting to primary master...')
ready = False index = 0
nm = self.nm ask = self._ask
while not ready: handler = self.primary_bootstrap_handler
while 1:
# Get network connection to primary master # Get network connection to primary master
index = 0 while 1:
connected = False
while not connected:
if self.primary_master_node is not None: if self.primary_master_node is not None:
# If I know a primary master node, pinpoint it. # If I know a primary master node, pinpoint it.
self.trying_master_node = self.primary_master_node self.trying_master_node = self.primary_master_node
self.primary_master_node = None self.primary_master_node = None
else: else:
# Otherwise, check one by one. # Otherwise, check one by one.
master_list = nm.getMasterList() master_list = self.nm.getMasterList()
try: try:
self.trying_master_node = master_list[index] self.trying_master_node = master_list[index]
except IndexError: except IndexError:
...@@ -288,37 +287,32 @@ class Application(object): ...@@ -288,37 +287,32 @@ class Application(object):
self.trying_master_node) self.trying_master_node)
continue continue
try: try:
self._ask(conn, Packets.RequestIdentification( ask(conn, Packets.RequestIdentification(
NodeTypes.CLIENT, self.uuid, None, self.name), NodeTypes.CLIENT, self.uuid, None, self.name),
handler=self.primary_bootstrap_handler) handler=handler)
except ConnectionClosed: except ConnectionClosed:
continue continue
# If we reached the primary master node, mark as connected # If we reached the primary master node, mark as connected
connected = self.primary_master_node is not None and \ if self.primary_master_node is not None and \
self.primary_master_node is self.trying_master_node self.primary_master_node is self.trying_master_node:
break
logging.info('Connected to %s', self.primary_master_node) logging.info('Connected to %s', self.primary_master_node)
try: try:
ready = self.identifyToPrimaryNode(conn) # Request identification and required informations to be
# operational. Might raise ConnectionClosed so that the new
# primary can be looked-up again.
logging.info('Initializing from master')
ask(conn, Packets.AskNodeInformation(), handler=handler)
ask(conn, Packets.AskPartitionTable(), handler=handler)
ask(conn, Packets.AskLastTransaction(), handler=handler)
if self.pt.operational():
break
except ConnectionClosed: except ConnectionClosed:
logging.error('Connection to %s lost', self.trying_master_node) logging.error('Connection to %s lost', self.trying_master_node)
self.primary_master_node = None self.primary_master_node = None
logging.info("Connected and ready") logging.info("Connected and ready")
return conn return conn
def identifyToPrimaryNode(self, conn):
"""
Request identification and required informations to be operational.
Might raise ConnectionClosed so that the new primary can be
looked-up again.
"""
logging.info('Initializing from master')
ask = self._ask
handler = self.primary_bootstrap_handler
ask(conn, Packets.AskNodeInformation(), handler=handler)
ask(conn, Packets.AskPartitionTable(), handler=handler)
self.lastTransaction()
return self.pt.operational()
def registerDB(self, db, limit): def registerDB(self, db, limit):
self._db = db self._db = db
......
...@@ -89,6 +89,9 @@ class PrimaryBootstrapHandler(AnswerBaseHandler): ...@@ -89,6 +89,9 @@ class PrimaryBootstrapHandler(AnswerBaseHandler):
def answerNodeInformation(self, conn): def answerNodeInformation(self, conn):
pass pass
def answerLastTransaction(self, conn, ltid):
pass
class PrimaryNotificationsHandler(BaseHandler): class PrimaryNotificationsHandler(BaseHandler):
""" Handler that process the notifications from the primary master """ """ Handler that process the notifications from the primary master """
......
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