mousio.etcd4j.responses.EtcdKeysResponse Java Examples

The following examples show how to use mousio.etcd4j.responses.EtcdKeysResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * In order key tests
 */
@Test
public void testWait() throws IOException, EtcdException, EtcdAuthenticationException, InterruptedException, TimeoutException {
  EtcdResponsePromise<EtcdKeysResponse> p = etcd.get("etcd4j_test/test").waitForChange().send();

  // Ensure the change is received after the listen command is received.
  new Timer().schedule(new TimerTask() {
    @Override
    public void run() {
      try {
        etcd.put("etcd4j_test/test", "changed").send().get();
      } catch (IOException | EtcdException | EtcdAuthenticationException | TimeoutException e) {
        fail();
      }
    }
  }, 20);

  EtcdKeysResponse r = p.get();
  assertEquals("changed", r.node.value);
}
 
Example #2
Source File: EtcdCoordinatorTest.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Test
public void queryRemoteNodes() {
    registerSelfRemote();

    EtcdKeysResponse.EtcdNode node = PowerMockito.mock(EtcdKeysResponse.EtcdNode.class);
    EtcdKeysResponse.EtcdNode node1 = PowerMockito.mock(EtcdKeysResponse.EtcdNode.class);

    when(response.getNode()).thenReturn(node);
    list = new ArrayList<>();
    List list1 = Mockito.spy(list);
    list1.add(node1);
    when(node.getNodes()).thenReturn(list1);
    when(node1.getValue()).thenReturn("{\"serviceId\":\"my-service\",\"host\":\"10.0.0.2\",\"port\":1001}");
    List<RemoteInstance> remoteInstances = coordinator.queryRemoteNodes();
    assertEquals(1, remoteInstances.size());

    RemoteInstance selfInstance = remoteInstances.get(0);
    velidate(selfRemoteAddress, selfInstance);
}
 
Example #3
Source File: EtcdUtil.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Gets the content of the key recursively as a JsonObject
 * @param path root path (i.e. /path1/path2)
 * @param etcdClient EtcdClient
 * @return JsonNode
 * @throws IOException
 * @throws EtcdAuthenticationException
 * @throws TimeoutException
 * @throws EtcdException
 */
public static JsonNode getAsJson(String path, EtcdClient etcdClient)
        throws IOException, EtcdAuthenticationException, TimeoutException, EtcdException {

  EtcdKeyGetRequest etcdKeyGetRequest = etcdClient.get(path).recursive();
  EtcdKeysResponse dataTree = etcdKeyGetRequest.send().get();

  ObjectNode jNode = JsonNodeFactory.instance.objectNode();

  if (dataTree.getNode().getNodes().isEmpty()) {
    iterateOverNodes(jNode, dataTree.getNode());
  } else {
    for (EtcdNode node : dataTree.getNode().getNodes()) {
      iterateOverNodes(jNode, node);
    }
  }

  return dotNotationToStandardJson(jNode.at(path));
}
 
Example #4
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Directory tests
 */
@Test
public void testDir() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  EtcdKeysResponse r = etcd.putDir("etcd4j_test/foo_dir").send().get();
  assertEquals(r.action, EtcdKeyAction.set);

  r = etcd.getDir("etcd4j_test/foo_dir").consistent().send().get();
  assertEquals(r.action, EtcdKeyAction.get);

  // Test slash before key
  r = etcd.getDir("/etcd4j_test/foo_dir").send().get();
  assertEquals(r.action, EtcdKeyAction.get);

  r = etcd.put("etcd4j_test/foo_dir/foo", "bar").send().get();
  assertEquals(r.node.value, "bar");

  r = etcd.putDir("etcd4j_test/foo_dir/foo_subdir").ttl(20).send().get();
  assertEquals(r.action, EtcdKeyAction.set);
  assertNotNull(r.node.expiration);

  r = etcd.deleteDir("etcd4j_test/foo_dir").recursive().send().get();
  assertEquals(r.action, EtcdKeyAction.delete);
}
 
