Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified interface #356

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.91.3
version=0.91.4
springVersion=4.3.3.RELEASE
springBootVersion=1.4.1.RELEASE
jerseyVersion=2.24
Expand Down
1 change: 1 addition & 0 deletions micro-couchbase/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dependencies {
compile project(':micro-manifest-comparator')
compile project(':micro-core')
compile project(':micro-guava')
compile project(':micro-events')
testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version:'2.3.3'
testCompile(group: 'org.spockframework', name: 'spock-core', version:'0.7-groovy-2.0') { exclude(module: 'groovy-all') }
testCompile group: 'com.cyrusinnovation', name: 'mockito-groovy-support', version:'1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.aol.micro.server.couchbase;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

/**
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we keeping these kind of comments in micro-server?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woops will remove...

* Created by gordonmorrow on 03/07/2017.
*/
@Configuration
public class ConfigureCacheTester {

@Autowired
private CouchbaseConnectionTester couchbaseConnectionTester;


@Scheduled(fixedDelay = 60000)
public synchronized void runCouchbaseConnectionTester(){
couchbaseConnectionTester.scheduleAndLog();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import com.aol.micro.server.couchbase.base.CouchbaseManifestComparator;
import com.aol.micro.server.couchbase.manifest.comparator.CouchbaseManifestComparator;
import com.couchbase.client.CouchbaseClient;
import com.couchbase.client.CouchbaseConnectionFactory;
import com.couchbase.client.CouchbaseConnectionFactoryBuilder;
Expand Down Expand Up @@ -45,15 +45,26 @@ public class ConfigureCouchbase {
@Value("${couchbaseClientOperationTimeout:120000}")
private long opTimeout;

@Value("${distributed.cache.default.expiration:691200}")
private int expiresAfterSeconds = 691200;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need to assign this value will be overwritten by Spring annotation same for line 52,55

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok removed the assignment


@Value("${distributed.cache.maxTry:5}")
private int maxTry = 5;

@Value("${distributed.cache.retryAfterSec:1}")
private int retryAfterSec = 1;

@SuppressWarnings("rawtypes")
@Bean(name = "couchbaseDistributedMap")
public CouchbaseDistributedMapClient simpleCouchbaseClient() throws IOException, URISyntaxException {
if (couchbaseClientEnabled) {
return new CouchbaseDistributedMapClient(
couchbaseClient());
couchbaseClient(), expiresAfterSeconds, maxTry,
retryAfterSec);
} else {
return new CouchbaseDistributedMapClient(
null);
null, expiresAfterSeconds, maxTry,
retryAfterSec);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.aol.micro.server.couchbase;

/**
* Created by gordonmorrow on 03/07/2017.
*/

import com.aol.micro.server.distributed.DistributedMap;
import com.aol.micro.server.events.ScheduledJob;
import com.aol.micro.server.events.SystemData;
import com.couchbase.client.CouchbaseClient;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

@Slf4j
public class CouchbaseConnectionTester implements ScheduledJob {

private static final Random random = new Random();

private final DistributedMap cache;
private final CouchbaseClient couchbaseClient;

public CouchbaseConnectionTester(DistributedMap cache, CouchbaseClient couchbaseClient) {

this.cache = cache;
this.couchbaseClient = couchbaseClient;
}

@Override
public SystemData scheduleAndLog() {

log.trace("runTestConnection()...");
boolean result = false;
try {
result = testConnection();
} catch (RuntimeException e) {
log.debug("Could not connect to Cache" + e.getMessage());
}
cache.setConnectionTested(result);

log.debug("Testing Couchbase connection: {}", result);
return null;

}

private boolean testConnection() {
String key = "PING_TEST";
log.trace("Testing connection using key {}", key);

int testValue = random.nextInt(1111);
couchbaseClient.set(key, 120, testValue);
int received = (Integer) couchbaseClient.get(key);

return received == testValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,112 @@
import java.util.concurrent.ExecutionException;

import com.aol.cyclops2.util.ExceptionSoftener;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aol.micro.server.distributed.DistributedMap;
import com.couchbase.client.CouchbaseClient;

@Slf4j
public class CouchbaseDistributedMapClient<V> implements DistributedMap<V> {

private final Logger logger = LoggerFactory.getLogger(getClass());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary when you have the lombok @slf4j annotation

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot thanks

private volatile boolean available = false;

private final Optional<CouchbaseClient> couchbaseClient;
private final int expiresAfterSeconds, maxTry, retryAfterSec;

public CouchbaseDistributedMapClient(CouchbaseClient couchbaseClient) {
public CouchbaseDistributedMapClient(CouchbaseClient couchbaseClient, final int expiresAfterSeconds,
final int maxTry, final int retryAfterSec) {

this.couchbaseClient = Optional.ofNullable(couchbaseClient);
this.expiresAfterSeconds = expiresAfterSeconds;
this.maxTry = maxTry;
this.retryAfterSec = retryAfterSec;
}

@Override
public boolean put(final String key, final V value) {

log.trace("put '{}', value:{}", key, value);
boolean success = false;
int tryCount = 0;

do {
try {
if (tryCount > 0) {
Thread.sleep(retryAfterSec * 1000);
log.warn("retry #{}", tryCount);
}
tryCount++;
success = couchbaseClient.map(c -> putInternal(c, key, value))
.orElse(false);

} catch (final Exception e) {

log.warn("memcache put: {}", e.getMessage());
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mean couchbase instead of memcache ?

}
} while (!success && tryCount < maxTry);

if (!success) {
log.error("Failed to place item in couchbase");
}
if (success && tryCount > 1) {
log.info("Connection restored OK");
}

available = success;

return success;
}

@Override
public boolean put(final String key, int expiry, final V value) {
logger.debug("put '{}', value:{}", key, value);
return couchbaseClient.map(c -> putInternal(c, key, value))
.orElse(false);
boolean success = false;
int tryCount = 0;

do {
try {
if (tryCount > 0) {
Thread.sleep(retryAfterSec * 1000);
log.warn("retry #{}", tryCount);
}
tryCount++;
success = couchbaseClient.map(c -> putInternalWithExpiry(c, key, value, expiry))
.orElse(false);
} catch (final Exception e) {

log.warn("memcache put: {}", e.getMessage());
}
} while (!success && tryCount < maxTry);

if (!success) {
log.error("Failed to place item in couchbase");
}
if (success && tryCount > 1) {
log.info("Connection restored OK");
}

available = success;

return success;

}

private boolean putInternalWithExpiry(final CouchbaseClient client, final String key, final V value, int expiry) {

try {
return client.set(key,expiry, value)
.get();
} catch (InterruptedException | ExecutionException e) {
throw ExceptionSoftener.throwSoftenedException(e);

}
}


private boolean putInternal(final CouchbaseClient client, final String key, final V value) {

try {
Expand All @@ -50,4 +131,5 @@ public Optional<V> get(String key) {
public void delete(String key) {
couchbaseClient.map(c -> c.delete(key));
}

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.aol.micro.server.couchbase.base;
package com.aol.micro.server.couchbase.manifest.comparator;

import java.util.Date;
import java.util.Optional;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public void stopServer() {
}

@Test
@Ignore
public void runAppAndBasicTest() throws InterruptedException, ExecutionException {
rest.get("http://localhost:8080/simple-app/couchbase/put");
assertThat(rest.get("http://localhost:8080/simple-app/couchbase/get"), containsString("world"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class SimpleCouchbaseClientConnectionTest {
@Before
public void setup() {
client = Mockito.mock(CouchbaseClient)
con = new CouchbaseDistributedMapClient(client)
con = new CouchbaseDistributedMapClient(client,1,1,1)
}
@Test
public void testDelete() {
Expand All @@ -35,7 +35,7 @@ class SimpleCouchbaseClientConnectionTest {

@Test
public void testGetDistributedCacheDisabled() {
con = new CouchbaseDistributedMapClient(null)
con = new CouchbaseDistributedMapClient(null,1,1,1)
Optional result = con.get("key")
assertThat(result, is(Optional.empty()))
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.aol.micro.server.couchbase.base;
package com.aol.micro.server.couchbase.manifest.comparator;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package com.aol.micro.server.couchbase.manifest.comparator;
2 changes: 2 additions & 0 deletions micro-elasticache/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ dependencies {
compile group: 'net.spy', name: 'spymemcached', version: '2.12.3'
compile project(':micro-core')
compile project(':micro-guava')
compile project(':micro-events')
compile project(':micro-manifest-comparator')
testCompile group: 'org.codehaus.groovy', name: 'groovy-all', version:'2.3.3'
testCompile(group: 'org.spockframework', name: 'spock-core', version:'0.7-groovy-2.0') { exclude(module: 'groovy-all') }
testCompile group: 'com.cyrusinnovation', name: 'mockito-groovy-support', version:'1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.aol.micro.server.elasticache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

/**
* Created by gordonmorrow on 03/07/2017.
*/
@Configuration
public class ConfigureCacheTester {

@Autowired
private ElasticacheConnectionTester elasticacheConnectionTester;


@Scheduled(fixedDelay = 60000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe expose this as a property ? but have it default to 60000

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed as @scheduled expects a constant I needed to assign it directly in code

public synchronized void runElasticacheConnectionTester(){
elasticacheConnectionTester.scheduleAndLog();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,20 @@



import com.aol.micro.server.distributed.DistributedMap;
import lombok.extern.slf4j.Slf4j;
import net.spy.memcached.*;
import net.spy.memcached.MemcachedClient;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;


import net.spy.memcached.auth.AuthDescriptor;
import net.spy.memcached.auth.PlainCallbackHandler;
import net.spy.memcached.MemcachedClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import java.net.InetSocketAddress;

import java.util.List;
import java.util.Optional;

@Slf4j
@Configuration
public class ConfigureElasticache {
Expand All @@ -47,7 +39,7 @@ public ConfigureElasticache( @Value("${elasticache.hostname:null}") String hostn


@Bean(name = "transientCache")
public DistributedCacheManager transientCache() throws IOException, URISyntaxException {
public DistributedMap transientCache() throws IOException, URISyntaxException {
try {
log.info("Creating Memcached Data connection for elasticache cluster: {}", hostname);
return new TransientElasticacheDataConnection(createMemcachedClient(), retryAfterSecs, maxRetries);
Expand Down

This file was deleted.

Loading