○ environment
zk client: github.com/samuel/go-zookeeper
1, zk client status
zookeeper is a popular distributed coordination component, which is widely used in leader election, distributed lock, service discovery, name service, configuration center and other scenarios.
1. Meaning of status
zk client and zk server will experience various states in the process of establishing, maintaining and disconnecting connections. As shown below
const ( // Not used yet StateUnknown State = -1 // When the connection with zk server is disconnected (including the initial state), zk client will continue to reconnect StateDisconnected State = 0 // The temporary state before establishing a connection with zk server indicates that zk server is about to be connected StateConnecting State = 1 // Not used yet StateAuthFailed State = 4 // Not used yet StateConnectedReadOnly State = 5 // Not used yet StateSaslAuthenticated State = 6 // After reestablishing the TCP connection with zk server, the handshake phase found that the session timed out StateExpired State = -112 // Status after successfully establishing TCP connection with zk server StateConnected = State(100) // Successfully establish TCP connection with zk server and successfully shake hands (i.e. successfully create session) StateHasSession = State(101) )
2. State transition
2, Timeout
The timeout time greatly affects the transition of the above states, and three timeout times deserve attention:
- sessionTimeout: the session timed out. When a ZK client is not connected to another zk server, it will reconnect with another ZK client. As long as the TCP connection is successfully established within the sessionTimeout and the handshake is successful, the temporary node and the watcher will be reserved as the resources of the existing session. In particular, it should be noted that the sessionTimeout is not completely set by the client. It is determined through negotiation between the client and the server: it must be between the upper and lower limits of the sessionTimeout configured on the server.
- pingInterval: is the interval between zk client and server to keep heartbeat. The default value is 1/3 * sessionTimeout
- recvTimeout: the default is 2/3 * sessionTimeout. The timeout time for the client to send a request and receive a response (including heartbeat). In addition, the read-write timeout of the client handshake phase is 10 * recvTimeout.
- Connecttimeout: timeout of establishing TCP connection between client and zk server
func (c *Conn) setTimeouts(sessionTimeoutMs int32) { c.sessionTimeoutMs = sessionTimeoutMs sessionTimeout := time.Duration(sessionTimeoutMs) * time.Millisecond c.recvTimeout = sessionTimeout * 2 / 3 c.pingInterval = c.recvTimeout / 2 }
3, Exception handling
// Connect establishes a new connection to a pool of zookeeper // servers. The provided session timeout sets the amount of time for which // a session is considered valid after losing connection to a server. Within // the session timeout it's possible to reestablish a connection to a different // server and keep the same session. This is means any ephemeral nodes and // watches are maintained
If the connection between client and server is abnormal, there are three situations:
- The connection has not been successfully established. At this time, zk client loops in connect(), and zk service is unavailable. According to the specific situation of the business, the user can make the application or exit, or downgrade, or dead cycle until zk service is restored.
- The connection was successfully established within sessionTimeout. Temporary nodes and watcher s can be retained without any processing
- The connection was not successfully established in sessionTimeout, but it succeeded later. At this time, the application should reset the internal zk related state or actively exit.
Recommended reading
- STL source code analysis -- memory allocator
- STL source code analysis -- vector
- STL source code analysis -- string
- STL source code analysis -- list
- STL source code analysis --hashtable
- STL source code analysis -- deque
- STL source code analysis -- iterator
- STL source code analysis -- traits
- STL source code analysis -- rbtree
- STL source code analysis -- bitset
- STL source code analysis -- algorithm
- STL source code analysis -- functional
More interesting content, please scan code concern WeChat official account: backend technology hut. If you think the article is helpful to you, please share, forward and read it.