Search in sources :

Example 46 with MemcachedClient

use of net.spy.memcached.MemcachedClient in project serverless by bluenimble.

the class MemCachedPlugin method createClients.

private void createClients(ApiSpace space) throws PluginRegistryException {
    // create sessions
    JsonObject msgFeature = Json.getObject(space.getFeatures(), feature);
    if (msgFeature == null || msgFeature.isEmpty()) {
        return;
    }
    Iterator<String> keys = msgFeature.keys();
    while (keys.hasNext()) {
        String key = keys.next();
        JsonObject feature = Json.getObject(msgFeature, key);
        if (!this.getName().equalsIgnoreCase(Json.getString(feature, ApiSpace.Features.Provider))) {
            continue;
        }
        String sessionKey = createKey(key);
        if (space.containsRecyclable(sessionKey)) {
            continue;
        }
        JsonObject spec = Json.getObject(feature, ApiSpace.Features.Spec);
        String[] nodes = Lang.split(Json.getString(spec, Spec.Cluster), Lang.COMMA);
        if (nodes == null) {
            continue;
        }
        final JsonObject oAuth = Json.getObject(spec, Spec.Auth);
        if (oAuth == null || oAuth.isEmpty()) {
            continue;
        }
        final String user = Json.getString(oAuth, Spec.User);
        final String password = Json.getString(oAuth, Spec.Password);
        AuthDescriptor ad = new AuthDescriptor(new String[] { "PLAIN" }, new PlainCallbackHandler(user, password));
        try {
            MemcachedClient client = new MemcachedClient(new ConnectionFactoryBuilder().setProtocol(ConnectionFactoryBuilder.Protocol.BINARY).setAuthDescriptor(ad).build(), AddrUtil.getAddresses(Arrays.asList(nodes)));
            space.addRecyclable(sessionKey, new RecyclableCacheClient(client));
        } catch (IOException e) {
            throw new PluginRegistryException(e.getMessage(), e);
        }
    }
}
Also used : PlainCallbackHandler(net.spy.memcached.auth.PlainCallbackHandler) ConnectionFactoryBuilder(net.spy.memcached.ConnectionFactoryBuilder) AuthDescriptor(net.spy.memcached.auth.AuthDescriptor) MemcachedClient(net.spy.memcached.MemcachedClient) JsonObject(com.bluenimble.platform.json.JsonObject) IOException(java.io.IOException) PluginRegistryException(com.bluenimble.platform.plugins.PluginRegistryException)

Example 47 with MemcachedClient

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

the class ZimbraMemcachedClient method remove.

/**
 * Removes the value for given key.
 * @param key
 * @param timeout in millis
 * @param waitForAck if true, block until ack'd or timeout; if false, return immediately
 * @return
 */
