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

The following examples show how to use com.twitter.util.Future#map() . 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: HeartbeatOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    // write a control record if heartbeat is the first request of the recovered log segment.
    if (writeControlRecord) {
        long txnId;
        Future<DLSN> writeResult;
        synchronized (txnLock) {
            txnId = sequencer.nextId();
            writeResult = ((BKAsyncLogWriter) writer).writeControlRecord(new LogRecord(txnId, HEARTBEAT_DATA));
        }
        return writeResult.map(new AbstractFunction1<DLSN, WriteResponse>() {
            @Override
            public WriteResponse apply(DLSN value) {
                return ResponseUtils.writeSuccess().setDlsn(value.serialize(dlsnVersion));
            }
        });
    } else {
        return Future.value(ResponseUtils.writeSuccess());
    }
}
 
Example 2
Source File: HeartbeatOp.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    // write a control record if heartbeat is the first request of the recovered log segment.
    if (writeControlRecord) {
        long txnId;
        Future<DLSN> writeResult;
        synchronized (txnLock) {
            txnId = sequencer.nextId();
            LogRecord hbRecord = new LogRecord(txnId, HEARTBEAT_DATA);
            hbRecord.setControl();
            writeResult = newTFuture(writer.write(hbRecord));
        }
        return writeResult.map(new AbstractFunction1<DLSN, WriteResponse>() {
            @Override
            public WriteResponse apply(DLSN value) {
                return ResponseUtils.writeSuccess().setDlsn(value.serialize(dlsnVersion));
            }
        });
    } else {
        return Future.value(ResponseUtils.writeSuccess());
    }
}
 
Example 3
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String mapConstN() throws Exception {
  Future<String> f = constFuture;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  return Await.result(f);
}
 
Example 4
Source File: ReleaseOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    Future<Void> result = streamManager.closeAndRemoveAsync(streamName());
    return result.map(new AbstractFunction1<Void, WriteResponse>() {
        @Override
        public WriteResponse apply(Void value) {
            return ResponseUtils.writeSuccess();
        }
    });
}
 
Example 5
Source File: WriteOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    if (!stream.equals(writer.getStreamName())) {
        logger.error("Write: Stream Name Mismatch in the Stream Map {}, {}", stream, writer.getStreamName());
        return Future.exception(new IllegalStateException("The stream mapping is incorrect, fail the request"));
    }

    long txnId;
    Future<DLSN> writeResult;
    synchronized (txnLock) {
        txnId = sequencer.nextId();
        LogRecord record = new LogRecord(txnId, payload);
        if (isRecordSet) {
            record.setRecordSet();
        }
        writeResult = writer.write(record);
    }
    return writeResult.map(new AbstractFunction1<DLSN, WriteResponse>() {
        @Override
        public WriteResponse apply(DLSN value) {
            successRecordCounter.inc();
            return ResponseUtils.writeSuccess().setDlsn(value.serialize(dlsnVersion));
        }
    });
}
 
Example 6
Source File: DeleteOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    Future<Void> result = streamManager.deleteAndRemoveAsync(streamName());
    return result.map(new AbstractFunction1<Void, WriteResponse>() {
        @Override
        public WriteResponse apply(Void value) {
            return ResponseUtils.writeSuccess();
        }
    });
}
 
Example 7
Source File: CreateOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
  Future<Void> result = streamManager.createStreamAsync(streamName());
  return result.map(new AbstractFunction1<Void, WriteResponse>() {
    @Override
    public WriteResponse apply(Void value) {
      return ResponseUtils.writeSuccess();
    }
  });
}
 
Example 8
Source File: BKAbstractLogWriter.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
synchronized protected Future<BKLogSegmentWriter> rollLogSegmentIfNecessary(
        final BKLogSegmentWriter segmentWriter,
        long startTxId,
        boolean bestEffort,
        boolean allowMaxTxID) {
    final BKLogWriteHandler writeHandler;
    try {
        writeHandler = getWriteHandler();
    } catch (IOException e) {
        return Future.exception(e);
    }
    Future<BKLogSegmentWriter> rollPromise;
    if (null != segmentWriter && (writeHandler.shouldStartNewSegment(segmentWriter) || forceRolling)) {
        rollPromise = closeOldLogSegmentAndStartNewOneWithPermit(
                segmentWriter, writeHandler, startTxId, bestEffort, allowMaxTxID);
    } else if (null == segmentWriter) {
        rollPromise = asyncStartNewLogSegment(writeHandler, startTxId, allowMaxTxID);
    } else {
        rollPromise = Future.value(segmentWriter);
    }
    return rollPromise.map(new AbstractFunction1<BKLogSegmentWriter, BKLogSegmentWriter>() {
        @Override
        public BKLogSegmentWriter apply(BKLogSegmentWriter newSegmentWriter) {
            if (segmentWriter == newSegmentWriter) {
                return newSegmentWriter;
            }
            truncateLogSegmentsIfNecessary(writeHandler);
            return newSegmentWriter;
        }
    });
}
 
