bsc-s2/pykit

zktx: refine dealing with connection state changes

Opened this issue · 0 comments

(做什么)

SUSPENDED 状态不需要设置连接中断, 可以继续处理事务.

# zktx.py:78
    def _on_conn_change(self, state):

        logger.debug('state changed: {state}'.format(state=state,))

        with self.state_lock:
            if state == KazooState.LOST or state == KazooState.SUSPENDED:
                self.connected = False

下面是个人理解:

          connection lost                   session lost,
          session not expired               ephemeral lost.
CONNECTED ---------------------> SUSPENDED ----------------> LOST
  ^                                |
  `--------------------------------'
        reconnected,
        session restored.
        same session-id

参考: https://kazoo.readthedocs.io/en/latest/basic_usage.html

When a connection transitions to SUSPENDED, if the client is performing an
action that requires agreement with other systems (using the Lock recipe for
example), it should pause what it’s doing. When the connection has been
re-established the client can continue depending on if the state is LOST or
transitions directly to CONNECTED again.

When a connection transitions to LOST, any ephemeral nodes that have been
created will be removed by Zookeeper. This affects all recipes that create
ephemeral nodes, such as the Lock recipe. Lock’s will need to be re-acquired
after the state transitions to CONNECTED again. This transition occurs when a
session expires or when you stop the clients connection.

(为什么要做)

尽量减少事务中断的几率, 在session 没有超时前能重连上继续运行.

(实现思路)

遇到SUSPENDED 需要继续执行, 可能需要记录SUSPENDED状态, 在SUSPENDED 应该禁止更多加锁操作等可能会更好. 待讨论.

增加测试在短时间内中断zk连接并恢复, 确认事务可以继续执行.