Search in sources :

Example 1 with HashAlgorithm

use of net.spy.memcached.HashAlgorithm in project zm-mailbox by Zimbra.

the class ZimbraMemcachedClient method connect.

/**
 * Connects/reconnects the memcached client with server list, protocol and hashing algorithm.
 * @param servers memcached server list
 * @param useBinaryProtocol if true, use the binary protocol; if false, use the ascii protocol
 * @param hashAlgorithm net.spy.memcached.HashAlgorithm enum
 * @param defaultExpiry in seconds
 * @param defaultTimeout in milliseconds
 * @throws ServiceException
 */
public void connect(String[] servers, boolean useBinaryProtocol, String hashAlgorithm, int defaultExpiry, long defaultTimeout) throws ServiceException {
    // Force spymemcached to use log4j rather than raw stdout/stderr.
    Properties props = System.getProperties();
    props.put("net.spy.log.LoggerImpl", "net.spy.memcached.compat.log.Log4JLogger");
    HashAlgorithm hashAlgo = DefaultHashAlgorithm.KETAMA_HASH;
    if (hashAlgorithm != null && hashAlgorithm.length() > 0) {
        HashAlgorithm ha = DefaultHashAlgorithm.valueOf(hashAlgorithm);
        if (ha != null)
            hashAlgo = ha;
    }
    int qLen = DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN;
    int bufSize = DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE;
    MemcachedClient client = null;
    StringBuilder serverList = new StringBuilder();
    if (servers != null && servers.length > 0) {
        // Eliminate duplicates and sort case-insensitively.  This negates operator error
        // configuring server list with inconsistent order on different memcached clients.
        // TreeSet provides deduping and sorting.
        TreeSet<String> tset = new TreeSet<String>();
        for (int i = 0; i < servers.length; ++i) {
            tset.add(servers[i].toLowerCase());
        }
        for (String s : tset) {
            if (serverList.length() > 0)
                serverList.append(", ");
            serverList.append(s);
        }
        List<InetSocketAddress> serverAddrs = parseServerList(tset.toArray(new String[0]));
        ConnectionFactory cf;
        if (useBinaryProtocol)
            cf = new BinaryConnectionFactory(qLen, bufSize, hashAlgo);
        else
            cf = new DefaultConnectionFactory(qLen, bufSize, hashAlgo);
        try {
            client = new MemcachedClient(cf, serverAddrs);
            boolean added = client.addObserver(new ConnObserver());
            if (!added)
                ZimbraLog.misc.error("Unable to add connection observer to memcached client");
        } catch (IOException e) {
            throw ServiceException.FAILURE("Unable to initialize memcached client", e);
        }
    }
    MemcachedClient oldClient = null;
    synchronized (this) {
        oldClient = mMCDClient;
        mMCDClient = client;
        mDefaultExpiry = defaultExpiry;
        mDefaultTimeout = defaultTimeout;
        mServerList = serverList.length() > 0 ? serverList.toString() : null;
        mHashAlgorithm = hashAlgo.toString();
        mBinaryProtocolEnabled = useBinaryProtocol;
    }
    // New client is ready for use by other threads at this point.
    if (oldClient != null)
        disconnect(oldClient, 30000);
}
Also used : DefaultConnectionFactory(net.spy.memcached.DefaultConnectionFactory) InetSocketAddress(java.net.InetSocketAddress) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory) IOException(java.io.IOException) Properties(java.util.Properties) ConnectionFactory(net.spy.memcached.ConnectionFactory) DefaultConnectionFactory(net.spy.memcached.DefaultConnectionFactory) BinaryConnectionFactory(net.spy.memcached.BinaryConnectionFactory) TreeSet(java.util.TreeSet) MemcachedClient(net.spy.memcached.MemcachedClient) DefaultHashAlgorithm(net.spy.memcached.DefaultHashAlgorithm) HashAlgorithm(net.spy.memcached.HashAlgorithm)

Aggregations

IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Properties (java.util.Properties)1 TreeSet (java.util.TreeSet)1 BinaryConnectionFactory (net.spy.memcached.BinaryConnectionFactory)1 ConnectionFactory (net.spy.memcached.ConnectionFactory)1 DefaultConnectionFactory (net.spy.memcached.DefaultConnectionFactory)1 DefaultHashAlgorithm (net.spy.memcached.DefaultHashAlgorithm)1 HashAlgorithm (net.spy.memcached.HashAlgorithm)1 MemcachedClient (net.spy.memcached.MemcachedClient)1