Example 9
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String setValueN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example 10
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String mapPromiseN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example 11
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String mapConstN() throws Exception {
  Future<String> f = constFuture;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  return Await.result(f);
}
 
Example 12
Source File: LeastLoadPlacementPolicy.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
public Future<String> placeStream(String stream) {
    String streamOwner = getStreamOwner(stream);
    if (null != streamOwner) {
        return Future.value(streamOwner);
    }
    Future<StreamLoad> streamLoadFuture = loadAppraiser.getStreamLoad(stream);
    return streamLoadFuture.map(new Function<StreamLoad, String>() {
        @Override
        public String apply(StreamLoad streamLoad) {
            return placeStreamSynchronized(streamLoad);
        }
    });
}
 
Example 13
Source File: ReleaseOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    Future<Void> result = streamManager.closeAndRemoveAsync(streamName());
    return result.map(new AbstractFunction1<Void, WriteResponse>() {
        @Override
        public WriteResponse apply(Void value) {
            return ResponseUtils.writeSuccess();
        }
    });
}
 
Example 14
Source File: WriteOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    if (!stream.equals(writer.getStreamName())) {
        logger.error("Write: Stream Name Mismatch in the Stream Map {}, {}", stream, writer.getStreamName());
        return Future.exception(new IllegalStateException("The stream mapping is incorrect, fail the request"));
    }

    long txnId;
    Future<DLSN> writeResult;
    synchronized (txnLock) {
        txnId = sequencer.nextId();
        LogRecord record = new LogRecord(txnId, payload);
        if (isRecordSet) {
            record.setRecordSet();
        }
        writeResult = newTFuture(writer.write(record));
    }
    return writeResult.map(new AbstractFunction1<DLSN, WriteResponse>() {
        @Override
        public WriteResponse apply(DLSN value) {
            successRecordCounter.inc();
            return ResponseUtils.writeSuccess().setDlsn(value.serialize(dlsnVersion));
        }
    });
}
 
Example 15
Source File: CreateOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp() {
  Future<Void> result = streamManager.createStreamAsync(stream);
  return result.map(new AbstractFunction1<Void, WriteResponse>() {
    @Override
    public WriteResponse apply(Void value) {
      return ResponseUtils.writeSuccess();
    }
  });
}
 
Example 16
Source File: DeleteOp.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Future<WriteResponse> executeOp(AsyncLogWriter writer,
                                          Sequencer sequencer,
                                          Object txnLock) {
    Future<Void> result = streamManager.deleteAndRemoveAsync(streamName());
    return result.map(new AbstractFunction1<Void, WriteResponse>() {
        @Override
        public WriteResponse apply(Void value) {
            return ResponseUtils.writeSuccess();
        }
    });
}
 
Example 17
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String setValueN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example 18
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 5 votes vote down vote up
@Benchmark
public String mapPromiseN() throws Exception {
  Promise<String> p = new Promise<String>();
  Future<String> f = p;
  for (int i = 0; i < N.n; i++)
    f = f.map(mapF);
  p.setValue(string);
  return Await.result(f);
}
 
Example 19
Source File: BKAsyncLogWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private synchronized Future<DLSN> asyncWrite(final LogRecord record,
                                             boolean flush) {
    // The passed in writer may be stale since we acquire the writer outside of sync
    // lock. If we recently rolled and the new writer is cached, use that instead.
    Future<DLSN> result = null;
    BKLogSegmentWriter w;
    try {
        w = getCachedLogSegmentWriter();
    } catch (WriteException we) {
        return Future.exception(we);
    }
    if (null != rollingFuture) {
        if (streamFailFast) {
            result = Future.exception(new StreamNotReadyException("Rolling log segment"));
        } else {
            result = queueRequest(record, flush);
        }
    } else if (shouldRollLog(w)) {
        // insert a last record, so when it called back, we will trigger a log segment rolling
        startQueueingRequests();
        if (null != w) {
            LastPendingLogRecord lastLogRecordInCurrentSegment = new LastPendingLogRecord(record, flush);
            w.asyncWrite(record, true).addEventListener(lastLogRecordInCurrentSegment);
            result = lastLogRecordInCurrentSegment.promise;
        } else { // no log segment yet. roll the log segment and issue pending requests.
            result = queueRequest(record, flush);
            rollLogSegmentAndIssuePendingRequests(record);
        }
    } else {
        result = w.asyncWrite(record, flush);
    }
    // use map here rather than onSuccess because we want lastTxId to be updated before
    // satisfying the future
    return result.map(new AbstractFunction1<DLSN, DLSN>() {
        @Override
        public DLSN apply(DLSN dlsn) {
            setLastTxId(record.getTransactionId());
            return dlsn;
        }
    });
}