use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class SimpleLoadBalancerSimulation method simulateMultithreaded.
/**
* Run the simulation from a play-back text file. The format of the text file is the
* same as the way the random messages are generated in getRandomMessage().
*
* @param threads
* Number of threads to run.
* @param simulationStepSize
* Number of operations to perform during each step.
* @param playbackStream
* The input stream of messages to play back.
* @throws IOException
* @throws ServiceUnavailableException
*/
public void simulateMultithreaded(int threads, int simulationStepSize, InputStream playbackStream) throws IOException, ServiceUnavailableException {
BufferedReader br = new BufferedReader(new InputStreamReader(playbackStream));
String line = "";
initQueues(threads);
int lineCount = 0;
while (line != null) {
// load simulation steps into queue
for (int i = 0; i < simulationStepSize && line != null; ++i) {
line = br.readLine();
if (line != null) {
lineCount++;
String[] message = line.split(" ");
_queues[getThreadId(message, threads)].add(message);
}
}
// simulate
runSimulation(threads);
// now validate that state is as we expect
verifyState();
reset();
}
try {
final CountDownLatch latch = new CountDownLatch(1);
PropertyEventShutdownCallback callback = new PropertyEventShutdownCallback() {
@Override
public void done() {
latch.countDown();
}
};
_state.shutdown(callback);
if (!latch.await(60, TimeUnit.SECONDS)) {
fail("unable to shutdown state");
}
} catch (InterruptedException e) {
fail("unable to shutdown");
}
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class SimpleLoadBalancerSimulation method simulateMultithreaded.
/**
* Run the simulation using randomly generated messages.
*
* @param threads
* Number of threads to run.
* @param simulationStepSize
* Number of operations to perform during each step.
* @param totalSteps
* The number of steps.
* @throws ServiceUnavailableException
*/
public void simulateMultithreaded(int threads, int simulationStepSize, int totalSteps, String messageLogPath) throws ServiceUnavailableException {
initQueues(threads);
initMessageLog(messageLogPath);
for (int step = 0; step < totalSteps; ++step) {
try {
// load simulation steps into queue
for (int i = 0; i < simulationStepSize; ++i) {
String[] message = getRandomMessage();
if (_messageLog != null) {
_messageLog.write((LoadBalancerUtil.join(Arrays.asList(message), " ") + "\n").getBytes("UTF-8"));
}
_queues[getThreadId(message, threads)].add(message);
++_totalMessages;
}
} catch (UnsupportedEncodingException e) {
fail("unable to encode message properly");
} catch (IOException e) {
fail("unable to write to file");
}
// simulate
long start = System.currentTimeMillis();
runSimulation(threads);
System.err.println(simulationStepSize + " in " + (System.currentTimeMillis() - start) + "ms (" + _totalMessages + " total)");
// now validate that state is as we expect
verifyState();
reset();
}
try {
final CountDownLatch latch = new CountDownLatch(1);
PropertyEventShutdownCallback callback = new PropertyEventShutdownCallback() {
@Override
public void done() {
latch.countDown();
}
};
_state.shutdown(callback);
if (!latch.await(60, TimeUnit.SECONDS)) {
fail("unable to shutdown state");
}
} catch (InterruptedException e) {
fail("interrupted during state and thread shutdown");
}
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class TestRouteLookupClient method testRouteLookupClientFuture.
@Test
public void testRouteLookupClientFuture() throws ExecutionException, InterruptedException {
RouteLookup routeLookup = new SimpleTestRouteLookup();
final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
d2Client.start(new FutureCallback<None>());
RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
Future<RestResponse> future = routeLookupClient.restRequest(dummyRestRequest, "5436");
try {
future.get();
// the request shouldn't succeed because we haven't set up a server or any service -> cluster -> uri
// mapping; we want it to fail because we can get the service name we tried to get at from the
// ServiceUnavailableException that is thrown.
Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
} catch (Exception e) {
String message = e.getMessage();
if (!message.contains("_serviceName=simple_uriWestCoast5436Foo")) {
Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5436Foo");
}
}
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class DynamicClient method getMetadata.
@Override
public Map<String, Object> getMetadata(URI uri) {
if (_balancer != null) {
try {
String serviceName = LoadBalancerUtil.getServiceNameFromUri(uri);
ServiceProperties serviceProperties = _balancer.getLoadBalancedServiceProperties(serviceName);
if (serviceProperties != null) {
return Collections.unmodifiableMap(serviceProperties.getServiceMetadataProperties());
}
} catch (ServiceUnavailableException e) {
error(_log, e);
}
}
return Collections.emptyMap();
}
use of com.linkedin.d2.balancer.ServiceUnavailableException in project rest.li by linkedin.
the class SimpleLoadBalancerStrawMan method main.
public static void main(String[] args) throws URISyntaxException, ServiceUnavailableException {
// define the load balancing strategies that we support (round robin, etc)
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
loadBalancerStrategyFactories.put("rr", new RandomLoadBalancerStrategyFactory());
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
// define the clients that we support (http, etc)
Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
clientFactories.put("http", new HttpClientFactory());
// listen for service updates (could be a glu discovery client, zk discovery client,
// config discovery client, etc)
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
MockStore<ServiceProperties> serviceRegistry = new MockStore<ServiceProperties>();
MockStore<ClusterProperties> clusterRegistry = new MockStore<ClusterProperties>();
MockStore<UriProperties> uriRegistry = new MockStore<UriProperties>();
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriRegistry, clusterRegistry, serviceRegistry, clientFactories, loadBalancerStrategyFactories);
// create the load balancer
SimpleLoadBalancer loadBalancer = new SimpleLoadBalancer(state);
final TransportClient tc = loadBalancer.getClient(new URIRequest("d2://browsemaps/52"), new RequestContext());
final Client c = new TransportClientAdapter(tc, true);
c.restRequest(null);
}
Aggregations