Java Code Examples for org.apache.flink.runtime.testingUtils.TestingUtils#defaultScheduledExecutor()

The following examples show how to use org.apache.flink.runtime.testingUtils.TestingUtils#defaultScheduledExecutor() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HeartbeatManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that regular heartbeat signal triggers the right callback functions in the
 * {@link HeartbeatListener}.
 */
@Test
public void testRegularHeartbeat() throws InterruptedException {
	final long heartbeatTimeout = 1000L;
	ResourceID ownResourceID = new ResourceID("foobar");
	ResourceID targetResourceID = new ResourceID("barfoo");
	final int outputPayload = 42;
	final ArrayBlockingQueue<String> reportedPayloads = new ArrayBlockingQueue<>(2);
	final TestingHeartbeatListener<String, Integer> heartbeatListener = new TestingHeartbeatListenerBuilder<String, Integer>()
		.setReportPayloadConsumer((ignored, payload) -> reportedPayloads.offer(payload))
		.setRetrievePayloadFunction((ignored) -> outputPayload)
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<String, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ownResourceID,
		heartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	final ArrayBlockingQueue<Integer> reportedPayloadsHeartbeatTarget = new ArrayBlockingQueue<>(2);
	final TestingHeartbeatTarget<Integer> heartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignoredA, payload) -> reportedPayloadsHeartbeatTarget.offer(payload))
		.createTestingHeartbeatTarget();

	heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);

	final String inputPayload1 = "foobar";
	heartbeatManager.requestHeartbeat(targetResourceID, inputPayload1);

	assertThat(reportedPayloads.take(), is(inputPayload1));
	assertThat(reportedPayloadsHeartbeatTarget.take(), is(outputPayload));

	final String inputPayload2 = "barfoo";
	heartbeatManager.receiveHeartbeat(targetResourceID, inputPayload2);
	assertThat(reportedPayloads.take(), is(inputPayload2));
}
 
Example 2
Source File: SlotManagerBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
private SlotManagerBuilder() {
	this.slotMatchingStrategy = AnyMatchingSlotMatchingStrategy.INSTANCE;
	this.scheduledExecutor = TestingUtils.defaultScheduledExecutor();
	this.taskManagerRequestTimeout = TestingUtils.infiniteTime();
	this.slotRequestTimeout = TestingUtils.infiniteTime();
	this.taskManagerTimeout = TestingUtils.infiniteTime();
	this.waitResultConsumedBeforeRelease = true;
	this.defaultWorkerResourceSpec = WorkerResourceSpec.ZERO;
	this.numSlotsPerWorker = 1;
	this.slotManagerMetricGroup = UnregisteredMetricGroups.createUnregisteredSlotManagerMetricGroup();
	this.maxSlotNum = ResourceManagerOptions.MAX_SLOT_NUM.defaultValue();
}
 
Example 3
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a heartbeat timeout is signaled if the heartbeat is not reported in time.
 */
@Test
public void testHeartbeatTimeout() throws Exception {
	int numHeartbeats = 6;
	final int payload = 42;

	ResourceID ownResourceID = new ResourceID("foobar");
	ResourceID targetResourceID = new ResourceID("barfoo");

	final CompletableFuture<ResourceID> timeoutFuture = new CompletableFuture<>();
	final TestingHeartbeatListener<Integer, Integer> heartbeatListener = new TestingHeartbeatListenerBuilder<Integer, Integer>()
		.setRetrievePayloadFunction(ignored -> payload)
		.setNotifyHeartbeatTimeoutConsumer(timeoutFuture::complete)
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<Integer, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		HEARTBEAT_TIMEOUT,
		ownResourceID,
		heartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	final HeartbeatTarget<Integer> heartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.createTestingHeartbeatTarget();

	heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);

	for (int i = 0; i < numHeartbeats; i++) {
		heartbeatManager.receiveHeartbeat(targetResourceID, payload);
		Thread.sleep(HEARTBEAT_INTERVAL);
	}

	assertFalse(timeoutFuture.isDone());

	ResourceID timeoutResourceID = timeoutFuture.get(2 * HEARTBEAT_TIMEOUT, TimeUnit.MILLISECONDS);

	assertEquals(targetResourceID, timeoutResourceID);
}
 
