Search in sources :

Example 1 with LogNotFoundException

use of com.twitter.distributedlog.exceptions.LogNotFoundException in project distributedlog by twitter.

the class TestDistributedLogServer method testDeleteStream.

@Test(timeout = 60000)
public void testDeleteStream() throws Exception {
    String name = "dlserver-delete-stream";
    dlClient.routingService.addHost(name, dlServer.getAddress());
    long txid = 101;
    for (long i = 1; i <= 10; i++) {
        long curTxId = txid++;
        logger.debug("Write entry {} to stream {}.", curTxId, name);
        dlClient.dlClient.write(name, ByteBuffer.wrap(("" + curTxId).getBytes())).get();
    }
    checkStream(1, 1, 1, name, dlServer.getAddress(), true, true);
    dlClient.dlClient.delete(name).get();
    checkStream(0, 0, 0, name, dlServer.getAddress(), false, false);
    Thread.sleep(1000);
    DistributedLogManager dlm101 = DLMTestUtil.createNewDLM(name, conf, getUri());
    AsyncLogReader reader101 = FutureUtils.result(dlm101.openAsyncLogReader(DLSN.InitialDLSN));
    try {
        FutureUtils.result(reader101.readNext());
        fail("Should fail with LogNotFoundException since the stream is deleted");
    } catch (LogNotFoundException lnfe) {
    // expected
    }
    FutureUtils.result(reader101.asyncClose());
    dlm101.close();
    txid = 201;
    for (long i = 1; i <= 10; i++) {
        long curTxId = txid++;
        logger.debug("Write entry {} to stream {}.", curTxId, name);
        DLSN dlsn = dlClient.dlClient.write(name, ByteBuffer.wrap(("" + curTxId).getBytes())).get();
    }
    Thread.sleep(1000);
    DistributedLogManager dlm201 = DLMTestUtil.createNewDLM(name, conf, getUri());
    LogReader reader201 = dlm201.getInputStream(1);
    int numRead = 0;
    int curTxId = 201;
    LogRecord r = reader201.readNext(false);
    while (null != r) {
        int i = Integer.parseInt(new String(r.getPayload()));
        assertEquals(curTxId++, i);
        ++numRead;
        r = reader201.readNext(false);
    }
    assertEquals(10, numRead);
    reader201.close();
    dlm201.close();
}
Also used : DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) LogRecord(com.twitter.distributedlog.LogRecord) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) LogReader(com.twitter.distributedlog.LogReader) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) Test(org.junit.Test)

Example 2 with LogNotFoundException

use of com.twitter.distributedlog.exceptions.LogNotFoundException in project distributedlog by twitter.

the class ZKLogMetadataForWriter method createMissingMetadata.

static void createMissingMetadata(final ZooKeeper zk, final String logRootPath, final List<Versioned<byte[]>> metadatas, final List<ACL> acl, final boolean ownAllocator, final boolean createIfNotExists, final Promise<List<Versioned<byte[]>>> promise) {
    final List<byte[]> pathsToCreate = Lists.newArrayListWithExpectedSize(metadatas.size());
    final List<Op> zkOps = Lists.newArrayListWithExpectedSize(metadatas.size());
    CreateMode createMode = CreateMode.PERSISTENT;
    // log root parent path
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT_PARENT))) {
        pathsToCreate.add(null);
    } else {
        String logRootParentPath = new File(logRootPath).getParent();
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootParentPath, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
    }
    // log root path
    if (pathExists(metadatas.get(MetadataIndex.LOG_ROOT))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
    }
    // max id
    if (pathExists(metadatas.get(MetadataIndex.MAX_TXID))) {
        pathsToCreate.add(null);
    } else {
        byte[] zeroTxnIdData = DLUtils.serializeTransactionId(0L);
        pathsToCreate.add(zeroTxnIdData);
        zkOps.add(Op.create(logRootPath + MAX_TXID_PATH, zeroTxnIdData, acl, createMode));
    }
    // version
    if (pathExists(metadatas.get(MetadataIndex.VERSION))) {
        pathsToCreate.add(null);
    } else {
        byte[] versionData = intToBytes(LAYOUT_VERSION);
        pathsToCreate.add(versionData);
        zkOps.add(Op.create(logRootPath + VERSION_PATH, versionData, acl, createMode));
    }
    // lock path
    if (pathExists(metadatas.get(MetadataIndex.LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + LOCK_PATH, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
    }
    // read lock path
    if (pathExists(metadatas.get(MetadataIndex.READ_LOCK))) {
        pathsToCreate.add(null);
    } else {
        pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
        zkOps.add(Op.create(logRootPath + READ_LOCK_PATH, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
    }
    // log segments path
    if (pathExists(metadatas.get(MetadataIndex.LOGSEGMENTS))) {
        pathsToCreate.add(null);
    } else {
        byte[] logSegmentsData = DLUtils.serializeLogSegmentSequenceNumber(DistributedLogConstants.UNASSIGNED_LOGSEGMENT_SEQNO);
        pathsToCreate.add(logSegmentsData);
        zkOps.add(Op.create(logRootPath + LOGSEGMENTS_PATH, logSegmentsData, acl, createMode));
    }
    // allocation path
    if (ownAllocator) {
        if (pathExists(metadatas.get(MetadataIndex.ALLOCATION))) {
            pathsToCreate.add(null);
        } else {
            pathsToCreate.add(DistributedLogConstants.EMPTY_BYTES);
            zkOps.add(Op.create(logRootPath + ALLOCATION_PATH, DistributedLogConstants.EMPTY_BYTES, acl, createMode));
        }
    }
    if (zkOps.isEmpty()) {
        // nothing missed
        promise.setValue(metadatas);
        return;
    }
    if (!createIfNotExists) {
        promise.setException(new LogNotFoundException("Log " + logRootPath + " not found"));
        return;
    }
    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 ZkVersion(0)));
                    }
                }
                promise.setValue(finalMetadatas);
            } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                promise.setException(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.setException(new ZKException("Failed to create log " + logRootPath, KeeperException.Code.get(rc)));
            }
        }
    }, null);
}
Also used : Op(org.apache.zookeeper.Op) LogExistsException(com.twitter.distributedlog.exceptions.LogExistsException) Versioned(org.apache.bookkeeper.versioning.Versioned) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZKException(com.twitter.distributedlog.exceptions.ZKException) CreateMode(org.apache.zookeeper.CreateMode) OpResult(org.apache.zookeeper.OpResult) List(java.util.List) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) File(java.io.File) ZkVersion(org.apache.bookkeeper.meta.ZkVersion)

