/** * Instantiates a new WorkerNodeTracker. * * @param zkClient the Zookeeper client */ public WorkerNodeTracker(CuratorFramework zkClient) { super(); this.zkClient = zkClient; init(); }
public boolean doZkClientAction(ZkClientAction action) throws IOException { return doZkClientAction(action, false); }
/** * On master change. * * @param currentData the current data */ protected void onMasterChange(ChildData currentData) { ControlNodeInfo controlServerInfo = extractControlServerInfo(currentData); for (ControlNodeListener listener : listeners) { listener.onControlNodeChange(controlServerInfo); } }
private void addNode(OperationsNodeInfo info) { LOG.info("Add/Update node {}", info); opsMap.put(getNameFromConnectionInfo(info.getConnectionInfo()), info); LOG.info("Cleanup cached responses"); cache.clear(); }
/** * Operations Node added. * * @param data the data */ protected void endpointAdded(ChildData data) { OperationsNodeInfo nodeInfo = extractOperationServerInfo(data); String endpointAddress = constructEndpointAddress(nodeInfo); operationNodesStartTimes.put(endpointAddress, nodeInfo.getTimeStarted()); for (OperationsNodeListener listener : endpointListeners) { listener.onNodeAdded(nodeInfo); } }
/** * Operations Node updated. * * @param data the data */ protected void endpointUpdated(ChildData data) { OperationsNodeInfo nodeInfo = extractOperationServerInfo(data); String endpointAddress = constructEndpointAddress(nodeInfo); operationNodesStartTimes.put(endpointAddress, nodeInfo.getTimeStarted()); for (OperationsNodeListener listener : endpointListeners) { listener.onNodeUpdated(nodeInfo); } }
@Override public void onNodeRemoved(BootstrapNodeInfo nodeInfo) { String dnsName = getNameFromConnectionInfo(nodeInfo.getConnectionInfo()); LOG.info("Bootstrap server {} removed", dnsName); bootstrapsMap.remove(dnsName); }
/** * Bootstrap updated. * * @param data the data */ protected void bootstrapUpdated(ChildData data) { BootstrapNodeInfo nodeInfo = extractBootstrapServerInfo(data); String bootstrapAddress = constructBootstrapAddress(nodeInfo); bootstrapNodesStartTimes.put(bootstrapAddress, nodeInfo.getTimeStarted()); for (BootstrapNodeListener listener : bootstrapListeners) { listener.onNodeUpdated(nodeInfo); } }
/** * Bootstrap added. * * @param data the data */ protected void bootstrapAdded(ChildData data) { BootstrapNodeInfo nodeInfo = extractBootstrapServerInfo(data); String bootstrapAddress = constructBootstrapAddress(nodeInfo); bootstrapNodesStartTimes.put(bootstrapAddress, nodeInfo.getTimeStarted()); for (BootstrapNodeListener listener : bootstrapListeners) { listener.onNodeAdded(nodeInfo); } }
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception { switch (event.getType()) { case CHILD_ADDED: endpointAdded(event.getData()); break; case CHILD_UPDATED: endpointUpdated(event.getData()); break; case CHILD_REMOVED: endpointRemoved(event.getData()); break; default: break; } } });
@Override protected void onNoMaster() throws IOException { super.onNoMaster(); createZkNode(); } }
/** * Do Zookeeper client action. * * @param action the Zookeeper client action * @param throwIoException define throw or not IOException * @return boolean 'true' if doWithZkClient method works without exceptions * @throws IOException the IOException */ public boolean doZkClientAction(ZkClientAction action, boolean throwIoException) throws IOException { try { action.doWithZkClient(zkClient); return true; } catch (Exception ex) { LOG.error("Unknown Error", ex); close(); if (throwIoException) { throw new IOException(ex); } else { return false; } } }
/** * Calculates the crc32 hash based on the connection info properties. * * @param connectionInfo the connection info * @return crc32 hash */ public static int crc32(ConnectionInfo connectionInfo) { CRC32 crc32 = new CRC32(); crc32.update(getNameFromConnectionInfo(connectionInfo).getBytes(UTF8)); return (int) crc32.getValue(); } }
@Override public void nodeChanged() throws Exception { ChildData currentData = controlCache.getCurrentData(); if (currentData == null) { LOG.warn("Control service node died!"); onNoMaster(); } else { LOG.warn("Control service node changed!"); onMasterChange(currentData); } } });
@Override public void unhandledError(String message, Throwable ex) { LOG.error("Unrecoverable error: " + message, ex); try { close(); } catch (IOException ioe) { LOG.warn("Exception when closing.", ioe); } } };
@Override public ControlNodeInfo getControlServerInfo() { if (controlCache != null && controlCache.getCurrentData() != null) { return extractControlServerInfo(controlCache.getCurrentData()); } else { return null; } }
private void removeNode(OperationsNodeInfo info) { if (opsMap.remove(getNameFromConnectionInfo(info.getConnectionInfo())) != null) { LOG.info("Removed node {}", info); } else { LOG.warn("Failed to remove node {}", info); } LOG.info("Cleanup cached responses"); cache.clear(); }
@Override public void onNodeUpdated(BootstrapNodeInfo nodeInfo) { String dnsName = getNameFromConnectionInfo(nodeInfo.getConnectionInfo()); LOG.info("Bootstrap server {} updated", dnsName); bootstrapsMap.put(dnsName, nodeInfo); }
public WorkerNodeTracker() { super(); init(); }
@Override public void close() throws IOException { endpointCache.close(); bootstrapCache.close(); super.close(); } }