Java Code Examples for org.apache.flink.runtime.concurrent.ScheduledExecutor#scheduleWithFixedDelay()

The following examples show how to use org.apache.flink.runtime.concurrent.ScheduledExecutor#scheduleWithFixedDelay() . 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: YarnApplicationStatusMonitor.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public YarnApplicationStatusMonitor(
		YarnClient yarnClient,
		ApplicationId yarnApplicationId,
		ScheduledExecutor scheduledExecutor) {
	this.yarnClient = Preconditions.checkNotNull(yarnClient);
	this.yarnApplicationId = Preconditions.checkNotNull(yarnApplicationId);

	applicationStatusUpdateFuture = scheduledExecutor.scheduleWithFixedDelay(
		this::updateApplicationStatus,
		0L,
		UPDATE_INTERVAL,
		TimeUnit.MILLISECONDS);

	applicationStatus = ApplicationStatus.UNKNOWN;
}
 
Example 2
Source File: AkkaRpcServiceTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the RPC service's scheduled executor service can execute runnable with a fixed
 * delay.
 */
@Test(timeout = 60000)
public void testScheduledExecutorServiceWithFixedDelaySchedule() throws Exception {
	ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();

	final int tries = 4;
	final long delay = 10L;
	final CountDownLatch countDownLatch = new CountDownLatch(tries);

	long currentTime = System.nanoTime();

	ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(
		countDownLatch::countDown,
		delay,
		delay,
		TimeUnit.MILLISECONDS);

	assertTrue(!future.isDone());

	countDownLatch.await();

	// the future should not complete since we have a periodic task
	assertTrue(!future.isDone());

	long finalTime = System.nanoTime() - currentTime;

	// the processing should have taken at least delay times the number of count downs.
	assertTrue(finalTime >= tries * delay);

	future.cancel(true);
}
 
Example 3
Source File: YarnApplicationStatusMonitor.java    From flink with Apache License 2.0 5 votes vote down vote up
public YarnApplicationStatusMonitor(
		YarnClient yarnClient,
		ApplicationId yarnApplicationId,
		ScheduledExecutor scheduledExecutor) {
	this.yarnClient = Preconditions.checkNotNull(yarnClient);
	this.yarnApplicationId = Preconditions.checkNotNull(yarnApplicationId);

	applicationStatusUpdateFuture = scheduledExecutor.scheduleWithFixedDelay(
		this::updateApplicationStatus,
		0L,
		UPDATE_INTERVAL,
		TimeUnit.MILLISECONDS);

	applicationStatus = ApplicationStatus.UNKNOWN;
}
 
Example 4
Source File: AkkaRpcServiceTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the RPC service's scheduled executor service can execute runnable with a fixed
 * delay.
 */
@Test(timeout = 60000)
public void testScheduledExecutorServiceWithFixedDelaySchedule() throws Exception {
	ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();

	final int tries = 4;
	final long delay = 10L;
	final CountDownLatch countDownLatch = new CountDownLatch(tries);

	long currentTime = System.nanoTime();

	ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(
		countDownLatch::countDown,
		delay,
		delay,
		TimeUnit.MILLISECONDS);

	assertTrue(!future.isDone());

	countDownLatch.await();

	// the future should not complete since we have a periodic task
	assertTrue(!future.isDone());

	long finalTime = System.nanoTime() - currentTime;

	// the processing should have taken at least delay times the number of count downs.
	assertTrue(finalTime >= tries * delay);

	future.cancel(true);
}
 
Example 5
Source File: YarnApplicationStatusMonitor.java    From flink with Apache License 2.0 5 votes vote down vote up
public YarnApplicationStatusMonitor(
		YarnClient yarnClient,
		ApplicationId yarnApplicationId,
		ScheduledExecutor scheduledExecutor) {
	this.yarnClient = Preconditions.checkNotNull(yarnClient);
	this.yarnApplicationId = Preconditions.checkNotNull(yarnApplicationId);

	applicationStatusUpdateFuture = scheduledExecutor.scheduleWithFixedDelay(
		this::updateApplicationStatus,
		0L,
		UPDATE_INTERVAL,
		TimeUnit.MILLISECONDS);

	applicationStatus = ApplicationStatus.UNKNOWN;
}
 
