/** * 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) { 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); }
/** * 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) { 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); }
@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 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(); } }
/** * Decodes the {@link ApplicationMasterLiveNodeData} from the given ZK node data. * * @return the {@link ApplicationMasterLiveNodeData} or {@code null} if failed to decode. */ @Nullable static ApplicationMasterLiveNodeData decode(@Nullable NodeData nodeData) { byte[] data = nodeData == null ? null : nodeData.getData(); if (data == null) { return null; } JsonElement json = GSON.fromJson(new String(data, Charsets.UTF_8), JsonElement.class); if (!json.isJsonObject()) { LOG.warn("Unable to decode live data node."); return null; } JsonObject jsonObj = json.getAsJsonObject(); json = jsonObj.get("data"); if (json == null || !json.isJsonObject()) { LOG.warn("Property data not found in live data node."); return null; } try { return GSON.fromJson(json, ApplicationMasterLiveNodeData.class); } catch (Exception e) { LOG.warn("Failed to decode application live node data.", e); return null; } }
/** * Decodes the {@link ApplicationMasterLiveNodeData} from the given ZK node data. * * @return the {@link ApplicationMasterLiveNodeData} or {@code null} if failed to decode. */ @Nullable static ApplicationMasterLiveNodeData decode(@Nullable NodeData nodeData) { byte[] data = nodeData == null ? null : nodeData.getData(); if (data == null) { return null; } JsonElement json = GSON.fromJson(new String(data, Charsets.UTF_8), JsonElement.class); if (!json.isJsonObject()) { LOG.warn("Unable to decode live data node."); return null; } JsonObject jsonObj = json.getAsJsonObject(); json = jsonObj.get("data"); if (json == null || !json.isJsonObject()) { LOG.warn("Property data not found in live data node."); return null; } try { return GSON.fromJson(json, ApplicationMasterLiveNodeData.class); } catch (Exception e) { LOG.warn("Failed to decode application live node data.", e); return null; } }
@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); } } }