Search in sources :

Example 1 with EtcdClient

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

the class LeaderElectionTest method basicLeaderElectionTest.

@Test
public void basicLeaderElectionTest() throws Exception {
    try (EtcdClient client1 = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build()) {
        ByteString electionKey = bs("/electiontest");
        EtcdLeaderElection observer = new EtcdLeaderElection(client1, electionKey);
        observer.start();
        assertNull(observer.getId());
        assertNull(observer.getLeaderId());
        EtcdLeaderElection alice = new EtcdLeaderElection(client1, electionKey, "alice");
        EtcdLeaderElection bob = new EtcdLeaderElection(client1, electionKey, "bob");
        EtcdLeaderElection claire = new EtcdLeaderElection(client1, electionKey, "claire");
        bob.start();
        Thread.sleep(500L);
        assertEquals("bob", bob.getId());
        assertEquals("bob", bob.getLeaderId());
        assertEquals("bob", observer.getLeaderId());
        alice.start();
        Thread.sleep(500L);
        assertEquals("alice", alice.getId());
        assertEquals("bob", observer.getLeaderId());
        assertEquals("bob", bob.getLeaderId());
        assertEquals("bob", alice.getLeaderId());
        assertTrue(bob.isLeader());
        assertFalse(alice.isLeader());
        claire.start();
        Thread.sleep(500L);
        assertEquals("bob", claire.getLeaderId());
        assertEquals("bob", observer.getLeaderId());
        bob.close();
        Thread.sleep(500L);
        assertEquals("alice", observer.getLeaderId());
        assertTrue(alice.isLeader());
        assertFalse(bob.isLeader());
        assertFalse(claire.isLeader());
        assertEquals("alice", claire.getLeaderId());
    }
}
Also used : EtcdClient(com.ibm.etcd.client.EtcdClient) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 2 with EtcdClient

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

the class EtcdClusterConfig method newClient.

private EtcdClient newClient() throws IOException, CertificateException {
    List<String> endpointList = new ArrayList<>(endpoints);
    EtcdClient.Builder builder = EtcdClient.forEndpoints(endpointList).withCredentials(user, password).withImmediateAuth().withMaxInboundMessageSize(maxMessageSize);
    TlsMode ssl = tlsMode;
    if (ssl == TlsMode.AUTO || ssl == null) {
        String ep = endpointList.get(0);
        ssl = ep.startsWith("https://") || (!ep.startsWith("http://") && certificate != null) ? TlsMode.TLS : TlsMode.PLAINTEXT;
    }
    if (ssl == TlsMode.PLAINTEXT)
        builder.withPlainText();
    else if (composeDeployment != null) {
        builder.withTrustManager(new ComposeTrustManagerFactory(composeDeployment, composeDeployment, certificate));
    } else if (certificate != null)
        builder.withCaCert(certificate);
    if (isShutdown)
        throw new IllegalStateException("shut  down");
    return builder.build();
}
Also used : EtcdClient(com.ibm.etcd.client.EtcdClient) ArrayList(java.util.ArrayList) ByteString(com.google.protobuf.ByteString)

Example 3 with EtcdClient

use of com.ibm.etcd.client.EtcdClient 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 4 with EtcdClient

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

the class PersistentLeaseKeyTest method testLeaseKey.