Example 6
Source File: AkkaRpcServiceTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the RPC service's scheduled executor service can execute runnable with a fixed
 * delay.
 */
@Test(timeout = 60000)
public void testScheduledExecutorServiceWithFixedDelaySchedule() throws Exception {
	ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();

	final int tries = 4;
	final long delay = 10L;
	final CountDownLatch countDownLatch = new CountDownLatch(tries);

	long currentTime = System.nanoTime();

	ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(
		countDownLatch::countDown,
		delay,
		delay,
		TimeUnit.MILLISECONDS);

	assertTrue(!future.isDone());

	countDownLatch.await();

	// the future should not complete since we have a periodic task
	assertTrue(!future.isDone());

	long finalTime = System.nanoTime() - currentTime;

	// the processing should have taken at least delay times the number of count downs.
	assertTrue(finalTime >= tries * delay);

	future.cancel(true);
}
 
Example 7
Source File: FileArchivedExecutionGraphStore.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
public FileArchivedExecutionGraphStore(
		File rootDir,
		Time expirationTime,
		long maximumCacheSizeBytes,
		ScheduledExecutor scheduledExecutor,
		Ticker ticker) throws IOException {

	final File storageDirectory = initExecutionGraphStorageDirectory(rootDir);

	LOG.info(
		"Initializing {}: Storage directory {}, expiration time {}, maximum cache size {} bytes.",
		FileArchivedExecutionGraphStore.class.getSimpleName(),
		storageDirectory,
		expirationTime.toMilliseconds(),
		maximumCacheSizeBytes);

	this.storageDir = Preconditions.checkNotNull(storageDirectory);
	Preconditions.checkArgument(
		storageDirectory.exists() && storageDirectory.isDirectory(),
		"The storage directory must exist and be a directory.");
	this.jobDetailsCache = CacheBuilder.newBuilder()
		.expireAfterWrite(expirationTime.toMilliseconds(), TimeUnit.MILLISECONDS)
		.removalListener(
			(RemovalListener<JobID, JobDetails>) notification -> deleteExecutionGraphFile(notification.getKey()))
		.ticker(ticker)
		.build();

	this.archivedExecutionGraphCache = CacheBuilder.newBuilder()
		.maximumWeight(maximumCacheSizeBytes)
		.weigher(this::calculateSize)
		.build(new CacheLoader<JobID, ArchivedExecutionGraph>() {
			@Override
			public ArchivedExecutionGraph load(JobID jobId) throws Exception {
				return loadExecutionGraph(jobId);
			}});

	this.cleanupFuture = scheduledExecutor.scheduleWithFixedDelay(
		jobDetailsCache::cleanUp,
		expirationTime.toMilliseconds(),
		expirationTime.toMilliseconds(),
		TimeUnit.MILLISECONDS);

	this.shutdownHook = ShutdownHookUtil.addShutdownHook(this, getClass().getSimpleName(), LOG);

	this.numFinishedJobs = 0;
	this.numFailedJobs = 0;
	this.numCanceledJobs = 0;
}
 
Example 8
Source File: AkkaRpcServiceTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that canceling the returned future will stop the execution of the scheduled runnable.
 */
