Search in sources :

Example 1 with LogEmptyException

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

the class BKLogHandler method asyncGetFirstLogRecord.

public Future<LogRecordWithDLSN> asyncGetFirstLogRecord() {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {

        @Override
        public void onSuccess(Void value) {
            asyncGetFullLedgerList(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

                @Override
                public void onSuccess(List<LogSegmentMetadata> ledgerList) {
                    if (ledgerList.isEmpty()) {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                        return;
                    }
                    Future<LogRecordWithDLSN> firstRecord = null;
                    for (LogSegmentMetadata ledger : ledgerList) {
                        if (!ledger.isTruncated() && (ledger.getRecordCount() > 0 || ledger.isInProgress())) {
                            firstRecord = asyncReadFirstUserRecord(ledger, DLSN.InitialDLSN);
                            break;
                        }
                    }
                    if (null != firstRecord) {
                        promise.become(firstRecord);
                    } else {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                    }
                }

                @Override
                public void onFailure(Throwable cause) {
                    promise.setException(cause);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.setException(cause);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) FutureEventListener(com.twitter.util.FutureEventListener) List(java.util.List) ArrayList(java.util.ArrayList)

Example 2 with LogEmptyException

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

the class BKLogHandler method getLastLogRecordAsync.

public Future<LogRecordWithDLSN> getLastLogRecordAsync(final boolean recover, final boolean includeEndOfStream) {
    final Promise<LogRecordWithDLSN> promise = new Promise<LogRecordWithDLSN>();
    checkLogStreamExistsAsync().addEventListener(new FutureEventListener<Void>() {

        @Override
        public void onSuccess(Void value) {
            asyncGetFullLedgerListDesc(true, true).addEventListener(new FutureEventListener<List<LogSegmentMetadata>>() {

                @Override
                public void onSuccess(List<LogSegmentMetadata> ledgerList) {
                    if (ledgerList.isEmpty()) {
                        promise.setException(new LogEmptyException("Log " + getFullyQualifiedName() + " has no records"));
                        return;
                    }
                    asyncGetLastLogRecord(ledgerList.iterator(), promise, recover, false, includeEndOfStream);
                }

                @Override
                public void onFailure(Throwable cause) {
                    promise.setException(cause);
                }
            });
        }

        @Override
        public void onFailure(Throwable cause) {
            promise.setException(cause);
        }
    });
    return promise;
}
Also used : Promise(com.twitter.util.Promise) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) FutureEventListener(com.twitter.util.FutureEventListener) List(java.util.List) ArrayList(java.util.ArrayList)

Example 3 with LogEmptyException

use of com.twitter.distributedlog.exceptions.LogEmptyException 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 LogEmptyException

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

the class MultiReader method main.

public static void main(String[] args) throws Exception {
    if (2 != args.length) {
        System.out.println(HELP);
        return;
    }
    String dlUriStr = args[0];
    final String streamList = args[1];
    URI uri = URI.create(dlUriStr);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    String[] streamNameList = StringUtils.split(streamList, ',');
    DistributedLogManager[] managers = new DistributedLogManager[streamNameList.length];
    for (int i = 0; i < managers.length; i++) {
        String streamName = streamNameList[i];
        // open the dlm
        System.out.println("Opening log stream " + streamName);
        managers[i] = namespace.openLog(streamName);
    }
    final CountDownLatch keepAliveLatch = new CountDownLatch(1);
    for (DistributedLogManager dlm : managers) {
        final DistributedLogManager manager = dlm;
        dlm.getLastLogRecordAsync().addEventListener(new FutureEventListener<LogRecordWithDLSN>() {

            @Override
            public void onFailure(Throwable cause) {
                if (cause instanceof LogNotFoundException) {
                    System.err.println("Log stream " + manager.getStreamName() + " is not found. Please create it first.");
                    keepAliveLatch.countDown();
                } else if (cause instanceof LogEmptyException) {
                    System.err.println("Log stream " + manager.getStreamName() + " is empty.");
                    readLoop(manager, DLSN.InitialDLSN, keepAliveLatch);
                } else {
                    System.err.println("Encountered exception on process stream " + manager.getStreamName());
                    keepAliveLatch.countDown();
                }
            }

            @Override
            public void onSuccess(LogRecordWithDLSN record) {
                readLoop(manager, record.getDlsn(), keepAliveLatch);
            }
        });
    }
    keepAliveLatch.await();
    for (DistributedLogManager dlm : managers) {
        dlm.close();
    }
    namespace.close();
}
Also used : DistributedLogNamespace(com.twitter.distributedlog.namespace.DistributedLogNamespace) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException)

Example 5 with LogEmptyException

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

the class TailReader method main.

public static void main(String[] args) throws Exception {
    if (2 != args.length) {
        System.out.println(HELP);
        return;
    }
    String dlUriStr = args[0];
    final String streamName = args[1];
    URI uri = URI.create(dlUriStr);
    DistributedLogConfiguration conf = new DistributedLogConfiguration();
    DistributedLogNamespace namespace = DistributedLogNamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    // open the dlm
    System.out.println("Opening log stream " + streamName);
    DistributedLogManager dlm = namespace.openLog(streamName);
    // get the last record
    LogRecordWithDLSN lastRecord;
    DLSN dlsn;
    try {
        lastRecord = dlm.getLastLogRecord();
        dlsn = lastRecord.getDlsn();
        readLoop(dlm, dlsn);
    } catch (LogNotFoundException lnfe) {
        System.err.println("Log stream " + streamName + " is not found. Please create it first.");
        return;
    } catch (LogEmptyException lee) {
        System.err.println("Log stream " + streamName + " is empty.");
        dlsn = DLSN.InitialDLSN;
        readLoop(dlm, dlsn);
    } finally {
        dlm.close();
        namespace.close();
    }
}
Also used : LogEmptyException(com.twitter.distributedlog.exceptions.LogEmptyException) DistributedLogNamespace(com.twitter.distributedlog.namespace.DistributedLogNamespace) LogNotFoundException(com.twitter.distributedlog.exceptions.LogNotFoundException) URI(java.net.URI)

Aggregations

LogEmptyException (com.twitter.distributedlog.exceptions.LogEmptyException)8 LogNotFoundException (com.twitter.distributedlog.exceptions.LogNotFoundException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)3 Promise (com.twitter.util.Promise)3 URI (java.net.URI)3 Stopwatch (com.google.common.base.Stopwatch)2 FutureEventListener (com.twitter.util.FutureEventListener)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 TransformedRecord (com.twitter.distributedlog.thrift.messaging.TransformedRecord)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 TException (org.apache.thrift.TException)1 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)1 KeeperException (org.apache.zookeeper.KeeperException)1