Skip to content

Instantly share code, notes, and snippets.

@ingenthr
Created November 3, 2012 02:55
Show Gist options
  • Save ingenthr/4005631 to your computer and use it in GitHub Desktop.
Save ingenthr/4005631 to your computer and use it in GitHub Desktop.
Sample wrapper to deal with timeouts using a workaround
package com.couchbase.demo;
import com.couchbase.client.CouchbaseClient;
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* A sample wrapper for the CouchbaseClient.
*
* The intent of this wrapper is to make sure that if timeouts are regularly
* occurring, the client object in use will be replaced.
*
* NOTE: this is a SAMPLE ONLY and there are several sections about
* configuration herein which should be changed.
*
*/
public class CouchbaseClientWrapper {
private final AtomicReference<CouchbaseClient> clientAr = new AtomicReference<CouchbaseClient>();
private AtomicInteger timeoutCount;
private final int VIEW_TIMEOUT_THRESHOLD = 30;
//TODO: perhaps make the threshold a certain number of times within a given
// time interval.
private final List<URI> baseList;
/**
* Create a new CouchbaseClientWrapper.
*/
public CouchbaseClientWrapper() {
// Should create the client with whatever is appropriate in a given environment!
baseList = Arrays.asList(
URI.create("http://192.168.0.1:8091/pools"),
URI.create("http://192.168.0.2:8091/pools"));
timeoutCount = new AtomicInteger(0);
}
/**
* Initialize the CouchbaseClientWrapper.
*
* Initialization will have the wrapper build and instantiate the
* CouchbaseClient object which this class wraps. The primary reason for
* separating initialization is that an IOException may be thrown.
*
* @throws IOException
*/
public void init() throws IOException {
// Should create the client with whatever is appropriate in a given environment!
CouchbaseClient client = new CouchbaseClient(baseList, "default", "");
clientAr.set(client);
}
/**
* Get the current CouchbaseClient.
*
* This method should be called at the beginning of an application method,
* and the reference for the returned CouchbaseClient should only be local
* to that method.
*
* The intent here is that application code will notify this wrapper about
* exceptions and based on that, the client returned by this method may be
* recreated and swapped out.
*
* @return the current CouchbaseClient
* @throws IOException
*/
public CouchbaseClient getClient() throws IOException {
if (timeoutCount.get() >= VIEW_TIMEOUT_THRESHOLD) {
recreateClient();
timeoutCount.set(0);
}
return clientAr.get();
}
/**
* Let the CouchbaseClientWrapper know that a timeout has occurred.
*
* This is intended to be used as a workaround for view timeouts. When a
* view timeout occurs, this method should be called. After a certain number
* of timeouts, the client will be replaced.
*/
public void incrTimeout() {
timeoutCount.incrementAndGet();
}
/**
* Shutdown the wrapped CouchbaseClient object.
*
* @param timeout
* @param unit
*/
public void shutdown(long timeout, TimeUnit unit) {
clientAr.get().shutdown(timeout, unit);
}
private void recreateClient() throws IOException {
// Should create the client with whatever is appropriate in a given environment!
CouchbaseClient replacementClient = new CouchbaseClient(baseList, "default", "");
CouchbaseClient replacedClient = clientAr.getAndSet(replacementClient);
replacedClient.shutdown(10, TimeUnit.SECONDS);
}
}
@ingenthr
Copy link
Author

ingenthr commented Nov 3, 2012

This needs a shutdown added to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment