use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class TestScatterGather method testSendGetKVSGRequests.
private static void testSendGetKVSGRequests(ScatterGatherBuilder<Greeting> sg, Long[] requestIds) throws ServiceUnavailableException, InterruptedException {
Collection<ScatterGatherBuilder.KVRequestInfo<Long, Greeting>> scatterGatherRequests = buildScatterGatherGetKVRequests(sg, requestIds);
final Map<Long, Greeting> results = new ConcurrentHashMap<Long, Greeting>();
final CountDownLatch latch = new CountDownLatch(scatterGatherRequests.size());
final List<Throwable> errors = new ArrayList<Throwable>();
final List<BatchKVResponse<Long, Greeting>> responses = new ArrayList<BatchKVResponse<Long, Greeting>>();
for (ScatterGatherBuilder.KVRequestInfo<Long, Greeting> requestInfo : scatterGatherRequests) {
Callback<Response<BatchKVResponse<Long, Greeting>>> cb = new Callback<Response<BatchKVResponse<Long, Greeting>>>() {
@Override
public void onSuccess(Response<BatchKVResponse<Long, Greeting>> response) {
results.putAll(response.getEntity().getResults());
synchronized (responses) {
responses.add(response.getEntity());
}
latch.countDown();
}
@Override
public void onError(Throwable e) {
synchronized (errors) {
errors.add(e);
}
latch.countDown();
}
};
REST_CLIENT.sendRequest(requestInfo.getRequest(), requestInfo.getRequestContext(), cb);
}
latch.await();
if (!errors.isEmpty()) {
Assert.fail("Errors in scatter/gather: " + errors.toString());
}
Assert.assertEquals(results.values().size(), requestIds.length);
Set<Set<Long>> responseIdSets = new HashSet<Set<Long>>();
Set<Long> responseIds = new HashSet<Long>();
for (BatchKVResponse<Long, Greeting> response : responses) {
Set<Long> theseIds = response.getResults().keySet();
//no duplicate requests
Assert.assertFalse(responseIdSets.contains(theseIds));
for (Long id : theseIds) {
//no duplicate ids
Assert.assertFalse(responseIds.contains(id));
responseIds.add(id);
}
responseIdSets.add(theseIds);
}
Assert.assertTrue(responseIds.containsAll(Arrays.asList(requestIds)));
Assert.assertEquals(responseIds.size(), requestIds.length);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class ConfigWriter method writeConfig.
public void writeConfig() throws ExecutionException, TimeoutException, InterruptedException {
long startTime = System.currentTimeMillis();
FutureCallback<None> callback = new FutureCallback<None>();
_store.start(callback);
callback.get(_timeout, _timeoutUnit);
final Semaphore outstandingPutSemaphore = new Semaphore(_maxOutstandingWrites);
for (final String key : _source.keySet()) {
Map<String, Object> map = merge(_source.get(key), _defaultMap);
T properties = _builder.fromMap(map);
Callback<None> putCallback = new Callback<None>() {
@Override
public void onSuccess(None none) {
outstandingPutSemaphore.release();
}
@Override
public void onError(Throwable e) {
_log.error("Put failed for {}", key, e);
outstandingPutSemaphore.release();
}
};
if (!outstandingPutSemaphore.tryAcquire(_timeout, _timeoutUnit)) {
_log.error("Put timed out for {}", key);
throw new TimeoutException();
}
_store.put(key, properties, putCallback);
}
// Wait until all puts are finished.
if (!outstandingPutSemaphore.tryAcquire(_maxOutstandingWrites, _timeout, _timeoutUnit)) {
_log.error("Put timed out with {} outstanding writes", _maxOutstandingWrites - outstandingPutSemaphore.availablePermits());
throw new TimeoutException();
}
FutureCallback<None> shutdownCallback = new FutureCallback<None>();
_store.shutdown(shutdownCallback);
shutdownCallback.get(_timeout, _timeoutUnit);
long elapsedTime = System.currentTimeMillis() - startTime;
_log.info("A total of {}.{}s elapsed to write configs to store.", elapsedTime / 1000, elapsedTime % 1000);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class ZooKeeperServer method markUp.
@Override
public void markUp(final String clusterName, final URI uri, final Map<Integer, PartitionData> partitionDataMap, final Map<String, Object> uriSpecificProperties, final Callback<None> callback) {
final Callback<None> doPutCallback = new Callback<None>() {
@Override
public void onSuccess(None none) {
Map<URI, Map<Integer, PartitionData>> partitionDesc = new HashMap<URI, Map<Integer, PartitionData>>();
partitionDesc.put(uri, partitionDataMap);
Map<URI, Map<String, Object>> myUriSpecificProperties;
if (uriSpecificProperties != null && !uriSpecificProperties.isEmpty()) {
myUriSpecificProperties = new HashMap<URI, Map<String, Object>>();
myUriSpecificProperties.put(uri, uriSpecificProperties);
} else {
myUriSpecificProperties = Collections.emptyMap();
}
if (_log.isInfoEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append(_store);
sb.append(" marked up for cluster: ");
sb.append(clusterName);
sb.append(", uri: ");
sb.append(uri);
sb.append(", announcing [partitionId: weight]s: {");
for (final int partitionId : partitionDataMap.keySet()) {
sb.append("[");
sb.append(partitionId);
sb.append(" : ");
sb.append(partitionDataMap.get(partitionId));
sb.append("]");
}
sb.append("}");
info(_log, sb);
}
_store.put(clusterName, new UriProperties(clusterName, partitionDesc, myUriSpecificProperties), callback);
}
@Override
public void onError(Throwable e) {
callback.onError(e);
}
};
Callback<UriProperties> getCallback = new Callback<UriProperties>() {
@Override
public void onSuccess(UriProperties uris) {
if (uris != null && uris.Uris().contains(uri)) {
warn(_log, "markUp called on a uri that already exists in cluster ", clusterName, ": ", uri);
// mark down before marking up with the new weight
markDown(clusterName, uri, doPutCallback);
} else {
doPutCallback.onSuccess(None.none());
}
}
@Override
public void onError(Throwable e) {
callback.onError(e);
}
};
_store.get(clusterName, getCallback);
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class SimpleLoadBalancerState method shutdown.
@Override
public void shutdown(final PropertyEventShutdownCallback shutdown) {
trace(_log, "shutdown");
// shutdown all three registries, all tracker clients, and the event thread
_executor.execute(new PropertyEvent("shutdown load balancer state") {
@Override
public void innerRun() {
// Need to shutdown loadBalancerStrategies before the transportClients are shutdown
for (Map<String, LoadBalancerStrategy> strategyEntry : _serviceStrategies.values()) {
strategyEntry.values().forEach(LoadBalancerStrategy::shutdown);
}
// put all tracker clients into a single set for convenience
Set<TransportClient> transportClients = new HashSet<TransportClient>();
for (Map<String, TransportClient> clientsByScheme : _serviceClients.values()) {
transportClients.addAll(clientsByScheme.values());
}
Callback<None> trackerCallback = Callbacks.countDown(Callbacks.<None>adaptSimple(new SimpleCallback() {
@Override
public void onDone() {
shutdown.done();
}
}), transportClients.size());
info(_log, "shutting down cluster clients");
for (TransportClient transportClient : transportClients) {
transportClient.shutdown(trackerCallback);
}
// so it is needed to notify all the listeners
for (SimpleLoadBalancerStateListener listener : _listeners) {
// Notify the strategy removal
for (Map.Entry<String, Map<String, LoadBalancerStrategy>> serviceStrategy : _serviceStrategies.entrySet()) {
for (Map.Entry<String, LoadBalancerStrategy> strategyEntry : serviceStrategy.getValue().entrySet()) {
listener.onStrategyRemoved(serviceStrategy.getKey(), strategyEntry.getKey(), strategyEntry.getValue());
}
// Also notify the client removal
Map<URI, TrackerClient> trackerClients = _trackerClients.get(serviceStrategy.getKey());
if (trackerClients != null) {
for (TrackerClient client : trackerClients.values()) {
listener.onClientRemoved(serviceStrategy.getKey(), client);
}
}
}
}
}
});
}
use of com.linkedin.common.callback.Callback in project rest.li by linkedin.
the class RouteLookupClient method restRequest.
@Override
public void restRequest(final RestRequest request, final RequestContext requestContext, final Callback<RestResponse> callback, String routeKey) {
String originalServiceName = request.getURI().getAuthority();
Callback<String> routeLookupCallback = new Callback<String>() {
@Override
public void onError(Throwable e) {
callback.onError(e);
}
@Override
public void onSuccess(String resultServiceName) {
RestRequest resultRequest = createNewRequestWithNewServiceName(request, resultServiceName);
_client.restRequest(resultRequest, requestContext, callback);
}
};
_routeLookup.run(originalServiceName, _routingGroup, routeKey, routeLookupCallback);
}
Aggregations