Search in sources :

Example 1 with LogMetadataForWriter

use of org.apache.distributedlog.metadata.LogMetadataForWriter 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 2 with LogMetadataForWriter

use of org.apache.distributedlog.metadata.LogMetadataForWriter in project bookkeeper by apache.

the class TestZKLogSegmentMetadataStore method testStoreMaxTxnIdBadVersion.

@Test(timeout = 60000)
public void testStoreMaxTxnIdBadVersion() throws Exception {
    Transaction<Object> updateTxn = lsmStore.transaction();
    Versioned<Long> value = new Versioned<Long>(999L, new LongVersion(10));
    final CompletableFuture<Version> result = new CompletableFuture<Version>();
    LogMetadataForWriter metadata = mock(LogMetadataForWriter.class);
    when(metadata.getMaxTxIdPath()).thenReturn(rootZkPath);
    lsmStore.storeMaxTxnId(updateTxn, metadata, value, new Transaction.OpListener<Version>() {

        @Override
        public void onCommit(Version r) {
            result.complete(r);
        }

        @Override
        public void onAbort(Throwable t) {
            result.completeExceptionally(t);
        }
    });
    try {
        Utils.ioResult(updateTxn.execute());
        fail("Should fail on storing log record transaction id if providing bad version");
    } catch (ZKException zke) {
        assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode());
    }
    try {
        Utils.ioResult(result);
        fail("Should fail on storing log record transaction id if providing bad version");
    } catch (ZKException ze) {
        assertEquals(KeeperException.Code.BADVERSION, ze.getKeeperExceptionCode());
    }
    Stat stat = new Stat();
    byte[] data = zkc.get().getData(rootZkPath, false, stat);
    assertEquals(0, stat.getVersion());
    assertEquals(0, data.length);
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) CompletableFuture(java.util.concurrent.CompletableFuture) ZKException(org.apache.distributedlog.exceptions.ZKException) Stat(org.apache.zookeeper.data.Stat) Transaction(org.apache.distributedlog.util.Transaction) LongVersion(org.apache.bookkeeper.versioning.LongVersion) Version(org.apache.bookkeeper.versioning.Version) LongVersion(org.apache.bookkeeper.versioning.LongVersion) LogMetadataForWriter(org.apache.distributedlog.metadata.LogMetadataForWriter) Test(org.junit.Test)

Example 3 with LogMetadataForWriter

use of org.apache.distributedlog.metadata.LogMetadataForWriter in project bookkeeper by apache.

the class TestZKLogStreamMetadataStoreUtils method testProcessLogMetadatasNoAllocatorPath.

@SuppressWarnings("unchecked")
@Test(timeout = 60000)
public void testProcessLogMetadatasNoAllocatorPath() throws Exception {
    String rootPath = "/test-missing-version";
    URI uri = DLMTestUtil.createDLMURI(2181, rootPath);
    String logName = "test-log";
    String logIdentifier = "<default>";
    Versioned<byte[]> maxTxnIdData = new Versioned<byte[]>(DLUtils.serializeTransactionId(1L), new LongVersion(1));
    Versioned<byte[]> logSegmentsData = new Versioned<byte[]>(DLUtils.serializeLogSegmentSequenceNumber(1L), new LongVersion(1));
    List<Versioned<byte[]>> metadatas = Lists.newArrayList(new Versioned<byte[]>(null, null), new Versioned<byte[]>(null, null), maxTxnIdData, new Versioned<byte[]>(intToBytes(LogMetadata.LAYOUT_VERSION), null), new Versioned<byte[]>(new byte[0], new LongVersion(1)), new Versioned<byte[]>(new byte[0], new LongVersion(1)), logSegmentsData);
    LogMetadataForWriter metadata = processLogMetadatas(uri, logName, logIdentifier, metadatas, false);
    assertTrue(maxTxnIdData == metadata.getMaxTxIdData());
    assertTrue(logSegmentsData == metadata.getMaxLSSNData());
    assertNull(metadata.getAllocationData().getValue());
    assertNull(metadata.getAllocationData().getVersion());
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) URI(java.net.URI) LogMetadataForWriter(org.apache.distributedlog.metadata.LogMetadataForWriter) Test(org.junit.Test)

Example 4 with LogMetadataForWriter

use of org.apache.distributedlog.metadata.LogMetadataForWriter in project bookkeeper by apache.

the class TestZKLogStreamMetadataStoreUtils method testProcessLogMetadatasAllocatorPath.

