@Override public boolean createZkNode() throws IOException { return doZkClientAction(new ZkClientAction() { @Override public void doWithZkClient(CuratorFramework client) throws Exception { nodePath = client .create() .creatingParentsIfNeeded() .withMode(CreateMode.EPHEMERAL_SEQUENTIAL) .forPath( BOOTSTRAP_SERVER_NODE_PATH + BOOTSTRAP_SERVER_NODE_PATH, bootstrapNodeAvroConverter.get().toByteArray(nodeInfo)); LOG.info("Created node with path: " + nodePath); } }); }
/** * Stop zk. */ private void stopZk() { try { if (bootstrapNode != null) { bootstrapNode.close(); } } catch (IOException ex) { LOG.warn("Exception when closing ZK node", ex); } finally { bootstrapNode = null; } }
@Override public void onTransportsStarted(List<TransportMetaData> mdList) { BootstrapNodeInfo info = bootstrapNode.getNodeInfo(); info.setTransports(mdList); try { bootstrapNode.updateNodeData(info); } catch (IOException ex) { LOG.error("Failed to update bootstrap node info", ex); } }
/** * Bean init-method. * * @param zkNode the bootstrap node */ public void init(BootstrapNode zkNode) { LOG.info("Initializing with {}", zkNode); opsMap.clear(); synchronized (listenerLock) { LOG.info("Registering as listener to ZK updates"); zkNode.addListener(this); LOG.info("Adding existing nodes"); for (OperationsNodeInfo info : zkNode.getCurrentOperationServerNodes()) { addNode(info); } LOG.info("Added existing nodes"); } }
/** * Start zk. * * @throws Exception in case of error */ private void startZk() throws Exception { // NOSONAR if (getNodeConfig().isZkEnabled()) { LOG.info("Bootstrap service starting ZooKepper connection to {}", getNodeConfig().getZkHostPortList()); BootstrapNodeInfo nodeInfo = new BootstrapNodeInfo(); ByteBuffer keyData = ByteBuffer.wrap(bootstrapKeyStoreService.getPublicKey().getEncoded()); LOG.trace("Bootstrap service: registering in ZK: thriftHost {}; " + "thriftPort {}; nettyHost {}; nettyPort {}", getNodeConfig().getThriftHost(), getNodeConfig().getThriftPort()); nodeInfo.setConnectionInfo(new ConnectionInfo( getNodeConfig().getThriftHost(), getNodeConfig().getThriftPort(), keyData)); nodeInfo.setTransports(new ArrayList<>()); nodeInfo.setTimeStarted(System.currentTimeMillis()); bootstrapNode = new BootstrapNode(nodeInfo, zkClient); if (bootstrapNode != null) { bootstrapNode.start(); } } }
/** * 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); } }
/** * Bootstrap removed. * * @param data the data */ protected void bootstrapRemoved(ChildData data) { BootstrapNodeInfo nodeInfo = extractBootstrapServerInfo(data); String bootstrapAddress = constructBootstrapAddress(nodeInfo); Long removeTime = nodeInfo.getTimeStarted(); Long updateTime = bootstrapNodesStartTimes.get(bootstrapAddress); if (updateTime == null || removeTime >= updateTime) { for (BootstrapNodeListener listener : bootstrapListeners) { listener.onNodeRemoved(nodeInfo); } } else { LOG.debug("Ignoring [{}] bootstrap removal, as it was before add/update", bootstrapAddress); } }
/** * Updates current ZK node data. * * @param currentNodeInfo the current node info * @throws IOException Signals that an I/O exception has occurred. */ public void updateNodeData(BootstrapNodeInfo currentNodeInfo) throws IOException { this.nodeInfo = currentNodeInfo; doZkClientAction(new ZkClientAction() { @Override public void doWithZkClient(CuratorFramework client) throws Exception { client.setData().forPath(nodePath, bootstrapNodeAvroConverter.get().toByteArray(nodeInfo)); } }); }