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

The following examples show how to use com.twitter.util.Future#value() . 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: DistributedLogServiceImpl.java    From distributedlog with Apache License 2.0 6 votes vote down vote up
@Override
public Future<ServerInfo> handshakeWithClientInfo(ClientInfo clientInfo) {
    ServerInfo serverInfo = new ServerInfo();
    closeLock.readLock().lock();
    try {
        serverInfo.setServerStatus(serverStatus);
    } finally {
        closeLock.readLock().unlock();
    }

    if (clientInfo.isSetGetOwnerships() && !clientInfo.isGetOwnerships()) {
        return Future.value(serverInfo);
    }

    Optional<String> regex = Optional.absent();
    if (clientInfo.isSetStreamNameRegex()) {
        regex = Optional.of(clientInfo.getStreamNameRegex());
    }

    Map<String, String> ownershipMap = streamManager.getStreamOwnershipMap(regex);
    serverInfo.setOwnerships(ownershipMap);
    return Future.value(serverInfo);
}
 
Example 2
Source File: ZKSessionLock.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Get client id and its ephemeral owner.
 *
 * @param zkClient
 *          zookeeper client
 * @param lockPath
 *          lock path
 * @param nodeName
 *          node name
 * @return client id and its ephemeral owner.
 */
static Future<Pair<String, Long>> asyncParseClientID(ZooKeeper zkClient, String lockPath, String nodeName) {
    String[] parts = nodeName.split("_");
    // member_<clientid>_s<owner_session>_
    if (4 == parts.length && parts[2].startsWith("s")) {
        long sessionOwner = Long.parseLong(parts[2].substring(1));
        String clientId;
        try {
            clientId = URLDecoder.decode(parts[1], UTF_8.name());
            return Future.value(Pair.of(clientId, sessionOwner));
        } catch (UnsupportedEncodingException e) {
            // if failed to parse client id, we have to get client id by zookeeper#getData.
        }
    }
    final Promise<Pair<String, Long>> promise = new Promise<Pair<String, Long>>();
    zkClient.getData(lockPath + "/" + nodeName, false, new AsyncCallback.DataCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            if (KeeperException.Code.OK.intValue() != rc) {
                promise.setException(KeeperException.create(KeeperException.Code.get(rc)));
            } else {
                promise.setValue(Pair.of(deserializeClientId(data), stat.getEphemeralOwner()));
            }
        }
    }, null);
    return promise;
}
 
Example 3
Source File: ZKSubscriptionStateStore.java    From distributedlog with Apache License 2.0 5 votes vote down vote up
/**
 * Get the last committed position stored for this subscription
 */
@Override
public Future<DLSN> getLastCommitPosition() {
    if (null != lastCommittedPosition.get()) {
        return Future.value(lastCommittedPosition.get());
    } else {
        return getLastCommitPositionFromZK();
    }
}
 
Example 4
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 5
Source File: DistributedLogMultiStreamWriter.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Boolean> issueSpeculativeRequest() {
    return Future.value(!complete.get() && null != sendNextWrite());
}
 
Example 6
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 4 votes vote down vote up
@Benchmark
public Future<String> value() {
  return Future.value(string);
}
 
Example 7
Source File: MockDistributedLogServices.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<WriteResponse> heartbeat(String stream, WriteContext ctx) {
    return Future.value(new WriteResponse());
}
 
Example 8
Source File: MockDistributedLogServices.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<ServerInfo> handshake() {
    return Future.value(serverInfo);
}
 
Example 9
Source File: MockDistributedLogServices.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<WriteResponse> writeWithContext(String stream,
                                              ByteBuffer data,
                                              WriteContext ctx) {
    return Future.value(new WriteResponse());
}
 
Example 10
Source File: FinagleRpcDemoServiceImpl.java    From skywalking with Apache License 2.0 4 votes vote down vote up
public Future<String> hello(String name) {
    return Future.value("hello, " + name);
}
 
Example 11
Source File: MockDistributedLogServices.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<WriteResponse> writeWithContext(String stream,
                                              ByteBuffer data,
                                              WriteContext ctx) {
    return Future.value(new WriteResponse());
}
 
Example 12
Source File: EqualLoadAppraiser.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Void> refreshCache() {
    return Future.value(null);
}
 
Example 13
Source File: EqualLoadAppraiser.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<StreamLoad> getStreamLoad(String stream) {
    return Future.value(new StreamLoad(stream, 1));
}
 
Example 14
Source File: ZKLogMetadataStore.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<Optional<URI>> getLogLocation(String logName) {
    return Future.value(nsOptional);
}
 
Example 15
Source File: DistributedLogAdmin.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
private static Future<LogSegmentCandidate> checkLogSegment(
        final String streamName,
        final LogSegmentMetadata metadata,
        final ExecutorService executorService,
        final BookKeeperClient bkc,
        final String digestpw) {
    if (metadata.isInProgress()) {
        return Future.value(null);
    }

    final LedgerHandleCache handleCache = LedgerHandleCache.newBuilder()
            .bkc(bkc)
            .conf(new DistributedLogConfiguration().setBKDigestPW(digestpw))
            .build();
    return ReadUtils.asyncReadLastRecord(
            streamName,
            metadata,
            true,
            false,
            true,
            4,
            16,
            new AtomicInteger(0),
            executorService,
            handleCache
    ).map(new Function<LogRecordWithDLSN, LogSegmentCandidate>() {
        @Override
        public LogSegmentCandidate apply(LogRecordWithDLSN record) {
            if (null != record &&
                (record.getDlsn().compareTo(metadata.getLastDLSN()) > 0 ||
                 record.getTransactionId() > metadata.getLastTxId() ||
                 !metadata.isRecordPositionWithinSegmentScope(record))) {
                return new LogSegmentCandidate(metadata, record);
            } else {
                return null;
            }
        }
    }).ensure(new AbstractFunction0<BoxedUnit>() {
        @Override
        public BoxedUnit apply() {
            handleCache.clear();
            return BoxedUnit.UNIT;
        }
    });
}
 
Example 16
Source File: MockDistributedLogServices.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<ServerInfo> handshake() {
    return Future.value(new ServerInfo());
}
 
Example 17
Source File: ResponseUtils.java    From linden with Apache License 2.0 4 votes vote down vote up
public static Future<Response> buildFailedFutureResponse(String errorStackInfo) {
  return Future.value(new Response(false).setError(errorStackInfo));
}
 
Example 18
Source File: ResponseUtils.java    From linden with Apache License 2.0 4 votes vote down vote up
public static Future<Response> buildFailedFutureResponse(Exception e) {
  return Future.value(new Response(false).setError(Throwables.getStackTraceAsString(e)));
}
 
Example 19
Source File: MockDistributedLogServices.java    From distributedlog with Apache License 2.0 4 votes vote down vote up
@Override
public Future<ServerInfo> handshakeWithClientInfo(ClientInfo clientInfo) {
    return Future.value(serverInfo);
}
 
Example 20
Source File: TwitterFutureBenchmark.java    From future with Apache License 2.0 4 votes vote down vote up
private Future<Integer> loop(int i) {
  if (i > 0)
    return Future.value(i - 1).flatMap(this::loop);
  else
    return Future.value(0);
}