use of org.apache.zookeeper.Op.Delete in project bookkeeper by apache.
the class ZKLogStreamMetadataStore method renameLogMetadata.
private CompletableFuture<Void> renameLogMetadata(URI uri, LogMetadataForWriter oldMetadata, String newStreamName) {
final LinkedList<Op> createOps = Lists.newLinkedList();
final LinkedList<Op> deleteOps = Lists.newLinkedList();
List<ACL> acls = zooKeeperClient.getDefaultACL();
// get the root path
String oldRootPath = oldMetadata.getLogRootPath();
String newRootPath = LogMetadata.getLogRootPath(uri, newStreamName, conf.getUnpartitionedStreamName());
// 0. the log path
deleteOps.addFirst(Op.delete(LogMetadata.getLogStreamPath(uri, oldMetadata.getLogName()), -1));
// 1. the root path
createOps.addLast(Op.create(newRootPath, EMPTY_BYTES, acls, CreateMode.PERSISTENT));
deleteOps.addFirst(Op.delete(oldRootPath, -1));
// 2. max id
Versioned<byte[]> maxTxIdData = oldMetadata.getMaxTxIdData();
deleteOldPathAndCreateNewPath(oldRootPath, MAX_TXID_PATH, maxTxIdData, newRootPath, DLUtils.serializeTransactionId(0L), acls, createOps, deleteOps);
// 3. version
createOps.addLast(Op.create(newRootPath + VERSION_PATH, intToBytes(LAYOUT_VERSION), acls, CreateMode.PERSISTENT));
deleteOps.addFirst(Op.delete(oldRootPath + VERSION_PATH, -1));
// 4. lock path (NOTE: if the stream is locked by a writer, then the delete will fail as you can not
// delete the lock path if children is not empty.
createOps.addLast(Op.create(newRootPath + LOCK_PATH, EMPTY_BYTES, acls, CreateMode.PERSISTENT));
deleteOps.addFirst(Op.delete(oldRootPath + LOCK_PATH, -1));
// 5. read lock path (NOTE: same reason as the write lock)
createOps.addLast(Op.create(newRootPath + READ_LOCK_PATH, EMPTY_BYTES, acls, CreateMode.PERSISTENT));
deleteOps.addFirst(Op.delete(oldRootPath + READ_LOCK_PATH, -1));
// 6. allocation path
Versioned<byte[]> allocationData = oldMetadata.getAllocationData();
deleteOldPathAndCreateNewPath(oldRootPath, ALLOCATION_PATH, allocationData, newRootPath, EMPTY_BYTES, acls, createOps, deleteOps);
// 7. log segments
Versioned<byte[]> maxLSSNData = oldMetadata.getMaxLSSNData();
deleteOldPathAndCreateNewPath(oldRootPath, LOGSEGMENTS_PATH, maxLSSNData, newRootPath, DLUtils.serializeLogSegmentSequenceNumber(UNASSIGNED_LOGSEGMENT_SEQNO), acls, createOps, deleteOps);
// 8. copy the log segments
CompletableFuture<List<LogSegmentMetadata>> segmentsFuture;
if (pathExists(maxLSSNData)) {
segmentsFuture = getLogSegments(zooKeeperClient, oldRootPath + LOGSEGMENTS_PATH);
} else {
segmentsFuture = FutureUtils.value(Collections.emptyList());
}
return segmentsFuture.thenApply(segments -> {
for (LogSegmentMetadata segment : segments) {
deleteOldSegmentAndCreateNewSegment(segment, newRootPath + LOGSEGMENTS_PATH, acls, createOps, deleteOps);
}
return null;
}).thenCompose(ignored -> getMissingPaths(zooKeeperClient, uri, newStreamName)).thenCompose(paths -> {
for (String path : paths) {
createOps.addFirst(Op.create(path, EMPTY_BYTES, acls, CreateMode.PERSISTENT));
}
return executeRenameTxn(oldRootPath, newRootPath, createOps, deleteOps);
});
}
use of org.apache.zookeeper.Op.Delete in project bookkeeper by apache.
the class ZKLogStreamMetadataStore method executeRenameTxn.
private CompletableFuture<Void> executeRenameTxn(String oldLogPath, String newLogPath, LinkedList<Op> createOps, LinkedList<Op> deleteOps) {
CompletableFuture<Void> future = FutureUtils.createFuture();
List<Op> zkOps = Lists.newArrayListWithExpectedSize(createOps.size() + deleteOps.size());
zkOps.addAll(createOps);
zkOps.addAll(deleteOps);
if (LOG.isDebugEnabled()) {
for (Op op : zkOps) {
if (op instanceof Create) {
Create create = (Create) op;
LOG.debug("op : create {}", create.getPath());
} else if (op instanceof Delete) {
Delete delete = (Delete) op;
LOG.debug("op : delete {}, record = {}", delete.getPath(), op.toRequestRecord());
} else {
LOG.debug("op : {}", op);
}
}
}
try {
zooKeeperClient.get().multi(zkOps, (rc, path, ctx, opResults) -> {
if (Code.OK.intValue() == rc) {
future.complete(null);
} else if (Code.NODEEXISTS.intValue() == rc) {
future.completeExceptionally(new LogExistsException("Someone just created new log " + newLogPath));
} else if (Code.NOTEMPTY.intValue() == rc) {
future.completeExceptionally(new LockingException(oldLogPath + LOCK_PATH, "Someone is holding a lock on log " + oldLogPath));
} else {
future.completeExceptionally(new ZKException("Failed to rename log " + oldLogPath + " to " + newLogPath + " at path " + path, Code.get(rc)));
}
}, null);
} catch (ZooKeeperConnectionException e) {
future.completeExceptionally(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
future.completeExceptionally(e);
}
return future;
}
Aggregations