Example 3 with LogNotFoundException

use of com.twitter.distributedlog.exceptions.LogNotFoundException in project distributedlog by twitter.

the class StreamTransformer method main.

public static void main(String[] args) throws Exception {
    if (3 != args.length) {
        System.out.println(HELP);
        return;
    }
    String dlUriStr = args[0];
    final String srcStreamName = args[1];
    final String targetStreamName = args[2];
    URI uri = URI.create(dlUriStr);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    // 16KB
    conf.setOutputBufferSize(16 * 1024);
    // 5ms
    conf.setPeriodicFlushFrequencyMilliSeconds(5);
    DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    // open the dlm
    System.out.println("Opening log stream " + srcStreamName);
    DistributedLogManager srcDlm = namespace.openLog(srcStreamName);
    System.out.println("Opening log stream " + targetStreamName);
    DistributedLogManager targetDlm = namespace.openLog(targetStreamName);
    Transformer<byte[], byte[]> replicationTransformer = new IdenticalTransformer<byte[]>();
    LogRecordWithDLSN lastTargetRecord;
    DLSN srcDlsn;
    try {
        lastTargetRecord = targetDlm.getLastLogRecord();
        TransformedRecord lastTransformedRecord = new TransformedRecord();
        try {
            lastTransformedRecord.read(protocolFactory.getProtocol(new TIOStreamTransport(new ByteArrayInputStream(lastTargetRecord.getPayload()))));
            srcDlsn = DLSN.deserializeBytes(lastTransformedRecord.getSrcDlsn());
            System.out.println("Last transformed record is " + srcDlsn);
        } catch (TException e) {
            System.err.println("Error on reading last transformed record");
            e.printStackTrace(System.err);
            srcDlsn = DLSN.InitialDLSN;
        }
    } catch (LogNotFoundException lnfe) {
        srcDlsn = DLSN.InitialDLSN;
    } catch (LogEmptyException lee) {
        srcDlsn = DLSN.InitialDLSN;
    }
    AsyncLogWriter targetWriter = FutureUtils.result(targetDlm.openAsyncLogWriter());
    try {
        readLoop(srcDlm, srcDlsn, targetWriter, replicationTransformer);
    } finally {
        FutureUtils.result(targetWriter.asyncClose(), Duration.apply(5, TimeUnit.SECONDS));
        targetDlm.close();
        srcDlm.close();
        namespace.close();
    }
}
Also used : TException(org.apache.thrift.TException) DistributedLogNamespace(com.twitter.distributedlog.namespace.DistributedLogNamespace) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) TransformedRecord(com.twitter.distributedlog.thrift.messaging.TransformedRecord) URI(java.net.URI) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) ByteArrayInputStream(java.io.ByteArrayInputStream) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Example 4 with LogNotFoundException

use of com.twitter.distributedlog.exceptions.LogNotFoundException in project distributedlog by twitter.

the class BKLogHandler method checkLogStreamExistsAsync.

