use of org.apache.heron.proto.scheduler.Scheduler in project heron by twitter.
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 org.apache.heron.proto.scheduler.Scheduler in project heron by twitter.
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();
}
use of org.apache.heron.proto.scheduler.Scheduler in project heron by twitter.
the class LibrarySchedulerClientTest method testKillTopology.
@Test
public void testKillTopology() throws Exception {
IScheduler scheduler = Mockito.mock(IScheduler.class);
Scheduler.KillTopologyRequest request = Scheduler.KillTopologyRequest.getDefaultInstance();
LibrarySchedulerClient client = new LibrarySchedulerClient(config, runtime, scheduler);
// Failure case
Mockito.when(scheduler.onKill(request)).thenReturn(false);
Assert.assertFalse(client.killTopology(request));
Mockito.verify(scheduler).initialize(config, runtime);
Mockito.verify(scheduler).close();
Mockito.verify(scheduler).onKill(request);
// Success case
Mockito.when(scheduler.onKill(request)).thenReturn(true);
Assert.assertTrue(client.killTopology(request));
Mockito.verify(scheduler, Mockito.times(2)).initialize(config, runtime);
Mockito.verify(scheduler, Mockito.times(2)).close();
Mockito.verify(scheduler, Mockito.times(2)).onKill(request);
}
use of org.apache.heron.proto.scheduler.Scheduler in project heron by twitter.
the class LibrarySchedulerClientTest method testRestartTopology.
@Test
public void testRestartTopology() throws Exception {
IScheduler scheduler = Mockito.mock(IScheduler.class);
Scheduler.RestartTopologyRequest request = Scheduler.RestartTopologyRequest.getDefaultInstance();
LibrarySchedulerClient client = new LibrarySchedulerClient(config, runtime, scheduler);
// Failure case
Mockito.when(scheduler.onRestart(request)).thenReturn(false);
Assert.assertFalse(client.restartTopology(request));
Mockito.verify(scheduler).initialize(config, runtime);
Mockito.verify(scheduler).close();
Mockito.verify(scheduler).onRestart(request);
// Success case
Mockito.when(scheduler.onRestart(request)).thenReturn(true);
Assert.assertTrue(client.restartTopology(request));
Mockito.verify(scheduler, Mockito.times(2)).initialize(config, runtime);
Mockito.verify(scheduler, Mockito.times(2)).close();
Mockito.verify(scheduler, Mockito.times(2)).onRestart(request);
}
use of org.apache.heron.proto.scheduler.Scheduler in project heron by twitter.
the class RuntimeManagerRunner method restartTopologyHandler.
/**
* Handler to restart a topology
*/
@VisibleForTesting
void restartTopologyHandler(String topologyName) throws TopologyRuntimeManagementException {
assert !potentialStaleExecutionData;
Integer containerId = Context.topologyContainerId(config);
Scheduler.RestartTopologyRequest restartTopologyRequest = Scheduler.RestartTopologyRequest.newBuilder().setTopologyName(topologyName).setContainerIndex(containerId).build();
// i.e. TManagerLocation does not exist
if (containerId == -1 || containerId == 0) {
// get the instance of state manager to clean state
SchedulerStateManagerAdaptor stateManager = Runtime.schedulerStateManagerAdaptor(runtime);
Boolean result = stateManager.deleteTManagerLocation(topologyName);
if (result == null || !result) {
throw new TopologyRuntimeManagementException("Failed to clear TManager location. Check whether TManager set it correctly.");
}
}
if (!schedulerClient.restartTopology(restartTopologyRequest)) {
throw new TopologyRuntimeManagementException(String.format("Failed to restart topology '%s'", topologyName));
}
// Clean the connection when we are done.
LOG.fine("Scheduler restarted topology successfully.");
}
Aggregations