Search in sources :

Example 6 with ZooKeeperClient

use of org.apache.distributedlog.ZooKeeperClient 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);
    });
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) LogExistsException(org.apache.distributedlog.exceptions.LogExistsException) ZKDistributedLock(org.apache.distributedlog.lock.ZKDistributedLock) LogSegmentMetadataStore(org.apache.distributedlog.logsegment.LogSegmentMetadataStore) LoggerFactory(org.slf4j.LoggerFactory) PermitManager(org.apache.distributedlog.common.util.PermitManager) LogMetadataForReader(org.apache.distributedlog.metadata.LogMetadataForReader) Stat(org.apache.zookeeper.data.Stat) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException) LOGSEGMENTS_PATH(org.apache.distributedlog.metadata.LogMetadata.LOGSEGMENTS_PATH) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) Optional(com.google.common.base.Optional) READ_LOCK_PATH(org.apache.distributedlog.metadata.LogMetadata.READ_LOCK_PATH) SchedulerUtils(org.apache.distributedlog.common.util.SchedulerUtils) Transaction(org.apache.distributedlog.util.Transaction) URI(java.net.URI) DistributedLogConstants(org.apache.distributedlog.DistributedLogConstants) ZKUtil(org.apache.zookeeper.ZKUtil) ZKException(org.apache.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) Op(org.apache.zookeeper.Op) OrderedScheduler(org.apache.bookkeeper.common.util.OrderedScheduler) CancellationException(java.util.concurrent.CancellationException) FutureUtils(org.apache.bookkeeper.common.concurrent.FutureUtils) Create(org.apache.zookeeper.Op.Create) List(java.util.List) LockCancelledException(org.apache.distributedlog.exceptions.LockCancelledException) StatsLogger(org.apache.bookkeeper.stats.StatsLogger) ZKLogSegmentMetadataStore(org.apache.distributedlog.impl.ZKLogSegmentMetadataStore) EMPTY_BYTES(org.apache.distributedlog.DistributedLogConstants.EMPTY_BYTES) Code(org.apache.zookeeper.KeeperException.Code) LongVersion(org.apache.bookkeeper.versioning.LongVersion) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LockingException(org.apache.distributedlog.exceptions.LockingException) CompletableFuture(java.util.concurrent.CompletableFuture) ACL(org.apache.zookeeper.data.ACL) UTF_8(com.google.common.base.Charsets.UTF_8) LOCK_PATH(org.apache.distributedlog.metadata.LogMetadata.LOCK_PATH) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) Lists(com.google.common.collect.Lists) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) Versioned(org.apache.bookkeeper.versioning.Versioned) Utils(org.apache.distributedlog.util.Utils) LimitedPermitManager(org.apache.distributedlog.zk.LimitedPermitManager) ZKTransaction(org.apache.distributedlog.zk.ZKTransaction) OpResult(org.apache.zookeeper.OpResult) LinkedList(java.util.LinkedList) LogStreamMetadataStore(org.apache.distributedlog.metadata.LogStreamMetadataStore) Delete(org.apache.zookeeper.Op.Delete) Logger(org.slf4j.Logger) LAYOUT_VERSION(org.apache.distributedlog.metadata.LogMetadata.LAYOUT_VERSION) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener) KeeperException(org.apache.zookeeper.KeeperException) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ALLOCATION_PATH(org.apache.distributedlog.metadata.LogMetadata.ALLOCATION_PATH) IOException(java.io.IOException) MAX_TXID_PATH(org.apache.distributedlog.metadata.LogMetadata.MAX_TXID_PATH) LogMetadata(org.apache.distributedlog.metadata.LogMetadata) TimeUnit(java.util.concurrent.TimeUnit) DLUtils(org.apache.distributedlog.util.DLUtils) DistributedLock(org.apache.distributedlog.lock.DistributedLock) PathUtils(org.apache.zookeeper.common.PathUtils) LogMetadataForWriter(org.apache.distributedlog.metadata.LogMetadataForWriter) AsyncCallback(org.apache.zookeeper.AsyncCallback) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ZKSessionLockFactory(org.apache.distributedlog.lock.ZKSessionLockFactory) UNASSIGNED_LOGSEGMENT_SEQNO(org.apache.distributedlog.DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO) Collections(java.util.Collections) InvalidStreamNameException(org.apache.distributedlog.exceptions.InvalidStreamNameException) VERSION_PATH(org.apache.distributedlog.metadata.LogMetadata.VERSION_PATH) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) SessionLockFactory(org.apache.distributedlog.lock.SessionLockFactory) Op(org.apache.zookeeper.Op) LogSegmentMetadata(org.apache.distributedlog.LogSegmentMetadata) ACL(org.apache.zookeeper.data.ACL) List(java.util.List) LinkedList(java.util.LinkedList)