public boolean remove(String key, long timeout, boolean waitForAck) {
    Boolean success = null;
    MemcachedClient client;
    synchronized (this) {
        client = mMCDClient;
        if (timeout == DEFAULT_TIMEOUT)
            timeout = mDefaultTimeout;
    }
    if (client == null)
        return false;
    Future<Boolean> future = client.delete(key);
    if (waitForAck) {
        try {
            success = future.get(timeout, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            ZimbraLog.misc.warn("memcached delete timed out after " + timeout + "ms", e);
            future.cancel(false);
        } catch (InterruptedException e) {
            ZimbraLog.misc.warn("InterruptedException during memcached delete operation", e);
        } catch (ExecutionException e) {
            ZimbraLog.misc.warn("ExecutionException during memcached delete operation", e);
        }
        return success != null && success.booleanValue();
    } else {
        return true;
    }
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 48 with MemcachedClient

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

the class ZimbraMemcachedClient method disconnect.

/**
 * Shutdown the memcached client.  Drain the queue and do a normal shutdown if possible.
 * Both drain and normal shutdown are attempted with timeout.  When unsuccessful, shutdown
 * the client immediately.
 * @param timeout in millis
 */
public void disconnect(long timeout) {
    MemcachedClient client;
    synchronized (this) {
        client = mMCDClient;
        mMCDClient = null;
        if (timeout == DEFAULT_TIMEOUT)
            timeout = mDefaultTimeout;
    }
    if (client != null) {
        disconnect(client, timeout);
    }
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient)

Example 49 with MemcachedClient

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

the class ZimbraMemcachedClient method getBigByteArray.

/**
 * Retrieves the big byte array value corresponding to the given key.  See putBigByteArray method
 * for more information.
 * @param key
 * @param timeout in millis
 * @return null if no value is found for the key
 */
public byte[] getBigByteArray(String key, long timeout) {
    MemcachedClient client;
    synchronized (this) {
        client = mMCDClient;
        if (timeout == DEFAULT_TIMEOUT)
            timeout = mDefaultTimeout;
    }
    if (client == null)
        return null;
    // Get the main value.  This may be the entire value or the table of contents.
    ByteArrayTranscoder bat = new ByteArrayTranscoder();
    ByteArray mainValue = null;
    Future<ByteArray> future = client.asyncGet(key, bat);
    try {
        mainValue = future.get(timeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        ZimbraLog.misc.warn("memcached asyncGetBulk timed out after " + timeout + "ms", e);
        future.cancel(false);
    } catch (InterruptedException e) {
        ZimbraLog.misc.warn("InterruptedException during memcached asyncGetBulk operation", e);
    } catch (ExecutionException e) {
        ZimbraLog.misc.warn("ExecutionException during memcached asyncGetBulk operation", e);
    }
    if (mainValue == null)
        return null;
    byte[] prefixed = mainValue.getBytes();
    if (prefixed == null || prefixed.length < 2)
        return null;
    // First byte is value/TOC indicator.  Remove it.
    byte[] value = new byte[prefixed.length - 1];
    System.arraycopy(prefixed, 0, value, 0, prefixed.length - 1);
    // If it's a short value, just return it.
    if (prefixed[prefixed.length - 1] == BBA_PREFIX_VALUE)
        return value;
    // We have a table of contents.
    String tocEncoded = null;
    ByteArrayChunksTOC toc;
    try {
        tocEncoded = new String(value, "utf-8");
        toc = new ByteArrayChunksTOC(tocEncoded);
    } catch (UnsupportedEncodingException e) {
        ZimbraLog.misc.warn("Unable to decode BBA table of contents", e);
        return null;
    } catch (ServiceException e) {
        ZimbraLog.misc.warn("Invalid big byte array TOC: " + tocEncoded);
        return null;
    }
    int numChunks = toc.getNumChunks();
    if (numChunks <= 0) {
        // This should never happen.  Just a sanity check.
        ZimbraLog.misc.warn("Big byte array TOC has numChunks=0");
        return null;
    }
    List<String> chunkKeys = new ArrayList<String>(numChunks);
    for (int i = 0; i < numChunks; ++i) {
        String ck = key + ":" + toc.getFingerprint() + "." + i;
        chunkKeys.add(ck);
    }
    // Get the chunks from memcached.
    Map<String, ByteArray> vals = null;
    Future<Map<String, ByteArray>> futureChunks = client.asyncGetBulk(chunkKeys, bat);
    try {
        vals = futureChunks.get(timeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException e) {
        ZimbraLog.misc.warn("memcached asyncGetBulk timed out after " + timeout + "ms", e);
        futureChunks.cancel(false);
    } catch (InterruptedException e) {
        ZimbraLog.misc.warn("InterruptedException during memcached asyncGetBulk operation", e);
    } catch (ExecutionException e) {
        ZimbraLog.misc.warn("ExecutionException during memcached asyncGetBulk operation", e);
    }
    // Make sure all chunks are there.  If not, just return null.
    if (vals == null)
        return null;
    ByteArray[] byteArrays = new ByteArray[numChunks];
    int index = 0;
    for (String ck : chunkKeys) {
        ByteArray ba = (ByteArray) vals.get(ck);
        if (ba == null)
            return null;
        byteArrays[index] = ba;
        ++index;
    }
    byte[] data = null;
    try {
        data = ByteArrayChunks.combine(byteArrays, toc);
    } catch (ServiceException e) {
        ZimbraLog.misc.warn("Unable to reassemble byte array from chunks", e);
    }
    return data;
}
Also used : ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ServiceException(com.zimbra.common.service.ServiceException) MemcachedClient(net.spy.memcached.MemcachedClient) ExecutionException(java.util.concurrent.ExecutionException) HashMap(java.util.HashMap) Map(java.util.Map) TimeoutException(java.util.concurrent.TimeoutException)

Example 50 with MemcachedClient

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

the class ZimbraMemcachedClient method put.

/**
 * Puts the key/value pair.
 * @param key
 * @param value
 * @param expirySec expiry in seconds
 * @param timeout in millis
 * @param waitForAck if true, block until ack'd or timeout; if false, return immediately
 * @return
 */
public boolean put(String key, Object value, int expirySec, long timeout, boolean waitForAck) {
    MemcachedClient client;
    synchronized (this) {
        client = mMCDClient;
        if (expirySec == DEFAULT_EXPIRY)
            expirySec = mDefaultExpiry;
        if (timeout == DEFAULT_TIMEOUT)
            timeout = mDefaultTimeout;
    }
    if (client == null)
        return false;
    Future<Boolean> future = client.set(key, expirySec, value);
    if (waitForAck) {
        Boolean success = null;
        try {
            success = future.get(timeout, TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            ZimbraLog.misc.warn("memcached set timed out after " + timeout + "ms", e);
            future.cancel(false);
        } catch (InterruptedException e) {
            ZimbraLog.misc.warn("InterruptedException during memcached set operation", e);
        } catch (ExecutionException e) {
            ZimbraLog.misc.warn("ExecutionException during memcached set operation", e);
        }
        return success != null && success.booleanValue();
    } else {
        return true;
    }
}
Also used : MemcachedClient(net.spy.memcached.MemcachedClient) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

MemcachedClient (net.spy.memcached.MemcachedClient)52 Test (org.junit.Test)22 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)20 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)17 InetSocketAddress (java.net.InetSocketAddress)11 ExecutionException (java.util.concurrent.ExecutionException)9 ConnectionFactoryBuilder (net.spy.memcached.ConnectionFactoryBuilder)8 ConnectionFactory (net.spy.memcached.ConnectionFactory)7 IOException (java.io.IOException)6 TimeoutException (java.util.concurrent.TimeoutException)6 Map (java.util.Map)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 BinaryConnectionFactory (net.spy.memcached.BinaryConnectionFactory)3 Predicate (com.google.common.base.Predicate)2 Supplier (com.google.common.base.Supplier)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableSet (com.google.common.collect.ImmutableSet)2 Config (com.hazelcast.config.Config)2