use of org.apache.distributedlog.exceptions.LogExistsException in project bookkeeper by apache.
the class ZKLogStreamMetadataStore method executeCreateMissingPathTxn.
private static void executeCreateMissingPathTxn(ZooKeeper zk, List<Op> zkOps, List<byte[]> pathsToCreate, List<Versioned<byte[]>> metadatas, String logRootPath, CompletableFuture<List<Versioned<byte[]>>> promise) {
zk.multi(zkOps, new AsyncCallback.MultiCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> resultList) {
if (KeeperException.Code.OK.intValue() == rc) {
List<Versioned<byte[]>> finalMetadatas = Lists.newArrayListWithExpectedSize(metadatas.size());
for (int i = 0; i < pathsToCreate.size(); i++) {
byte[] dataCreated = pathsToCreate.get(i);
if (null == dataCreated) {
finalMetadatas.add(metadatas.get(i));
} else {
finalMetadatas.add(new Versioned<byte[]>(dataCreated, new LongVersion(0)));
}
}
promise.complete(finalMetadatas);
} else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
promise.completeExceptionally(new LogExistsException("Someone just created log " + logRootPath));
} else {
if (LOG.isDebugEnabled()) {
StringBuilder builder = new StringBuilder();
for (OpResult result : resultList) {
if (result instanceof OpResult.ErrorResult) {
OpResult.ErrorResult errorResult = (OpResult.ErrorResult) result;
builder.append(errorResult.getErr()).append(",");
} else {
builder.append(0).append(",");
}
}
String resultCodeList = builder.substring(0, builder.length() - 1);
LOG.debug("Failed to create log, full rc list = {}", resultCodeList);
}
promise.completeExceptionally(new ZKException("Failed to create log " + logRootPath, KeeperException.Code.get(rc)));
}
}
}, null);
}
use of org.apache.distributedlog.exceptions.LogExistsException 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;
}
use of org.apache.distributedlog.exceptions.LogExistsException in project bookkeeper by apache.
the class FederatedZKLogMetadataStore method createLogInNamespaceSync.
void createLogInNamespaceSync(URI uri, String logName) throws InterruptedException, IOException, KeeperException {
Transaction txn = zkc.get().transaction();
// we don't have the zk version yet. set it to 0 instead of -1, to prevent non CAS operation.
int zkVersion = null == zkSubnamespacesVersion.get() ? 0 : zkSubnamespacesVersion.get();
txn.setData(zkSubnamespacesPath, uri.getPath().getBytes(UTF_8), zkVersion);
String logPath = uri.getPath() + "/" + logName;
txn.create(logPath, new byte[0], zkc.getDefaultACL(), CreateMode.PERSISTENT);
try {
txn.commit();
// if the transaction succeed, the zk version is advanced
setZkSubnamespacesVersion(zkVersion + 1);
} catch (KeeperException ke) {
List<OpResult> opResults = ke.getResults();
OpResult createResult = opResults.get(1);
if (createResult instanceof OpResult.ErrorResult) {
OpResult.ErrorResult errorResult = (OpResult.ErrorResult) createResult;
if (Code.NODEEXISTS.intValue() == errorResult.getErr()) {
throw new LogExistsException("Log " + logName + " already exists");
}
}
OpResult setResult = opResults.get(0);
if (setResult instanceof OpResult.ErrorResult) {
OpResult.ErrorResult errorResult = (OpResult.ErrorResult) setResult;
if (Code.BADVERSION.intValue() == errorResult.getErr()) {
throw KeeperException.create(Code.BADVERSION);
}
}
throw new ZKException("ZK exception in creating log " + logName + " in " + uri, ke);
}
}
Aggregations