Example 7 with ZooKeeperClient

use of org.apache.distributedlog.ZooKeeperClient in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method ensureReadLockPathExist.

// 
// Create Read Lock
// 
private CompletableFuture<Void> ensureReadLockPathExist(final LogMetadata logMetadata, final String readLockPath) {
    final CompletableFuture<Void> promise = new CompletableFuture<Void>();
    promise.whenComplete((value, cause) -> {
        if (cause instanceof CancellationException) {
            FutureUtils.completeExceptionally(promise, new LockCancelledException(readLockPath, "Could not ensure read lock path", cause));
        }
    });
    Optional<String> parentPathShouldNotCreate = Optional.of(logMetadata.getLogRootPath());
    Utils.zkAsyncCreateFullPathOptimisticRecursive(zooKeeperClient, readLockPath, parentPathShouldNotCreate, new byte[0], zooKeeperClient.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.StringCallback() {

        @Override
        public void processResult(final int rc, final String path, Object ctx, String name) {
            if (KeeperException.Code.NONODE.intValue() == rc) {
                FutureUtils.completeExceptionally(promise, new LogNotFoundException(String.format("Log %s does not exist or has been deleted", logMetadata.getFullyQualifiedName())));
            } else if (KeeperException.Code.OK.intValue() == rc) {
                FutureUtils.complete(promise, null);
                LOG.trace("Created path {}.", path);
            } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                FutureUtils.complete(promise, null);
                LOG.trace("Path {} is already existed.", path);
            } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
                FutureUtils.completeExceptionally(promise, new ZooKeeperClient.ZooKeeperConnectionException(path));
            } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
                FutureUtils.completeExceptionally(promise, new DLInterruptedException(path));
            } else {
                FutureUtils.completeExceptionally(promise, KeeperException.create(KeeperException.Code.get(rc)));
            }
        }
    }, null);
    return promise;
}
Also used : LockCancelledException(org.apache.distributedlog.exceptions.LockCancelledException) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) CompletableFuture(java.util.concurrent.CompletableFuture) CancellationException(java.util.concurrent.CancellationException) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Example 8 with ZooKeeperClient

use of org.apache.distributedlog.ZooKeeperClient in project bookkeeper by apache.

the class Utils method zkDeleteIfNotExist.

/**
 * Delete the given <i>path</i> from zookeeper.
 *
 * @param zkc
 *          zookeeper client
 * @param path
 *          path to delete
 * @param version
 *          version used to set data
 * @return future representing if the delete is successful. Return true if the node is deleted,
 * false if the node doesn't exist, otherwise future will throw exception
 */
public static CompletableFuture<Boolean> zkDeleteIfNotExist(ZooKeeperClient zkc, String path, LongVersion version) {
    ZooKeeper zk;
    try {
        zk = zkc.get();
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return FutureUtils.exception(zkException(e, path));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return FutureUtils.exception(zkException(e, path));
    }
    final CompletableFuture<Boolean> promise = new CompletableFuture<Boolean>();
    zk.delete(path, (int) version.getLongVersion(), new AsyncCallback.VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (KeeperException.Code.OK.intValue() == rc) {
                promise.complete(true);
            } else if (KeeperException.Code.NONODE.intValue() == rc) {
                promise.complete(false);
            } else {
                promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
            }
        }
    }, null);
    return promise;
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) CompletableFuture(java.util.concurrent.CompletableFuture) ZooKeeper(org.apache.zookeeper.ZooKeeper) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient)