Example 4
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that regular heartbeat signal triggers the right callback functions in the
 * {@link HeartbeatListener}.
 */
@Test
public void testRegularHeartbeat() throws InterruptedException {
	final long heartbeatTimeout = 1000L;
	ResourceID ownResourceID = new ResourceID("foobar");
	ResourceID targetResourceID = new ResourceID("barfoo");
	final int outputPayload = 42;
	final ArrayBlockingQueue<String> reportedPayloads = new ArrayBlockingQueue<>(2);
	final TestingHeartbeatListener<String, Integer> heartbeatListener = new TestingHeartbeatListenerBuilder<String, Integer>()
		.setReportPayloadConsumer((ignored, payload) -> reportedPayloads.offer(payload))
		.setRetrievePayloadFunction((ignored) -> outputPayload)
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<String, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ownResourceID,
		heartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	final ArrayBlockingQueue<Integer> reportedPayloadsHeartbeatTarget = new ArrayBlockingQueue<>(2);
	final TestingHeartbeatTarget<Integer> heartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignoredA, payload) -> reportedPayloadsHeartbeatTarget.offer(payload))
		.createTestingHeartbeatTarget();

	heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);

	final String inputPayload1 = "foobar";
	heartbeatManager.requestHeartbeat(targetResourceID, inputPayload1);

	assertThat(reportedPayloads.take(), is(inputPayload1));
	assertThat(reportedPayloadsHeartbeatTarget.take(), is(outputPayload));

	final String inputPayload2 = "barfoo";
	heartbeatManager.receiveHeartbeat(targetResourceID, inputPayload2);
	assertThat(reportedPayloads.take(), is(inputPayload2));
}
 
Example 5
Source File: FileArchivedExecutionGraphStoreTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private FileArchivedExecutionGraphStore createDefaultExecutionGraphStore(File storageDirectory) throws IOException {
	return new FileArchivedExecutionGraphStore(
		storageDirectory,
		Time.hours(1L),
		Integer.MAX_VALUE,
		10000L,
		TestingUtils.defaultScheduledExecutor(),
		Ticker.systemTicker());
}
 
Example 6
Source File: SlotManagerBuilder.java    From flink with Apache License 2.0 5 votes vote down vote up
private SlotManagerBuilder() {
	this.scheduledExecutor = TestingUtils.defaultScheduledExecutor();
	this.taskManagerRequestTimeout = TestingUtils.infiniteTime();
	this.slotRequestTimeout = TestingUtils.infiniteTime();
	this.taskManagerTimeout = TestingUtils.infiniteTime();
	this.waitResultConsumedBeforeRelease = true;
}
 
Example 7
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a heartbeat timeout is signaled if the heartbeat is not reported in time.
 */
@Test
public void testHeartbeatTimeout() throws Exception {
	long heartbeatTimeout = 100L;
	int numHeartbeats = 6;
	long heartbeatInterval = 20L;
	final int payload = 42;

	ResourceID ownResourceID = new ResourceID("foobar");
	ResourceID targetResourceID = new ResourceID("barfoo");

	final CompletableFuture<ResourceID> timeoutFuture = new CompletableFuture<>();
	final TestingHeartbeatListener<Integer, Integer> heartbeatListener = new TestingHeartbeatListenerBuilder<Integer, Integer>()
		.setRetrievePayloadFunction(ignored -> payload)
		.setNotifyHeartbeatTimeoutConsumer(timeoutFuture::complete)
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<Integer, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ownResourceID,
		heartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	final HeartbeatTarget<Integer> heartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.createTestingHeartbeatTarget();

	heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);

	for (int i = 0; i < numHeartbeats; i++) {
		heartbeatManager.receiveHeartbeat(targetResourceID, payload);
		Thread.sleep(heartbeatInterval);
	}

	assertFalse(timeoutFuture.isDone());

	ResourceID timeoutResourceID = timeoutFuture.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);

	assertEquals(targetResourceID, timeoutResourceID);
}
 
Example 8
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that regular heartbeat signal triggers the right callback functions in the
 * {@link HeartbeatListener}.
 */
