Skip to content

Commit 3a1e786

Browse files
rajaprabhuXinzhi Zou
authored andcommitted
Auto Discovery support for Amazon ElastiCache
* Introducing Dynamic and Static client modes for the memcached client. Dynamic allows the client to automatically pick up the node list using the ElastiCache configuration endpoint throughout the lifetime of the client object. Static mode is the existing behavior, where the client is initialized only once using the configuration provided by the user. * Support for a new configuration command used in ElastiCache Nodes. This command supports the config get, set and delete commands in ASCII and binary mode. * Periodic poller to check for node list changes in the ElastiCache cluster and dynamically update client connections according to the configuration. This operates only in Dynamic mode. * In dynamic mode, if IP addresses are specified in the configuration, then the connection is established using the IP address directly. For more information, please see http://docs.amazonwebservices.com/AmazonElastiCache/latest/UserGuide/AutoDiscovery.html
1 parent 953c322 commit 3a1e786

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2598
-131
lines changed

LICENSE.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
Licensed under the Amazon Software License (the "License"). You may not use this
4+
file except in compliance with the License. A copy of the License is located at
5+
http://aws.amazon.com/asl/
6+
or in the "license" file accompanying this file. This file is distributed on
7+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
8+
implied. See the License for the specific language governing permissions and
9+
limitations under the License.
10+
11+
Derived from spymemcached 2.8.1 under the MIT license.
12+
113
Copyright (c) 2006-2009 Dustin Sallings
214
Copyright (c) 2009-2011 Couchbase, Inc.
315

README.markdown

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,20 @@
11
# Building
22

3-
Spymemcached can be compiled using Apache Ant by running the following
3+
AmazonElastiCacheClusterClient can be compiled using Apache Ant by running the following
44
command:
55

66
ant
77

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

11-
To run the Spymemcached tests against Membase Server run the
12-
following command:
13-
14-
ant test -Dserver.type=membase
15-
16-
To test Spymemcached against Membase running on a different host
17-
use the following command:
18-
19-
ant test -Dserver.type=membase \
20-
-Dserver.address_v4=ip_address_of_membase
11+
More test info will be updated shortly.
2112

2213
# Testing
2314

24-
The latest version of spymemcached has a set of command line arguments
15+
_Note: The ant test target is in the process of being updated to run the additional tests written for Auto Discovery._
16+
17+
The latest version of AmazonElastiCacheClusterClient has a set of command line arguments
2518
that can be used to configure the location of your testing server. The
2619
arguments are listed below.
2720

@@ -30,12 +23,6 @@ arguments are listed below.
3023
This argument is used to specify the ipv4 address of your testing
3124
server. By default it is set to localhost.
3225

33-
-Dserver.address_v6=ipv6_address_of_testing_server
34-
35-
This argument is used to set the ipv6 address of your testing server.
36-
By default it is set to ::1. If an ipv6 address is specified then an
37-
ipv4 address must be specified otherwise there may be test failures.
38-
3926
-Dserver.port_number=port_number_of_memcached
4027

4128
This argument is used when memcahched is started on a port other than
@@ -46,11 +33,13 @@ This argument is used when memcahched is started on a port other than
4633
This argument is used for CI testing where certain unit tests might
4734
be temporarily failing.
4835

