Giter VIP home page Giter VIP logo

awslabs / aws-elasticache-cluster-client-memcached-for-java Goto Github PK

View Code? Open in Web Editor NEW

This project forked from dustin/java-memcached-client

86.0 35.0 55.0 3.31 MB

Amazon ElastiCache Cluster Client for Java - enhanced library to connect to ElastiCache clusters.

Home Page: http://docs.amazonwebservices.com/AmazonElastiCache/latest/UserGuide/AutoDiscovery.html

License: Apache License 2.0

Java 98.90% Shell 0.27% XSLT 0.40% HTML 0.44%

aws-elasticache-cluster-client-memcached-for-java's Introduction

Amazon ElastiCache Cluster Client

Build Status

Amazon ElastiCache Cluster Client is an enhanced Java library to connect to ElastiCache clusters. This client library has been built upon Spymemcached and is released under the Apache 2.0 License.

Building

Amazon ElastiCache Cluster Client can be compiled using Apache Ant by running the following command:

ant

This will generate binary, source, and javadoc jars in the build directory of the project.

Configuring the client in TLS mode

As memcached supports TLS since version 1.5.13, Amazon ElastiCache Cluster Client added TLS support for better security.

In order to create a client in TLS mode, do the following to initialize the client with the appropriate SSLContext:

import java.security.cert.CertificateFactory;
import java.security.KeyStore;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import net.spy.memcached.AddrUtil;
import net.spy.memcached.ConnectionFactoryBuilder;
import net.spy.memcached.MemcachedClient;
public class TLSDemo {
    public static void main(String[] args) throws Exception {
        ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder();
        // Build SSLContext
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init((KeyStore) null);
        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, tmf.getTrustManagers(), null);
        // Create the client in TLS mode
        connectionFactoryBuilder.setSSLContext(sslContext);
        // TLS mode enables hostname verification by default. It is always recommended to do that.
        connectionFactoryBuilder.setHostnameForTlsVerification("my_website.com");
        // To disable hostname verification, do the following:
        // connectionFactoryBuilder.setSkipTlsHostnameVerification(true);
        MemcachedClient client = new MemcachedClient(connectionFactoryBuilder.build(), AddrUtil.getAddresses("my_website.com:11211"));
        // Store a data item
        client.set("theKey", 3600, "This is the data value");
    }
}

To create the TLS mode client with customized TLS certificate, initialize the SSLContext as follows:

InputStream inputStream = new FileInputStream("/tmp/my_certificate");
KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
ks.load(null);
ks.setCertificateEntry("cert", CertificateFactory.getInstance("X.509").generateCertificate(inputStream));
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(ks);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, tmf.getTrustManagers(), null);

The rest of the logic to create the client follows.

Testing

The latest version of Amazon ElastiCache Cluster Client supports unit tests and integration tests.

Unit Tests

Unit tests do not require any running memcached servers, and can be run using Apache Ant by the following command:

ant test

Integration Tests

Integration tests are always run against local memcached servers. Start integration tests by the following command:

ant it

NOTE: Integration tests will start servers with ports: 11200, 11201, 11211, 11212, 22211, 22212. The integration tests can not run if one or more of those ports are using.

It has a set of command line arguments that can be used to configure your client mode, your local testing server, your type of testing server, and your certificates directory (needed for TLS mode). The arguments are listed below.

-Dclient.mode=memcached_client_mode

This argument is used to specify the mode of the client that you want to run. Supported options are Static and Dynamic. Dynamic mode enables Auto Discovery feature. Static mode runs without Auto Discovery and has the same functionality as a classic spymemcached client. By default it is set to Static.

-Dserver.bin=local_binary_of_testing_server

This argument is used to specify the location of your testing server binary. By default it is set to /usr/bin/memcached.

-Dserver.type=type_of_testing_server

This argument is used to specify the type of your testing server. Supported options are oss and elasticache. By default it is set to oss.

Additional argument for running integration tests with TLS mode

-Dcert.folder=certificates_folder_of_testing_server

This argument is used to specify the folder of the 2 certificates for starting memcached server with TLS enabled. Named those 2 certificates as private.cert and public.cert. This is mandatory if you want to run integration tests with TLS mode. Besides, your testing server should be built with TLS capability. See instruction: https://github.com/memcached/memcached/wiki/TLS

More Information for Amazon ElastiCache Cluster Client

