use of co.cask.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class FlowTest method testFlow.
@Test
public void testFlow() throws Exception {
final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(WordCountApp.class, TEMP_FOLDER_SUPPLIER);
List<ProgramController> controllers = Lists.newArrayList();
for (ProgramDescriptor programDescriptor : app.getPrograms()) {
// running mapreduce is out of scope of this tests (there's separate unit-test for that)
if (programDescriptor.getProgramId().getType() == ProgramType.MAPREDUCE) {
continue;
}
controllers.add(AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER));
}
TimeUnit.SECONDS.sleep(1);
TransactionSystemClient txSystemClient = AppFabricTestHelper.getInjector().getInstance(TransactionSystemClient.class);
QueueName queueName = QueueName.fromStream(app.getApplicationId().getNamespace(), "text");
QueueClientFactory queueClientFactory = AppFabricTestHelper.getInjector().getInstance(QueueClientFactory.class);
QueueProducer producer = queueClientFactory.createProducer(queueName);
// start tx to write in queue in tx
Transaction tx = txSystemClient.startShort();
((TransactionAware) producer).startTx(tx);
StreamEventCodec codec = new StreamEventCodec();
for (int i = 0; i < 10; i++) {
String msg = "Testing message " + i;
StreamEvent event = new StreamEvent(ImmutableMap.<String, String>of(), ByteBuffer.wrap(msg.getBytes(Charsets.UTF_8)));
producer.enqueue(new QueueEntry(codec.encodePayload(event)));
}
// commit tx
((TransactionAware) producer).commitTx();
txSystemClient.commit(tx);
// Query the service for at most 10 seconds for the expected result
Gson gson = new Gson();
DiscoveryServiceClient discoveryServiceClient = AppFabricTestHelper.getInjector().getInstance(DiscoveryServiceClient.class);
ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(String.format("service.%s.%s.%s", DefaultId.NAMESPACE.getNamespace(), "WordCountApp", "WordFrequencyService"));
EndpointStrategy endpointStrategy = new RandomEndpointStrategy(serviceDiscovered);
int trials = 0;
while (trials++ < 10) {
Discoverable discoverable = endpointStrategy.pick(2, TimeUnit.SECONDS);
URL url = new URL(String.format("http://%s:%d/v3/namespaces/default/apps/%s/services/%s/methods/%s/%s", discoverable.getSocketAddress().getHostName(), discoverable.getSocketAddress().getPort(), "WordCountApp", "WordFrequencyService", "wordfreq", "text:Testing"));
try {
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
Map<String, Long> responseContent = gson.fromJson(new InputStreamReader(urlConn.getInputStream(), Charsets.UTF_8), new TypeToken<Map<String, Long>>() {
}.getType());
LOG.info("Service response: " + responseContent);
if (ImmutableMap.of("text:Testing", 10L).equals(responseContent)) {
break;
}
} catch (Throwable t) {
LOG.info("Exception when trying to query service.", t);
}
TimeUnit.SECONDS.sleep(1);
}
Assert.assertTrue(trials < 10);
for (ProgramController controller : controllers) {
controller.stop().get();
}
}
use of co.cask.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class ProgramLifecycleHttpHandler method getServiceAvailability.
/**
* Return the availability (i.e. discoverable registration) status of a service.
*/
@GET
@Path("/apps/{app-name}/versions/{app-version}/services/{service-name}/available")
public void getServiceAvailability(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("service-name") String serviceName) throws Exception {
ServiceId serviceId = new ApplicationId(namespaceId, appName, appVersion).service(serviceName);
ProgramStatus status = lifecycleService.getProgramStatus(serviceId);
if (status == ProgramStatus.STOPPED) {
responder.sendString(HttpResponseStatus.SERVICE_UNAVAILABLE, "Service is stopped. Please start it.");
} else {
// Construct discoverable name and return 200 OK if discoverable is present. If not return 503.
String serviceDiscoverableName = ServiceDiscoverable.getName(serviceId);
EndpointStrategy endpointStrategy = new RandomEndpointStrategy(discoveryServiceClient.discover(serviceDiscoverableName));
if (endpointStrategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
LOG.trace("Discoverable endpoint {} not found", serviceDiscoverableName);
responder.sendString(HttpResponseStatus.SERVICE_UNAVAILABLE, "Service is running but not accepting requests at this time.");
} else {
responder.sendString(HttpResponseStatus.OK, "Service is available to accept requests.");
}
}
}
use of co.cask.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class RouterServiceLookup method discoverService.
private EndpointStrategy discoverService(CacheKey key) throws UnsupportedEncodingException, ExecutionException {
// First try with path routing
String lookupService = genLookupName(key.getRouteDestination().getServiceName(), key.getHost(), key.getFirstPathPart());
EndpointStrategy endpointStrategy = discover(new RouteDestination(lookupService));
if (endpointStrategy.pick() == null) {
// Try without path routing
lookupService = genLookupName(key.getRouteDestination().getServiceName(), key.getHost());
endpointStrategy = discover(new RouteDestination(lookupService));
}
return endpointStrategy;
}
use of co.cask.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class RouterServiceLookup method discover.
private EndpointStrategy discover(RouteDestination routeDestination) throws ExecutionException {
LOG.debug("Looking up service name {}", routeDestination);
// If its a user service, then use DistributionEndpoint Strategy
String serviceName = routeDestination.getServiceName();
ServiceDiscovered serviceDiscovered = discoveryServiceClient.discover(serviceName);
EndpointStrategy endpointStrategy = ServiceDiscoverable.isServiceDiscoverable(serviceName) ? new UserServiceEndpointStrategy(serviceDiscovered, routeStore, ServiceDiscoverable.getId(serviceName), fallbackStrategy, routeDestination.getVersion()) : new RandomEndpointStrategy(serviceDiscovered);
if (endpointStrategy.pick(300L, TimeUnit.MILLISECONDS) == null) {
LOG.debug("Discoverable endpoint {} not found", routeDestination);
}
return endpointStrategy;
}
use of co.cask.cdap.common.discovery.EndpointStrategy in project cdap by caskdata.
the class HttpRequestHandler method getDiscoverable.
private WrappedDiscoverable getDiscoverable(final HttpRequest httpRequest, final InetSocketAddress address) {
EndpointStrategy strategy = serviceLookup.getDiscoverable(address.getPort(), httpRequest);
if (strategy == null) {
throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, String.format("No endpoint strategy found for request : %s", httpRequest.getUri()));
}
Discoverable discoverable = strategy.pick();
if (discoverable == null) {
throw new HandlerException(HttpResponseStatus.SERVICE_UNAVAILABLE, String.format("No discoverable found for request : %s", httpRequest.getUri()));
}
return new WrappedDiscoverable(discoverable);
}
Aggregations