Java memcached release [1]-sockiopool

Source: Internet
Author: User

1. sockiopool-sockio pool management. The interface provided for the upper layer is the instantiation function [mainly to specify the memcached server address, each
Machine weight]; obtain the sockio-network connection handle based on key & hashcode; obtain sockio Based on the server address; disable the pool. Obtain the upper layer
After sockio, you can perform read and write operations. After using sockio normally, return it to the pool. If a problem occurs during use, the Network
The connection is closed, and the socketchannel provides interfaces for NiO programming.
Sockiopool maintains a iopool ing from the string pool name to sockiopool within the static visible range.
Sockio writes the entry time to the availpool or busypool as a reference for idle time or use time.
------------------
2. sockiopool attributes
Boolean initialized = false-Indicates whether Initialization is complete. The upper layer can use the pool normally only after Initialization is complete.
Int initconn = 10-Number of connections established for each server during initialization
Int minconn = 5-each server establishes the minimum number of connections. When the self-check thread finds that the number of connections established with a server is smaller than this number
Makes up for the remaining connections
Int maxconn = 100-the maximum number of connections established on each server. When the self-check thread finds that the number of connections established with a server is greater than this number
Check whether the idle time of these connections is greater than maxconn one by one. If the idle time is greater than maxconn, close these connections until the number of connections equals to maxconn.
Long maxidle = 1000*60*5-maximum idle time
Long maxbusytime = 1000*30-the longest lease time. There are two main points in use. First, the self-check thread checks the connections being rented,
If it is found that the rented time exceeds this value, it will be removed from the rented record and the connection will be closed; another application is
When the upper layer performs the mutil operation, the time for reading all data cannot exceed this time.
Long maintsleep = 1000*30-self-check thread cycle, its sleep time
Int socketto = 1000*3-timeout for blocking Data Reading through socket
Int socketconnectto = 1000*3-wait time for socket blocking to establish a connection
Boolean alivecheck = false-when obtaining sockio based on key & hashcode, after obtaining sockio through hash bucket, if this value
If it is true, the system checks whether the socket is connected. If the connection is established normally, the server wants to send the "version \ r \ n" command and read the data.
If no error occurs, sockio is returned to the upper layer. Otherwise, null is returned. Therefore, it is generally set to false.
Boolean Nagle = false-socket parameter. If it is true, it is sent immediately if it is not buffered when data is written.
Int hashingalg = native_hash-the hash Bucket Method of the pool. It is mainly divided into simple hashcode to get the number of hash buckets.
And consistent hash, the former will cause a large decline in the hit rate during resizing, the latter advantage is that the expansion greatly reduces the cache re-
Distribution
String [] servers-memcached server address Configuration
Integer [] weights-memcached server weight Configuration
List <string> buckets-Hash Bucket: A server address with a weight of N. Then, add n server addresses to the bucket.
Treemap <long, string> consistentbuckets-consistent hash table: A server address, whose weight is N, iteration 0-N-1, take
String server address + I MD5 value, which is divided into 4 segments, each segment is converted into a long value, put this long value to the server address
To consistent hash table
Map <string, date> hostdead-used to record the map of the latest time of connection failure between the local host and the server address
Map <string, long> hostdeaddur-If hostdead contains the server address, it indicates that the last connection setup failed.
In hostdeaddur, there is no need to try the connection interval value for this server. If the last failed time + there is no need to try the connection interval
No connection is initiated. After each failed connection to the server, the latest time in hostdead will be replaced, and the interval between hostdeaddur will be replaced.
Value. The initial value is 1000 ms, doubling every failure.
Map <string, Map <sockio, long> availpool-currently available connection records
Map <string, Map <sockio, long> busypool-currently running connection records
Map <sockio, integer> deadpool-used to maintain the sockio that should close the connection. The self-check thread will connect all the sockio
Close
Boolean failover/failback
------------------
3. Pool Initialization
In the initialization phase, the hash bucket is set based on hashingalg.
Consistent_hash: Use the consistent hash algorithm mentioned above to create a consistent hash table.
Hash bucket. Next, the number of connections created based on initconn and all server addresses is the number of initconn connections. If maintsleep is greater than 0
Start the self-check thread of the entire connection pool. The connection creation logic is as follows: if both hostdead and hostdeaddur hold a server address
You can check whether the recent failure time + unnecessary connection time interval is greater than the current time. If the time difference is greater than the current time, you can quit the connection creation process,
If the value is smaller than the value, a connection is initiated. If a connection is created successfully within the specified time period, the hostdead and
Hostdeaddur record, and then put this connection into the availpool record; one is that the connection is not created successfully, then put sockio
In the deadpool, disconnect and release resources for the self-check thread. The last one is due to network exceptions or timeout exceptions.
When this happens, you need to add the relevant records of the server address to hostdead and hostdeaddur, and add the server in the availpool
Clear the address records.
------------------
4. Rent sockiopool
A. Key & hashcode-rent
1. First, find the server address that should be connected to the hash bucket based on the key & hashcode: If hashcode exists, directly use
Hashcode. If not, the hash value is obtained based on hashingalg. If it is a consistent hash algorithm, find the calculated large hash in consistentbuckets.
Value Point. If no value is found, the first value is used. If the value is other values, the modulo of the hash bucket is used.
2. Find the available connection list in the availpool Based on the server address and iterate this list. If there is any available connection list, retrieve it from the availpool,
And add it to busypool. If not, add it to deadpool to close the connection for the self-check thread. Call if the connection is not obtained after the above process
The connection creation logic mentioned above should be added to busypool if the connection is successfully created.
3. After obtaining a connection sockio, perform some checks. If the connection is unavailable, add it to the deadpool. If the connection is alivecheck
If the value is true, the self-check operation mentioned above will be performed. If an error occurs in the self-check, the connection will be removed from the busypool and closed. If the connection fails at 2
If the value of failover is false, the system will directly return and no longer try to connect to other machines. Otherwise, some new keys will be assembled into the hash bucket to find the remaining keys.
The server address under is 2 to establish a connection.
B. Host-rent
Find the available connection list in the availpool Based on the server address and iterate the list. If available, retrieve the available connection list from the availpool,
And add it to busypool. If not, add it to deadpool to close the connection for the self-check thread. Call if the connection is not obtained after the above process
The connection creation logic mentioned above should be added to busypool if the connection is successfully created.
C. Also
Remove the sockio connection from the busypool record. If the connection is available, add the connection to the availpool record.
------------------
5. self-check thread
The self-check thread mainly provides two Logics: Start and Stop. When the thread is started, the self-check logic of the pool is called periodically.
To mark the stop. When the thread is interrupted, it may be sleep.
The self-check logic of the pool is as follows:
A. Check the number of currently available connections of each server in availpool. If the number is smaller than minconn, create a connection to make up the difference.
To the availpool.
B. Check the number of available connections on each server in the availpool. If the number is greater than maxconn, check the idle time of these connections one by one.
Whether it is greater than maxconn. If it is greater than maxconn, these connections will be closed until the number of connections is equal to maxconn. The idle time is calculated based on the current time and
The difference between the time when the connection enters the availpool.
C. Check the busypool and find that a connection has been rented for more than the value. It will remove it from the rented record and add it
To deadpool.
D. iterate all connections in the deadpool and close these connections.
------------------
6. Stop the self-check thread until the self-check thread stops running, and iterate availpool and busypool to connect the network.
Sockio: the main function of disabling sockio is to close the input/output stream and socket.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.