Github link: https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java. This repository is a fork of the spymemcached Java client for connecting to memcached (specifically the https://github.com/dustin/java-memcached-client repo).

Additional changes have been made to support Amazon ElastiCache Auto Discovery. To read more about Auto Discovery, please go here: https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/AutoDiscovery.html.

For more information about Spymemcached see the link below:

Spymemcached Project Home contains a wiki, issue tracker, and downloads section.

aws-elasticache-cluster-client-memcached-for-java's People

Contributors

bairon avatar blair avatar chinguyen21 avatar ciaranj avatar daschl avatar deeptida avatar dependabot[bot] avatar dustin avatar fabienrenaud avatar hadanas avatar ingenthr avatar kevin-laff avatar kreide avatar magictoken avatar mattbertolini avatar mschoch avatar nbrachet avatar nelz9999 avatar pasha407 avatar paulisio avatar quchen88 avatar ragsns avatar rajaprabhu avatar raykrueger avatar shawn-higgins1 avatar sideshowcoder avatar spatsatzis avatar suryanarayanan avatar tootedom avatar yuetang95 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

aws-elasticache-cluster-client-memcached-for-java's Issues

Build failed with error "Source option 6 is no longer supported. Use 7 or later."

[ivy:cachepath] DEPRECATED: 'ivy.conf.file' is deprecated, use 'ivy.settings.file' instead
[ivy:cachepath] :: loading settings :: file = /tmp/aws-elasticache-cluster-client-memcached-for-java/ivy/ivysettings.xml

compile:
[mkdir] Created dir: /tmp/aws-elasticache-cluster-client-memcached-for-java/build/classes
[javac] Compiling 224 source files to /tmp/aws-elasticache-cluster-client-memcached-for-java/build/classes
[javac] warning: [options] bootstrap class path not set in conjunction with -source 6
[javac] error: Source option 6 is no longer supported. Use 7 or later.
[javac] error: Target option 6 is no longer supported. Use 7 or later.

BUILD FAILED
/tmp/aws-elasticache-cluster-client-memcached-for-java/build.xml:511: Compile failed; see the compiler error output for details.

Timed out getting config from Elasticache cluster can fail MemcachedClient's constructor instead of falling back to configuration poller

We're using Dynamic client mode and the latest version of the client (1.2.2). Occasionally we found some of our application nodes failing to start because the constructor of MemcachedClient threw RuntimeException when our Memcached cluster was under heavy load.

Stacktrace:

Caused by: java.lang.RuntimeException: Exception waiting for config
	at net.spy.memcached.MemcachedClient.getConfig(MemcachedClient.java:1953)
	at net.spy.memcached.MemcachedClient.initializeClientUsingConfigEndPoint(MemcachedClient.java:329)
	at net.spy.memcached.MemcachedClient.<init>(MemcachedClient.java:294)
	at net.spy.memcached.MemcachedClient.<init>(MemcachedClient.java:229)
... 69 common frames omitted
Caused by: java.util.concurrent.ExecutionException: net.spy.memcached.internal.CheckedOperationTimeoutException: Operation timed out. - failing node: /10.202.157.190:11211
	at net.spy.memcached.internal.OperationFuture.get(OperationFuture.java:180)
	at net.spy.memcached.internal.GetConfigFuture.get(GetConfigFuture.java:46)
	at net.spy.memcached.MemcachedClient.getConfig(MemcachedClient.java:1941)
	... 95 common frames omitted
Caused by: net.spy.memcached.internal.CheckedOperationTimeoutException: Operation timed out. - failing node: /10.202.157.190:11211
	... 98 common frames omitted

The issue is in this code block inside MemcachedClient:

    public <T> T getConfig(InetSocketAddress addr, ConfigurationType type, Transcoder<T> tc) {
        try {
            return this.asyncGetConfig(addr, type, tc).get(this.operationTimeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException var6) {
            throw new RuntimeException("Interrupted waiting for config", var6);
        } catch (OperationNotSupportedException var7) {
            throw var7;
        } catch (ExecutionException var8) {
            if (var8.getCause() instanceof OperationException) {
                OperationException exp = (OperationException)var8.getCause();
                if (OperationErrorType.GENERAL.equals(exp.getType())) {
                    throw new OperationNotSupportedException("This version of getConfig command is not supported.");
                }
            }

            throw new RuntimeException("Exception waiting for config", var8);
        } catch (TimeoutException var9) {
            throw new OperationTimeoutException("Timeout waiting for config", var9);
        }
    }

Since an OperationFuture is returned by the asyncGetConfig, sometimes a CheckedOperationTimeoutException wrapped inside a ExecutionException is thrown by OperationFuture.get when a timeout occurs instead of the usual TimeoutException from Future. This exception is then wrapped inside a RuntimeException causing the client ctor to fail immediately.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.