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); }
public ZooStore(String path, IZooReaderWriter zk) throws KeeperException, InterruptedException { this.path = path; this.zk = zk; this.reserved = new HashSet<>(); this.defered = new HashMap<>(); this.idgenerator = new SecureRandom(); zk.putPersistentData(path, new byte[0], NodeExistsPolicy.SKIP); }
public static void prepareNewTableState(IZooReaderWriter zoo, String instanceId, Table.ID tableId, Namespace.ID namespaceId, String tableName, TableState state, NodeExistsPolicy existsPolicy) throws KeeperException, InterruptedException { // state gets created last log.debug("Creating ZooKeeper entries for new table {} (ID: {}) in namespace (ID: {})", tableName, tableId, namespaceId); Pair<String,String> qualifiedTableName = Tables.qualify(tableName); tableName = qualifiedTableName.getSecond(); String zTablePath = Constants.ZROOT + "/" + instanceId + Constants.ZTABLES + "/" + tableId; zoo.putPersistentData(zTablePath, new byte[0], existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_CONF, new byte[0], existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_NAMESPACE, namespaceId.getUtf8(), existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_NAME, tableName.getBytes(UTF_8), existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_FLUSH_ID, ZERO_BYTE, existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_COMPACT_ID, ZERO_BYTE, existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_COMPACT_CANCEL_ID, ZERO_BYTE, existsPolicy); zoo.putPersistentData(zTablePath + Constants.ZTABLE_STATE, state.name().getBytes(UTF_8), existsPolicy); }
public static void start(ServerContext context, String type, long tid) throws KeeperException, InterruptedException { IZooReaderWriter writer = context.getZooReaderWriter(); writer.putPersistentData(context.getZooKeeperRoot() + "/" + type, new byte[] {}, NodeExistsPolicy.OVERWRITE); writer.putPersistentData(context.getZooKeeperRoot() + "/" + type + "/" + tid, new byte[] {}, NodeExistsPolicy.OVERWRITE); writer.putPersistentData(context.getZooKeeperRoot() + "/" + type + "/" + tid + "-running", new byte[] {}, NodeExistsPolicy.OVERWRITE); }
public static void setRootTabletDir(ServerContext context, String dir) throws IOException { IZooReaderWriter zoo = context.getZooReaderWriter(); String zpath = context.getZooKeeperRoot() + RootTable.ZROOT_TABLET_PATH; try { zoo.putPersistentData(zpath, dir.getBytes(UTF_8), -1, NodeExistsPolicy.OVERWRITE); } catch (KeeperException e) { throw new IOException(e); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException(e); } }
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); } } }
public static boolean setNamespaceProperty(ServerContext context, Namespace.ID namespaceId, String property, String value) throws KeeperException, InterruptedException { if (!isPropertyValid(property, value)) return false; IZooReaderWriter zoo = context.getZooReaderWriter(); // create the zk node for per-namespace properties for this namespace if it doesn't already // exist String zkNamespacePath = getPath(context, namespaceId); zoo.putPersistentData(zkNamespacePath, new byte[0], NodeExistsPolicy.SKIP); // create the zk node for this property and set it's data to the specified value String zPath = zkNamespacePath + "/" + property; zoo.putPersistentData(zPath, value.getBytes(UTF_8), NodeExistsPolicy.OVERWRITE); return true; }
private void create(String path, String value, String encoding) { byte[] data = value.getBytes(UTF_8); if ("base64".equals(encoding)) data = Base64.getDecoder().decode(data); try { try { zk.putPersistentData(path, data, overwrite ? NodeExistsPolicy.OVERWRITE : NodeExistsPolicy.FAIL); } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.NODEEXISTS)) throw new RuntimeException(path + " exists. Remove it first."); throw e; } } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public long create() { while (true) { try { // looking at the code for SecureRandom, it appears to be thread safe long tid = idgenerator.nextLong() & 0x7fffffffffffffffL; zk.putPersistentData(getTXPath(tid), TStatus.NEW.name().getBytes(UTF_8), NodeExistsPolicy.FAIL); return tid; } catch (NodeExistsException nee) { // exist, so just try another random # } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public void initUser(String user) throws AccumuloSecurityException { IZooReaderWriter zoo = context.getZooReaderWriter(); try { zoo.putPersistentData(ZKUserPath + "/" + user, new byte[0], NodeExistsPolicy.SKIP); } catch (KeeperException e) { log.error("{}", e.getMessage(), e); throw new AccumuloSecurityException(user, SecurityErrorCode.CONNECTION_ERROR, e); } catch (InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } }
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); }
@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); } }
@Override public void setStatus(long tid, TStatus status) { verifyReserved(tid); try { zk.putPersistentData(getTXPath(tid), status.name().getBytes(UTF_8), NodeExistsPolicy.OVERWRITE); } catch (Exception e) { throw new RuntimeException(e); } synchronized (this) { statusChangeEvents++; } }
@Override public void setProperty(long tid, String prop, Serializable so) { verifyReserved(tid); try { if (so instanceof String) { zk.putPersistentData(getTXPath(tid) + "/prop_" + prop, ("S " + so).getBytes(UTF_8), NodeExistsPolicy.OVERWRITE); } else { byte[] sera = serialize(so); byte[] data = new byte[sera.length + 2]; System.arraycopy(sera, 0, data, 2, sera.length); data[0] = 'O'; data[1] = ' '; zk.putPersistentData(getTXPath(tid) + "/prop_" + prop, data, NodeExistsPolicy.OVERWRITE); } } catch (Exception e2) { throw new RuntimeException(e2); } }
@Override public void visit(ZooReader zoo, String path) throws Exception { String newPath = path.replace(context.getInstanceID(), newInstanceId); byte[] data = zoo.getData(path, null); List<ACL> acls = orig.getZooKeeper().getACL(path, new Stat()); if (acls.containsAll(Ids.READ_ACL_UNSAFE)) { new_.putPersistentData(newPath, data, NodeExistsPolicy.FAIL); } else { // upgrade if (acls.containsAll(Ids.OPEN_ACL_UNSAFE)) { // make user nodes private, they contain the user's password String parts[] = path.split("/"); if (parts[parts.length - 2].equals("users")) { new_.putPrivatePersistentData(newPath, data, NodeExistsPolicy.FAIL); } else { // everything else can have the readable acl new_.putPersistentData(newPath, data, NodeExistsPolicy.FAIL); } } else { new_.putPrivatePersistentData(newPath, data, NodeExistsPolicy.FAIL); } } } });
@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); } }
@Override public void run(IZooReaderWriter rw) throws KeeperException, InterruptedException, IOException { String root = getZookeeperLogLocation(context); rw.putPersistentData(root + "/" + entry.getUniqueID(), entry.toBytes(), NodeExistsPolicy.OVERWRITE); } });
@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 zoo.putPersistentData(ZKUserPath, principal.getBytes(UTF_8), NodeExistsPolicy.FAIL); constructUser(principal, ZKSecurityTool.createPass(token)); } } catch (KeeperException | AccumuloException | InterruptedException e) { log.error("{}", e.getMessage(), e); throw new RuntimeException(e); } }
zoo.putPersistentData(dirZPath, location.toString().getBytes(), NodeExistsPolicy.FAIL);
String path = "/accumulo/instances/" + context.getInstanceName(); orig.recursiveDelete(path, NodeMissingPolicy.SKIP); new_.putPersistentData(path, newInstanceId.getBytes(UTF_8), NodeExistsPolicy.OVERWRITE);