@Test
public void testRegularHeartbeat() throws InterruptedException {
	final long heartbeatTimeout = 1000L;
	ResourceID ownResourceID = new ResourceID("foobar");
	ResourceID targetResourceID = new ResourceID("barfoo");
	final int outputPayload = 42;
	final ArrayBlockingQueue<String> reportedPayloads = new ArrayBlockingQueue<>(2);
	final TestingHeartbeatListener<String, Integer> heartbeatListener = new TestingHeartbeatListenerBuilder<String, Integer>()
		.setReportPayloadConsumer((ignored, payload) -> reportedPayloads.offer(payload))
		.setRetrievePayloadFunction((ignored) -> outputPayload)
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<String, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ownResourceID,
		heartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	final ArrayBlockingQueue<Integer> reportedPayloadsHeartbeatTarget = new ArrayBlockingQueue<>(2);
	final TestingHeartbeatTarget<Integer> heartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignoredA, payload) -> reportedPayloadsHeartbeatTarget.offer(payload))
		.createTestingHeartbeatTarget();

	heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);

	final String inputPayload1 = "foobar";
	heartbeatManager.requestHeartbeat(targetResourceID, inputPayload1);

	assertThat(reportedPayloads.take(), is(inputPayload1));
	assertThat(reportedPayloadsHeartbeatTarget.take(), is(outputPayload));

	final String inputPayload2 = "barfoo";
	heartbeatManager.receiveHeartbeat(targetResourceID, inputPayload2);
	assertThat(reportedPayloads.take(), is(inputPayload2));
}
 
Example 9
Source File: FileArchivedExecutionGraphStoreTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private FileArchivedExecutionGraphStore createDefaultExecutionGraphStore(File storageDirectory) throws IOException {
	return new FileArchivedExecutionGraphStore(
		storageDirectory,
		Time.hours(1L),
		10000L,
		TestingUtils.defaultScheduledExecutor(),
		Ticker.systemTicker());
}
 
Example 10
Source File: SlotManagerBuilder.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private SlotManagerBuilder() {
	this.scheduledExecutor = TestingUtils.defaultScheduledExecutor();
	this.taskManagerRequestTimeout = TestingUtils.infiniteTime();
	this.slotRequestTimeout = TestingUtils.infiniteTime();
	this.taskManagerTimeout = TestingUtils.infiniteTime();
	this.waitResultConsumedBeforeRelease = true;
}
 
Example 11
Source File: HeartbeatManagerTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that a heartbeat timeout is signaled if the heartbeat is not reported in time.
 */
@Test
public void testHeartbeatTimeout() throws Exception {
	long heartbeatTimeout = 100L;
	int numHeartbeats = 6;
	long heartbeatInterval = 20L;
	final int payload = 42;

	ResourceID ownResourceID = new ResourceID("foobar");
	ResourceID targetResourceID = new ResourceID("barfoo");

	final CompletableFuture<ResourceID> timeoutFuture = new CompletableFuture<>();
	final TestingHeartbeatListener<Integer, Integer> heartbeatListener = new TestingHeartbeatListenerBuilder<Integer, Integer>()
		.setRetrievePayloadFunction(ignored -> payload)
		.setNotifyHeartbeatTimeoutConsumer(timeoutFuture::complete)
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<Integer, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ownResourceID,
		heartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	final HeartbeatTarget<Integer> heartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.createTestingHeartbeatTarget();

	heartbeatManager.monitorTarget(targetResourceID, heartbeatTarget);

	for (int i = 0; i < numHeartbeats; i++) {
		heartbeatManager.receiveHeartbeat(targetResourceID, payload);
		Thread.sleep(heartbeatInterval);
	}

	assertFalse(timeoutFuture.isDone());

	ResourceID timeoutResourceID = timeoutFuture.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);

	assertEquals(targetResourceID, timeoutResourceID);
}
 
Example 12
Source File: FileArchivedExecutionGraphStoreTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private FileArchivedExecutionGraphStore createDefaultExecutionGraphStore(File storageDirectory) throws IOException {
	return new FileArchivedExecutionGraphStore(
		storageDirectory,
		Time.hours(1L),
		10000L,
		TestingUtils.defaultScheduledExecutor(),
		Ticker.systemTicker());
}
 