49-
# More Information
36+
# More Information for Amazon ElastiCache Cluster Client
37+
Github link: https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java
38+
This repository is a fork of the spymemcached Java client for connecting to memcached (specifically the https://github.com/dustin/java-memcached-client repo).
5039

51-
For more information about Spymemcached see the links below:
40+
Additional changes have been made to support Amazon ElastiCache Auto Discovery. To read more about Auto Discovery, please go here: http://docs.amazonwebservices.com/AmazonElastiCache/latest/UserGuide/AutoDiscovery.html.
5241

53-
## Project Page The
42+
For more information about Spymemcached see the links below:
5443

5544
[Spymemcached Project Home](http://code.google.com/p/spymemcached/)
5645
contains a wiki, issue tracker, and downloads section.

build.xml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@
1818
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1919
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
SOFTWARE.
21+
22+
Portions Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
23+
24+
Licensed under the Amazon Software License (the "License"). You may not use this
25+
file except in compliance with the License. A copy of the License is located at
26+
http://aws.amazon.com/asl/
27+
or in the "license" file accompanying this file. This file is distributed on
28+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
29+
implied. See the License for the specific language governing permissions and
30+
limitations under the License.
2131
-->
2232

23-
<project name="spymemcached" default="package"
33+
<project name="AmazonElastiCacheClusterClient" default="package"
2434
xmlns:artifact="urn:maven-artifact-ant"
2535
xmlns:ivy="antlib:org.apache.ivy.ant">
2636

@@ -31,9 +41,9 @@
3141
</classpath>
3242
</taskdef>
3343

34-
<property name="name" value="spymemcached"/>
35-
<property name="copyright" value="2006-2011 Dustin Sallings, Matt Ingenthron" />
36-
<property name="group" value="net.spy" />
44+
<property name="name" value="AmazonElastiCacheClusterClient"/>
45+
<property name="copyright" value="2006-2011 Dustin Sallings, Matt Ingenthron, 2012-2012 Amazon.com, Inc. or its affiliates" />
46+
<property name="group" value="spy" />
3747

3848
<property name="base.src.dir" value="${basedir}/src" />
3949
<property name="build.dir" value="${basedir}/build" />

ivy/libraries.properties

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
# SOFTWARE.
20+
21+
# Portions Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
22+
23+
# Licensed under the Amazon Software License (the "License"). You may not use this
24+
# file except in compliance with the License. A copy of the License is located at
25+
# http://aws.amazon.com/asl/
26+
# or in the "license" file accompanying this file. This file is distributed on
27+
# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
28+
# implied. See the License for the specific language governing permissions and
29+
# limitations under the License.
2030

2131
checkstyle.version=5.0
2232

@@ -26,7 +36,7 @@ ivy.version=2.2.0
2636
mvn.version=2.0.10
2737

2838
jmock.version=1.2.0
29-
junit.version=4.7
39+
junit.version=4.8.2
3040
log4j.version=1.2.16
3141
slf4j.version=1.7.5
3242
spring-beans.version=3.0.3.RELEASE

ivy/spymemcached.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,23 @@
2020
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
23+
24+
Portions Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
25+
26+
Licensed under the Amazon Software License (the "License"). You may not use this
27+
file except in compliance with the License. A copy of the License is located at
28+
http://aws.amazon.com/asl/
29+
or in the "license" file accompanying this file. This file is distributed on
30+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
31+
implied. See the License for the specific language governing permissions and
32+
limitations under the License.
2333
-->
2434
<ivy-module version="1.0">
2535
<info organisation="spy" module="${name}" revision="${version}">
26-
<license name="Apache 2.0"/>
27-
<ivyauthor name="Couchbase" url="http://github.com/dustin/java-memcached-client" />
36+
<license name="Amazon Software License"/>
37+
<ivyauthor name="Amazon.com Inc" url="https://github.com/amazonwebservices/aws-elasticache-cluster-client-memcached-for-java" />
2838
<description>
29-
Spymemcached
39+
AmazonElastiCacheClusterClient
3040
</description>
3141
</info>
3242
<configurations defaultconfmapping="default">

src/main/java/net/spy/memcached/AddrUtil.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2020
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
2121
* IN THE SOFTWARE.
22+
*
23+
*
24+
* Portions Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
25+
*
26+
* Licensed under the Amazon Software License (the "License"). You may not use this
27+
* file except in compliance with the License. A copy of the License is located at
28+
* http://aws.amazon.com/asl/
29+
* or in the "license" file accompanying this file. This file is distributed on
30+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
31+
* implied. See the License for the specific language governing permissions and
32+
* limitations under the License.
2233
*/
2334

2435
package net.spy.memcached;
@@ -28,10 +39,14 @@
2839
import java.util.ArrayList;
2940
import java.util.List;
3041

42+
import net.spy.memcached.config.ClusterConfiguration;
43+
import net.spy.memcached.config.NodeEndPoint;
44+
3145
/**
3246
* Convenience utilities for simplifying common address parsing.
3347
*/
3448
public final class AddrUtil {
49+
public static final char HOST_CONFIG_DELIMITER = '|';
3550

3651
private AddrUtil() {
3752
// Empty
@@ -103,4 +118,64 @@ public static List<InetSocketAddress> getAddresses(List<String> servers) {
103118
}
104119
return addrs;
105120
}
121+
122+
/**
123+
* Parse response from getConfig for cluster type.
124+
* version number
125+
* hostname1|ipaddress1|port hostname2|ipaddress2|port
126+
*
127+
* returns the ClusterConfiguration object which contains the parsed results.
128+
*/
129+
public static ClusterConfiguration parseClusterTypeConfiguration(String configurationResponse) {
130+
if (configurationResponse == null) {
131+
throw new NullPointerException("Null configuration");
132+
}
133+
if (configurationResponse.trim().equals("")) {
134+
throw new IllegalArgumentException("No configuration in the response:" + configurationResponse);
135+
}
136+
String[] lines = configurationResponse.trim().split("(?:\\r?\\n)");
137+
if(lines == null || lines.length != 2) {
138+
throw new IllegalArgumentException("Incorrect response format. Response:" + configurationResponse);
139+
}
140+
141+
String versionString = lines[0].trim();
142+
if(versionString.equals("")){
143+
throw new IllegalArgumentException("Version number is missing. Response:" + configurationResponse);
144+
}
145+
146+
long versionNumber = Long.parseLong(versionString);
147+
148+
String hostList = lines[1].trim();
149+
if (hostList.equals("")) {
150+
throw new IllegalArgumentException("Empty host list in the response:" + configurationResponse);
151+
}
152+
153+
List<NodeEndPoint> endPoints = new ArrayList<NodeEndPoint>();
154+
155+
for (String hostDetails : hostList.split("(?:\\s)+")) {
156+
if (hostDetails.equals("")) {
157+
continue;
158+
}
159+
160+
int firstDelimiter = hostDetails.indexOf(HOST_CONFIG_DELIMITER);
161+
int secondDelimiter = hostDetails.lastIndexOf(HOST_CONFIG_DELIMITER);
162+
if (firstDelimiter < 1 || firstDelimiter == secondDelimiter) {
163+
throw new IllegalArgumentException("Invalid server ''" + hostDetails
164+
+ "'' in response: " + configurationResponse);
165+
}
166+
String hostName = hostDetails.substring(0, firstDelimiter).trim();
167+
String ipAddress = hostDetails.substring(firstDelimiter+1, secondDelimiter).trim();
168+
String portNum = hostDetails.substring(secondDelimiter + 1).trim();
169+
int port = Integer.parseInt(portNum);
170+
171+
NodeEndPoint endPoint = new NodeEndPoint(hostName, ipAddress, port);
172+
endPoints.add(endPoint);
173+
}
174+
assert !endPoints.isEmpty() : "No endpoints found";
175+
176+
ClusterConfiguration config = new ClusterConfiguration(versionNumber, endPoints);
177+
178+
return config;
179+
}
180+
106181
}

src/main/java/net/spy/memcached/BinaryConnectionFactory.java

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@
1919
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2020
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALING
2121
* IN THE SOFTWARE.
22+
*
23+
*
24+
* Portions Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
25+
*
26+
* Licensed under the Amazon Software License (the "License"). You may not use this
27+
* file except in compliance with the License. A copy of the License is located at
28+
* http://aws.amazon.com/asl/
29+
* or in the "license" file accompanying this file. This file is distributed on
30+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
31+
* implied. See the License for the specific language governing permissions and
32+
* limitations under the License.
2233
*/
2334

2435
package net.spy.memcached;
@@ -35,18 +46,39 @@
3546
public class BinaryConnectionFactory extends DefaultConnectionFactory {
3647

3748
/**
38-
* Create a DefaultConnectionFactory with the default parameters.
49+
* Create a BinaryConnectionFactory with the default parameters.
3950
*/
4051
public BinaryConnectionFactory() {
4152
super();
4253
}
54+
55+
/**
56+
* Create a BinaryConnectionFactory with the given clientMode.
57+
* @param clientMode
58+
*/
59+
public BinaryConnectionFactory(ClientMode clientMode){
60+
super(clientMode);
61+
}
4362

4463
/**
45-
* Create a BinaryConnectionFactory with the given maximum operation queue
46-
* length, and the given read buffer size.
64+
* Create a BinaryConnectionFactory with the given parameters
65+
*
66+
* @param len the queue length.
67+
* @param bufSize the buffer size
4768
*/
4869
public BinaryConnectionFactory(int len, int bufSize) {
49-
super(len, bufSize);
70+
super(ClientMode.Dynamic, len, bufSize);
71+
}
72+
73+
/**
74+
* Create a BinaryConnectionFactory with the given parameters
75+
*
76+
* @param clientMode the mode of the client to indicate whether dynamic server list management is done.
77+
* @param len the queue length.
78+
* @param bufSize the buffer size
79+
*/
80+
public BinaryConnectionFactory(ClientMode clientMode, int len, int bufSize) {
81+
super(clientMode, len, bufSize);
5082
}
5183

5284
/**
@@ -57,7 +89,19 @@ public BinaryConnectionFactory(int len, int bufSize) {
5789
* @param hash the algorithm to use for hashing
5890
*/
5991
public BinaryConnectionFactory(int len, int bufSize, HashAlgorithm hash) {
60-
super(len, bufSize, hash);
92+
super(ClientMode.Dynamic, len, bufSize, hash);
93+
}
94+
95+
/**
96+
* Construct a BinaryConnectionFactory with the given parameters.
97+
*
98+
* @param clientMode the mode of the client to indicate whether dynamic server list management is done.
99+
* @param len the queue length.
100+
* @param bufSize the buffer size
101+
* @param hash the algorithm to use for hashing
102+
*/
103+
public BinaryConnectionFactory(ClientMode clientMode, int len, int bufSize, HashAlgorithm hash) {
104+
super(clientMode, len, bufSize, hash);
61105
}
62106

63107
@Override
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright (C) 2012-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Amazon Software License (the "License"). You may not use this
5+
* file except in compliance with the License. A copy of the License is located at
6+
* http://aws.amazon.com/asl/
7+
* or in the "license" file accompanying this file. This file is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, express or
9+
* implied. See the License for the specific language governing permissions and
10+
* limitations under the License.
11+
*/
12+
package net.spy.memcached;
13+
14+
/**
15+
* The modes in which the client can operate.
16+
*/
17+
public enum ClientMode {
18+
19+
/**
20+
* In Static Client mode, the set of endpoints specified during initialization is used throughout the lifetime of the client object.
21+
*/
22+
Static,
23+
/**
24+
* In Dynamic Client mode, the set of cache node endpoints and any updates to it is dynamically managed in this mode.
25+
* The client is initialized with a configuration endpoint. The client will periodically learn about the cache nodes in the
26+
* cluster.
27+
*/
28+
Dynamic
29+
}

0 commit comments

Comments
 (0)