Example #5
Source File: EtcdCoordinator.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<RemoteInstance> queryRemoteNodes() {

    List<RemoteInstance> res = new ArrayList<>();
    try {
        EtcdKeysResponse response = client.get(serviceName + "/").send().get();
        List<EtcdKeysResponse.EtcdNode> nodes = response.getNode().getNodes();

        Gson gson = new Gson();
        if (nodes != null) {
            nodes.forEach(node -> {
                EtcdEndpoint endpoint = gson.fromJson(node.getValue(), EtcdEndpoint.class);
                Address address = new Address(endpoint.getHost(), endpoint.getPort(), true);
                if (!address.equals(selfAddress)) {
                    address.setSelf(false);
                }
                res.add(new RemoteInstance(address));
            });
        }

    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return res;
}
 
Example #6
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Recursive
 */
@Test
public void testRecursive() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  etcd.put("etcd4j_test/nested/root/key-1", "key1").send().get();
  etcd.put("etcd4j_test/nested/root/node-1/key-2", "key2").send().get();
  etcd.put("etcd4j_test/nested/root/node-1/child/key-3", "key3").send().get();
  etcd.put("etcd4j_test/nested/root/node-2/key-4", "key4").send().get();

  EtcdKeysResponse r;

  r =  etcd.get("etcd4j_test/nested").recursive().timeout(10, TimeUnit.SECONDS).send().get();
  assertEquals(1, r.node.nodes.size());
  assertEquals(3, r.node.nodes.get(0).nodes.size());

  r =  etcd.getDir("etcd4j_test/nested").recursive().timeout(10, TimeUnit.SECONDS).send().get();
  assertEquals(1, r.node.nodes.size());
  assertEquals(3, r.node.nodes.get(0).nodes.size());

  r = etcd.deleteDir("etcd4j_test/nested").recursive().send().get();
  assertEquals(r.action, EtcdKeyAction.delete);
}
 
Example #7
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
/**
 * In order key tests
 */
@Test
public void testInOrderKeys() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  EtcdKeysResponse r = etcd.post("etcd4j_test/queue", "Job1").send().get();
  assertEquals(r.action, EtcdKeyAction.create);

  r = etcd.post("etcd4j_test/queue", "Job2").ttl(20).send().get();
  assertEquals(r.action, EtcdKeyAction.create);

  r = etcd.get(r.node.key).consistent().send().get();
  assertTrue(r.node.key.endsWith(r.node.createdIndex+""));
  assertEquals(r.node.value, "Job2");

  r = etcd.get("etcd4j_test/queue").consistent().recursive().sorted().send().get();
  assertEquals(2, r.node.nodes.size());
  assertEquals("Job2", r.node.nodes.get(1).value);

  r = etcd.deleteDir("etcd4j_test/queue").recursive().send().get();
  assertEquals(r.action, EtcdKeyAction.delete);
}
 
Example #8
Source File: EtcdConfigWatcherRegister.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void onDataValueChanged(ResponsePromise<EtcdKeysResponse> promise, String dataId) {
    String key = getRealKey(dataId, settings.getGroup());
    try {
        EtcdKeysResponse.EtcdNode node = promise.get().getNode();
        String value = node.getValue();
        if (logger.isInfoEnabled()) {
            logger.info("Etcd config changed: {}: {}", key, node.getValue());
        }

        configItemKeyedByName.put(key, Optional.ofNullable(value));
    } catch (Exception e) {
        if (e instanceof EtcdException) {
            if (EtcdErrorCode.KeyNotFound == ((EtcdException) e).errorCode) {
                configItemKeyedByName.put(key, Optional.empty());
                return;
            }
        }
        throw new EtcdConfigException("wait for value changed fail", e);
    }
}
 
Example #9
Source File: EtcdConfigWatcherRegister.java    From skywalking with Apache License 2.0 6 votes vote down vote up
private void registerKeyListeners(final Set<String> keys) {
    for (final String key : keys) {
        String dataId = "/" + settings.getGroup() + "/" + key;
        if (listenersByKey.containsKey(dataId)) {
            continue;
        }

        listenersByKey.putIfAbsent(dataId, p -> {
            onDataValueChanged(p, dataId);
        });

        try {
            EtcdResponsePromise<EtcdKeysResponse> responsePromise = client.get(dataId).waitForChange().send();
            responsePromise.addListener(listenersByKey.get(dataId));
            responsePromiseByKey.putIfAbsent(dataId, responsePromise);

            // the key is newly added, read the config for the first time
            EtcdResponsePromise<EtcdKeysResponse> promise = client.get(dataId).send();
            onDataValueChanged(promise, dataId);
        } catch (Exception e) {
            throw new EtcdConfigException("wait for etcd value change fail", e);
        }
    }
}
 
