public static void prepareNewNamespaceState(IZooReaderWriter zoo, String instanceId, Namespace.ID namespaceId, String namespace, NodeExistsPolicy existsPolicy) throws KeeperException, InterruptedException { log.debug("Creating ZooKeeper entries for new namespace {} (ID: {})", namespace, namespaceId); String zPath = Constants.ZROOT + "/" + instanceId + Constants.ZNAMESPACES + "/" + namespaceId; zoo.putPersistentData(zPath, new byte[0], existsPolicy); zoo.putPersistentData(zPath + Constants.ZNAMESPACE_NAME, namespace.getBytes(UTF_8), existsPolicy); zoo.putPersistentData(zPath + Constants.ZNAMESPACE_CONF, new byte[0], existsPolicy); }
IZooReaderWriter zk = context.getZooReaderWriter(); if (opts.auth != null) { zk.getZooKeeper().addAuthInfo("digest", ("accumulo:" + opts.auth).getBytes(UTF_8)); for (String child : zk.getChildren(root)) { if (Constants.ZINSTANCES.equals("/" + child)) { for (String instanceName : zk.getChildren(root + Constants.ZINSTANCES)) { String instanceNamePath = root + Constants.ZINSTANCES + "/" + instanceName; byte[] id = zk.getData(instanceNamePath, null); if (id != null && !new String(id, UTF_8).equals(context.getInstanceID())) { try { zk.recursiveDelete(instanceNamePath, NodeMissingPolicy.SKIP); } catch (KeeperException.NoAuthException ex) { log.warn("Unable to delete {}", instanceNamePath); String path = root + "/" + child; try { zk.recursiveDelete(path, NodeMissingPolicy.SKIP); } catch (KeeperException.NoAuthException ex) { log.warn("Unable to delete {}", path);
@Override public void initializeSecurity(String principal, byte[] token) { try { // remove old settings from zookeeper first, if any IZooReaderWriter zoo = context.getZooReaderWriter(); synchronized (zooCache) { zooCache.clear(); if (zoo.exists(zkUserPath)) { zoo.recursiveDelete(zkUserPath, NodeMissingPolicy.SKIP); log.info("Removed {}/ from zookeeper", zkUserPath); } // prep parent node of users with root username // ACCUMULO-4140 The root user needs to be stored un-base64 encoded in the znode's value byte[] principalData = principal.getBytes(UTF_8); zoo.putPersistentData(zkUserPath, principalData, NodeExistsPolicy.FAIL); // Create the root user in ZK using base64 encoded name (since the name is included in the // znode) createUserNodeInZk(Base64.getEncoder().encodeToString(principalData)); } } catch (KeeperException | InterruptedException e) { log.error("Failed to initialize security", e); throw new RuntimeException(e); } }
public void deleteLocks(IZooReaderWriter zk, String path, String txidStr) throws KeeperException, InterruptedException { // delete any locks assoc w/ fate operation List<String> lockedIds = zk.getChildren(path); for (String id : lockedIds) { List<String> lockNodes = zk.getChildren(path + "/" + id); for (String node : lockNodes) { String lockPath = path + "/" + id + "/" + node; byte[] data = zk.getData(path + "/" + id + "/" + node, null); String lda[] = new String(data, UTF_8).split(":"); if (lda[1].equals(txidStr)) zk.recursiveDelete(lockPath, NodeMissingPolicy.SKIP); } } }
private void registerInZooKeeper(String name, String root) throws Exception { IZooReaderWriter zoo = context.getZooReaderWriter(); zoo.putPersistentData(root, new byte[0], NodeExistsPolicy.SKIP); log.info("Registering tracer {} at {}", name, root); String path = zoo.putEphemeralSequential(root + "/trace-", name.getBytes(UTF_8)); zoo.exists(path, this); }
private static void zapDirectory(IZooReaderWriter zoo, String path, Opts opts) throws KeeperException, InterruptedException { List<String> children = zoo.getChildren(path); for (String child : children) { message("Deleting " + path + "/" + child + " from zookeeper", opts); zoo.recursiveDelete(path + "/" + child, NodeMissingPolicy.SKIP); } } }
@Override public SortedMap<Long,byte[]> getEarlierEntries(long entry) { SortedMap<Long,byte[]> result = new TreeMap<>(); try { List<String> children = Collections.emptyList(); try { children = zoo.getChildren(path); } catch (KeeperException.NoNodeException ex) { // the path does not exist (it was deleted or not created yet), that is ok there are no // earlier entries then } for (String name : children) { // this try catch must be done inside the loop because some subset of the children may exist try { byte[] data = zoo.getData(path + "/" + name, null); long order = Long.parseLong(name.substring(PREFIX.length())); if (order <= entry) result.put(order, data); } catch (KeeperException.NoNodeException ex) { // ignored } } } catch (Exception ex) { throw new RuntimeException(ex); } return result; }
List<String> children = zooKeeper.getChildren(path); Stat stat = zooKeeper.getStatus(lockToWatch, new Watcher() {
/** * Update the data file for the root tablet */ private static void updateRootTabletDataFile(ServerContext context, Set<String> unusedWalLogs) { IZooReaderWriter zk = context.getZooReaderWriter(); String root = MetadataTableUtil.getZookeeperLogLocation(context); for (String entry : unusedWalLogs) { String[] parts = entry.split("/"); String zpath = root + "/" + parts[parts.length - 1]; while (true) { try { if (zk.exists(zpath)) { log.debug("Removing WAL reference for root table {}", zpath); zk.recursiveDelete(zpath, NodeMissingPolicy.SKIP); } break; } catch (KeeperException | InterruptedException e) { log.error("{}", e.getMessage(), e); } sleepUninterruptibly(1, TimeUnit.SECONDS); } } }
@Override public long addEntry(byte[] data) { String newPath; try { while (true) { try { if (ephemeral) { newPath = zoo.putEphemeralSequential(path + "/" + PREFIX, data); } else { newPath = zoo.putPersistentSequential(path + "/" + PREFIX, data); } String[] parts = newPath.split("/"); String last = parts[parts.length - 1]; return Long.parseLong(last.substring(PREFIX.length())); } catch (NoNodeException nne) { // the parent does not exist so try to create it zoo.putPersistentData(path, new byte[] {}, NodeExistsPolicy.SKIP); } } } catch (Exception ex) { throw new RuntimeException(ex); } }
private static void initZooKeeper(Opts opts, String uuid, String instanceNamePath) throws KeeperException, InterruptedException { ZooUtil.putPersistentData(zoo.getZooKeeper(), Constants.ZROOT, new byte[0], -1, NodeExistsPolicy.SKIP, Ids.OPEN_ACL_UNSAFE); ZooUtil.putPersistentData(zoo.getZooKeeper(), Constants.ZROOT + Constants.ZINSTANCES, new byte[0], -1, NodeExistsPolicy.SKIP, Ids.OPEN_ACL_UNSAFE); zoo.recursiveDelete(instanceNamePath, NodeMissingPolicy.SKIP); zoo.putPersistentData(instanceNamePath, uuid.getBytes(UTF_8), NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot, new byte[0], NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZTABLES, Constants.ZTABLES_INITIAL_ID, NodeExistsPolicy.FAIL); TableManager.prepareNewTableState(uuid, Constants.METADATA_TABLE_ID, Constants.METADATA_TABLE_NAME, TableState.ONLINE, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZTSERVERS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZPROBLEMS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZROOT_TABLET, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZROOT_TABLET_WALOGS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZTRACERS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZMASTERS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZMASTER_LOCK, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZMASTER_GOAL_STATE, MasterGoalState.NORMAL.toString().getBytes(UTF_8), NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZGC, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZGC_LOCK, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZCONFIG, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZTABLE_LOCKS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZHDFS_RESERVATIONS, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZNEXT_FILE, ZERO_CHAR_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZRECOVERY, ZERO_CHAR_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZMONITOR, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL); zoo.putPersistentData(zkInstanceRoot + Constants.ZMONITOR_LOCK, EMPTY_BYTE_ARRAY, NodeExistsPolicy.FAIL);
public static boolean attempt(IZooReaderWriter zk, String path, String reservationID, String debugInfo) throws KeeperException, InterruptedException { if (reservationID.contains(":")) throw new IllegalArgumentException(); while (true) { try { zk.putPersistentData(path, (reservationID + ":" + debugInfo).getBytes(UTF_8), NodeExistsPolicy.FAIL); return true; } catch (NodeExistsException nee) { Stat stat = new Stat(); byte[] zooData; try { zooData = zk.getData(path, stat); } catch (NoNodeException nne) { continue; } String idInZoo = new String(zooData, UTF_8).split(":")[0]; return idInZoo.equals(reservationID); } } }
static void deleteRetry(IZooReaderWriter zk, String path) throws Exception { for (int i = 0; i < 10; i++) { try { zk.recursiveDelete(path, NodeMissingPolicy.SKIP); return; } catch (KeeperException.NotEmptyException ex) { // ignored } catch (Exception ex) { throw ex; } } }
public static void release(IZooReaderWriter zk, String path, String reservationID) throws KeeperException, InterruptedException { byte[] zooData; try { zooData = zk.getData(path, null); } catch (NoNodeException e) { // Just logging a warning, if data is gone then our work here is done. LoggerFactory.getLogger(ZooReservation.class).debug("Node does not exist {}", path); return; } String zooDataStr = new String(zooData, UTF_8); String idInZoo = zooDataStr.split(":")[0]; if (!idInZoo.equals(reservationID)) { throw new IllegalStateException("Tried to release reservation " + path + " with data mismatch " + reservationID + " " + zooDataStr); } zk.recursiveDelete(path, NodeMissingPolicy.SKIP); }
public static String getRootTabletDir(ServerContext context) throws IOException { IZooReaderWriter zoo = context.getZooReaderWriter(); String zpath = context.getZooKeeperRoot() + RootTable.ZROOT_TABLET_PATH; try { return new String(zoo.getData(zpath, null), UTF_8); } catch (KeeperException e) { throw new IOException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } }
private String findTop(String txpath) throws KeeperException, InterruptedException { List<String> ops = zk.getChildren(txpath); ops = new ArrayList<>(ops); String max = ""; for (String child : ops) if (child.startsWith("repo_") && child.compareTo(max) > 0) max = child; if (max.equals("")) return null; return max; }
private void moveRootTabletToRootTable(IZooReaderWriter zoo) throws Exception { String dirZPath = getZooKeeperRoot() + RootTable.ZROOT_TABLET_PATH; if (!zoo.exists(dirZPath)) { Path oldPath = fs.getFullPath(FileType.TABLE, "/" + MetadataTable.ID + "/root_tablet"); if (fs.exists(oldPath)) { zoo.putPersistentData(dirZPath, location.toString().getBytes(), NodeExistsPolicy.FAIL);
public synchronized LockID getLockID() { if (lock == null) { throw new IllegalStateException("Lock not held"); } return new LockID(path, lock, zooKeeper.getZooKeeper().getSessionId()); }
protected ZooLock(ZooCache zc, IZooReaderWriter zrw, String path) { getLockDataZooCache = zc; this.path = path; zooKeeper = zrw; try { zooKeeper.getStatus(path, this); watchingParent = true; } catch (Exception ex) { log.warn("Error getting setting initial watch on ZooLock", ex); throw new RuntimeException(ex); } }
final String asyncLockPath = zooKeeper.putEphemeralSequential(path + "/" + LOCK_PREFIX, data); log.trace("Ephemeral node {} created", asyncLockPath); Stat stat = zooKeeper.getStatus(asyncLockPath, new Watcher() {