use of akka.testkit.JavaTestKit in project flink by apache.
the class TaskManagerRegistrationTest method testShutdownAfterRegistrationDurationExpired.
/**
* Tests that the TaskManager shuts down when it cannot register at the
* JobManager within the given maximum duration.
*
* Unfortunately, this test does not give good error messages.
* (I have not figured out how to get any better message out of the
* Akka TestKit than "ask timeout exception".)
*
* Anyways: An "ask timeout exception" here means that the TaskManager
* did not shut down after its registration timeout expired.
*/
@Test
public void testShutdownAfterRegistrationDurationExpired() {
new JavaTestKit(actorSystem) {
{
ActorGateway taskManager = null;
try {
// registration timeout of 1 second
Configuration tmConfig = new Configuration();
tmConfig.setString(ConfigConstants.TASK_MANAGER_MAX_REGISTRATION_DURATION, "500 ms");
// start the taskManager actor
taskManager = createTaskManager(actorSystem, JobManager.getLocalJobManagerAkkaURL(Option.<String>empty()), tmConfig, true, false);
// make sure it terminates in time, since it cannot register at a JobManager
watch(taskManager.actor());
final ActorGateway tm = taskManager;
new Within(timeout) {
@Override
protected void run() {
expectTerminated(tm.actor());
}
};
} catch (Throwable e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
stopActor(taskManager);
}
}
};
}
use of akka.testkit.JavaTestKit in project flink by apache.
the class TaskManagerRegistrationTest method testTaskManagerNoExcessiveRegistrationMessages.
/**
* Tests that the TaskManager does not send an excessive amount of registration messages to
* the job manager if its registration was rejected.
*/
@Test
public void testTaskManagerNoExcessiveRegistrationMessages() throws Exception {
new JavaTestKit(actorSystem) {
{
ActorGateway jm = null;
ActorGateway taskManager = null;
try {
FiniteDuration timeout = new FiniteDuration(5, TimeUnit.SECONDS);
jm = TestingUtils.createForwardingActor(actorSystem, getTestActor(), Option.<String>empty());
final ActorGateway jmGateway = jm;
long refusedRegistrationPause = 500;
long initialRegistrationPause = 100;
long maxDelay = 30000;
Configuration tmConfig = new Configuration(config);
tmConfig.setString(ConfigConstants.TASK_MANAGER_REFUSED_REGISTRATION_PAUSE, refusedRegistrationPause + " ms");
tmConfig.setString(ConfigConstants.TASK_MANAGER_INITIAL_REGISTRATION_PAUSE, initialRegistrationPause + " ms");
// we make the test actor (the test kit) the JobManager to intercept
// the messages
taskManager = createTaskManager(actorSystem, jmGateway, tmConfig, true, false);
final ActorGateway taskManagerGateway = taskManager;
final Deadline deadline = timeout.fromNow();
try {
while (deadline.hasTimeLeft()) {
// the TaskManager should try to register
expectMsgClass(deadline.timeLeft(), RegisterTaskManager.class);
// we decline the registration
taskManagerGateway.tell(new RefuseRegistration(new Exception("test reason")), jmGateway);
}
} catch (AssertionError error) {
// ignore since it simply means that we have used up all our time
}
RegisterTaskManager[] registerTaskManagerMessages = new ReceiveWhile<RegisterTaskManager>(RegisterTaskManager.class, timeout) {
@Override
protected RegisterTaskManager match(Object msg) throws Exception {
if (msg instanceof RegisterTaskManager) {
return (RegisterTaskManager) msg;
} else {
throw noMatch();
}
}
}.get();
int maxExponent = (int) Math.floor(Math.log(((double) maxDelay / initialRegistrationPause + 1)) / Math.log(2));
int exponent = (int) Math.ceil(Math.log(((double) timeout.toMillis() / initialRegistrationPause + 1)) / Math.log(2));
int exp = Math.min(maxExponent, exponent);
long difference = timeout.toMillis() - (initialRegistrationPause * (1 << exp));
int numberRegisterTaskManagerMessages = exp;
if (difference > 0) {
numberRegisterTaskManagerMessages += Math.ceil((double) difference / maxDelay);
}
int maxExpectedNumberOfRegisterTaskManagerMessages = numberRegisterTaskManagerMessages * 2;
assertTrue("The number of RegisterTaskManager messages #" + registerTaskManagerMessages.length + " should be less than #" + maxExpectedNumberOfRegisterTaskManagerMessages, registerTaskManagerMessages.length <= maxExpectedNumberOfRegisterTaskManagerMessages);
} finally {
stopActor(taskManager);
stopActor(jm);
}
}
};
}
use of akka.testkit.JavaTestKit in project flink by apache.
the class TaskManagerRegistrationTest method testDelayedRegistration.
/**
* A test that verifies that two TaskManagers correctly register at the
* JobManager.
*/
@Test
public void testDelayedRegistration() {
new JavaTestKit(actorSystem) {
{
ActorGateway jobManager = null;
ActorGateway taskManager = null;
FiniteDuration delayedTimeout = timeout.$times(3);
try {
// start a TaskManager that tries to register at the JobManager before the JobManager is
// available. we give it the regular JobManager akka URL
taskManager = createTaskManager(actorSystem, JobManager.getLocalJobManagerAkkaURL(Option.<String>empty()), new Configuration(), true, false);
// let it try for a bit
Thread.sleep(6000);
// now start the JobManager, with the regular akka URL
jobManager = createJobManager(actorSystem, TestingUtils.defaultExecutor(), TestingUtils.defaultExecutor(), new Configuration());
startResourceManager(config, jobManager.actor());
startResourceManager(config, jobManager.actor());
// check that the TaskManagers are registered
Future<Object> responseFuture = taskManager.ask(TaskManagerMessages.getNotifyWhenRegisteredAtJobManagerMessage(), delayedTimeout);
Object response = Await.result(responseFuture, delayedTimeout);
// this is a hack to work around the way Java can interact with scala case objects
Class<?> confirmClass = TaskManagerMessages.getRegisteredAtJobManagerMessage().getClass();
assertTrue(response != null && confirmClass.isAssignableFrom(response.getClass()));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
stopActor(taskManager);
stopActor(jobManager);
}
}
};
}
use of akka.testkit.JavaTestKit in project flink by apache.
the class TaskManagerTest method testTriggerStackTraceSampleMessage.
// ------------------------------------------------------------------------
// Stack trace sample
// ------------------------------------------------------------------------
/**
* Tests sampling of task stack traces.
*/
@Test
@SuppressWarnings("unchecked")
public void testTriggerStackTraceSampleMessage() throws Exception {
new JavaTestKit(system) {
{
ActorGateway taskManagerActorGateway = null;
// We need this to be a JM that answers to update messages for
// robustness on Travis (if jobs need to be resubmitted in (4)).
ActorRef jm = system.actorOf(Props.create(new SimpleLookupJobManagerCreator(null)));
ActorGateway jobManagerActorGateway = new AkkaActorGateway(jm, null);
final ActorGateway testActorGateway = new AkkaActorGateway(getTestActor(), leaderSessionID);
try {
final ActorGateway jobManager = jobManagerActorGateway;
final ActorGateway taskManager = TestingUtils.createTaskManager(system, jobManager, new Configuration(), true, false);
final JobID jobId = new JobID();
// Single blocking task
final TaskDeploymentDescriptor tdd = createTaskDeploymentDescriptor(jobId, "Job", new JobVertexID(), new ExecutionAttemptID(), new SerializedValue<>(new ExecutionConfig()), "Task", 1, 0, 1, 0, new Configuration(), new Configuration(), BlockingNoOpInvokable.class.getName(), Collections.<ResultPartitionDeploymentDescriptor>emptyList(), Collections.<InputGateDeploymentDescriptor>emptyList(), Collections.<BlobKey>emptyList(), Collections.<URL>emptyList(), 0);
// Submit the task
new Within(d) {
@Override
protected void run() {
try {
// Make sure to register
Future<?> connectFuture = taskManager.ask(new TestingTaskManagerMessages.NotifyWhenRegisteredAtJobManager(jobManager.actor()), remaining());
Await.ready(connectFuture, remaining());
Future<Object> taskRunningFuture = taskManager.ask(new TestingTaskManagerMessages.NotifyWhenTaskIsRunning(tdd.getExecutionAttemptId()), timeout);
taskManager.tell(new SubmitTask(tdd));
Await.ready(taskRunningFuture, d);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
//
// 1) Trigger sample for non-existing task
//
new Within(d) {
@Override
protected void run() {
try {
ExecutionAttemptID taskId = new ExecutionAttemptID();
taskManager.tell(new TriggerStackTraceSample(112223, taskId, 100, timeD, 0), testActorGateway);
// Receive the expected message (heartbeat races possible)
Object[] msg = receiveN(1);
while (!(msg[0] instanceof Status.Failure)) {
msg = receiveN(1);
}
Status.Failure response = (Status.Failure) msg[0];
assertEquals(IllegalStateException.class, response.cause().getClass());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
//
// 2) Trigger sample for the blocking task
//
new Within(d) {
@Override
protected void run() {
boolean success = false;
Throwable lastError = null;
for (int i = 0; i < 100 && !success; i++) {
try {
int numSamples = 5;
taskManager.tell(new TriggerStackTraceSample(19230, tdd.getExecutionAttemptId(), numSamples, Time.milliseconds(100L), 0), testActorGateway);
// Receive the expected message (heartbeat races possible)
Object[] msg = receiveN(1);
while (!(msg[0] instanceof StackTraceSampleResponse)) {
msg = receiveN(1);
}
StackTraceSampleResponse response = (StackTraceSampleResponse) msg[0];
// ---- Verify response ----
assertEquals(19230, response.getSampleId());
assertEquals(tdd.getExecutionAttemptId(), response.getExecutionAttemptID());
List<StackTraceElement[]> traces = response.getSamples();
assertEquals("Number of samples", numSamples, traces.size());
for (StackTraceElement[] trace : traces) {
// Look for BlockingNoOpInvokable#invoke
for (StackTraceElement elem : trace) {
if (elem.getClassName().equals(BlockingNoOpInvokable.class.getName())) {
assertEquals("invoke", elem.getMethodName());
success = true;
break;
}
}
assertTrue("Unexpected stack trace: " + Arrays.toString(trace), success);
}
} catch (Throwable t) {
lastError = t;
LOG.warn("Failed to find invokable.", t);
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
LOG.error("Interrupted while sleeping before retry.", e);
break;
}
}
if (!success) {
if (lastError == null) {
fail("Failed to find invokable");
} else {
fail(lastError.getMessage());
}
}
}
};
//
// 3) Trigger sample for the blocking task with max depth
//
new Within(d) {
@Override
protected void run() {
try {
int numSamples = 5;
int maxDepth = 2;
taskManager.tell(new TriggerStackTraceSample(1337, tdd.getExecutionAttemptId(), numSamples, Time.milliseconds(100L), maxDepth), testActorGateway);
// Receive the expected message (heartbeat races possible)
Object[] msg = receiveN(1);
while (!(msg[0] instanceof StackTraceSampleResponse)) {
msg = receiveN(1);
}
StackTraceSampleResponse response = (StackTraceSampleResponse) msg[0];
// ---- Verify response ----
assertEquals(1337, response.getSampleId());
assertEquals(tdd.getExecutionAttemptId(), response.getExecutionAttemptID());
List<StackTraceElement[]> traces = response.getSamples();
assertEquals("Number of samples", numSamples, traces.size());
for (StackTraceElement[] trace : traces) {
assertEquals("Max depth", maxDepth, trace.length);
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
//
// 4) Trigger sample for the blocking task, but cancel it during sampling
//
new Within(d) {
@Override
protected void run() {
try {
int maxAttempts = 10;
int sleepTime = 100;
for (int i = 0; i < maxAttempts; i++, sleepTime *= 2) {
// Trigger many samples in order to cancel the task
// during a sample
taskManager.tell(new TriggerStackTraceSample(44, tdd.getExecutionAttemptId(), Integer.MAX_VALUE, Time.milliseconds(10L), 0), testActorGateway);
Thread.sleep(sleepTime);
Future<?> removeFuture = taskManager.ask(new TestingJobManagerMessages.NotifyWhenJobRemoved(jobId), remaining());
// Cancel the task
taskManager.tell(new CancelTask(tdd.getExecutionAttemptId()));
// Receive the expected message (heartbeat races possible)
while (true) {
Object[] msg = receiveN(1);
if (msg[0] instanceof StackTraceSampleResponse) {
StackTraceSampleResponse response = (StackTraceSampleResponse) msg[0];
assertEquals(tdd.getExecutionAttemptId(), response.getExecutionAttemptID());
assertEquals(44, response.getSampleId());
// Done
return;
} else if (msg[0] instanceof Failure) {
// Wait for removal before resubmitting
Await.ready(removeFuture, remaining());
Future<?> taskRunningFuture = taskManager.ask(new TestingTaskManagerMessages.NotifyWhenTaskIsRunning(tdd.getExecutionAttemptId()), timeout);
// Resubmit
taskManager.tell(new SubmitTask(tdd));
Await.ready(taskRunningFuture, remaining());
// Retry the sample message
break;
} else {
// Different message
continue;
}
}
}
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
} finally {
TestingUtils.stopActor(taskManagerActorGateway);
TestingUtils.stopActor(jobManagerActorGateway);
}
}
};
}
use of akka.testkit.JavaTestKit in project flink by apache.
the class TaskManagerTest method testCancellingDependentAndStateUpdateFails.
@Test
public void testCancellingDependentAndStateUpdateFails() {
// this tests creates two tasks. the sender sends data, and fails to send the
// state update back to the job manager
// the second one blocks to be canceled
new JavaTestKit(system) {
{
ActorGateway jobManager = null;
ActorGateway taskManager = null;
final ActorGateway testActorGateway = new AkkaActorGateway(getTestActor(), leaderSessionID);
try {
final JobID jid = new JobID();
JobVertexID vid1 = new JobVertexID();
JobVertexID vid2 = new JobVertexID();
final ExecutionAttemptID eid1 = new ExecutionAttemptID();
final ExecutionAttemptID eid2 = new ExecutionAttemptID();
ActorRef jm = system.actorOf(Props.create(new SimpleLookupFailingUpdateJobManagerCreator(leaderSessionID, eid2)));
jobManager = new AkkaActorGateway(jm, leaderSessionID);
taskManager = TestingUtils.createTaskManager(system, jobManager, new Configuration(), true, true);
final ActorGateway tm = taskManager;
IntermediateResultPartitionID partitionId = new IntermediateResultPartitionID();
List<ResultPartitionDeploymentDescriptor> irpdd = new ArrayList<ResultPartitionDeploymentDescriptor>();
irpdd.add(new ResultPartitionDeploymentDescriptor(new IntermediateDataSetID(), partitionId, ResultPartitionType.PIPELINED, 1, 1, true));
InputGateDeploymentDescriptor ircdd = new InputGateDeploymentDescriptor(new IntermediateDataSetID(), ResultPartitionType.PIPELINED, 0, new InputChannelDeploymentDescriptor[] { new InputChannelDeploymentDescriptor(new ResultPartitionID(partitionId, eid1), ResultPartitionLocation.createLocal()) });
final TaskDeploymentDescriptor tdd1 = createTaskDeploymentDescriptor(jid, "TestJob", vid1, eid1, new SerializedValue<>(new ExecutionConfig()), "Sender", 1, 0, 1, 0, new Configuration(), new Configuration(), Tasks.Sender.class.getName(), irpdd, Collections.<InputGateDeploymentDescriptor>emptyList(), new ArrayList<BlobKey>(), Collections.<URL>emptyList(), 0);
final TaskDeploymentDescriptor tdd2 = createTaskDeploymentDescriptor(jid, "TestJob", vid2, eid2, new SerializedValue<>(new ExecutionConfig()), "Receiver", 7, 2, 7, 0, new Configuration(), new Configuration(), Tasks.BlockingReceiver.class.getName(), Collections.<ResultPartitionDeploymentDescriptor>emptyList(), Collections.singletonList(ircdd), new ArrayList<BlobKey>(), Collections.<URL>emptyList(), 0);
new Within(d) {
@Override
protected void run() {
try {
Future<Object> t1Running = tm.ask(new TestingTaskManagerMessages.NotifyWhenTaskIsRunning(eid1), timeout);
Future<Object> t2Running = tm.ask(new TestingTaskManagerMessages.NotifyWhenTaskIsRunning(eid2), timeout);
tm.tell(new SubmitTask(tdd2), testActorGateway);
tm.tell(new SubmitTask(tdd1), testActorGateway);
expectMsgEquals(Acknowledge.get());
expectMsgEquals(Acknowledge.get());
Await.ready(t1Running, d);
Await.ready(t2Running, d);
tm.tell(TestingTaskManagerMessages.getRequestRunningTasksMessage(), testActorGateway);
Map<ExecutionAttemptID, Task> tasks = expectMsgClass(TestingTaskManagerMessages.ResponseRunningTasks.class).asJava();
Task t1 = tasks.get(eid1);
Task t2 = tasks.get(eid2);
tm.tell(new CancelTask(eid2), testActorGateway);
expectMsgEquals(Acknowledge.get());
if (t2 != null) {
Future<Object> response = tm.ask(new TestingTaskManagerMessages.NotifyWhenTaskRemoved(eid2), timeout);
Await.ready(response, d);
}
if (t1 != null) {
if (t1.getExecutionState() == ExecutionState.RUNNING) {
tm.tell(new CancelTask(eid1), testActorGateway);
expectMsgEquals(Acknowledge.get());
}
Future<Object> response = tm.ask(new TestingTaskManagerMessages.NotifyWhenTaskRemoved(eid1), timeout);
Await.ready(response, d);
}
tm.tell(TestingTaskManagerMessages.getRequestRunningTasksMessage(), testActorGateway);
tasks = expectMsgClass(TestingTaskManagerMessages.ResponseRunningTasks.class).asJava();
assertEquals(0, tasks.size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
};
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
} finally {
// shut down the actors
TestingUtils.stopActor(taskManager);
TestingUtils.stopActor(jobManager);
}
}
};
}
Aggregations