Example #10
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfCleanClose() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  etcd.setRetryHandler(new RetryWithExponentialBackOff(20, 4, 1000));

  EtcdResponsePromise<EtcdKeysResponse> p = etcd.get("etcd4j_test/test").waitForChange().send();
  etcd.close();

  try {
    p.get();
    fail();
  } catch (IOException e){
    // should be catched because connection was canceled
    if (!(e.getCause() instanceof CancellationException)) {
      fail();
    }
  }
}
 
Example #11
Source File: EtcdFlagFieldUpdater.java    From java-flagz with MIT License 6 votes vote down vote up
/**
 * Handles watch issues with watching. See Etcd docs:
 *
 * @see <a href="https://github.com/coreos/etcd/blob/master/Documentation/api.md">Docs</a>
 */
private EtcdKeysResponse handleEtcdWatchErrors(EtcdException exception) {
  //
  if (exception.errorCode == ETCD_EVENT_INDEX_CLEARED_CODE) {
    // If our watching failed due to index, re-read everything because we might have missed
    // something. The lastKnownFlagzModificationIndex will be reset in fetchAllFlagzNodes.
    initialSetAllFlagz();
    return null;
  } else if (exception.errorCode == ETCD_WATCHER_CLEARED_CODE) {
    // This means that etcd is recovering from a problem.
    try {
      Thread.sleep(reelectionBackoffMs.get());
    } catch (InterruptedException e1) {
      // ignore
    }
    return null;
  } else {
    throw new EtcdFlagFieldUpdaterException.EtcdFetchingFailed(exception);
  }
}
 
Example #12
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAll() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  EtcdKeysResponse.EtcdNode root;
  List<EtcdKeysResponse.EtcdNode> nodes;

  root = etcd.getAll().timeout(30, TimeUnit.SECONDS).send().get().getNode();
  nodes = root.getNodes();

  LOGGER.info("Nodes (1) {}", nodes);

  assertNotNull(nodes);
  assertTrue(root.isDir());

  etcd.put("etcd4j_testGetAll_1/foo1", "bar").prevExist(false).send().get();
  etcd.put("etcd4j_testGetAll_2/foo1", "bar").prevExist(false).send().get();

  root = etcd.getAll().timeout(30, TimeUnit.SECONDS).send().get().getNode();
  nodes = root.getNodes();

  LOGGER.info("Nodes (2) {}", nodes);

  assertNotNull(nodes);
  assertEquals(2, nodes.size());
}
 
Example #13
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessAfterRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(sequence(FAILURE, SUCCESS));

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 10))
                .send();

        EtcdKeysResponse resp = promise.get();

        assertThat(resp.node.value).isEqualTo("bar");
        assertThat(promise.getException()).isNull();

    }

    verifyHttp(server).times(2,
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example #14
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSuccessWithoutRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(SUCCESS);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 10))
                .send();

        EtcdKeysResponse resp = promise.get();

        assertThat(resp.node.value).isEqualTo("bar");
        assertThat(promise.getException()).isNull();

    }

    verifyHttp(server).once(
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example #15
Source File: EtcdClientRetryPolicyTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureAfterRetrying() throws Exception {

    whenHttp(server)
            .match(get("/v2/keys/foo"))
            .then(FAILURE);

    try (EtcdClient etcd = new EtcdClient(serverURI)) {

        EtcdResponsePromise<EtcdKeysResponse> promise = etcd.get("foo")
                .setRetryPolicy(new RetryNTimes(1, 3))
                .send();

        Exception err = null;

        try {
            promise.get();
        } catch (Exception e) {
            err = e;
        }

        assertThat(err).isNotNull();
        assertThat(err).isEqualTo(promise.getException());

    }

    verifyHttp(server).times(4,
            method(Method.GET),
            uri("/v2/keys/foo")
    );
}
 
Example #16
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Simple value tests
 */
@Test
public void testKey() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  try {
    EtcdKeysResponse response = etcd.put("etcd4j_test/foo", "bar").send().get();
    assertEquals(EtcdKeyAction.set, response.action);

    response = etcd.put("etcd4j_test/foo2", "bar").prevExist(false).send().get();
    assertEquals(EtcdKeyAction.create, response.action);

    response = etcd.put("etcd4j_test/foo", "bar1").ttl(40).prevExist(true).send().get();
    assertEquals(EtcdKeyAction.update, response.action);
    assertNotNull(response.node.expiration);

    response = etcd.put("etcd4j_test/foo", "bar2").prevValue("bar1").send().get();
    assertEquals(EtcdKeyAction.compareAndSwap, response.action);

    response = etcd.put("etcd4j_test/foo", "bar3").prevIndex(response.node.modifiedIndex).send().get();
    assertEquals(EtcdKeyAction.compareAndSwap, response.action);

    response = etcd.get("etcd4j_test/foo").consistent().send().get();
    assertEquals("bar3", response.node.value);

    // Test slash before key
    response = etcd.get("/etcd4j_test/foo").consistent().send().get();
    assertEquals("bar3", response.node.value);

    response = etcd.delete("etcd4j_test/foo").send().get();
    assertEquals(EtcdKeyAction.delete, response.action);
  } finally {
    etcd.deleteDir("etcd4j_test").recursive().send().get();
  }
}
 
