-
Notifications
You must be signed in to change notification settings - Fork 212
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
base: master
Are you sure you want to change the base?
Unified interface #356
Changes from 4 commits
78c73da
d1baf27
b1de0e0
ab47860
c129319
5d2fe57
27f84fc
bcf247c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
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; | ||
|
||
/** | ||
* 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 |
---|---|---|
|
@@ -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; | ||
|
@@ -45,15 +45,26 @@ public class ConfigureCouchbase { | |
@Value("${couchbaseClientOperationTimeout:120000}") | ||
private long opTimeout; | ||
|
||
@Value("${distributed.cache.default.expiration:691200}") | ||
private int expiresAfterSeconds = 691200; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
|
||
|
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 |
---|---|---|
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary when you have the lombok @slf4j annotation There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
|
@@ -50,4 +131,5 @@ public Optional<V> get(String key) { | |
public void delete(String key) { | ||
couchbaseClient.map(c -> c.delete(key)); | ||
} | ||
|
||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package com.aol.micro.server.couchbase.manifest.comparator; |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe expose this as a property ? but have it default to 60000 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
This file was deleted.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
woops will remove...