public static <T extends AbstractId> T getNextId(String name, ServerContext context, Function<String,T> newIdFunction) throws AcceptableThriftTableOperationException { try { IZooReaderWriter zoo = context.getZooReaderWriter(); final String ntp = context.getZooKeeperRoot() + Constants.ZTABLES; byte[] nid = zoo.mutate(ntp, ZERO_BYTE, ZooUtil.PUBLIC, currentValue -> { BigInteger nextId = new BigInteger(new String(currentValue, UTF_8), Character.MAX_RADIX); nextId = nextId.add(BigInteger.ONE); return nextId.toString(Character.MAX_RADIX).getBytes(UTF_8); }); return newIdFunction.apply(new String(nid, UTF_8)); } catch (Exception e1) { log.error("Failed to assign id to " + name, e1); throw new AcceptableThriftTableOperationException(null, name, TableOperation.CREATE, TableOperationExceptionType.OTHER, e1.getMessage()); } }
static void removeIterators(Master environment, final long txid, Table.ID tableId) throws Exception { String zTablePath = Constants.ZROOT + "/" + environment.getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID; IZooReaderWriter zoo = environment.getContext().getZooReaderWriter(); zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) { String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); long flushID = Long.parseLong(tokens[0]); String txidString = String.format("%016x", txid); StringBuilder encodedIterators = new StringBuilder(); for (int i = 1; i < tokens.length; i++) { if (tokens[i].startsWith(txidString)) continue; encodedIterators.append(","); encodedIterators.append(tokens[i]); } return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8); } }); }
@Override public Repo<Master> call(long tid, Master environment) throws Exception { String zCompactID = Constants.ZROOT + "/" + environment.getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID; String zCancelID = Constants.ZROOT + "/" + environment.getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_CANCEL_ID; IZooReaderWriter zoo = environment.getContext().getZooReaderWriter(); byte[] currentValue = zoo.getData(zCompactID, null); String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); final long flushID = Long.parseLong(tokens[0]); zoo.mutate(zCancelID, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) { long cid = Long.parseLong(new String(currentValue, UTF_8)); if (cid < flushID) return Long.toString(flushID).getBytes(UTF_8); else return Long.toString(cid).getBytes(UTF_8); } }); return new FinishCancelCompaction(namespaceId, tableId); }
byte[] cid; try { cid = zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception {
@Override public long initiateFlush(TInfo tinfo, TCredentials c, String tableIdStr) throws ThriftSecurityException, ThriftTableOperationException { Table.ID tableId = Table.ID.of(tableIdStr); Namespace.ID namespaceId = getNamespaceIdFromTableId(TableOperation.FLUSH, tableId); master.security.canFlush(c, tableId, namespaceId); String zTablePath = Constants.ZROOT + "/" + master.getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_FLUSH_ID; IZooReaderWriter zoo = master.getContext().getZooReaderWriter(); byte fid[]; try { fid = zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) { long flushID = Long.parseLong(new String(currentValue)); flushID++; return ("" + flushID).getBytes(); } }); } catch (NoNodeException nne) { throw new ThriftTableOperationException(tableId.canonicalID(), null, TableOperation.FLUSH, TableOperationExceptionType.NOTFOUND, null); } catch (Exception e) { Master.log.warn("{}", e.getMessage(), e); throw new ThriftTableOperationException(tableId.canonicalID(), null, TableOperation.FLUSH, TableOperationExceptionType.OTHER, null); } return Long.parseLong(new String(fid)); }
+ Constants.ZNAMESPACE_NAME; zoo.mutate(tap, null, null, new Mutator() { @Override public byte[] mutate(byte[] current) throws Exception {
+ Constants.ZTABLE_NAME; zoo.mutate(tap, null, null, current -> { final String currentName = new String(current, UTF_8); if (currentName.equals(newName))
public synchronized String getNextName() { while (next >= maxAllocated) { final int allocate = 100 + rand.nextInt(100); try { byte[] max = ZooReaderWriter.getRetryingInstance().mutate(nextNamePath, null, ZooUtil.PRIVATE, new ZooReaderWriter.Mutator() { public byte[] mutate(byte[] currentValue) throws Exception { long l = Long.parseLong(new String(currentValue, UTF_8), Character.MAX_RADIX); l += allocate; return Long.toString(l, Character.MAX_RADIX).getBytes(UTF_8); } }); maxAllocated = Long.parseLong(new String(max, UTF_8), Character.MAX_RADIX); next = maxAllocated - allocate; } catch (Exception e) { throw new RuntimeException(e); } } return new String(FastFormat.toZeroPaddedString(next++, 7, Character.MAX_RADIX, new byte[0]), UTF_8); }
static String getNextTableId(String tableName, Instance instance) throws ThriftTableOperationException { String tableId = null; try { IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance(); final String ntp = ZooUtil.getRoot(instance) + Constants.ZTABLES; byte[] nid = zoo.mutate(ntp, ZERO_BYTE, ZooUtil.PUBLIC, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { BigInteger nextId = new BigInteger(new String(currentValue, UTF_8), Character.MAX_RADIX); nextId = nextId.add(BigInteger.ONE); return nextId.toString(Character.MAX_RADIX).getBytes(UTF_8); } }); return new String(nid, UTF_8); } catch (Exception e1) { Logger.getLogger(CreateTable.class).error("Failed to assign tableId to " + tableName, e1); throw new ThriftTableOperationException(tableId, tableName, TableOperation.CREATE, TableOperationExceptionType.OTHER, e1.getMessage()); } }
static String getNextTableId(String tableName, Instance instance) throws AcceptableThriftTableOperationException { String tableId = null; try { IZooReaderWriter zoo = ZooReaderWriter.getInstance(); final String ntp = ZooUtil.getRoot(instance) + Constants.ZTABLES; byte[] nid = zoo.mutate(ntp, ZERO_BYTE, ZooUtil.PUBLIC, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { BigInteger nextId = new BigInteger(new String(currentValue, UTF_8), Character.MAX_RADIX); nextId = nextId.add(BigInteger.ONE); return nextId.toString(Character.MAX_RADIX).getBytes(UTF_8); } }); return new String(nid, UTF_8); } catch (Exception e1) { log.error("Failed to assign tableId to " + tableName, e1); throw new AcceptableThriftTableOperationException(tableId, tableName, TableOperation.CREATE, TableOperationExceptionType.OTHER, e1.getMessage()); } }
static void removeIterators(Master environment, final long txid, String tableId) throws Exception { String zTablePath = Constants.ZROOT + "/" + environment.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID; IZooReaderWriter zoo = ZooReaderWriter.getInstance(); zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); long flushID = Long.parseLong(tokens[0]); String txidString = String.format("%016x", txid); StringBuilder encodedIterators = new StringBuilder(); for (int i = 1; i < tokens.length; i++) { if (tokens[i].startsWith(txidString)) continue; encodedIterators.append(","); encodedIterators.append(tokens[i]); } return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8); } }); }
static void removeIterators(final long txid, String tableId) throws Exception { String zTablePath = Constants.ZROOT + "/" + HdfsZooInstance.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID; IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance(); zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); long flushID = Long.parseLong(tokens[0]); String txidString = String.format("%016x", txid); StringBuilder encodedIterators = new StringBuilder(); for (int i = 1; i < tokens.length; i++) { if (tokens[i].startsWith(txidString)) continue; encodedIterators.append(","); encodedIterators.append(tokens[i]); } return (Long.toString(flushID) + encodedIterators).getBytes(UTF_8); } }); }
ZooReaderWriter.getRetryingInstance().mutate(statePath, (byte[]) newState.name().getBytes(UTF_8), ZooUtil.PUBLIC, new Mutator() { @Override public byte[] mutate(byte[] oldData) throws Exception {
byte[] cid; try { cid = zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception {
@Override public long initiateFlush(TInfo tinfo, TCredentials c, String tableId) throws ThriftSecurityException, ThriftTableOperationException { String namespaceId = getNamespaceIdFromTableId(TableOperation.FLUSH, tableId); master.security.canFlush(c, tableId, namespaceId); String zTablePath = Constants.ZROOT + "/" + master.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_FLUSH_ID; IZooReaderWriter zoo = ZooReaderWriter.getInstance(); byte fid[]; try { fid = zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { long flushID = Long.parseLong(new String(currentValue)); flushID++; return ("" + flushID).getBytes(); } }); } catch (NoNodeException nne) { throw new ThriftTableOperationException(tableId, null, TableOperation.FLUSH, TableOperationExceptionType.NOTFOUND, null); } catch (Exception e) { Master.log.warn("{}", e.getMessage(), e); throw new ThriftTableOperationException(tableId, null, TableOperation.FLUSH, TableOperationExceptionType.OTHER, null); } return Long.parseLong(new String(fid)); }
@Override public long initiateFlush(TInfo tinfo, TCredentials c, String tableId) throws ThriftSecurityException, ThriftTableOperationException, TException { security.canFlush(c, tableId); String zTablePath = Constants.ZROOT + "/" + getConfiguration().getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_FLUSH_ID; IZooReaderWriter zoo = ZooReaderWriter.getInstance(); byte fid[]; try { fid = zoo.mutate(zTablePath, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { long flushID = Long.parseLong(new String(currentValue, UTF_8)); flushID++; return Long.toString(flushID).getBytes(UTF_8); } }); } catch (NoNodeException nne) { throw new ThriftTableOperationException(tableId, null, TableOperation.FLUSH, TableOperationExceptionType.NOTFOUND, null); } catch (Exception e) { log.warn(e.getMessage(), e); throw new ThriftTableOperationException(tableId, null, TableOperation.FLUSH, TableOperationExceptionType.OTHER, null); } return Long.parseLong(new String(fid, UTF_8)); }
@Override public Repo<Master> call(long tid, Master master) throws Exception { Instance instance = master.getInstance(); IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance(); Utils.tableNameLock.lock(); try { Utils.checkTableDoesNotExist(instance, newTableName, tableId, TableOperation.RENAME); final String tap = ZooUtil.getRoot(instance) + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_NAME; zoo.mutate(tap, null, null, new Mutator() { public byte[] mutate(byte[] current) throws Exception { final String currentName = new String(current, UTF_8); if (currentName.equals(newTableName)) return null; // assume in this case the operation is running again, so we are done if (!currentName.equals(oldTableName)) { throw new ThriftTableOperationException(null, oldTableName, TableOperation.RENAME, TableOperationExceptionType.NOTFOUND, "Name changed while processing"); } return newTableName.getBytes(UTF_8); } }); Tables.clearCache(instance); } finally { Utils.tableNameLock.unlock(); Utils.unreserveTable(tableId, tid, true); } Logger.getLogger(RenameTable.class).debug("Renamed table " + tableId + " " + oldTableName + " " + newTableName); return null; }
@Override public Repo<Master> call(long tid, Master environment) throws Exception { String zCompactID = Constants.ZROOT + "/" + HdfsZooInstance.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID; String zCancelID = Constants.ZROOT + "/" + HdfsZooInstance.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_CANCEL_ID; IZooReaderWriter zoo = ZooReaderWriter.getRetryingInstance(); byte[] currentValue = zoo.getData(zCompactID, null); String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); final long flushID = Long.parseLong(tokens[0]); zoo.mutate(zCancelID, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { long cid = Long.parseLong(new String(currentValue, UTF_8)); if (cid < flushID) return Long.toString(flushID).getBytes(UTF_8); else return Long.toString(cid).getBytes(UTF_8); } }); return new FinishCancelCompaction(tableId); }
+ Constants.ZNAMESPACE_NAME; zoo.mutate(tap, null, null, new Mutator() { @Override public byte[] mutate(byte[] current) throws Exception {
@Override public Repo<Master> call(long tid, Master environment) throws Exception { String zCompactID = Constants.ZROOT + "/" + environment.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_ID; String zCancelID = Constants.ZROOT + "/" + environment.getInstance().getInstanceID() + Constants.ZTABLES + "/" + tableId + Constants.ZTABLE_COMPACT_CANCEL_ID; IZooReaderWriter zoo = ZooReaderWriter.getInstance(); byte[] currentValue = zoo.getData(zCompactID, null); String cvs = new String(currentValue, UTF_8); String[] tokens = cvs.split(","); final long flushID = Long.parseLong(tokens[0]); zoo.mutate(zCancelID, null, null, new Mutator() { @Override public byte[] mutate(byte[] currentValue) throws Exception { long cid = Long.parseLong(new String(currentValue, UTF_8)); if (cid < flushID) return Long.toString(flushID).getBytes(UTF_8); else return Long.toString(cid).getBytes(UTF_8); } }); return new FinishCancelCompaction(getNamespaceId(environment), tableId); }