/** * Gson decode the NodeData into object. * @param nodeData The data to decode * @param type Object class to decode into. * @param <T> Type of the object. * @return The decoded object or {@code null} if node data is null. */ private <T> T decodeNodeData(NodeData nodeData, Class<T> type) { byte[] data = nodeData == null ? null : nodeData.getData(); if (data == null) { return null; } return GSON.fromJson(new String(data, Charsets.UTF_8), type); }
/** * Gson decode the NodeData into object. * @param nodeData The data to decode * @param type Object class to decode into. * @param <T> Type of the object. * @return The decoded object or {@code null} if node data is null. */ private <T> T decodeNodeData(NodeData nodeData, Class<T> type) { byte[] data = nodeData == null ? null : nodeData.getData(); if (data == null) { return null; } return GSON.fromJson(new String(data, Charsets.UTF_8), type); }
@Override public void onSuccess(NodeData result) { try { RouteConfig route = ROUTE_CONFIG_CODEC.decode(result.getData()); settableFuture.set(route); // Replace the future in the routeConfigMap in order to reflect the route config changes routeConfigMap.replace(serviceId, oldSettableFuture, settableFuture); } catch (Exception ex) { LOG.debug("Unable to deserialize the config for service {}. Got data {}", serviceId, result.getData()); // Need to remove the future from the map since later calls will continue to use this future and will think // that there is an exception routeConfigMap.remove(serviceId, settableFuture); settableFuture.setException(ex); } }
@Override public void onSuccess(NodeData result) { try { RouteConfig route = ROUTE_CONFIG_CODEC.decode(result.getData()); settableFuture.set(route); // Replace the future in the routeConfigMap in order to reflect the route config changes routeConfigMap.replace(serviceId, oldSettableFuture, settableFuture); } catch (Exception ex) { LOG.debug("Unable to deserialize the config for service {}. Got data {}", serviceId, result.getData()); // Need to remove the future from the map since later calls will continue to use this future and will think // that there is an exception routeConfigMap.remove(serviceId, settableFuture); settableFuture.setException(ex); } }
@Override protected void instanceNodeUpdated(NodeData nodeData) { if (nodeData == null || nodeData.getData() == null) { LOG.warn("Instance node was updated but data is null."); return; } try { Gson gson = new Gson(); JsonElement json = gson.fromJson(new String(nodeData.getData(), Charsets.UTF_8), JsonElement.class); if (json.isJsonObject()) { JsonElement data = json.getAsJsonObject().get("data"); if (data != null) { this.liveData = gson.fromJson(data, ContainerLiveNodeData.class); LOG.info("Container LiveNodeData updated: " + new String(nodeData.getData(), Charsets.UTF_8)); } } } catch (Throwable t) { LOG.warn("Error deserializing updated instance node data", t); } }
@Override public ResourceRequirement apply(@Nullable NodeData input) { if (input == null) { return null; } try { return CoordinationConstants.RESOURCE_REQUIREMENT_CODEC.decode(input.getData()); } catch (Throwable t) { LOG.error("Failed to decode resource requirement: {}", Bytes.toStringBinary(input.getData()), t); throw Throwables.propagate(t); } } };
@Override public ResourceRequirement apply(@Nullable NodeData input) { if (input == null) { return null; } try { return CoordinationConstants.RESOURCE_REQUIREMENT_CODEC.decode(input.getData()); } catch (Throwable t) { LOG.error("Failed to decode resource requirement: {}", Bytes.toStringBinary(input.getData()), t); throw Throwables.propagate(t); } } };
@Override public void onSuccess(NodeData result) { try { ResourceAssignment assignment = CoordinationConstants.RESOURCE_ASSIGNMENT_CODEC.decode(result.getData()); LOG.debug("Received resource assignment for {}. {}", serviceName, assignment.getAssignments()); handleAssignmentChange(serviceName, assignment); } catch (Exception e) { LOG.error("Failed to decode ResourceAssignment {}", Bytes.toStringBinary(result.getData()), e); } }
@Override public void onSuccess(NodeData result) { try { ResourceAssignment assignment = CoordinationConstants.RESOURCE_ASSIGNMENT_CODEC.decode(result.getData()); LOG.debug("Received resource assignment for {}. {}", serviceName, assignment.getAssignments()); handleAssignmentChange(serviceName, assignment); } catch (Exception e) { LOG.error("Failed to decode ResourceAssignment {}", Bytes.toStringBinary(result.getData()), e); } }
@Override public void onSuccess(NodeData result) { byte[] data = result.getData(); String hostname = (data == null) ? null : new String(data, StandardCharsets.UTF_8); Participant newInfo = new Participant(path, hostname); participants.replace(participantId, oldInfo, newInfo); if (readyLatch != null) { readyLatch.countDown(); } }
@Override public void onSuccess(NodeData result) { byte[] data = result.getData(); String hostname = (data == null) ? null : new String(data, StandardCharsets.UTF_8); Participant newInfo = new Participant(path, hostname); participants.replace(participantId, oldInfo, newInfo); if (readyLatch != null) { readyLatch.countDown(); } }
@Override public void onSuccess(NodeData result) { if (assignments.get(name) != null) { // Assignment should has been performed while this one is fetching. So, ignore this. return; } byte[] data = result.getData(); ResourceAssignment resourceAssignment = new ResourceAssignment(name); try { if (data != null) { resourceAssignment = CoordinationConstants.RESOURCE_ASSIGNMENT_CODEC.decode(data); } } catch (Throwable t) { LOG.warn("Failed to decode resource assignment. Perform assignment as if no assignment existed.", t); } assignments.put(name, resourceAssignment); performAssignment(requirement, serviceDiscovered); }
@Override public void onSuccess(NodeData result) { byte[] data = result.getData(); if (data == null) { updateAndNotify(name, null); } else { try { updateAndNotify(name, codec.decode(data)); } catch (IOException e) { LOG.error("Failed to decode property data for {}: {}", name, Bytes.toStringBinary(data), e); notifyError(name, e); } } }
@Override public void onSuccess(NodeData result) { if (assignments.get(name) != null) { // Assignment should has been performed while this one is fetching. So, ignore this. return; } byte[] data = result.getData(); ResourceAssignment resourceAssignment = new ResourceAssignment(name); try { if (data != null) { resourceAssignment = CoordinationConstants.RESOURCE_ASSIGNMENT_CODEC.decode(data); } } catch (Throwable t) { LOG.warn("Failed to decode resource assignment. Perform assignment as if no assignment existed.", t); } assignments.put(name, resourceAssignment); performAssignment(requirement, serviceDiscovered); }
@Override public void onSuccess(NodeData result) { byte[] data = result.getData(); if (data == null) { updateAndNotify(name, null); } else { try { updateAndNotify(name, codec.decode(data)); } catch (IOException e) { LOG.error("Failed to decode property data for {}: {}", name, Bytes.toStringBinary(data), e); notifyError(name, e); } } }
@Override public void onSuccess(NodeData result) { Runnable messageRemover = createMessageRemover(path, result.getStat().getVersion()); Message message = MessageCodec.decode(result.getData()); if (message == null) { LOG.error("Failed to decode message for {} in {}", messageId, path); messageRemover.run(); return; } if (LOG.isDebugEnabled()) { LOG.debug("Message received from {}: {}", path, new String(MessageCodec.encode(message), Charsets.UTF_8)); } // Handle the stop message if (handleStopMessage(message, messageRemover)) { return; } // Otherwise, delegate to the child class to handle the message handleMessage(messageId, message, messageRemover); }
private long generateId(Type type) { idLock.get().lock(); try { NodeData nodeData = Futures.getUnchecked(zkClient.getData(type.path)); long counterVal = Longs.fromByteArray(nodeData.getData()); Futures.getUnchecked(zkClient.setData(type.path, Longs.toByteArray(counterVal + incrementBy))); return counterVal; } finally { idLock.get().unlock(); } }
@Override public void onSuccess(NodeData result) { T resource = null; try { resource = codec.decode(result.getData()); resourceCallback.onSuccess(resource); } catch (IOException ioe) { resourceCallback.onFailure(ioe); } }
@Test public void testCreateOrSet() throws Exception { String path = "/parent/testCreateOrSet"; ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build(); zkClient.startAndWait(); // Create with "1" Assert.assertEquals(1, ZKExtOperations.createOrSet(zkClient, path, Suppliers.ofInstance(1), INT_CODEC, 0).get().intValue()); // Should get "1" back Assert.assertEquals(1, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); // Set with "2" Assert.assertEquals(2, ZKExtOperations.createOrSet(zkClient, path, Suppliers.ofInstance(2), INT_CODEC, 0).get().intValue()); // Should get "2" back Assert.assertEquals(2, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); zkClient.stopAndWait(); }
@Test public void testSetOrCreate() throws Exception { String path = "/parent/testSetOrCreate"; ZKClientService zkClient = ZKClientService.Builder.of(zkServer.getConnectionStr()).build(); zkClient.startAndWait(); // Create with "1" Assert.assertEquals(1, ZKExtOperations.setOrCreate(zkClient, path, Suppliers.ofInstance(1), INT_CODEC, 0).get().intValue()); // Should get "1" back Assert.assertEquals(1, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); // Set with "2" Assert.assertEquals(2, ZKExtOperations.setOrCreate(zkClient, path, Suppliers.ofInstance(2), INT_CODEC, 0).get().intValue()); // Should get "2" back Assert.assertEquals(2, INT_CODEC.decode(zkClient.getData(path).get().getData()).intValue()); zkClient.stopAndWait(); }