Example 13
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the heartbeat interplay between the {@link HeartbeatManagerImpl} and the
 * {@link HeartbeatManagerSenderImpl}. The sender should regularly trigger heartbeat requests
 * which are fulfilled by the receiver. Upon stopping the receiver, the sender should notify
 * the heartbeat listener about the heartbeat timeout.
 *
 * @throws Exception
 */
@Test
public void testHeartbeatCluster() throws Exception {
	long heartbeatTimeout = 100L;
	long heartbeatPeriod = 20L;
	ResourceID resourceIdTarget = new ResourceID("foobar");
	ResourceID resourceIDSender = new ResourceID("barfoo");
	final int targetPayload = 42;
	final AtomicInteger numReportPayloadCallsTarget = new AtomicInteger(0);
	final TestingHeartbeatListener<String, Integer> heartbeatListenerTarget = new TestingHeartbeatListenerBuilder<String, Integer>()
		.setRetrievePayloadFunction(ignored -> targetPayload)
		.setReportPayloadConsumer((ignoredA, ignoredB) -> numReportPayloadCallsTarget.incrementAndGet())
		.createNewTestingHeartbeatListener();

	final String senderPayload = "1337";
	final CompletableFuture<ResourceID> targetHeartbeatTimeoutFuture = new CompletableFuture<>();
	final AtomicInteger numReportPayloadCallsSender = new AtomicInteger(0);
	final TestingHeartbeatListener<Integer, String> heartbeatListenerSender = new TestingHeartbeatListenerBuilder<Integer, String>()
		.setRetrievePayloadFunction(ignored -> senderPayload)
		.setNotifyHeartbeatTimeoutConsumer(targetHeartbeatTimeoutFuture::complete)
		.setReportPayloadConsumer((ignoredA, ignoredB) -> numReportPayloadCallsSender.incrementAndGet())
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<String, Integer> heartbeatManagerTarget = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		resourceIdTarget,
		heartbeatListenerTarget,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	HeartbeatManagerSenderImpl<Integer, String> heartbeatManagerSender = new HeartbeatManagerSenderImpl<>(
		heartbeatPeriod,
		heartbeatTimeout,
		resourceIDSender,
		heartbeatListenerSender,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	heartbeatManagerTarget.monitorTarget(resourceIDSender, heartbeatManagerSender);
	heartbeatManagerSender.monitorTarget(resourceIdTarget, heartbeatManagerTarget);

	Thread.sleep(2 * heartbeatTimeout);

	assertFalse(targetHeartbeatTimeoutFuture.isDone());

	heartbeatManagerTarget.stop();

	ResourceID timeoutResourceID = targetHeartbeatTimeoutFuture.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);

	assertThat(timeoutResourceID, is(resourceIdTarget));

	int numberHeartbeats = (int) (2 * heartbeatTimeout / heartbeatPeriod);

	final Matcher<Integer> numberHeartbeatsMatcher = greaterThanOrEqualTo(numberHeartbeats / 2);
	assertThat(numReportPayloadCallsTarget.get(), is(numberHeartbeatsMatcher));
	assertThat(numReportPayloadCallsSender.get(), is(numberHeartbeatsMatcher));
}
 
Example 14
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the heartbeat target {@link ResourceID} is properly passed to the {@link HeartbeatListener} by the
 * {@link HeartbeatManagerImpl}.
 */