Future<Void> checkLogStreamExistsAsync() {
    final Promise<Void> promise = new Promise<Void>();
    try {
        final ZooKeeper zk = zooKeeperClient.get();
        zk.sync(logMetadata.getLogSegmentsPath(), new AsyncCallback.VoidCallback() {

            @Override
            public void processResult(int syncRc, String path, Object syncCtx) {
                if (KeeperException.Code.NONODE.intValue() == syncRc) {
                    promise.setException(new LogNotFoundException(String.format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                    return;
                } else if (KeeperException.Code.OK.intValue() != syncRc) {
                    promise.setException(new ZKException("Error on checking log existence for " + getFullyQualifiedName(), KeeperException.create(KeeperException.Code.get(syncRc))));
                    return;
                }
                zk.exists(logMetadata.getLogSegmentsPath(), false, new AsyncCallback.StatCallback() {

                    @Override
                    public void processResult(int rc, String path, Object ctx, Stat stat) {
                        if (KeeperException.Code.OK.intValue() == rc) {
                            promise.setValue(null);
                        } else if (KeeperException.Code.NONODE.intValue() == rc) {
                            promise.setException(new LogNotFoundException(String.format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                        } else {
                            promise.setException(new ZKException("Error on checking log existence for " + getFullyQualifiedName(), KeeperException.create(KeeperException.Code.get(rc))));
                        }
                    }
                }, null);
            }
        }, null);
    } catch (InterruptedException ie) {
        LOG.error("Interrupted while reading {}", logMetadata.getLogSegmentsPath(), ie);
        promise.setException(new DLInterruptedException("Interrupted while checking " + logMetadata.getLogSegmentsPath(), ie));
    } catch (ZooKeeperClient.ZooKeeperConnectionException e) {
        promise.setException(e);
    }
    return promise;
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) Promise(com.twitter.util.Promise) ZKException(com.twitter.distributedlog.exceptions.ZKException) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Example 5 with LogNotFoundException

use of com.twitter.distributedlog.exceptions.LogNotFoundException in project distributedlog by twitter.

the class BKLogReadHandler method ensureReadLockPathExist.

private Future<Void> ensureReadLockPathExist() {
    final Promise<Void> promise = new Promise<Void>();
    promise.setInterruptHandler(new com.twitter.util.Function<Throwable, BoxedUnit>() {

        @Override
        public BoxedUnit apply(Throwable t) {
            FutureUtils.setException(promise, new LockCancelledException(readLockPath, "Could not ensure read lock path", t));
            return null;
        }
    });
    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) {
            scheduler.submit(new Runnable() {

                @Override
                public void run() {
                    if (KeeperException.Code.NONODE.intValue() == rc) {
                        FutureUtils.setException(promise, new LogNotFoundException(String.format("Log %s does not exist or has been deleted", getFullyQualifiedName())));
                    } else if (KeeperException.Code.OK.intValue() == rc) {
                        FutureUtils.setValue(promise, null);
                        LOG.trace("Created path {}.", path);
                    } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                        FutureUtils.setValue(promise, null);
                        LOG.trace("Path {} is already existed.", path);
                    } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
                        FutureUtils.setException(promise, new ZooKeeperClient.ZooKeeperConnectionException(path));
                    } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
                        FutureUtils.setException(promise, new DLInterruptedException(path));
                    } else {
                        FutureUtils.setException(promise, KeeperException.create(KeeperException.Code.get(rc)));
                    }
                }
            });
        }
    }, null);
    return promise;
}
Also used : LockCancelledException(com.twitter.distributedlog.exceptions.LockCancelledException) Promise(com.twitter.util.Promise) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) DLInterruptedException(com.twitter.distributedlog.exceptions.DLInterruptedException) BoxedUnit(scala.runtime.BoxedUnit) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Aggregations

LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)10 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)4 LogEmptyException (com.twitter.distributedlog.exceptions.LogEmptyException)4 ZKException (com.twitter.distributedlog.exceptions.ZKException)3 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)3 Promise (com.twitter.util.Promise)3 URI (java.net.URI)3 List (java.util.List)3 Stopwatch (com.google.common.base.Stopwatch)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AsyncCallback (org.apache.zookeeper.AsyncCallback)2 AsyncLogReader (com.twitter.distributedlog.AsyncLogReader)1 DLSN (com.twitter.distributedlog.DLSN)1 DistributedLogManager (com.twitter.distributedlog.DistributedLogManager)1 LogReader (com.twitter.distributedlog.LogReader)1 LogRecord (com.twitter.distributedlog.LogRecord)1 LogRecordWithDLSN (com.twitter.distributedlog.LogRecordWithDLSN)1 DLIllegalStateException (com.twitter.distributedlog.exceptions.DLIllegalStateException)1