Search in sources :

Example 1 with LocalNettyProxy

use of com.ibm.etcd.client.LocalNettyProxy in project etcd-java by IBM.

the class RangeCacheTest method testResiliency.

@Test
public void testResiliency() throws Exception {
    directClient.getKvClient().delete(bs("tmp2/")).asPrefix().sync();
    try (EtcdClient rcClient = EtcdClient.forEndpoint("localhost", 2395).withPlainText().build();
        RangeCache rc = new RangeCache(rcClient, bs("tmp2/"), false)) {
        rc.start();
        Map<ByteString, ByteString> localMap = new HashMap<>();
        try (final LocalNettyProxy prox = new LocalNettyProxy(2395)) {
            Thread proxyThread = new Thread() {

                {
                    setDaemon(true);
                }

                @Override
                public void run() {
                    try {
                        int N = 6;
                        for (int i = 1; i <= N; i++) {
                            prox.start();
                            Thread.sleep(1000L + (long) (Math.random() * 5000));
                            System.out.println("killing proxy " + i);
                            // finish in running state
                            if (i < N)
                                prox.kill();
                            Thread.sleep((long) (Math.random() * 4000));
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            proxyThread.start();
            KvClient directKv = directClient.getKvClient();
            int i = 0;
            // the proxy is stopped/started
            while (proxyThread.isAlive()) {
                // put a key
                ByteString key = bs("tmp2/" + Math.random());
                ByteString value = bs("value " + (i++));
                directKv.put(key, value).sync();
                localMap.put(key, value);
                if (i > 5 && !localMap.isEmpty()) {
                    // delete a key
                    Thread.sleep(((long) (Math.random() * 100)));
                    ByteString randomKey = Iterables.get(localMap.keySet(), (int) (Math.random() * localMap.size()));
                    directKv.delete(randomKey).sync();
                    localMap.remove(randomKey);
                    Thread.sleep(((long) (Math.random() * 100)));
                }
                if (i > 3) {
                    // perform batch update (3 puts, 1 delete)
                    FluentTxnOps<?> batch = directKv.batch();
                    if (!localMap.isEmpty()) {
                        ByteString randomKey = Iterables.get(localMap.keySet(), (int) (Math.random() * localMap.size()));
                        batch.delete(directKv.delete(randomKey).asRequest());
                        localMap.remove(randomKey);
                    }
                    for (int j = 0; j < 3; j++) {
                        key = bs("tmp2/" + Math.random());
                        value = bs("value " + (i++));
                        batch.put(directKv.put(key, value).asRequest());
                        localMap.put(key, value);
                    }
                    // commit batch txn
                    batch.sync();
                    Thread.sleep(((long) (Math.random() * 100)));
                }
            }
            int ls = localMap.size(), rs = (int) directKv.get(bs("tmp2/")).asPrefix().countOnly().sync().getCount();
            System.out.println("local map size is " + localMap.size());
            System.out.println("remote size is " + rs);
            assertEquals(ls, rs);
            // wait until connected and to catch up
            rcClient.getKvClient().get(bs("tmp/")).backoffRetry().sync();
            Thread.sleep(5_000L);
            System.out.println("rc size is " + rc.size());
            // check contents of cache == contents of local map
            assertEquals(localMap.entrySet(), Sets.newHashSet(Iterables.transform(rc, kv -> Maps.immutableEntry(kv.getKey(), kv.getValue()))));
        }
    }
}
Also used : LocalNettyProxy(com.ibm.etcd.client.LocalNettyProxy) EtcdClient(com.ibm.etcd.client.EtcdClient) HashMap(java.util.HashMap) ByteString(com.google.protobuf.ByteString) KvClient(com.ibm.etcd.client.kv.KvClient) Test(org.junit.Test)

Example 2 with LocalNettyProxy

use of com.ibm.etcd.client.LocalNettyProxy in project etcd-java by IBM.

the class RangeCacheTest method setup.

@BeforeClass
public static void setup() throws Exception {
    (proxy = new LocalNettyProxy(2394)).start();
    client = EtcdClient.forEndpoint("localhost", 2394).withPlainText().build();
    directClient = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build();
}
Also used : LocalNettyProxy(com.ibm.etcd.client.LocalNettyProxy) BeforeClass(org.junit.BeforeClass)

Aggregations

LocalNettyProxy (com.ibm.etcd.client.LocalNettyProxy)2 ByteString (com.google.protobuf.ByteString)1 EtcdClient (com.ibm.etcd.client.EtcdClient)1 KvClient (com.ibm.etcd.client.kv.KvClient)1 HashMap (java.util.HashMap)1 BeforeClass (org.junit.BeforeClass)1 Test (org.junit.Test)1