@Test
public void testHeartbeatManagerTargetPayload() throws Exception {
	final long heartbeatTimeout = 100L;

	final ResourceID someTargetId = ResourceID.generate();
	final ResourceID specialTargetId = ResourceID.generate();

	final Map<ResourceID, Integer> payloads = new HashMap<>(2);
	payloads.put(someTargetId, 0);
	payloads.put(specialTargetId, 1);

	final CompletableFuture<Integer> someHeartbeatPayloadFuture = new CompletableFuture<>();
	final TestingHeartbeatTarget<Integer> someHeartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignored, payload) -> someHeartbeatPayloadFuture.complete(payload))
		.createTestingHeartbeatTarget();

	final CompletableFuture<Integer> specialHeartbeatPayloadFuture = new CompletableFuture<>();
	final TestingHeartbeatTarget<Integer> specialHeartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignored, payload) -> specialHeartbeatPayloadFuture.complete(payload))
		.createTestingHeartbeatTarget();

	final TestingHeartbeatListener<Void, Integer> testingHeartbeatListener = new TestingHeartbeatListenerBuilder<Void, Integer>()
		.setRetrievePayloadFunction(payloads::get)
		.createNewTestingHeartbeatListener();

	HeartbeatManager<?, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ResourceID.generate(),
		testingHeartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	try {
		heartbeatManager.monitorTarget(someTargetId, someHeartbeatTarget);
		heartbeatManager.monitorTarget(specialTargetId, specialHeartbeatTarget);

		heartbeatManager.requestHeartbeat(someTargetId, null);
		assertThat(someHeartbeatPayloadFuture.get(), is(payloads.get(someTargetId)));

		heartbeatManager.requestHeartbeat(specialTargetId, null);
		assertThat(specialHeartbeatPayloadFuture.get(), is(payloads.get(specialTargetId)));
	} finally {
		heartbeatManager.stop();
	}
}
 
Example 15
Source File: HeartbeatManagerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the heartbeat target {@link ResourceID} is properly passed to the {@link HeartbeatListener} by the
 * {@link HeartbeatManagerImpl}.
 */
@Test
public void testHeartbeatManagerTargetPayload() throws Exception {
	final long heartbeatTimeout = 100L;

	final ResourceID someTargetId = ResourceID.generate();
	final ResourceID specialTargetId = ResourceID.generate();

	final Map<ResourceID, Integer> payloads = new HashMap<>(2);
	payloads.put(someTargetId, 0);
	payloads.put(specialTargetId, 1);

	final CompletableFuture<Integer> someHeartbeatPayloadFuture = new CompletableFuture<>();
	final TestingHeartbeatTarget<Integer> someHeartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignored, payload) -> someHeartbeatPayloadFuture.complete(payload))
		.createTestingHeartbeatTarget();

	final CompletableFuture<Integer> specialHeartbeatPayloadFuture = new CompletableFuture<>();
	final TestingHeartbeatTarget<Integer> specialHeartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignored, payload) -> specialHeartbeatPayloadFuture.complete(payload))
		.createTestingHeartbeatTarget();

	final TestingHeartbeatListener<Void, Integer> testingHeartbeatListener = new TestingHeartbeatListenerBuilder<Void, Integer>()
		.setRetrievePayloadFunction(payloads::get)
		.createNewTestingHeartbeatListener();

	HeartbeatManager<?, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ResourceID.generate(),
		testingHeartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	try {
		heartbeatManager.monitorTarget(someTargetId, someHeartbeatTarget);
		heartbeatManager.monitorTarget(specialTargetId, specialHeartbeatTarget);

		heartbeatManager.requestHeartbeat(someTargetId, null);
		assertThat(someHeartbeatPayloadFuture.get(), is(payloads.get(someTargetId)));

		heartbeatManager.requestHeartbeat(specialTargetId, null);
		assertThat(specialHeartbeatPayloadFuture.get(), is(payloads.get(specialTargetId)));
	} finally {
		heartbeatManager.stop();
	}
}
 
Example 16
Source File: HeartbeatManagerTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the heartbeat interplay between the {@link HeartbeatManagerImpl} and the
 * {@link HeartbeatManagerSenderImpl}. The sender should regularly trigger heartbeat requests
 * which are fulfilled by the receiver. Upon stopping the receiver, the sender should notify
 * the heartbeat listener about the heartbeat timeout.
 *
 * @throws Exception
 */
