Java Code Examples for com.twitter.util.Future#flatMap()

The following examples show how to use com.twitter.util.Future#flatMap() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BKAsyncLogWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
private Future<BKLogSegmentWriter> doGetLogSegmentWriter(final long firstTxid,
                                                         final boolean bestEffort,
                                                         final boolean rollLog,
                                                         final boolean allowMaxTxID) {
    if (encounteredError) {
        return Future.exception(new WriteException(bkDistributedLogManager.getStreamName(),
                "writer has been closed due to error."));
    }
    Future<BKLogSegmentWriter> writerFuture = asyncGetLedgerWriter(!disableRollOnSegmentError);
    if (null == writerFuture) {
        return rollLogSegmentIfNecessary(null, firstTxid, bestEffort, allowMaxTxID);
    } else if (rollLog) {
        return writerFuture.flatMap(new AbstractFunction1<BKLogSegmentWriter, Future<BKLogSegmentWriter>>() {
            @Override
            public Future<BKLogSegmentWriter> apply(BKLogSegmentWriter writer) {
                return rollLogSegmentIfNecessary(writer, firstTxid, bestEffort, allowMaxTxID);
            }
        });
    } else {
        return writerFuture;
    }
}
 
Example 2
Source File: BKAsyncLogWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
Future<Long> flushAndCommit() {
    Future<BKLogSegmentWriter> writerFuture;
    synchronized (this) {
        if (null != this.rollingFuture) {
            writerFuture = this.rollingFuture;
        } else {
            writerFuture = getCachedLogWriterFuture();
        }
    }
    if (null == writerFuture) {
        return Future.value(getLastTxId());
    }
    return writerFuture.flatMap(new AbstractFunction1<BKLogSegmentWriter, Future<Long>>() {
        @Override
        public Future<Long> apply(BKLogSegmentWriter writer) {
            return writer.flushAndCommit();
        }
    });
}
 
Example 3
Source File: BKAsyncLogWriter.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
protected Future<Void> asyncCloseAndComplete() {
    Future<BKLogSegmentWriter> logSegmentWriterFuture;
    synchronized (this) {
        logSegmentWriterFuture = this.rollingFuture;
    }

    if (null == logSegmentWriterFuture) {
        return super.asyncCloseAndComplete();
    } else {
        return logSegmentWriterFuture.flatMap(new AbstractFunction1<BKLogSegmentWriter, Future<Void>>() {
            @Override
            public Future<Void> apply(BKLogSegmentWriter segmentWriter) {
                return BKAsyncLogWriter.super.asyncCloseAndComplete();
            }
        });
    }
}
 
Example 4
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String flatMapConstN() throws Exception {
  Future<String> f = constFuture;
  for (int i = 0; i < N.n; i++)
    f = f.flatMap(flatMapF);
  return Await.result(f);
}
 
