In spymemcached, a connection established between a mc client and a mc node times out frequently (net. spy. memcached. operationTimeoutException), then there is a counter specifically records the number of times of timeout (connected or reconnecting will be cleared ).
When a mc node times out continuously to a certain threshold (for example, 998 times ), It will ......
- Disable the original socket channel and put the mc node into the reconnectQueue: lostConnection (mn) reconnect queue ),
- Traverse reconnectQueue and try to re-establish socket connections one by one: attemptReconnects (),
- Then, redistributeOperations (retryOps). (Rehash refers to re-calculating the hash value to see if you want to connect to other nodes ).
The timeout threshold can be set as follows in spring, or the default value is 998:
| SpringIntegration |
<Beanid = "memcachedClient" class = "net. spy. memcached. spring. MemcachedClientFactoryBean"> ...... <Propertyname = "opTimeout" value = "1000"/> the operation timeout value is 1 second. If this parameter is not set, the default value is DEFAULT_OPERATION_TIMEOUT, Which is 2.5 seconds. <Propertyname = "timeoutExceptionThreshold" value = "1998"/> set the maximum number of times of timeout to 1998. The default value is DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD, that is, 998 times. ...... </Bean> |
Related code:Each time handle io over the connections, it checks whether the "number of consecutive times out" registered on the corresponding node of the connection has reached the threshold:
| MemcachedConnection. java |
| Private final int timeoutExceptionThreshold ;...... TimeoutExceptionThreshold = f. getTimeoutExceptionThreshold ();...... |
| /*** MemcachedClient callthis method to handle IO over the connections. */public void handleIO () throws IOException {...... // See if any connections blew up with large number of timeouts for (SelectionKey sk: selector. keys () {MemcachedNode mn = (MemcachedNode) sk. attachment (); if (Mn. getContinuousTimeout ()> timeoutExceptionThreshold) {// Check getLogger (). warn ("% s exceeded continuous timeout threshold", sk); lostConnection (mn); // disconnect and put it in the reconnection queue} if (! ShutDown &&! ReconnectQueue. isEmpty () {attemptReconnects (); // reconnect} // rehash any operations that are in retry state redistributeOperations (retryOps); // retryOps. clear (); |
| |
If the configuration file is not configured with timeoutExceptionThreshold, this parameter is not passed during initialization,
Then DefaultConnectionFactory takes the default valueDEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD, That is998 times (anyway, maybe 1000 times)! |
| DefaultConnectionFactory. java |
| /*** Maximum number + 2 of timeout exception for shutdown connection. */public static final int DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD = 998; |
| DefaultConnectionFactory. java |
| Public int getTimeoutExceptionThreshold () {return DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD ;} |
Set a "continuous timeout count" counter for each memcache node:
| TCPMemcachedNodeImpl. java |
| // Operation Future. get timeout counter private final AtomicInteger continuousTimeout = new AtomicInteger (0 );...... Public void setContinuousTimeout (boolean timedOut) {if (timedOut & isActive () {continuousTimeout. incrementAndGet ();} else {continuousTimeout. set (0 );}} |
| MemcachedConnection. java |
| /*** Helper method: increase timeout count on node attached to this op. ** @ param op */public static void opTimedOut (Operation op) {MemcachedConnection. setTimeout (op, true );} |
Reference resources: 1) zhishen, 2012, spymemcached source code in-depth analysis; 2) 2011, Issue 173: Spymemcached fail reconnect to local memcahed under high load ..; 3) My related article: spymemcached's useNagle problem and TCP/IP delayed Data Transmission 1) 55 Best Practices Series: MongoDB Best Practices)
2) 55 Best Practices Series: Logging Best Practices () 3) Best Practices Series: front-end code standards and best practices) 1) E-commerce subject I: Throttle Limits for CILS/requests in a clustered environment)
2) E-commerce Subject V: distributed locks)
3) E-commerce Subject: cookie tamper-proofing () 4) E-commerce Subject VI: distributed Session)
5) E-commerce Subject: RBAC permission control)
6) E-commerce Subject: idempotence)
7) e-commerce issues: Client IP address forgery, CDN, reverse proxy, and acquisition)
8) e-commerce issues: a simple analysis of malicious access behaviors such as flash sales)
9) E-commerce Subject VII: General guidelines for payment transactions)
One image