Example #17
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
protected void cleanup() {
  try {
    for (EtcdKeysResponse.EtcdNode node: etcd.getAll().send().get().getNode().getNodes()) {
      if (node.isDir()) {
        LOGGER.info("Delete dir {}", node.key);
        etcd.deleteDir(node.key).recursive().send().get();
      } else {
        LOGGER.info("Delete entry {}", node.key);
        etcd.delete(node.key).send().get();
      }
    }
  } catch (Exception e) {
  }
}
 
Example #18
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public KvNode set(String key, String value) {
    try {
        EtcdResponsePromise<EtcdKeysResponse> send = etcd.put(key, value).send();
        EtcdKeysResponse resp = send.get();
        log.debug("set value {} for key {}", resp.node.value, resp.node.key);
        return toNode(resp);
    } catch (Exception e) {
        throw Throwables.asRuntime(e);
    }
}
 
Example #19
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Refresh test
 */
@Test
public void testRefreshTtl() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  EtcdKeysResponse initialResponse = etcd.put("etcd4j_test/foo", "bar").ttl(60).send().get();
  assertEquals(EtcdKeyAction.set, initialResponse.action);

  final EtcdKeysResponse refreshedResponse = etcd.refresh("etcd4j_test/foo", 120).send().get();

  assertEquals(initialResponse.node.createdIndex, refreshedResponse.node.createdIndex);
  assertTrue("expected ttl to be updated", refreshedResponse.node.ttl > 60);
}
 
Example #20
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Tests redirect by sending a key with too many slashes.
 */
@Test
public void testRedirect() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  etcd.put("etcd4j_test/redirect", "bar").send().get();

  // Test redirect with a double slash
  EtcdKeysResponse response = etcd.get("//etcd4j_test/redirect").consistent().send().get();
  assertEquals("bar", response.node.value);
}
 
Example #21
Source File: TestFunctionality.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 1000)
public void testChunkedData() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  //creating very long key to force content to be chunked
  StringBuilder stringBuilder = new StringBuilder(15000);
  for (int i = 0; i < 15000; i++) {
    stringBuilder.append("a");
  }
  EtcdKeysResponse response = etcd.put("etcd4j_test/foo", stringBuilder.toString()).send().get();
  assertEquals(EtcdKeyAction.set, response.action);
}
 
Example #22
Source File: TestHugeDir.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
protected void cleanup() {
  try {
    for (EtcdKeysResponse.EtcdNode node: etcd.getAll().send().get().getNode().getNodes()) {
      if (node.isDir()) {
        LOGGER.info("Delete dir {}", node.key);
        etcd.deleteDir(node.key).recursive().send().get();
      } else {
        LOGGER.info("Delete entry {}", node.key);
        etcd.delete(node.key).send().get();
      }
    }
  } catch (Exception e) {
  }
}
 
