@clusterdown_wrapper def execute_command(self, *args, **kwargs): """ Send a command to a node in the cluster """ import logging log=logging.getLogger('service.log') log.error(u'redis execute_command 1 %s ' % str(args)) # If set externally we must update it before calling any commands if self.refresh_table_asap: #執行self.connection_pool.nodes.initialize()的代碼段 log.error(u'redis execute_command 2 %s ' % str(args)) self.connection_pool.nodes.initialize() log.error(u'redis execute_command 3 %s ' % str(args)) self.refresh_table_asap = False log.error(u'redis execute_command 4 %s ' % str(args)) redirect_addr = None asking = False try_random_node = False log.error(u'redis execute_command 7 %s ' % str(args)) slot = self._determine_slot(*args) log.error(u'redis execute_command 8 %s ' % str(args)) ttl = int(self.RedisClusterRequestTTL) while ttl > 0: ttl -= 1 if asking: node = self.connection_pool.nodes.nodes[redirect_addr] r = self.connection_pool.get_connection_by_node(node) elif try_random_node: r = self.connection_pool.get_random_connection() try_random_node = False else: if self.refresh_table_asap: # MOVED node = self.connection_pool.get_master_node_by_slot(slot) else: node = self.connection_pool.get_node_by_slot(slot) r = self.connection_pool.get_connection_by_node(node) try: r.send_command(*args) log.error(u'redis execute_command 10 %s ' % str(args)) ret= self.parse_response(r, command, **kwargs) log.error(u'redis execute_command 11 %s ' % str(args)) return ret except (RedisClusterException, BusyLoadingError): raise except (ConnectionError, TimeoutError): try_random_node = True log.error(u'redis execute_command 14 %s ' % str(args)) if ttl < self.RedisClusterRequestTTL / 2: log.error(u'redis execute_command 15 %s ' % str(args)) time.sleep(0.1) except ClusterDownError as e: log.error(u'redis execute_command 17 %s ' % str(args)) self.connection_pool.disconnect() self.connection_pool.reset() self.refresh_table_asap = True raise e except MovedError as e: # Reinitialize on ever x number of MovedError. # This counter will increase faster when the same client object # is shared between multiple threads. To reduce the frequency you # can set the variable 'reinitialize_steps' in the constructor. import traceback print traceback.format_exc() log.error(u'redis execute_command 16 %s ' % str(args)) self.refresh_table_asap = True self.connection_pool.nodes.increment_reinitialize_counter() node = self.connection_pool.nodes.set_node(e.host, e.port, server_type='master') self.connection_pool.nodes.slots[e.slot_id][0] = node
優化:node
r.send_command(*args) ret= self.parse_response(r, command, **kwargs) return ret
改成python
try: r.send_command(*args) return self.parse_response(r, command, **kwargs) except ConnectionError as e: from redis.connection import SERVER_CLOSED_CONNECTION_ERROR if SERVER_CLOSED_CONNECTION_ERROR in e.message: r.disconnect() r.send_command(*args) return self.parse_response(r, command, **kwargs) else: raise
未經許可,請不要轉載git