Commit e0b638b5 authored by Julien Muchembled's avatar Julien Muchembled

client: close failing connection explicitly in case of unhandled exception

The changes consist in:
- indenting many lines without exceeding 80 chars
- setting conn = None ASAP on ConnectionClosed
- adding a try..except clause

Starting from next commit, the exception can be POSException.ReadOnlyError.
parent eb3a3937
...@@ -200,56 +200,64 @@ class Application(ThreadedApplication): ...@@ -200,56 +200,64 @@ class Application(ThreadedApplication):
fail_count = 0 fail_count = 0
ask = self._ask ask = self._ask
handler = self.primary_bootstrap_handler handler = self.primary_bootstrap_handler
while 1: conn = None
self.ignore_invalidations = True try:
# Get network connection to primary master while 1:
while fail_count < self.max_reconnection_to_master: self.ignore_invalidations = True
self.nm.reset() # Get network connection to primary master
if self.primary_master_node is not None: while fail_count < self.max_reconnection_to_master:
# If I know a primary master node, pinpoint it. self.nm.reset()
node = self.primary_master_node if self.primary_master_node is not None:
self.primary_master_node = None # If I know a primary master node, pinpoint it.
else: node = self.primary_master_node
# Otherwise, check one by one. self.primary_master_node = None
master_list = self.nm.getMasterList() else:
if not master_list: # Otherwise, check one by one.
# XXX: On shutdown, it already happened that this list master_list = self.nm.getMasterList()
# is empty, leading to ZeroDivisionError. This if not master_list:
# looks a minor issue so let's wait to have more # XXX: On shutdown, it already happened that this
# information. # list is empty, leading to ZeroDivisionError.
logging.error('%r', self.__dict__) # This looks a minor issue so let's wait to
index = (index + 1) % len(master_list) # have more information.
node = master_list[index] logging.error('%r', self.__dict__)
# Connect to master index = (index + 1) % len(master_list)
conn = MTClientConnection(self, node = master_list[index]
# Connect to master
conn = MTClientConnection(self,
self.notifications_handler, self.notifications_handler,
node=node, node=node,
dispatcher=self.dispatcher) dispatcher=self.dispatcher)
p = Packets.RequestIdentification(NodeTypes.CLIENT, p = Packets.RequestIdentification(NodeTypes.CLIENT,
self.uuid, None, self.name, None, {}) self.uuid, None, self.name, None, {})
try:
ask(conn, p, handler=handler)
except ConnectionClosed:
conn = None
fail_count += 1
else:
self.primary_master_node = node
break
else:
raise NEOPrimaryMasterLost(
"Too many connection failures to the primary master")
logging.info('Connected to %s', self.primary_master_node)
try: try:
ask(conn, p, handler=handler) # 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.AskLastTransaction(), handler=handler)
if self.pt.operational():
break
except ConnectionClosed: except ConnectionClosed:
fail_count += 1 conn = self.primary_master_node = None
else: logging.error('Connection to %s lost',
self.primary_master_node = node self.trying_master_node)
break fail_count += 1
else: except:
raise NEOPrimaryMasterLost( if conn is not None:
"Too many connection failures to the primary master") conn.close()
logging.info('Connected to %s', self.primary_master_node) raise
try:
# 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.AskLastTransaction(), handler=handler)
if self.pt.operational():
break
except ConnectionClosed:
logging.error('Connection to %s lost', self.trying_master_node)
self.primary_master_node = None
fail_count += 1
logging.info("Connected and ready") logging.info("Connected and ready")
return conn return conn
......
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