Java Code Examples for mousio.etcd4j.responses.EtcdKeysResponse#EtcdNode

The following examples show how to use mousio.etcd4j.responses.EtcdKeysResponse#EtcdNode . 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: 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 2
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 3
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 4
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 5
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 6
Source File: EtcdPropertySource.java    From spring-cloud-etcd with Apache License 2.0 5 votes vote down vote up
private void process(final EtcdKeysResponse.EtcdNode root) {
	if (!StringUtils.isEmpty(root.value)) {
		final String key = root.key.substring(this.prefix.length());

		properties.put(key.replace(EtcdConstants.PATH_SEPARATOR,
				EtcdConstants.PROPERTIES_SEPARATOR), root.value);
	}

	if (!CollectionUtils.isEmpty(root.nodes)) {
		for (EtcdKeysResponse.EtcdNode node : root.nodes) {
			process(node);
		}
	}
}
 
Example 7
Source File: EtcdCoordinatorTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void queryRemoteNodesWithNonOrEmpty() {
    EtcdKeysResponse.EtcdNode node = PowerMockito.mock(EtcdKeysResponse.EtcdNode.class);
    when(response.getNode()).thenReturn(node);
    when(node.getValue()).thenReturn("{}");
    assertEquals(0, coordinator.queryRemoteNodes().size());
    assertEquals(0, coordinator.queryRemoteNodes().size());
}
 
Example 8
Source File: ITClusterEtcdPluginTest.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void clear() throws Throwable {
    EtcdKeysResponse response = client.get(SERVICE_NAME + "/").send().get();
    List<EtcdKeysResponse.EtcdNode> nodes = response.getNode().getNodes();

    for (EtcdKeysResponse.EtcdNode node : nodes) {
        client.delete(node.getKey()).send().get();
    }
}
 
Example 9
Source File: EtcdKeystoreTest.java    From etcd4j with Apache License 2.0 5 votes vote down vote up
protected void cleanup(EtcdClient etcd) {
    try {
        for (EtcdKeysResponse.EtcdNode node: etcd.getAll().send().get().getNode().getNodes()) {
            if (node.isDir()) {
                etcd.deleteDir(node.key).recursive().send().get();
            } else {
                etcd.delete(node.key).send().get();
            }
        }
    } catch (Exception e) {}
}
 
Example 10
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 11
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 12
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 13
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 14
Source File: EtcdConfigWatcherRegisterTest.java    From skywalking with Apache License 2.0 4 votes vote down vote up
@Before
@Test
public void shouldReadConfigs() throws Exception {
    final String group = "skywalking";
    final String testKey1 = "receiver-trace.default.slowDBAccessThreshold";
    final String testVal1 = "test";
    final String testKey2 = "testKey";
    final String testVal2 = "testVal";

    final EtcdServerSettings mockSettings = mock(EtcdServerSettings.class);
    when(mockSettings.getGroup()).thenReturn(group);
    mockStatic(EtcdUtils.class);

    List<URI> uris = mock(List.class);
    when(EtcdUtils.parse(any())).thenReturn(uris);

    final EtcdClient client = PowerMockito.mock(EtcdClient.class);
    whenNew(EtcdClient.class).withAnyArguments().thenReturn(client);

    String port = System.getProperty("etcd.port");
    URI uri = new URI("http://localhost:" + port);
    List<URI> urisArray = spy(ArrayList.class);
    urisArray.add(uri);
    URI[] array = urisArray.toArray(new URI[] {});
    when(uris.toArray(new URI[] {})).thenReturn(array);

    final EtcdConfigWatcherRegister mockRegister = spy(new EtcdConfigWatcherRegister(mockSettings));

    Whitebox.setInternalState(mockRegister, "client", client);
    Whitebox.setInternalState(mockRegister, "settings", mockSettings);

    final EtcdKeysResponse response = PowerMockito.mock(EtcdKeysResponse.class);
    final EtcdKeysResponse response1 = PowerMockito.mock(EtcdKeysResponse.class);

    final EtcdKeyGetRequest request = PowerMockito.mock(EtcdKeyGetRequest.class);

    when(client.get("/skywalking/receiver-trace.default.slowDBAccessThreshold")).thenReturn(request);
    when(request.waitForChange()).thenReturn(request);

    final EtcdResponsePromise<EtcdKeysResponse> promise = mock(EtcdResponsePromise.class);
    final ResponsePromise<EtcdKeysResponse> responseResponsePromise = mock(ResponsePromise.class);
    when(request.send()).thenReturn(promise);
    when(promise.get()).thenReturn(response);
    when(responseResponsePromise.get()).thenReturn(response);

    final EtcdKeysResponse.EtcdNode node = mock(EtcdKeysResponse.EtcdNode.class);
    when(response.getNode()).thenReturn(node);
    when(node.getKey()).thenReturn("/skywalking/receiver-trace.default.slowDBAccessThreshold");
    when(node.getValue()).thenReturn("test");

    final EtcdKeyGetRequest request1 = mock(EtcdKeyGetRequest.class);
    when(client.get("/skywalking/testKey")).thenReturn(request1);
    when(request1.waitForChange()).thenReturn(request1);
    final EtcdResponsePromise<EtcdKeysResponse> promise1 = mock(EtcdResponsePromise.class);
    final ResponsePromise<EtcdKeysResponse> responseResponsePromise1 = mock(ResponsePromise.class);
    when(request1.send()).thenReturn(promise1);
    when(promise1.get()).thenReturn(response1);
    when(responseResponsePromise1.get()).thenReturn(response1);

    final EtcdKeysResponse.EtcdNode node1 = mock(EtcdKeysResponse.EtcdNode.class);
    when(response1.getNode()).thenReturn(node1);
    when(node1.getKey()).thenReturn("/skywalking/testKey");
    when(node1.getValue()).thenReturn("testVal");

    final ConfigTable configTable = mockRegister.readConfig(Sets.newHashSet(testKey1, testKey2)).get();

    assertEquals(2, configTable.getItems().size());
    Map<String, String> kvs = new HashMap<>();
    for (ConfigTable.ConfigItem item : configTable.getItems()) {
        kvs.put(item.getName(), item.getValue());
    }
    assertEquals(testVal1, kvs.get(testKey1));
    assertEquals(testVal2, kvs.get(testKey2));
}