Example 9 with ZooKeeperClient

use of org.apache.distributedlog.ZooKeeperClient in project bookkeeper by apache.

the class SimpleLedgerAllocator method createAllocationData.

private static CompletableFuture<Versioned<byte[]>> createAllocationData(final String allocatePath, final ZooKeeperClient zkc) {
    try {
        final CompletableFuture<Versioned<byte[]>> promise = new CompletableFuture<Versioned<byte[]>>();
        zkc.get().create(allocatePath, DistributedLogConstants.EMPTY_BYTES, zkc.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.Create2Callback() {

            @Override
            public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
                if (KeeperException.Code.OK.intValue() == rc) {
                    promise.complete(new Versioned<byte[]>(DistributedLogConstants.EMPTY_BYTES, new LongVersion(stat.getVersion())));
                } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                    FutureUtils.proxyTo(Utils.zkGetData(zkc, allocatePath, false), promise);
                } else {
                    promise.completeExceptionally(Utils.zkException(KeeperException.create(KeeperException.Code.get(rc)), allocatePath));
                }
            }
        }, null);
        return promise;
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        return FutureUtils.exception(Utils.zkException(e, allocatePath));
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        return FutureUtils.exception(Utils.zkException(e, allocatePath));
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) LongVersion(org.apache.bookkeeper.versioning.LongVersion)

Example 10 with ZooKeeperClient

use of org.apache.distributedlog.ZooKeeperClient in project bookkeeper by apache.

the class TestFederatedZKLogMetadataStore method testCreateLog.

@Test(timeout = 60000)
public void testCreateLog() throws Exception {
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    conf.addConfiguration(baseConf);
    ZooKeeperClient anotherZkc = TestZooKeeperClientBuilder.newBuilder().uri(uri).sessionTimeoutMs(zkSessionTimeoutMs).build();
    FederatedZKLogMetadataStore anotherMetadataStore = new FederatedZKLogMetadataStore(conf, uri, anotherZkc, scheduler);
    for (int i = 0; i < 2 * maxLogsPerSubnamespace; i++) {
        LogMetadataStore createStore, checkStore;
        if (i % 2 == 0) {
            createStore = metadataStore;
            checkStore = anotherMetadataStore;
        } else {
            createStore = anotherMetadataStore;
            checkStore = metadataStore;
        }
        String logName = "test-create-log-" + i;
        URI logUri = Utils.ioResult(createStore.createLog(logName));
        Optional<URI> logLocation = Utils.ioResult(checkStore.getLogLocation(logName));
        assertTrue("Log " + logName + " doesn't exist", logLocation.isPresent());
        assertEquals("Different log location " + logLocation.get() + " is found", logUri, logLocation.get());
    }
    assertEquals(2, metadataStore.getSubnamespaces().size());
    assertEquals(2, anotherMetadataStore.getSubnamespaces().size());
}
Also used : DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) LogMetadataStore(org.apache.distributedlog.metadata.LogMetadataStore) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) URI(java.net.URI) Test(org.junit.Test)

Aggregations

ZooKeeperClient (org.apache.distributedlog.ZooKeeperClient)16 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)6 Test (org.junit.Test)6 KeeperException (org.apache.zookeeper.KeeperException)5 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 IOException (java.io.IOException)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 ZKException (org.apache.distributedlog.exceptions.ZKException)4 URI (java.net.URI)3 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)3 AsyncCallback (org.apache.zookeeper.AsyncCallback)3 Stat (org.apache.zookeeper.data.Stat)3 CancellationException (java.util.concurrent.CancellationException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutorService (java.util.concurrent.ExecutorService)2 LongVersion (org.apache.bookkeeper.versioning.LongVersion)2 Versioned (org.apache.bookkeeper.versioning.Versioned)2 BoundExponentialBackoffRetryPolicy (org.apache.bookkeeper.zookeeper.BoundExponentialBackoffRetryPolicy)2 RetryPolicy (org.apache.bookkeeper.zookeeper.RetryPolicy)2 BookKeeperClient (org.apache.distributedlog.BookKeeperClient)2