Example #23
Source File: TestHugeDir.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetHugeDir() throws IOException, EtcdException, EtcdAuthenticationException, TimeoutException {
  for (int i = 0; i < 2000; i++) {
    etcd.put("/etcd4j_test/huge-dir/node-" + i, "bar").send().get();
  }

  List<EtcdKeysResponse.EtcdNode> nodes;

  nodes = etcd.getDir("/etcd4j_test/huge-dir/").send().get().getNode().getNodes();
  assertNotNull(nodes);
  assertEquals(2000, nodes.size());
}
 
Example #24
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
protected void cleanup() {
    try {
        for (EtcdKeysResponse.EtcdNode node: etcd.getAll().send().get().getNode().getNodes()) {
            if (node.isDir()) {
                LOGGER.info("Delete dir {}", node.key);
                etcd.deleteDir(node.key).recursive().send().get();
            } else {
                LOGGER.info("Delete entry {}", node.key);
                etcd.delete(node.key).send().get();
            }
        }
    } catch (Exception e) {
    }
}
 
Example #25
Source File: EtcdJsonTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutJson() throws EtcdAuthenticationException, TimeoutException, EtcdException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    File testJson = new File("src/test/resources/test_data.json");
    JsonNode toEtcd = mapper.readTree(testJson);
    EtcdUtil.putAsJson("/etcd4j_test/put-json", toEtcd, etcd);

    EtcdKeysResponse widget = etcd.get("/etcd4j_test/put-json").send().get();
    assertEquals(widget.getNode().getNodes().size(), 1);

    EtcdKeysResponse widgets = etcd.get("/etcd4j_test/put-json/widget").send().get();
    assertEquals(widgets.getNode().getNodes().size(), 5);
}
 
Example #26
Source File: ITEtcdConfigurationTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private boolean publishConfig(String key, String group, String value) {
    try {
        client.putDir(group).send().get();
        EtcdResponsePromise<EtcdKeysResponse> promise = client.put(generateKey(key, group), value).send();
        promise.get();
        return true;
    } catch (Exception e) {
        return false;
    }
}
 
Example #27
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
private KvNode toNode(EtcdKeysResponse resp) {
    EtcdKeysResponse.EtcdNode node = resp.node;
    if(node.dir) {
        // we must not return dir value
        return KvNode.dir(node.modifiedIndex);
    }
    return KvNode.leaf(node.modifiedIndex, node.value);
}
 
Example #28
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public KvNode set(String key, String value, WriteOptions ops) {
    EtcdKeyPutRequest req = etcd.put(key, value);
    fillPutReq(ops, req);
    try {
        EtcdKeysResponse resp = executeRequest(req);
        log.debug("set value {} for key {}, ops {}", resp.node.value, resp.node.key, ops);
        return toNode(resp);
    } catch (Exception e) {
        throw Throwables.asRuntime(e);
    }

}
 
Example #29
Source File: EtcdClientWrapper.java    From haven-platform with Apache License 2.0 5 votes vote down vote up
@Override
public KvNode delete(String key, WriteOptions ops) {
    EtcdKeyDeleteRequest req = etcd.delete(key);
    fillDeleteReq(ops, req);
    try {
        EtcdKeysResponse resp = executeRequest(req);

        log.debug("deleted key {}", resp.node.key);
        return toNode(resp);
    } catch (Exception e) {
        throw Throwables.asRuntime(e);
    }
}
 
Example #30
Source File: EtcdFlagFieldUpdater.java    From java-flagz with MIT License 5 votes vote down vote up
private List<EtcdNode> fetchAllFlagzNodes() {
  try {
    EtcdResponsePromise<EtcdKeysResponse> promise = client
        .get(this.directoryPrefix)
        .dir()
        .send();
    EtcdKeysResponse response = promise.get();
    // NOTE: We use etcdIndex here, because we know we got latest data up to this point.
    lastKnownFlagzModificationIndex = response.etcdIndex;
    return MoreObjects.firstNonNull(response.node.nodes, ImmutableList.<EtcdNode>of());
  } catch (IOException | EtcdException
      | TimeoutException | EtcdAuthenticationException exception) {
    throw new EtcdFlagFieldUpdaterException.EtcdFetchingFailed(exception);
  }
}