List of usage examples for org.apache.zookeeper ZooKeeper getData
public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, InterruptedException
From source file:blazingcache.zookeeper.ZKCacheServerLocator.java
License:Apache License
@Override protected ServerHostData getServer() { String leaderPath = basePath + "/leader"; try {//from w w w .j a v a2s.c o m byte[] data; if (ownedZk) { CountDownLatch connection = new CountDownLatch(1); try { LOGGER.finest("creating temp ZK client for discovery"); ZooKeeper client = new ZooKeeper(zkAddress, zkSessiontimeout, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected || event.getState() == Event.KeeperState.ConnectedReadOnly) { connection.countDown(); } LOGGER.severe("process ZK event " + event.getState() + " " + event.getType() + " " + event.getPath()); } }); try { connection.await(connectTimeout, TimeUnit.MILLISECONDS); LOGGER.finest("ZK client is " + client); // se la connessione non sar stabilita in tempo o c' qualche problem troveremo un ConnectionLoss ad esempio data = client.getData(leaderPath, false, null); } finally { client.close(); } } catch (IOException err) { LOGGER.log(Level.SEVERE, "zookeeper client not available: " + err); return null; } } else { ZooKeeper client = zkSupplier.get(); if (client == null) { LOGGER.log(Level.SEVERE, "zookeeper client available"); return null; } data = client.getData(leaderPath, false, null); } lastKnownServer = ServerHostData.parseHostdata(data); return lastKnownServer; } catch (KeeperException.NoNodeException nobroker) { LOGGER.log(Level.SEVERE, "zookeeper client error", nobroker); return null; } catch (KeeperException | InterruptedException err) { LOGGER.log(Level.SEVERE, "zookeeper client error", err); return null; } }
From source file:cn.dataprocess.cfzk.ZkWatherUtil.java
/** * ?/*w ww. ja v a 2s.c o m*/ * * @param zooKeeper * @param watcher * @param pPath * @throws KeeperException * @throws InterruptedException */ public static void watch(ZooKeeper zooKeeper, Watcher watcher, String pPath) throws KeeperException, InterruptedException { zooKeeper.exists(pPath, watcher); zooKeeper.getData(pPath, watcher, null); zooKeeper.getChildren(pPath, watcher); }
From source file:cn.dataprocess.cfzk.ZkWatherUtil.java
public static Object getData(ZooKeeper zooKeeper, String pPath) throws IOException, KeeperException, InterruptedException { byte[] data = zooKeeper.getData(pPath, null, null); log.debug("data=" + data); String datas = new String(data); Object ret = datas;// w w w . j a v a2 s . co m if (datas != null && !datas.trim().isEmpty()) { datas = datas.trim(); if (datas.startsWith("{") && datas.endsWith("}")) { Map<String, Object> map = JsonUtil.toJavaBean(datas, Map.class); ret = map; } else if (datas.startsWith("[") && datas.endsWith("]")) { Collection<Object> ocoll = JsonUtil.toJavaBean(datas, Collection.class); ret = ocoll; } } log.debug("ret=" + ret); return ret; }
From source file:com.alibaba.otter.shared.arbitrate.impl.ArbitrateViewServiceImpl.java
License:Apache License
public List<ProcessStat> listProcesses(Long channelId, Long pipelineId) { List<ProcessStat> processStats = new ArrayList<ProcessStat>(); String processRoot = ManagePathUtils.getProcessRoot(channelId, pipelineId); IZkConnection connection = zookeeper.getConnection(); // zkclient?stat??zk ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper(); // ?process/*from www.j a v a 2 s. c o m*/ List<String> processNodes = zookeeper.getChildren(processRoot); List<Long> processIds = new ArrayList<Long>(); for (String processNode : processNodes) { processIds.add(ManagePathUtils.getProcessId(processNode)); } Collections.sort(processIds); for (int i = 0; i < processIds.size(); i++) { Long processId = processIds.get(i); // ?process?? ProcessStat processStat = new ProcessStat(); processStat.setPipelineId(pipelineId); processStat.setProcessId(processId); List<StageStat> stageStats = new ArrayList<StageStat>(); processStat.setStageStats(stageStats); try { String processPath = ManagePathUtils.getProcess(channelId, pipelineId, processId); Stat zkProcessStat = new Stat(); List<String> stages = orginZk.getChildren(processPath, false, zkProcessStat); Collections.sort(stages, new StageComparator()); StageStat prev = null; for (String stage : stages) {// ?processstage String stagePath = processPath + "/" + stage; Stat zkStat = new Stat(); StageStat stageStat = new StageStat(); stageStat.setPipelineId(pipelineId); stageStat.setProcessId(processId); byte[] bytes = orginZk.getData(stagePath, false, zkStat); if (bytes != null && bytes.length > 0) { // ?zookeeperdata?managernodePipeKey?????'@'? String json = StringUtils.remove(new String(bytes, "UTF-8"), '@'); EtlEventData data = JsonUtils.unmarshalFromString(json, EtlEventData.class); stageStat.setNumber(data.getNumber()); stageStat.setSize(data.getSize()); Map exts = new HashMap(); if (!CollectionUtils.isEmpty(data.getExts())) { exts.putAll(data.getExts()); } exts.put("currNid", data.getCurrNid()); exts.put("nextNid", data.getNextNid()); exts.put("desc", data.getDesc()); stageStat.setExts(exts); } if (prev != null) {// start? stageStat.setStartTime(prev.getEndTime()); } else { stageStat.setStartTime(zkProcessStat.getMtime()); // process?,select // await??USED? } stageStat.setEndTime(zkStat.getMtime()); if (ArbitrateConstants.NODE_SELECTED.equals(stage)) { stageStat.setStage(StageType.SELECT); } else if (ArbitrateConstants.NODE_EXTRACTED.equals(stage)) { stageStat.setStage(StageType.EXTRACT); } else if (ArbitrateConstants.NODE_TRANSFORMED.equals(stage)) { stageStat.setStage(StageType.TRANSFORM); // } else if // (ArbitrateConstants.NODE_LOADED.equals(stage)) { // stageStat.setStage(StageType.LOAD); } prev = stageStat; stageStats.add(stageStat); } // ?? StageStat currentStageStat = new StageStat(); currentStageStat.setPipelineId(pipelineId); currentStageStat.setProcessId(processId); if (prev == null) { byte[] bytes = orginZk.getData(processPath, false, zkProcessStat); if (bytes == null || bytes.length == 0) { continue; // } ProcessNodeEventData nodeData = JsonUtils.unmarshalFromByte(bytes, ProcessNodeEventData.class); if (nodeData.getStatus().isUnUsed()) {// process, continue; // process } else { currentStageStat.setStage(StageType.SELECT);// select? currentStageStat.setStartTime(zkProcessStat.getMtime()); } } else { // ?stage StageType stage = prev.getStage(); if (stage.isSelect()) { currentStageStat.setStage(StageType.EXTRACT); } else if (stage.isExtract()) { currentStageStat.setStage(StageType.TRANSFORM); } else if (stage.isTransform()) { currentStageStat.setStage(StageType.LOAD); } else if (stage.isLoad()) {// ?? continue; } currentStageStat.setStartTime(prev.getEndTime());// ? } if (currentStageStat.getStage().isLoad()) {// loadprocess if (i == 0) { stageStats.add(currentStageStat); } } else { stageStats.add(currentStageStat);// } } catch (NoNodeException e) { // ignore } catch (KeeperException e) { throw new ArbitrateException(e); } catch (InterruptedException e) { // ignore } catch (UnsupportedEncodingException e) { // ignore } processStats.add(processStat); } return processStats; }
From source file:com.alibaba.otter.shared.arbitrate.impl.ArbitrateViewServiceImpl.java
License:Apache License
public PositionEventData getCanalCursor(String destination, short clientId) { String path = String.format(CANAL_CURSOR_PATH, destination, String.valueOf(clientId)); try {/*from ww w . j a va2s . c om*/ IZkConnection connection = zookeeper.getConnection(); // zkclient?stat??zk ZooKeeper orginZk = ((ZooKeeperx) connection).getZookeeper(); Stat stat = new Stat(); byte[] bytes = orginZk.getData(path, false, stat); PositionEventData eventData = new PositionEventData(); eventData.setCreateTime(new Date(stat.getCtime())); eventData.setModifiedTime(new Date(stat.getMtime())); eventData.setPosition(new String(bytes, "UTF-8")); return eventData; } catch (Exception e) { return null; } }
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Attempts to get data from ZooKeeper in a single operation. * <p>/*from w ww.j ava 2 s .co m*/ * Because it is possible that the node under request does not exist, it is possible that * a KeeperException.NONODE exception will be thrown. This method swallows * NONODE exceptions and returns an empty byte[] when that node does not exist. * * @param zk the ZooKeeper client * @param node the node to get data for * @param watcher any watcher to attach to this node, if it exists * @param stat a stat object for use in the getData call * @return the byte[] information contained in that node, if that node exists. If {@code node} does not exist, * then this returns an empty byte[] * @throws InterruptedException if communication between the ZooKeeper client and servers are broken * @throws KeeperException if there is a problem getting Data from the ZooKeeper server that <i>doesn't</i> have * the error code of NONODE */ public static byte[] safeGetData(ZooKeeper zk, String node, Watcher watcher, Stat stat) throws InterruptedException, KeeperException { try { return zk.getData(node, watcher, stat); } catch (KeeperException e) { if (e.code() != KeeperException.Code.NONODE) throw e; else return new byte[] {}; } }
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Attempts to get data from ZooKeeper in a single operation. Because it is possible that the node under request * does not exist, it is possible that a KeeperException.NONODE exception will be thrown. This method swallows * NONODE exceptions and returns an empty byte[] when that node does not exist. * * @param zk the ZooKeeper client// w w w .j a va 2 s. c om * @param node the node to get data for * @param watch true if the default watcher is to be attached to this node's data * @param stat a stat object for use in the getData call * @return the byte[] information contained in that node, if that node exists. If {@code node} does not exist, * then this returns an empty byte[] * @throws InterruptedException if communication between the ZooKeeper client and servers are broken * @throws KeeperException if there is a problem getting Data from the ZooKeeper server that <i>doesn't</i> have * the error code of NONODE */ public static byte[] safeGetData(ZooKeeper zk, String node, boolean watch, Stat stat) throws InterruptedException, KeeperException { try { return zk.getData(node, watch, stat); } catch (KeeperException e) { if (e.code() != KeeperException.Code.NONODE) throw e; else return new byte[] {}; } }
From source file:com.asuraiv.coordination.menu.MainMenu.java
License:Open Source License
/** * ? ./*w w w. ja v a2s .c o m*/ */ private void doTaskListUp() { try { ZooKeeper zk = new ZooKeeper("10.113.182.195:2181", 15000, this); System.out.println(); System.out.println("### ###"); List<String> tasks = zk.getChildren("/tasks", false, null); if (CollectionUtils.isEmpty(tasks)) { System.out.println("[WARNNING] ?? ? ."); } for (String task : tasks) { String status = new String(zk.getData("/tasks/" + task, false, null)); System.out.println(task + ": " + status); } zk.close(); } catch (IOException e) { e.printStackTrace(); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:com.bigdata.jini.start.TestServiceStarter.java
License:Open Source License
/** * Unit test verifies that we can start and destroy a service instance using * a {@link BigdataServiceConfiguration}. The test waits until the service * has been assigned its serviceId by jini and then verifies that the * serviceId is recorded in the physicalService znode. * /*from ww w. j a v a 2 s . c om*/ * @throws ConfigurationException * @throws Exception */ public void test_startServer() throws ConfigurationException, Exception { // // create a unique fake zroot // final String zroot = createTestZRoot(); // // // the config for that fake zroot. // final String zconfig = zroot + BigdataZooDefs.ZSLASH // + BigdataZooDefs.CONFIG; final ZooKeeper zookeeper = fed.getZookeeper(); final TransactionServerConfiguration serviceConfig = new TransactionServerConfiguration(config); // znode for serviceConfiguration final String zserviceConfig = zookeeper.create( fed.getZooConfig().zroot + BigdataZooDefs.ZSLASH + BigdataZooDefs.CONFIG + BigdataZooDefs.ZSLASH + TransactionServer.class.getName(), SerializerUtil.serialize(serviceConfig), acl, CreateMode.PERSISTENT); /* * znode for a logical service (the logical service is either a * collection of peers or a service failover chain, depending on the * type of the service). Logical services are persistent. Each one is * assigned a unique (sequential) identifier by zookeeper. It is also * assigned a random UUID. */ final String logicalServiceZPath = zookeeper.create(zserviceConfig + BigdataZooDefs.LOGICAL_SERVICE_PREFIX, SerializerUtil.serialize(UUID.randomUUID()), acl, CreateMode.PERSISTENT_SEQUENTIAL); /* * Create the znode that is the parent for the physical service * instances (direct child of the logicalSevice znode). */ final String parentZNode = logicalServiceZPath + "/" + BigdataZooDefs.PHYSICAL_SERVICES_CONTAINER; final ManagedServiceStarter<?> serviceStarter = (ManagedServiceStarter<?>) serviceConfig .newServiceStarter(fed, listener, logicalServiceZPath, null/* attributes */); zookeeper.create(parentZNode, SerializerUtil.serialize(serviceStarter.serviceUUID), acl, CreateMode.PERSISTENT); /* * Create the znode for the election of the primary physical service for * this logical service (direct child of the logicalSevice znode). */ zookeeper.create(logicalServiceZPath + "/" + BigdataZooDefs.MASTER_ELECTION, new byte[0], acl, CreateMode.PERSISTENT); // will be zero unless we started a zookeeper server above. final int processCountBefore = listener.running.size(); // start the service. final ProcessHelper processHelper = serviceStarter.call(); // verify listener was notified of service start. assertEquals(processCountBefore + 1, listener.running.size()); if (log.isInfoEnabled()) log.info(dumpZooKeeperState("Zookeeper dump:\n", zookeeper)); // verify that the physicalService was registered with zookeeper. final ServiceItem serviceItem; final IService proxy; final String physicalServiceZPath; { final List<String> children = zookeeper.getChildren(logicalServiceZPath, false/* watch */); if (log.isInfoEnabled()) log.info("physicalServices=" + children); // will fail if the znode was not registered. assertEquals(2, children.size()); /* * The znode which is the container for the physical service that we * created. * * Note: You could explicitly build the correct zpath using the * serviceUUID obtained from the service proxy. */ physicalServiceZPath = logicalServiceZPath + "/" + BigdataZooDefs.PHYSICAL_SERVICES_CONTAINER; // get the serviceUUID from the physicalServiceZNode's data. { final byte[] data = zookeeper.getData(physicalServiceZPath, false/* watch */, new Stat()); assertNotNull(physicalServiceZPath, data); assertTrue(physicalServiceZPath, data.length > 0); final UUID serviceUUID = (UUID) SerializerUtil.deserialize(data); serviceItem = discoverService(serviceUUID); } // verify that the service item is registered with jini. assertNotNull(serviceItem); // save reference to the service proxy. proxy = (IService) serviceItem.service; } // Verify the service UUID using the proxy assertEquals(JiniUtil.serviceID2UUID(serviceItem.serviceID), proxy.getServiceUUID()); // Verify the service name using the proxy assertEquals(serviceStarter.serviceName, proxy.getServiceName()); // Tell the service to destroy itself. ((RemoteDestroyAdmin) proxy).destroy(); // listener.running.get(0).destroy(); // wait a bit for the process to die. processHelper.exitValue(10L, TimeUnit.SECONDS); // verify that it has been removed from our listener. assertEquals("Expected " + processCountBefore + ", but #running=" + listener.running.size() + ", processes=" + listener.running.toString(), processCountBefore, listener.running.size()); ZooHelper.destroyZNodes(zookeeper, parentZNode, 1); /* * Wait until the znode for the physical service has been removed. * * Note: An ephemeral znode will be removed once the zookeeper client * either times out or is explicitly closed. Since we are killing the * process rather than terminating the service normally we may have to * raise the timeout before zookeeper will delete the service's znode on * its behalf. */ if (!ZNodeDeletedWatcher.awaitDelete(zookeeper, physicalServiceZPath, 20000, TimeUnit.MILLISECONDS)) { fail("znode not removed: zpath=" + physicalServiceZPath); } }
From source file:com.bigdata.service.jini.master.TaskMaster.java
License:Open Source License
/** * Sets up the {@link JobState} in zookeeper, including the assignment of * service {@link UUID}s to each client. {@link #jobState} will be replaced * with the {@link JobState} read from zookeeper if a pre-existing job is * found in zookeeper.//from w w w .j a v a 2 s .com * * @return The global lock for the master running the job. * * @throws KeeperException * @throws InterruptedException * @throws TimeoutException */ protected ZLock setupJob() throws KeeperException, InterruptedException, TimeoutException { final ZooKeeper zookeeper = fed.getZookeeperAccessor().getZookeeper(); try { // ensure znode exists. zookeeper.create(fed.getZooConfig().zroot + "/" + BigdataZooDefs.JOBS, new byte[0], fed.getZooConfig().acl, CreateMode.PERSISTENT); } catch (NodeExistsException ex) { // ignore. } final String jobClassZPath = jobState.getJobClassZPath(fed); try { // ensure znode exists. zookeeper.create(jobClassZPath, new byte[0], fed.getZooConfig().acl, CreateMode.PERSISTENT); } catch (NodeExistsException ex) { // ignore. } /* * Use a global lock to protect the job. * * Note: We just created parent of this lock node (or at any rate, * ensured that it exists). */ final ZLock zlock = ZLockImpl.getLock(zookeeper, jobClassZPath + "/" + "locknode_" + jobState.jobName, fed.getZooConfig().acl); zlock.lock(); try { final String jobZPath = jobState.getJobZPath(fed); if (jobState.deleteJob && zookeeper.exists(jobZPath, false/* watch */) != null) { /* * Delete old job. */ log.warn("Deleting old job: " + jobZPath); ZooHelper.destroyZNodes(fed.getZookeeperAccessor().getZookeeper(), jobZPath, 0/* depth */); // detach the performance counters for the old job. detachPerformanceCounters(); } try { // create znode that is the root for the job. zookeeper.create(jobZPath, SerializerUtil.serialize(jobState), fed.getZooConfig().acl, CreateMode.PERSISTENT); if (log.isInfoEnabled()) log.info("New job: " + jobState); try { /* * Assign clients to services. */ final DiscoveredServices discoveredServices = new DiscoverServicesWithPreconditionsTask() .call(); jobState.clientServiceMap.assignClientsToServices(discoveredServices.clientServiceItems); jobState.aggregatorServiceMap .assignClientsToServices(discoveredServices.aggregatorServiceItems); // write those assignments into zookeeper. zookeeper.setData(jobZPath, SerializerUtil.serialize(jobState), -1/* version */); if (log.isInfoEnabled()) log.info("Wrote client assignments into zookeeper."); } catch (Throwable t) { /* * Since we created the jobState znode, delete the jobState * while we are still holding the zlock. */ try { zookeeper.delete(jobZPath, -1/* version */); } catch (Throwable t2) { log.error(t2); } throw new RuntimeException(t); } } catch (NodeExistsException ex) { /* * Resuming a job already in progress and/or providing backup * clients for a job that is currently running. * * Note: We use the client to data service UUID assignments read * from the znode data which are part of the jobState * * @todo stable assignments are only required when clients will * read or write local data. This is not the common case and * should be a declarative configuration option. */ jobState = (S) SerializerUtil.deserialize(zookeeper.getData(jobZPath, false, new Stat())); jobState.clientServiceMap.resolveServiceUUIDs(fed); jobState.aggregatorServiceMap.resolveServiceUUIDs(fed); jobState.resumedJob = true; log.warn("Pre-existing job: " + jobZPath); } } catch (KeeperException t) { zlock.unlock(); throw t; } catch (InterruptedException t) { zlock.unlock(); throw t; } catch (Throwable t) { zlock.unlock(); throw new RuntimeException(t); } return zlock; }