package com.couchbase.failover.testing; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.UUID; import net.spy.memcached.OperationTimeoutException; import com.couchbase.client.CouchbaseClient; import com.couchbase.client.CouchbaseConnectionFactoryBuilder; public class CouchbaseClientTester { public static void main( String[] args ) throws Exception { //System.setProperty("cbclient.disableCarrierBootstrap", "true"); List hosts = new ArrayList(); hosts.add(new URI("http://node1:8091/pools")); hosts.add(new URI("http://node2:8091/pools")); CouchbaseConnectionFactoryBuilder cfb = new CouchbaseConnectionFactoryBuilder(); //cfb.setAuthWaitTime(3000); //cfb.setTimeoutExceptionThreshold(3); //cfb.setObsTimeout(300); //cfb.setFailureMode(FailureMode.Cancel); cfb.setOpTimeout(200); CouchbaseClient client = new CouchbaseClient(cfb.buildCouchbaseConnection(hosts, "bucketname", "password")); String key = UUID.randomUUID().toString(); client.add(key, "value"); for(int i = 0; i < 5; i++) { couchbaseGet(client, key); Thread.sleep(1000); } System.out.println("Block traffic from master node then Press 'Enter'"); System.in.read(); System.out.println("Look at admin console 'Server Nodes' tab and wait until the master node is marked as down (this can take 30-60 seconds). Then fail it over."); CouchbaseClientTester cct = new CouchbaseClientTester(); for(int i = 0; i < 1; i++) { List threads = new ArrayList(); for(int j = 0; j < 6; j++) { Thread aThread = cct.new TestThread(client, key); threads.add(aThread); } for(Thread aThread : threads) { aThread.start(); } for(Thread aThread : threads) { aThread.join(); } } } private class TestThread extends Thread { private CouchbaseClient client; private String key; public TestThread(CouchbaseClient client, String key) { this.client = client; this.key = key; } public void run() { while(true) { couchbaseGet(client, key); } } } private static Object couchbaseGet(CouchbaseClient client, String key) { Object result = null; try { result = client.get(key); System.out.println("Got From Master"); } catch(OperationTimeoutException ex) { try { result = client.getFromReplica(key); System.out.println("Got From Replica"); } catch (OperationTimeoutException innerEx) { //client.shutdown(); //createClient(); System.out.println("Exception"); innerEx.printStackTrace(); } } return result; } }