@Test
public void testLeaseKey() throws Exception {
    try (EtcdClient client = EtcdClient.forEndpoint("localhost", 2392).withPlainText().build();
        EtcdClient directClient = EtcdClient.forEndpoint("localhost", 2379).withPlainText().build()) {
        KvClient directKvClient = directClient.getKvClient();
        ByteString mykey = bs("mykeyy");
        PersistentLeaseKey plk = new PersistentLeaseKey(client, mykey, bs("defaultdata"), null);
        ListenableFuture<ByteString> startFuture = plk.startWithFuture();
        Thread.sleep(300L);
        // network conn to server not established yet
        assertFalse(startFuture.isDone());
        // key won't exist yet
        assertEquals(0, directKvClient.get(mykey).countOnly().sync().getCount());
        // reestablish network
        proxy.start();
        assertEquals(mykey, startFuture.get(3000, MILLISECONDS));
        // check key is present via other client
        assertEquals(bs("defaultdata"), directKvClient.get(mykey).sync().getKvs(0).getValue());
        plk.closeWithFuture().get(500, MILLISECONDS);
        // key should now be gone
        assertEquals(0, directKvClient.get(bs("mykeyy")).countOnly().sync().getCount());
        // -----------------
        PersistentLease pl = client.getLeaseClient().maintain().minTtl(2).start();
        System.out.println("PL state: " + pl.getState());
        plk = new PersistentLeaseKey(client, pl, mykey, bs("somedata"), null);
        plk.start();
        assertFalse(pl.isDone());
        Long leaseId = pl.get(1, TimeUnit.SECONDS);
        assertNotNull(leaseId);
        System.out.println("PL state: " + pl.getState());
        // will take a small amount of time after lease is created for PLK to be
        // created
        assertFalse(plk.isDone());
        plk.get(1, TimeUnit.SECONDS);
        KeyValue kv = directKvClient.get(mykey).sync().getKvs(0);
        assertEquals(bs("somedata"), kv.getValue());
        assertEquals((long) leaseId, kv.getLease());
        plk.setDefaultValue(bs("updateddata"));
        Thread.sleep(200L);
        // data doesn't change until key has to be recreated
        assertEquals(bs("somedata"), directKvClient.get(mykey).sync().getKvs(0).getValue());
        proxy.kill();
        long ttl = pl.getCurrentTtlSecs();
        System.out.println("TTL after kill is " + ttl);
        Thread.sleep(1000L);
        // key should still be there (lease not yet expired)
        kv = directKvClient.get(mykey).sync().getKvs(0);
        assertEquals(bs("somedata"), kv.getValue());
        assertEquals((long) leaseId, kv.getLease());
        Thread.sleep((pl.getCurrentTtlSecs() + 2) * 1000L);
        // lease should have now expired and key should be gone
        assertEquals(0, directKvClient.get(bs("mykeyy")).sync().getCount());
        proxy.start();
        long before = System.nanoTime();
        try (WatchIterator it = directKvClient.watch(bs("mykeyy")).start()) {
            for (int i = 0; i < 5; i++) {
                List<Event> events = it.next().getEvents();
                if (!events.isEmpty()) {
                    // key should be updated with new value once
                    // connection is reestablished
                    assertEquals(bs("updateddata"), events.get(0).getKv().getValue());
                    System.out.println("took " + (System.nanoTime() - before) / 1000_000L + "ms for key to re-appear");
                    break;
                }
            }
        }
    }
}
Also used : KeyValue(com.ibm.etcd.api.KeyValue) ByteString(com.google.protobuf.ByteString) EtcdClient(com.ibm.etcd.client.EtcdClient) KvClient(com.ibm.etcd.client.kv.KvClient) PersistentLease(com.ibm.etcd.client.lease.PersistentLease) Event(com.ibm.etcd.api.Event) WatchIterator(com.ibm.etcd.client.kv.KvClient.WatchIterator) Test(org.junit.Test)

Aggregations

ByteString (com.google.protobuf.ByteString)4 EtcdClient (com.ibm.etcd.client.EtcdClient)4 Test (org.junit.Test)3 KvClient (com.ibm.etcd.client.kv.KvClient)2 Event (com.ibm.etcd.api.Event)1 KeyValue (com.ibm.etcd.api.KeyValue)1 LocalNettyProxy (com.ibm.etcd.client.LocalNettyProxy)1 WatchIterator (com.ibm.etcd.client.kv.KvClient.WatchIterator)1 PersistentLease (com.ibm.etcd.client.lease.PersistentLease)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1