Skip to content

Commit 18667b6

Browse files
committed
Adding in Elasticache support
1 parent 1eaecfc commit 18667b6

File tree

10 files changed

+115
-118
lines changed

10 files changed

+115
-118
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version=0.90.4
1+
version=0.90.7
22
springVersion=4.3.3.RELEASE
33
springBootVersion=1.4.1.RELEASE
44
jerseyVersion=2.24

micro-elasticache/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ repositories {
88
description = 'micro-elasticache'
99
dependencies {
1010
compile group: 'com.amazonaws', name: 'aws-java-sdk-elasticache', version: '1.10.42'
11+
compile group: 'net.spy', name: 'spymemcached', version: '2.12.3'
1112
compile project(':micro-core')
1213
compile project(':micro-guava')
1314
testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version:'2.3.3'

micro-elasticache/src/main/java/com/aol/micro/server/elasticache/CacheService.java

Lines changed: 0 additions & 19 deletions
This file was deleted.

micro-elasticache/src/main/java/com/aol/micro/server/elasticache/ConfigureElasticache.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import net.spy.memcached.auth.AuthDescriptor;
1616
import net.spy.memcached.auth.PlainCallbackHandler;
1717
import net.spy.memcached.MemcachedClient;
18+
import org.springframework.beans.factory.annotation.Autowired;
1819
import org.springframework.beans.factory.annotation.Value;
1920
import org.springframework.context.annotation.Bean;
2021
import org.springframework.context.annotation.Configuration;
@@ -29,35 +30,43 @@
2930
@Configuration
3031
public class ConfigureElasticache {
3132

32-
private ClientMode mode = ClientMode.Dynamic;
3333

34-
@Value("${elasticache.hostname:lana-staging-ec.6o3auf.cfg.use1.cache.amazonaws.com")
35-
private String hostname = "lana-staging-ec.6o3auf.cfg.use1.cache.amazonaws.com";
36-
37-
@Value("${elasticache.port:6379")
38-
private int port = 6379;
39-
40-
@Value("${elasticache.retries:3")
41-
private int retries = 3;
42-
43-
@Value("${elastiache.retry.after.seconds:1")
44-
private int retryAfterSeconds = 1;
45-
46-
private MemcachedClient cache;
34+
private final String hostname;
35+
private final int port;
36+
private final int retryAfterSecs;
37+
private final int maxRetries;
38+
39+
@Autowired
40+
public ConfigureElasticache( @Value("${elasticache.hostname:null}") String hostname, //TO-DO remove this before review
41+
@Value("${elasticache.port:6379}") int port,
42+
@Value("${elasticache.retry.after.seconds:1}") int retryAfterSecs,
43+
@Value("${elasticache.max.retries:3}") int maxRetries) {
44+
this.hostname = hostname;
45+
this.port = port;
46+
this.retryAfterSecs = retryAfterSecs;
47+
this.maxRetries = maxRetries;
48+
}
4749

48-
InetSocketAddress socketAddress = new InetSocketAddress(hostname, port);
4950

50-
@Bean(name = "elasticacheClient")
51-
public TransientElasticacheDataConnection transientCache() throws IOException, URISyntaxException {
52-
log.info("Creating MemcacheClient for servers: {}", hostname);
53-
return new TransientElasticacheDataConnection(createMemcachedClient(socketAddress), retries, retryAfterSeconds);
51+
@Bean(name = "transientCache")
52+
public DistributedCacheManager transientCache() throws IOException, URISyntaxException {
53+
try {
54+
log.info("Creating Memcached Data connection for elasticache cluster: {}", hostname);
55+
return new TransientElasticacheDataConnection(createMemcachedClient(), retryAfterSecs, maxRetries);
56+
}
57+
catch (Exception e) {
58+
log.error("Failed to create transient data connection", e);
59+
return null;
60+
}
5461
}
5562

56-
private MemcachedClient createMemcachedClient(InetSocketAddress socketAddress) throws IOException {
57-
63+
@Bean(name = "memcachedClient")
64+
public MemcachedClient createMemcachedClient() throws IOException {
5865
try {
59-
return new MemcachedClient(socketAddress);
66+
log.info("Starting an instance of memcache client towards elasticache cluster");
67+
return new MemcachedClient(new InetSocketAddress(hostname, port));
6068
} catch (IOException e) {
69+
log.error("Could not initilise connection to elasticache cluster", e);
6170
e.printStackTrace();
6271
return null;
6372
}

micro-elasticache/src/main/java/com/aol/micro/server/elasticache/DistributedCacheManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
public interface DistributedCacheManager<V> {
77
void setConnectionTested(boolean result);
88
boolean isAvailable();
9-
boolean put(String key, int exp, V value);
9+
boolean add(String key, int exp, Object value);
1010
Optional<V> get(String key);
1111
}

micro-elasticache/src/main/java/com/aol/micro/server/elasticache/ElasticachePlugin.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
*/
99
public class ElasticachePlugin implements Plugin {
1010

11-
public PSetX<Class> springClasses() {
11+
@Override
12+
public PSetX<Class> springClasses() {
1213
return PSetX.of(ConfigureElasticache.class);
1314
}
14-
}
15+
}

micro-elasticache/src/main/java/com/aol/micro/server/elasticache/TransientElasticacheDataConnection.java

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,53 @@
44

55
import java.util.Optional;
66
import net.spy.memcached.MemcachedClient;
7-
87
import lombok.extern.slf4j.Slf4j;
9-
import org.springframework.stereotype.Component;
10-
11-
import java.util.Optional;
128

139
/**
1410
* Created by gordonmorrow on 5/3/17.
1511
*/
16-
@Component
12+
1713
@Slf4j
1814
public class TransientElasticacheDataConnection<V> implements DistributedCacheManager<V> {
1915

2016
private volatile boolean available = false;
2117
private final MemcachedClient memcachedClient;
22-
private final int maxTry, retryAfterSec;
18+
private final int retryAfterSec;
19+
private final int maxTry;
2320

24-
public TransientElasticacheDataConnection(MemcachedClient memcachedClient, final int maxTry, final int retryAfterSec) {
21+
public TransientElasticacheDataConnection(MemcachedClient memcachedClient,int retryAfterSec, int maxTry) {
2522
this.memcachedClient = memcachedClient;
26-
this.maxTry = maxTry;
2723
this.retryAfterSec = retryAfterSec;
24+
this.maxTry = maxTry;
2825
}
2926

3027
@Override
31-
public boolean put(final String key, int exp, final V value) {
28+
public boolean add(final String key, int exp, final Object value) {
3229

33-
log.trace("put '{}', value:{}", key, value);
30+
log.trace("Memcached add operation on key '{}', with value:{}", key, value);
3431
boolean success = false;
3532
int tryCount = 0;
3633

3734
do {
3835
try {
3936
if (tryCount > 0) {
4037
Thread.sleep(retryAfterSec * 1000);
41-
log.warn("retry #{}", tryCount);
38+
log.warn("retrying operation #{}", tryCount);
4239
}
4340
tryCount++;
44-
success = memcachedClient.set(key, exp, value)
41+
success = memcachedClient.add(key, exp, value)
4542
.get();
4643
} catch (final Exception e) {
47-
log.warn("memcache put: {}", e.getMessage());
44+
log.warn("memcache set: {}", e.getMessage());
45+
System.out.println(e.getMessage());
4846
}
4947
} while (!success && tryCount < maxTry);
5048

5149
if (!success) {
5250
log.error("Failed to add key to Elasticache {}", key);
5351
}
5452
if (success && tryCount > 1) {
55-
log.info("Connection restored OK");
53+
log.info("Connection restored OK to Elasticache cluster");
5654
}
5755

5856
available = success;
@@ -73,6 +71,4 @@ public boolean isAvailable() {
7371
public final void setConnectionTested(final boolean available) {
7472
this.available = available;
7573
}
76-
77-
}
78-
74+
}

micro-elasticache/src/test/java/com/aol/micros/server/elasticache/ConfigureElasticacheTest.java

Lines changed: 0 additions & 56 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.aol.micros.server.elasticache;
2+
3+
/**
4+
* Created by gordonmorrow on 5/9/17.
5+
*/
6+
import static org.mockito.Mockito.mock;
7+
import static org.mockito.Mockito.stub;
8+
import static org.junit.Assert.assertEquals;
9+
import com.aol.micro.server.elasticache.TransientElasticacheDataConnection;
10+
import net.spy.memcached.internal.OperationFuture;
11+
import org.junit.Before;
12+
import org.junit.Test;
13+
import net.spy.memcached.MemcachedClient;
14+
import java.util.Optional;
15+
16+
public class TransientElasticacheDataConnectionTest {
17+
18+
MemcachedClient memcachedClient;
19+
20+
@Before
21+
public void setup() {
22+
memcachedClient = mock(MemcachedClient.class);
23+
24+
stub(memcachedClient.get("key1")).toReturn("value1");
25+
stub(memcachedClient.get("key2")).toReturn("value2");
26+
OperationFuture<Boolean> mockedFuture = mock(OperationFuture.class);
27+
stub(memcachedClient.add("keyAdd", 3600, "valueadd")).toReturn(mockedFuture);
28+
}
29+
30+
@Test
31+
public void happyPathGetTest() {
32+
TransientElasticacheDataConnection transientClient = new TransientElasticacheDataConnection(memcachedClient, 3, 1);
33+
assertEquals(Optional.ofNullable("value1"), transientClient.get("key1"));
34+
assertEquals(Optional.ofNullable("value2"), transientClient.get("key2"));
35+
}
36+
37+
@Test
38+
public void notExistingKeyGetTest() {
39+
TransientElasticacheDataConnection transientClient = new TransientElasticacheDataConnection(memcachedClient, 3, 1);
40+
assertEquals(Optional.empty(), transientClient.get("key3"));
41+
}
42+
43+
@Test
44+
public void notExistingKeyPutTest() {
45+
TransientElasticacheDataConnection transientClient = new TransientElasticacheDataConnection(memcachedClient, 3, 1);
46+
assertEquals(false, transientClient.add("keyAdd", 3600, "valueadd"));
47+
}
48+
49+
@Test
50+
public void testIsAvailableFalse() {
51+
TransientElasticacheDataConnection transientClient = new TransientElasticacheDataConnection(memcachedClient, 3, 1);
52+
transientClient.setConnectionTested(false);
53+
assertEquals(false, transientClient.isAvailable());
54+
}
55+
56+
@Test
57+
public void testIsAvailableTrue() {
58+
TransientElasticacheDataConnection transientClient = new TransientElasticacheDataConnection(memcachedClient, 3, 1);
59+
transientClient.setConnectionTested(true);
60+
assertEquals(true, transientClient.isAvailable());
61+
}
62+
63+
64+
65+
}

0 commit comments

Comments
 (0)