Example 5
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String flatMapPromiseN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.flatMap(flatMapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example 6
Source File: BulkWriteOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private Future<List<Try<DLSN>>> asTryList(Future<List<Future<DLSN>>> futureList) {
    return futureList.flatMap(new AbstractFunction1<List<Future<DLSN>>, Future<List<Try<DLSN>>>>() {
        @Override
        public Future<List<Try<DLSN>>> apply(List<Future<DLSN>> results) {
            return Future$.MODULE$.collectToTry(results);
        }
    });
}
 
Example 7
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String flatMapConstN() throws Exception {
  Future<String> f = constFuture;
  for (int i = 0; i < N.n; i++)
    f = f.flatMap(flatMapF);
  return Await.result(f);
}
 
Example 8
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String flatMapPromiseN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.flatMap(flatMapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example 9
Source File: TestAsyncReaderLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Test(timeout = 60000)
public void testReaderLockCloseInAcquireCallback() throws Exception {
    final String name = runtime.getMethodName();
    DistributedLogManager dlm = createNewDLM(conf, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter)(dlm.startAsyncLogSegmentNonPartitioned());
    writer.write(DLMTestUtil.getLogRecordInstance(1L));
    writer.closeAndComplete();

    final CountDownLatch latch = new CountDownLatch(1);

    Future<AsyncLogReader> futureReader1 = dlm.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
    futureReader1.flatMap(new ExceptionalFunction<AsyncLogReader, Future<Void>>() {
        @Override
        public Future<Void> applyE(AsyncLogReader reader) throws IOException {
            return reader.asyncClose().map(new AbstractFunction1<Void, Void>() {
                @Override
                public Void apply(Void result) {
                    latch.countDown();
                    return null;
                }
            });
        }
    });

    latch.await();
    dlm.close();
}
 
Example 10
Source File: BulkWriteOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
private Future<List<Try<DLSN>>> asTryList(Future<List<Future<DLSN>>> futureList) {
    return futureList.flatMap(new AbstractFunction1<List<Future<DLSN>>, Future<List<Try<DLSN>>>>() {
        @Override
        public Future<List<Try<DLSN>>> apply(List<Future<DLSN>> results) {
            return Future$.MODULE$.collectToTry(results);
        }
    });
}
 
Example 11
Source File: BKDistributedLogManager.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<AsyncLogWriter> openAsyncLogWriter() {
    try {
        checkClosedOrInError("startLogSegmentNonPartitioned");
    } catch (AlreadyClosedException e) {
        return Future.exception(e);
    }

    Future<BKLogWriteHandler> createWriteHandleFuture;
    synchronized (this) {
        // 1. create the locked write handler
        createWriteHandleFuture = asyncCreateWriteHandler(true);
    }
    return createWriteHandleFuture.flatMap(new AbstractFunction1<BKLogWriteHandler, Future<AsyncLogWriter>>() {
        @Override
        public Future<AsyncLogWriter> apply(final BKLogWriteHandler writeHandler) {
            final BKAsyncLogWriter writer;
            synchronized (BKDistributedLogManager.this) {
                // 2. create the writer with the handler
                writer = new BKAsyncLogWriter(
                        conf,
                        dynConf,
                        BKDistributedLogManager.this,
                        writeHandler,
                        featureProvider,
                        statsLogger);
            }
            // 3. recover the incomplete log segments
            return writeHandler.recoverIncompleteLogSegments()
                    .map(new AbstractFunction1<Long, AsyncLogWriter>() {
                        @Override
                        public AsyncLogWriter apply(Long lastTxId) {
                            // 4. update last tx id if successfully recovered
                            writer.setLastTxId(lastTxId);
                            return writer;
                        }
                    }).onFailure(new AbstractFunction1<Throwable, BoxedUnit>() {
                        @Override
                        public BoxedUnit apply(Throwable cause) {
                            // 5. close the writer if recovery failed
                            writer.asyncAbort();
                            return BoxedUnit.UNIT;
                        }
                    });
        }
    });
}
 
Example 12
Source File: BKAbstractLogWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
synchronized protected Future<BKLogSegmentWriter> asyncGetLedgerWriter(boolean resetOnError) {
    final BKLogSegmentWriter ledgerWriter = getCachedLogWriter();
    Future<BKLogSegmentWriter> ledgerWriterFuture = getCachedLogWriterFuture();
    if (null == ledgerWriterFuture || null == ledgerWriter) {
        return null;
    }

    // Handle the case where the last call to write actually caused an error in the log
    if ((ledgerWriter.isLogSegmentInError() || forceRecovery) && resetOnError) {
        // Close the ledger writer so that we will recover and start a new log segment
        Future<Void> closeFuture;
        if (ledgerWriter.isLogSegmentInError()) {
            closeFuture = ledgerWriter.asyncAbort();
        } else {
            closeFuture = ledgerWriter.asyncClose();
        }
        return closeFuture.flatMap(
                new AbstractFunction1<Void, Future<BKLogSegmentWriter>>() {
            @Override
            public Future<BKLogSegmentWriter> apply(Void result) {
                removeCachedLogWriter();

                if (ledgerWriter.isLogSegmentInError()) {
                    return Future.value(null);
                }

                BKLogWriteHandler writeHandler;
                try {
                    writeHandler = getWriteHandler();
                } catch (IOException e) {
                    return Future.exception(e);
                }
                if (null != writeHandler && forceRecovery) {
                    return writeHandler.completeAndCloseLogSegment(ledgerWriter)
                            .map(new AbstractFunction1<LogSegmentMetadata, BKLogSegmentWriter>() {
                        @Override
                        public BKLogSegmentWriter apply(LogSegmentMetadata completedLogSegment) {
                            return null;
                        }
                    });
                } else {
                    return Future.value(null);
                }
            }
        });
    } else {
        return ledgerWriterFuture;
    }
}