@Test
public void testHeartbeatCluster() throws Exception {
	long heartbeatTimeout = 100L;
	long heartbeatPeriod = 20L;
	ResourceID resourceIdTarget = new ResourceID("foobar");
	ResourceID resourceIDSender = new ResourceID("barfoo");
	final int targetPayload = 42;
	final AtomicInteger numReportPayloadCallsTarget = new AtomicInteger(0);
	final TestingHeartbeatListener<String, Integer> heartbeatListenerTarget = new TestingHeartbeatListenerBuilder<String, Integer>()
		.setRetrievePayloadFunction(ignored -> targetPayload)
		.setReportPayloadConsumer((ignoredA, ignoredB) -> numReportPayloadCallsTarget.incrementAndGet())
		.createNewTestingHeartbeatListener();

	final String senderPayload = "1337";
	final CompletableFuture<ResourceID> targetHeartbeatTimeoutFuture = new CompletableFuture<>();
	final AtomicInteger numReportPayloadCallsSender = new AtomicInteger(0);
	final TestingHeartbeatListener<Integer, String> heartbeatListenerSender = new TestingHeartbeatListenerBuilder<Integer, String>()
		.setRetrievePayloadFunction(ignored -> senderPayload)
		.setNotifyHeartbeatTimeoutConsumer(targetHeartbeatTimeoutFuture::complete)
		.setReportPayloadConsumer((ignoredA, ignoredB) -> numReportPayloadCallsSender.incrementAndGet())
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<String, Integer> heartbeatManagerTarget = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		resourceIdTarget,
		heartbeatListenerTarget,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	HeartbeatManagerSenderImpl<Integer, String> heartbeatManagerSender = new HeartbeatManagerSenderImpl<>(
		heartbeatPeriod,
		heartbeatTimeout,
		resourceIDSender,
		heartbeatListenerSender,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	heartbeatManagerTarget.monitorTarget(resourceIDSender, heartbeatManagerSender);
	heartbeatManagerSender.monitorTarget(resourceIdTarget, heartbeatManagerTarget);

	Thread.sleep(2 * heartbeatTimeout);

	assertFalse(targetHeartbeatTimeoutFuture.isDone());

	heartbeatManagerTarget.stop();

	ResourceID timeoutResourceID = targetHeartbeatTimeoutFuture.get(2 * heartbeatTimeout, TimeUnit.MILLISECONDS);

	assertThat(timeoutResourceID, is(resourceIdTarget));

	int numberHeartbeats = (int) (2 * heartbeatTimeout / heartbeatPeriod);

	final Matcher<Integer> numberHeartbeatsMatcher = greaterThanOrEqualTo(numberHeartbeats / 2);
	assertThat(numReportPayloadCallsTarget.get(), is(numberHeartbeatsMatcher));
	assertThat(numReportPayloadCallsSender.get(), is(numberHeartbeatsMatcher));
}
 
Example 17
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the heartbeat interplay between the {@link HeartbeatManagerImpl} and the
 * {@link HeartbeatManagerSenderImpl}. The sender should regularly trigger heartbeat requests
 * which are fulfilled by the receiver. Upon stopping the receiver, the sender should notify
 * the heartbeat listener about the heartbeat timeout.
 *
 * @throws Exception
 */
@Test
public void testHeartbeatCluster() throws Exception {
	ResourceID resourceIdTarget = new ResourceID("foobar");
	ResourceID resourceIDSender = new ResourceID("barfoo");
	final int targetPayload = 42;
	final AtomicInteger numReportPayloadCallsTarget = new AtomicInteger(0);
	final TestingHeartbeatListener<String, Integer> heartbeatListenerTarget = new TestingHeartbeatListenerBuilder<String, Integer>()
		.setRetrievePayloadFunction(ignored -> targetPayload)
		.setReportPayloadConsumer((ignoredA, ignoredB) -> numReportPayloadCallsTarget.incrementAndGet())
		.createNewTestingHeartbeatListener();

	final String senderPayload = "1337";
	final CompletableFuture<ResourceID> targetHeartbeatTimeoutFuture = new CompletableFuture<>();
	final AtomicInteger numReportPayloadCallsSender = new AtomicInteger(0);
	final TestingHeartbeatListener<Integer, String> heartbeatListenerSender = new TestingHeartbeatListenerBuilder<Integer, String>()
		.setRetrievePayloadFunction(ignored -> senderPayload)
		.setNotifyHeartbeatTimeoutConsumer(targetHeartbeatTimeoutFuture::complete)
		.setReportPayloadConsumer((ignoredA, ignoredB) -> numReportPayloadCallsSender.incrementAndGet())
		.createNewTestingHeartbeatListener();

	HeartbeatManagerImpl<String, Integer> heartbeatManagerTarget = new HeartbeatManagerImpl<>(
		HEARTBEAT_TIMEOUT,
		resourceIdTarget,
		heartbeatListenerTarget,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	HeartbeatManagerSenderImpl<Integer, String> heartbeatManagerSender = new HeartbeatManagerSenderImpl<>(
		HEARTBEAT_INTERVAL,
		HEARTBEAT_TIMEOUT,
		resourceIDSender,
		heartbeatListenerSender,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	heartbeatManagerTarget.monitorTarget(resourceIDSender, heartbeatManagerSender);
	heartbeatManagerSender.monitorTarget(resourceIdTarget, heartbeatManagerTarget);

	Thread.sleep(2 * HEARTBEAT_TIMEOUT);

	assertFalse(targetHeartbeatTimeoutFuture.isDone());

	heartbeatManagerTarget.stop();

	ResourceID timeoutResourceID = targetHeartbeatTimeoutFuture.get(2 * HEARTBEAT_TIMEOUT, TimeUnit.MILLISECONDS);

	assertThat(timeoutResourceID, is(resourceIdTarget));

	int numberHeartbeats = (int) (2 * HEARTBEAT_TIMEOUT / HEARTBEAT_INTERVAL);

	final Matcher<Integer> numberHeartbeatsMatcher = greaterThanOrEqualTo(numberHeartbeats / 2);
	assertThat(numReportPayloadCallsTarget.get(), is(numberHeartbeatsMatcher));
	assertThat(numReportPayloadCallsSender.get(), is(numberHeartbeatsMatcher));
}
 
Example 18
Source File: HeartbeatManagerTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that the heartbeat target {@link ResourceID} is properly passed to the {@link HeartbeatListener} by the
 * {@link HeartbeatManagerImpl}.
 */
@Test
public void testHeartbeatManagerTargetPayload() throws Exception {
	final long heartbeatTimeout = 100L;

	final ResourceID someTargetId = ResourceID.generate();
	final ResourceID specialTargetId = ResourceID.generate();

	final Map<ResourceID, Integer> payloads = new HashMap<>(2);
	payloads.put(someTargetId, 0);
	payloads.put(specialTargetId, 1);

	final CompletableFuture<Integer> someHeartbeatPayloadFuture = new CompletableFuture<>();
	final TestingHeartbeatTarget<Integer> someHeartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignored, payload) -> someHeartbeatPayloadFuture.complete(payload))
		.createTestingHeartbeatTarget();

	final CompletableFuture<Integer> specialHeartbeatPayloadFuture = new CompletableFuture<>();
	final TestingHeartbeatTarget<Integer> specialHeartbeatTarget = new TestingHeartbeatTargetBuilder<Integer>()
		.setReceiveHeartbeatConsumer((ignored, payload) -> specialHeartbeatPayloadFuture.complete(payload))
		.createTestingHeartbeatTarget();

	final TestingHeartbeatListener<Void, Integer> testingHeartbeatListener = new TestingHeartbeatListenerBuilder<Void, Integer>()
		.setRetrievePayloadFunction(payloads::get)
		.createNewTestingHeartbeatListener();

	HeartbeatManager<?, Integer> heartbeatManager = new HeartbeatManagerImpl<>(
		heartbeatTimeout,
		ResourceID.generate(),
		testingHeartbeatListener,
		TestingUtils.defaultScheduledExecutor(),
		LOG);

	try {
		heartbeatManager.monitorTarget(someTargetId, someHeartbeatTarget);
		heartbeatManager.monitorTarget(specialTargetId, specialHeartbeatTarget);

		heartbeatManager.requestHeartbeat(someTargetId, null);
		assertThat(someHeartbeatPayloadFuture.get(), is(payloads.get(someTargetId)));

		heartbeatManager.requestHeartbeat(specialTargetId, null);
		assertThat(specialHeartbeatPayloadFuture.get(), is(payloads.get(specialTargetId)));
	} finally {
		heartbeatManager.stop();
	}
}
 
Example 19
Source File: SlotManagerImplTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that pending slot requests are rejected if a slot report with a different allocation
 * is received.
 */
@Test
public void testSlotReportWhileActiveSlotRequest() throws Exception {
	final ResourceManagerId resourceManagerId = ResourceManagerId.generate();
	final ResourceActions resourceManagerActions = new TestingResourceActionsBuilder().build();

	final JobID jobId = new JobID();
	final AllocationID allocationId = new AllocationID();
	final ResourceProfile resourceProfile = ResourceProfile.fromResources(42.0, 1337);
	final SlotRequest slotRequest = new SlotRequest(jobId, allocationId, resourceProfile, "foobar");
	final CompletableFuture<Acknowledge> slotRequestFuture1 = new CompletableFuture<>();

	final Iterator<CompletableFuture<Acknowledge>> slotRequestFutureIterator = Arrays.asList(
		slotRequestFuture1,
		CompletableFuture.completedFuture(Acknowledge.get())).iterator();
	final ArrayBlockingQueue<SlotID> slotIds = new ArrayBlockingQueue<>(2);

	final TestingTaskExecutorGateway taskExecutorGateway = new TestingTaskExecutorGatewayBuilder()
		.setRequestSlotFunction(FunctionUtils.uncheckedFunction(
			requestSlotParameters -> {
				slotIds.put(requestSlotParameters.f0);
				return slotRequestFutureIterator.next();
			}))
		.createTestingTaskExecutorGateway();

	final ResourceID resourceId = ResourceID.generate();
	final TaskExecutorConnection taskManagerConnection = new TaskExecutorConnection(resourceId, taskExecutorGateway);

	final SlotID slotId1 = new SlotID(resourceId, 0);
	final SlotID slotId2 = new SlotID(resourceId, 1);
	final SlotStatus slotStatus1 = new SlotStatus(slotId1, resourceProfile);
	final SlotStatus slotStatus2 = new SlotStatus(slotId2, resourceProfile);
	final SlotReport slotReport = new SlotReport(Arrays.asList(slotStatus1, slotStatus2));

	final ScheduledExecutor mainThreadExecutor = TestingUtils.defaultScheduledExecutor();

	final SlotManagerImpl slotManager = createSlotManagerBuilder()
		.setScheduledExecutor(mainThreadExecutor)
		.build();

	try {

		slotManager.start(resourceManagerId, mainThreadExecutor, resourceManagerActions);

		CompletableFuture<Void> registrationFuture = CompletableFuture.supplyAsync(
			() -> {
				slotManager.registerTaskManager(taskManagerConnection, slotReport);

				return null;
			},
			mainThreadExecutor)
		.thenAccept(
			(Object value) -> {
				try {
					slotManager.registerSlotRequest(slotRequest);
				} catch (ResourceManagerException e) {
					throw new RuntimeException("Could not register slots.", e);
				}
			});

		// check that no exception has been thrown
		registrationFuture.get();

		final SlotID requestedSlotId = slotIds.take();
		final SlotID freeSlotId = requestedSlotId.equals(slotId1) ? slotId2 : slotId1;

		final SlotStatus newSlotStatus1 = new SlotStatus(requestedSlotId, resourceProfile, new JobID(), new AllocationID());
		final SlotStatus newSlotStatus2 = new SlotStatus(freeSlotId, resourceProfile);
		final SlotReport newSlotReport = new SlotReport(Arrays.asList(newSlotStatus1, newSlotStatus2));

		CompletableFuture<Boolean> reportSlotStatusFuture = CompletableFuture.supplyAsync(
			// this should update the slot with the pending slot request triggering the reassignment of it
			() -> slotManager.reportSlotStatus(taskManagerConnection.getInstanceID(), newSlotReport),
			mainThreadExecutor);

		assertTrue(reportSlotStatusFuture.get());

		final SlotID requestedSlotId2 = slotIds.take();

		assertEquals(freeSlotId, requestedSlotId2);
	} finally {
		CompletableFuture.runAsync(
			ThrowingRunnable.unchecked(slotManager::close),
			mainThreadExecutor);
	}
}