@Test
public void testScheduledExecutorServiceCancelWithFixedDelay() throws InterruptedException {
	ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();

	long delay = 10L;

	final OneShotLatch futureTask = new OneShotLatch();
	final OneShotLatch latch = new OneShotLatch();
	final OneShotLatch shouldNotBeTriggeredLatch = new OneShotLatch();

	ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(
		() -> {
			try {
				if (futureTask.isTriggered()) {
					shouldNotBeTriggeredLatch.trigger();
				} else {
					// first run
					futureTask.trigger();
					latch.await();
				}
			} catch (InterruptedException ignored) {
				// ignore
			}
		},
		delay,
		delay,
		TimeUnit.MILLISECONDS);

	// wait until we're in the runnable
	futureTask.await();

	// cancel the scheduled future
	future.cancel(false);

	latch.trigger();

	try {
		shouldNotBeTriggeredLatch.await(5 * delay, TimeUnit.MILLISECONDS);
		fail("The shouldNotBeTriggeredLatch should never be triggered.");
	} catch (TimeoutException e) {
		// expected
	}
}
 
Example 9
Source File: FileArchivedExecutionGraphStore.java    From flink with Apache License 2.0 4 votes vote down vote up
public FileArchivedExecutionGraphStore(
		File rootDir,
		Time expirationTime,
		long maximumCacheSizeBytes,
		ScheduledExecutor scheduledExecutor,
		Ticker ticker) throws IOException {

	final File storageDirectory = initExecutionGraphStorageDirectory(rootDir);

	LOG.info(
		"Initializing {}: Storage directory {}, expiration time {}, maximum cache size {} bytes.",
		FileArchivedExecutionGraphStore.class.getSimpleName(),
		storageDirectory,
		expirationTime.toMilliseconds(),
		maximumCacheSizeBytes);

	this.storageDir = Preconditions.checkNotNull(storageDirectory);
	Preconditions.checkArgument(
		storageDirectory.exists() && storageDirectory.isDirectory(),
		"The storage directory must exist and be a directory.");
	this.jobDetailsCache = CacheBuilder.newBuilder()
		.expireAfterWrite(expirationTime.toMilliseconds(), TimeUnit.MILLISECONDS)
		.removalListener(
			(RemovalListener<JobID, JobDetails>) notification -> deleteExecutionGraphFile(notification.getKey()))
		.ticker(ticker)
		.build();

	this.archivedExecutionGraphCache = CacheBuilder.newBuilder()
		.maximumWeight(maximumCacheSizeBytes)
		.weigher(this::calculateSize)
		.build(new CacheLoader<JobID, ArchivedExecutionGraph>() {
			@Override
			public ArchivedExecutionGraph load(JobID jobId) throws Exception {
				return loadExecutionGraph(jobId);
			}});

	this.cleanupFuture = scheduledExecutor.scheduleWithFixedDelay(
		jobDetailsCache::cleanUp,
		expirationTime.toMilliseconds(),
		expirationTime.toMilliseconds(),
		TimeUnit.MILLISECONDS);

	this.shutdownHook = ShutdownHookUtil.addShutdownHook(this, getClass().getSimpleName(), LOG);

	this.numFinishedJobs = 0;
	this.numFailedJobs = 0;
	this.numCanceledJobs = 0;
}
 
Example 10
Source File: AkkaRpcServiceTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that canceling the returned future will stop the execution of the scheduled runnable.
 */
@Test
public void testScheduledExecutorServiceCancelWithFixedDelay() throws InterruptedException {
	ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();

	long delay = 10L;

	final OneShotLatch futureTask = new OneShotLatch();
	final OneShotLatch latch = new OneShotLatch();
	final OneShotLatch shouldNotBeTriggeredLatch = new OneShotLatch();

	ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(
		() -> {
			try {
				if (futureTask.isTriggered()) {
					shouldNotBeTriggeredLatch.trigger();
				} else {
					// first run
					futureTask.trigger();
					latch.await();
				}
			} catch (InterruptedException ignored) {
				// ignore
			}
		},
		delay,
		delay,
		TimeUnit.MILLISECONDS);

	// wait until we're in the runnable
	futureTask.await();

	// cancel the scheduled future
	future.cancel(false);

	latch.trigger();

	try {
		shouldNotBeTriggeredLatch.await(5 * delay, TimeUnit.MILLISECONDS);
		fail("The shouldNotBeTriggeredLatch should never be triggered.");
	} catch (TimeoutException e) {
		// expected
	}
}
 