@SuppressWarnings("unchecked")
@Test(timeout = 60000)
public void testProcessLogMetadatasAllocatorPath() throws Exception {
    String rootPath = "/test-missing-version";
    URI uri = DLMTestUtil.createDLMURI(2181, rootPath);
    String logName = "test-log";
    String logIdentifier = "<default>";
    Versioned<byte[]> maxTxnIdData = new Versioned<byte[]>(DLUtils.serializeTransactionId(1L), new LongVersion(1));
    Versioned<byte[]> logSegmentsData = new Versioned<byte[]>(DLUtils.serializeLogSegmentSequenceNumber(1L), new LongVersion(1));
    Versioned<byte[]> allocationData = new Versioned<byte[]>(DLUtils.logSegmentId2Bytes(1L), new LongVersion(1));
    List<Versioned<byte[]>> metadatas = Lists.newArrayList(new Versioned<byte[]>(null, null), new Versioned<byte[]>(null, null), maxTxnIdData, new Versioned<byte[]>(intToBytes(LogMetadata.LAYOUT_VERSION), null), new Versioned<byte[]>(new byte[0], new LongVersion(1)), new Versioned<byte[]>(new byte[0], new LongVersion(1)), logSegmentsData, allocationData);
    LogMetadataForWriter metadata = processLogMetadatas(uri, logName, logIdentifier, metadatas, true);
    assertTrue(maxTxnIdData == metadata.getMaxTxIdData());
    assertTrue(logSegmentsData == metadata.getMaxLSSNData());
    assertTrue(allocationData == metadata.getAllocationData());
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) URI(java.net.URI) LogMetadataForWriter(org.apache.distributedlog.metadata.LogMetadataForWriter) Test(org.junit.Test)

Example 5 with LogMetadataForWriter

use of org.apache.distributedlog.metadata.LogMetadataForWriter in project bookkeeper by apache.

the class TestZKLogStreamMetadataStore method testCreateLogMetadataWithMissingPaths.

private void testCreateLogMetadataWithMissingPaths(URI uri, String logName, String logIdentifier, List<String> pathsToDelete, boolean ownAllocator, boolean createLogFirst) throws Exception {
    if (createLogFirst) {
        createLog(zkc, uri, logName, logIdentifier);
    }
    // delete a path
    for (String path : pathsToDelete) {
        zkc.get().delete(path, -1);
    }
    LogMetadataForWriter logMetadata = Utils.ioResult(getLog(uri, logName, logIdentifier, zkc, ownAllocator, true));
    final String logRootPath = getLogRootPath(uri, logName, logIdentifier);
    List<Versioned<byte[]>> metadatas = Utils.ioResult(checkLogMetadataPaths(zkc.get(), logRootPath, ownAllocator));
    if (ownAllocator) {
        assertEquals("Should have 8 paths : ownAllocator = " + ownAllocator, 8, metadatas.size());
    } else {
        assertEquals("Should have 7 paths : ownAllocator = " + ownAllocator, 7, metadatas.size());
    }
    for (Versioned<byte[]> metadata : metadatas) {
        assertTrue(pathExists(metadata));
        assertTrue(((LongVersion) metadata.getVersion()).getLongVersion() >= 0L);
    }
    Versioned<byte[]> logSegmentsData = logMetadata.getMaxLSSNData();
    assertEquals(DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO, DLUtils.deserializeLogSegmentSequenceNumber(logSegmentsData.getValue()));
    Versioned<byte[]> maxTxIdData = logMetadata.getMaxTxIdData();
    assertEquals(0L, DLUtils.deserializeTransactionId(maxTxIdData.getValue()));
    if (ownAllocator) {
        Versioned<byte[]> allocationData = logMetadata.getAllocationData();
        assertEquals(0, allocationData.getValue().length);
    }
}
Also used : Versioned(org.apache.bookkeeper.versioning.Versioned) LongVersion(org.apache.bookkeeper.versioning.LongVersion) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) LogMetadataForWriter(org.apache.distributedlog.metadata.LogMetadataForWriter)

Aggregations

LogMetadataForWriter (org.apache.distributedlog.metadata.LogMetadataForWriter)8 LongVersion (org.apache.bookkeeper.versioning.LongVersion)7 Versioned (org.apache.bookkeeper.versioning.Versioned)7 Test (org.junit.Test)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 Transaction (org.apache.distributedlog.util.Transaction)4 URI (java.net.URI)3 Version (org.apache.bookkeeper.versioning.Version)3 ZKException (org.apache.distributedlog.exceptions.ZKException)3 Stat (org.apache.zookeeper.data.Stat)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 UTF_8 (com.google.common.base.Charsets.UTF_8)1 Optional (com.google.common.base.Optional)1 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 Collections (java.util.Collections)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1