use of com.twitter.heron.proto.scheduler.Scheduler in project incubator-heron by apache.
the class SchedulerClientFactory method getSchedulerClient.
/**
* Implementation of getSchedulerClient - Used to create objects
* Currently it creates either HttpServiceSchedulerClient or LibrarySchedulerClient
*
* @return getSchedulerClient created. return null if failed to create ISchedulerClient instance
*/
public ISchedulerClient getSchedulerClient() throws SchedulerException {
LOG.fine("Creating scheduler client");
ISchedulerClient schedulerClient;
if (Context.schedulerService(config)) {
// get the instance of the state manager
SchedulerStateManagerAdaptor statemgr = Runtime.schedulerStateManagerAdaptor(runtime);
Scheduler.SchedulerLocation schedulerLocation = statemgr.getSchedulerLocation(Runtime.topologyName(runtime));
if (schedulerLocation == null) {
throw new SchedulerException("Failed to get scheduler location from state manager");
}
LOG.log(Level.FINE, "Scheduler is listening on location: {0} ", schedulerLocation.toString());
schedulerClient = new HttpServiceSchedulerClient(config, runtime, schedulerLocation.getHttpEndpoint());
} else {
// create an instance of scheduler
final IScheduler scheduler = LauncherUtils.getInstance().getSchedulerInstance(config, runtime);
LOG.fine("Invoke scheduler as a library");
schedulerClient = new LibrarySchedulerClient(config, runtime, scheduler);
}
return schedulerClient;
}
use of com.twitter.heron.proto.scheduler.Scheduler in project incubator-heron by apache.
the class HttpServiceSchedulerClientTest method testRequestSchedulerService.
/**
* Test Request Scheduler Service
*/
@Test
public void testRequestSchedulerService() throws Exception {
HttpServiceSchedulerClient client = Mockito.spy(new HttpServiceSchedulerClient(config, runtime, SCHEDULER_HTTP_ENDPOINT));
// Failed to create new http connection
PowerMockito.spy(NetworkUtils.class);
PowerMockito.doReturn(null).when(NetworkUtils.class, "getHttpConnection", Mockito.any(String.class));
Mockito.doReturn(COMMAND_ENDPOINT).when(client).getCommandEndpoint(Mockito.any(String.class), Mockito.any(Command.class));
Assert.assertFalse(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
HttpURLConnection connection = Mockito.mock(HttpURLConnection.class);
PowerMockito.doReturn(connection).when(NetworkUtils.class, "getHttpConnection", Mockito.any(String.class));
// Failed to send http post request
PowerMockito.doReturn(false).when(NetworkUtils.class, "sendHttpPostRequest", Mockito.eq(connection), Mockito.any(String.class), Mockito.any(byte[].class));
Assert.assertFalse(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
Mockito.verify(connection).disconnect();
// Received non-ok response
PowerMockito.doReturn(true).when(NetworkUtils.class, "sendHttpPostRequest", Mockito.eq(connection), Mockito.any(String.class), Mockito.any(byte[].class));
Scheduler.SchedulerResponse notOKResponse = SchedulerUtils.constructSchedulerResponse(false);
PowerMockito.doReturn(notOKResponse.toByteArray()).when(NetworkUtils.class, "readHttpResponse", Mockito.eq(connection));
Assert.assertFalse(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
Mockito.verify(connection, Mockito.times(2)).disconnect();
// Received ok response -- success case
Scheduler.SchedulerResponse oKResponse = SchedulerUtils.constructSchedulerResponse(true);
PowerMockito.doReturn(oKResponse.toByteArray()).when(NetworkUtils.class, "readHttpResponse", Mockito.eq(connection));
Assert.assertTrue(client.requestSchedulerService(Mockito.any(Command.class), Mockito.any(byte[].class)));
Mockito.verify(connection, Mockito.times(3)).disconnect();
}
Aggregations