/** * Start the cache. The cache is not started automatically. You must call this method. * * @throws Exception errors */ public void start() throws Exception { start(StartMode.NORMAL); }
public static byte[] getByteData(TreeCacheExtended cache, String path) throws Exception { ChildData cacheData = cache.getCurrentData(path); if (cacheData != null) { return cacheData.getData(); } else { return null; } }
@Override public void invoke() { cache.callListeners(event); }
public List<TreeData> getChildren(String fullPath) { List<TreeData> children = Lists.newArrayList(); for (String child : getChildrenNames(fullPath)) { String childFullPath = ZKPaths.makePath(fullPath, child); children.add(getCurrentData(childFullPath)); } return children; }
this.tree = new TreeCacheExtended(curator, DOSGI_REGISTRY, true); this.tree.getListenable().addListener(this); this.tree.start();
/** * NOTE: this is a BLOCKING method. Completely rebuild the internal cache by querying * for all needed data WITHOUT generating any events to send to listeners. * * @throws Exception errors */ public void rebuild() throws Exception { Preconditions.checkState(!executorService.isShutdown(), "cache has been closed"); ensurePath.ensure(client.getZookeeperClient()); clear(); List<String> children = client.getChildren().forPath(path); for ( String child : children ) { String fullPath = ZKPaths.makePath(path, child); internalRebuildNode(fullPath); if ( rebuildTestExchanger != null ) { rebuildTestExchanger.exchange(new Object()); } } // this is necessary so that any updates that occurred while rebuilding are taken offerOperation(new TreeRefreshOperation(this, path, RefreshMode.FORCE_GET_DATA_AND_STAT)); }
private void applyNewData(String fullPath, int resultCode, Stat stat, byte[] bytes, List<String> children) { if ( resultCode == KeeperException.Code.OK.intValue() ) // otherwise - node must have dropped or something - we should be getting another event { TreeData data = new TreeData(fullPath, stat, bytes, children); TreeData previousData; synchronized (this) { previousData = currentData.getIfPresent(fullPath); currentData.put(fullPath, data); addToParent(fullPath); } if ( previousData == null ) // i.e. new { offerOperation(new TreeEventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_ADDED, data))); } else if ( previousData.getStat().getVersion() != stat.getVersion() ) { if (!diffData || !Arrays.equals(data.getData(), previousData.getData())) { offerOperation(new TreeEventOperation(this, new PathChildrenCacheEvent(PathChildrenCacheEvent.Type.CHILD_UPDATED, data))); } } updateInitialSet(ZKPaths.getNodeFromPath(fullPath), data); } }
private void deactivateInternal() { configCache.getListenable().removeListener(this); Closeables.closeQuietly(configCache); containerCache.getListenable().removeListener(this); Closeables.closeQuietly(containerCache); Closeables.closeQuietly(commandRequestsQueue); Closeables.closeQuietly(commandResponsesQueue); commandsMapper.getTypeFactory().clearCache(); commandsExecutor.shutdownNow(); commandProcessorExecutor.shutdownNow(); callbacksExecutor.shutdownNow(); cacheExecutor.shutdownNow(); }
void getDataAndStat(final String fullPath) throws Exception { if (client.checkExists().forPath(fullPath) == null) { return; } final List<String> children = client.getChildren().usingWatcher(watcher).forPath(fullPath); if ( cacheData ) { Stat stat = new Stat(); byte[] data = null; if ( dataIsCompressed ) { data = client.getData().decompressed().storingStatIn(stat).usingWatcher(watcher).forPath(fullPath); } else { data = client.getData().storingStatIn(stat).usingWatcher(watcher).forPath(fullPath); } applyNewData(fullPath, KeeperException.Code.OK.intValue(), stat, data, children); } else { Stat stat = client.checkExists().usingWatcher(watcher).forPath(fullPath); applyNewData(fullPath, KeeperException.Code.OK.intValue(), stat, null, children); } }
/** * As a memory optimization, you can clear the cached data bytes for a node. Subsequent * calls to {@link ChildData#getData()} for this node will return <code>null</code>. * * @param fullPath the path of the node to clear */ public void clearDataBytes(String fullPath) { clearDataBytes(fullPath, -1); }
private void activateInternal() throws Exception { configCache = new TreeCacheExtended(curator.get(), ZkPath.CONFIGS.getPath(), true, false, true, cacheExecutor); configCache.start(TreeCacheExtended.StartMode.NORMAL); configCache.getListenable().addListener(this); containerCache = new TreeCacheExtended(curator.get(), ZkPath.CONTAINERS.getPath(), true, false, true, cacheExecutor); containerCache.start(TreeCacheExtended.StartMode.NORMAL); containerCache.getListenable().addListener(this); commandsMapper = new ObjectMapper(); commandsMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); commandsMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); // ENTESB-5654: queues to communicate between containers String name = runtimeProperties.get().getRuntimeIdentity(); commandRequestsQueue = QueueBuilder.builder(curator.get(), new CommandConsumer<>(commandRequests, JMXRequest.class), PublicStringSerializer.serializer(), ZkPath.COMMANDS_REQUESTS.getPath(name)).buildQueue(); commandRequestsQueue.start(); commandResponsesQueue = QueueBuilder.builder(curator.get(), null, PublicStringSerializer.serializer(), ZkPath.COMMANDS_RESPONSES.getPath(name)).buildQueue(); commandResponsesQueue.start(); commandProcessorExecutor.submit(new ZkCommandProcessor()); }
private void updateIfNeeded(String path, Stat stat, List<String> children) throws Exception { TreeData data = currentData.getIfPresent(path); if (data != null && stat != null) { if (stat.getMzxid() > data.getStat().getMzxid()) { applyNewData(path, KeeperException.Code.OK.intValue(), stat, data.getData(), children); } } }
public List<String> getChildrenNames(String fullPath) { TreeData parentData = getCurrentData(fullPath); if (parentData != null) { return new ArrayList<String>(parentData.getChildren()); } return Lists.newArrayList(); }
/** * Same as {@link #start()} but gives the option of doing an initial build * * @param buildInitial if true, {@link #rebuild()} will be called before this method * returns in order to get an initial view of the node; otherwise, * the cache will be initialized asynchronously * @deprecated use {@link #start(StartMode)} * @throws Exception errors */ public void start(boolean buildInitial) throws Exception { start(buildInitial ? StartMode.BUILD_INITIAL_CACHE : StartMode.NORMAL); }
@Override public FabricRequirements getRequirements() { assertValid(); try { FabricRequirements answer = null; if (configCache.getCurrentData(REQUIREMENTS_JSON_PATH) != null) { String json = getStringData(configCache, REQUIREMENTS_JSON_PATH); answer = RequirementsJson.fromJSON(json); } if (answer == null) { answer = new FabricRequirements(); } return answer; } catch (Exception e) { throw FabricException.launderThrowable(e); } }
@Override public AutoScaleStatus getAutoScaleStatus() { assertValid(); try { AutoScaleStatus answer = null; String zkPath = ZkPath.AUTO_SCALE_STATUS.getPath(); if (configCache.getCurrentData(zkPath) != null) { String json = getStringData(configCache, zkPath); answer = RequirementsJson.autoScaleStatusFromJSON(json); } if (answer == null) { answer = new AutoScaleStatus(); } return answer; } catch (Exception e) { throw FabricException.launderThrowable(e); } }
@Override public String getDefaultVersion() { assertValid(); try { String version = null; if (configCache.getCurrentData(ZkPath.CONFIG_DEFAULT_VERSION.getPath()) != null) { version = getStringData(configCache, ZkPath.CONFIG_DEFAULT_VERSION.getPath()); } if (version == null || version.isEmpty()) { version = ZkDefs.DEFAULT_VERSION; setData(curator.get(), ZkPath.CONFIG_DEFAULT_VERSION.getPath(), version); setData(curator.get(), ZkPath.CONFIG_VERSION.getPath(version), (String) null); } return version; } catch (Exception e) { throw FabricException.launderThrowable(e); } }