Example 11
Source File: FileArchivedExecutionGraphStore.java    From flink with Apache License 2.0 4 votes vote down vote up
public FileArchivedExecutionGraphStore(
		File rootDir,
		Time expirationTime,
		int maximumCapacity,
		long maximumCacheSizeBytes,
		ScheduledExecutor scheduledExecutor,
		Ticker ticker) throws IOException {

	final File storageDirectory = initExecutionGraphStorageDirectory(rootDir);

	LOG.info(
		"Initializing {}: Storage directory {}, expiration time {}, maximum cache size {} bytes.",
		FileArchivedExecutionGraphStore.class.getSimpleName(),
		storageDirectory,
		expirationTime.toMilliseconds(),
		maximumCacheSizeBytes);

	this.storageDir = Preconditions.checkNotNull(storageDirectory);
	Preconditions.checkArgument(
		storageDirectory.exists() && storageDirectory.isDirectory(),
		"The storage directory must exist and be a directory.");
	this.jobDetailsCache = CacheBuilder.newBuilder()
		.expireAfterWrite(expirationTime.toMilliseconds(), TimeUnit.MILLISECONDS)
		.maximumSize(maximumCapacity)
		.removalListener(
			(RemovalListener<JobID, JobDetails>) notification -> deleteExecutionGraphFile(notification.getKey()))
		.ticker(ticker)
		.build();

	this.archivedExecutionGraphCache = CacheBuilder.newBuilder()
		.maximumWeight(maximumCacheSizeBytes)
		.weigher(this::calculateSize)
		.build(new CacheLoader<JobID, ArchivedExecutionGraph>() {
			@Override
			public ArchivedExecutionGraph load(JobID jobId) throws Exception {
				return loadExecutionGraph(jobId);
			}});

	this.cleanupFuture = scheduledExecutor.scheduleWithFixedDelay(
		jobDetailsCache::cleanUp,
		expirationTime.toMilliseconds(),
		expirationTime.toMilliseconds(),
		TimeUnit.MILLISECONDS);

	this.shutdownHook = ShutdownHookUtil.addShutdownHook(this, getClass().getSimpleName(), LOG);

	this.numFinishedJobs = 0;
	this.numFailedJobs = 0;
	this.numCanceledJobs = 0;
}
 
Example 12
Source File: AkkaRpcServiceTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests that canceling the returned future will stop the execution of the scheduled runnable.
 */
@Test
public void testScheduledExecutorServiceCancelWithFixedDelay() throws InterruptedException {
	ScheduledExecutor scheduledExecutor = akkaRpcService.getScheduledExecutor();

	long delay = 10L;

	final OneShotLatch futureTask = new OneShotLatch();
	final OneShotLatch latch = new OneShotLatch();
	final OneShotLatch shouldNotBeTriggeredLatch = new OneShotLatch();

	ScheduledFuture<?> future = scheduledExecutor.scheduleWithFixedDelay(
		() -> {
			try {
				if (futureTask.isTriggered()) {
					shouldNotBeTriggeredLatch.trigger();
				} else {
					// first run
					futureTask.trigger();
					latch.await();
				}
			} catch (InterruptedException ignored) {
				// ignore
			}
		},
		delay,
		delay,
		TimeUnit.MILLISECONDS);

	// wait until we're in the runnable
	futureTask.await();

	// cancel the scheduled future
	future.cancel(false);

	latch.trigger();

	try {
		shouldNotBeTriggeredLatch.await(5 * delay, TimeUnit.MILLISECONDS);
		fail("The shouldNotBeTriggeredLatch should never be triggered.");
	} catch (TimeoutException e) {
		// expected
	}
}