Java Code Examples for org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService#stop()

The following examples show how to use org.apache.flink.runtime.leaderretrieval.LeaderRetrievalService#stop() . 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: JobLeaderService.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the given job from being monitored by the job leader service.
 *
 * @param jobId identifying the job to remove from monitoring
 * @throws Exception if an error occurred while stopping the leader retrieval service and listener
 */
public void removeJob(JobID jobId) throws Exception {
	Preconditions.checkState(JobLeaderService.State.STARTED == state, "The service is currently not running.");

	Tuple2<LeaderRetrievalService, JobLeaderService.JobManagerLeaderListener> entry = jobLeaderServices.remove(jobId);

	if (entry != null) {
		LOG.info("Remove job {} from job leader monitoring.", jobId);

		LeaderRetrievalService leaderRetrievalService = entry.f0;
		JobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = entry.f1;

		leaderRetrievalService.stop();
		jobManagerLeaderListener.stop();
	}
}
 
Example 2
Source File: LeaderRetrievalUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the leader akka url and the current leader session ID. The values are stored in a
 * {@link LeaderConnectionInfo} instance.
 *
 * @param leaderRetrievalService Leader retrieval service to retrieve the leader connection
 *                               information
 * @param timeout Timeout when to give up looking for the leader
 * @return LeaderConnectionInfo containing the leader's akka URL and the current leader session
 * ID
 * @throws LeaderRetrievalException
 */
public static LeaderConnectionInfo retrieveLeaderConnectionInfo(
		LeaderRetrievalService leaderRetrievalService,
		Duration timeout) throws LeaderRetrievalException {

	LeaderConnectionInfoListener listener = new LeaderConnectionInfoListener();

	try {
		leaderRetrievalService.start(listener);

		return listener.getLeaderConnectionInfoFuture().get(timeout.toMillis(), TimeUnit.MILLISECONDS);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not retrieve the leader address and leader " +
			"session ID.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 3
Source File: DefaultJobLeaderService.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void removeJob(JobID jobId) {
	Preconditions.checkState(DefaultJobLeaderService.State.STARTED == state, "The service is currently not running.");

	Tuple2<LeaderRetrievalService, DefaultJobLeaderService.JobManagerLeaderListener> entry = jobLeaderServices.remove(jobId);

	if (entry != null) {
		LOG.info("Remove job {} from job leader monitoring.", jobId);

		LeaderRetrievalService leaderRetrievalService = entry.f0;
		DefaultJobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = entry.f1;

		jobManagerLeaderListener.stop();

		try {
			leaderRetrievalService.stop();
		} catch (Exception e) {
			LOG.info("Could not properly stop the LeaderRetrievalService for job {}.", jobId, e);
		}
	}
}
 
Example 4
Source File: DefaultJobLeaderService.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void stop() throws Exception {
	LOG.info("Stop job leader service.");

	if (DefaultJobLeaderService.State.STARTED == state) {

		for (Tuple2<LeaderRetrievalService, DefaultJobLeaderService.JobManagerLeaderListener> leaderRetrievalServiceEntry: jobLeaderServices.values()) {
			LeaderRetrievalService leaderRetrievalService = leaderRetrievalServiceEntry.f0;
			DefaultJobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = leaderRetrievalServiceEntry.f1;

			jobManagerLeaderListener.stop();
			leaderRetrievalService.stop();
		}

		jobLeaderServices.clear();
	}

	state = DefaultJobLeaderService.State.STOPPED;
}
 
Example 5
Source File: LeaderRetrievalUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
public static InetAddress findConnectingAddress(
		LeaderRetrievalService leaderRetrievalService,
		FiniteDuration timeout) throws LeaderRetrievalException {
	ConnectionUtils.LeaderConnectingAddressListener listener = new ConnectionUtils.LeaderConnectingAddressListener();

	try {
		leaderRetrievalService.start(listener);

		LOG.info("Trying to select the network interface and address to use " +
				"by connecting to the leading JobManager.");

		LOG.info("TaskManager will try to connect for " + timeout +
				" before falling back to heuristics");

		return listener.findConnectingAddress(timeout);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not find the connecting address by " +
				"connecting to the current leader.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 6
Source File: LeaderRetrievalUtils.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the leader akka url and the current leader session ID. The values are stored in a
 * {@link LeaderConnectionInfo} instance.
 *
 * @param leaderRetrievalService Leader retrieval service to retrieve the leader connection
 *                               information
 * @param timeout Timeout when to give up looking for the leader
 * @return LeaderConnectionInfo containing the leader's akka URL and the current leader session
 * ID
 * @throws LeaderRetrievalException
 */
public static LeaderConnectionInfo retrieveLeaderConnectionInfo(
		LeaderRetrievalService leaderRetrievalService,
		FiniteDuration timeout
) throws LeaderRetrievalException {
	LeaderConnectionInfoListener listener = new LeaderConnectionInfoListener();

	try {
		leaderRetrievalService.start(listener);

		Future<LeaderConnectionInfo> connectionInfoFuture = listener.getLeaderConnectionInfoFuture();

		return Await.result(connectionInfoFuture, timeout);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not retrieve the leader address and leader " +
				"session ID.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 7
Source File: JobLeaderService.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Remove the given job from being monitored by the job leader service.
 *
 * @param jobId identifying the job to remove from monitoring
 * @throws Exception if an error occurred while stopping the leader retrieval service and listener
 */
public void removeJob(JobID jobId) throws Exception {
	Preconditions.checkState(JobLeaderService.State.STARTED == state, "The service is currently not running.");

	Tuple2<LeaderRetrievalService, JobLeaderService.JobManagerLeaderListener> entry = jobLeaderServices.remove(jobId);

	if (entry != null) {
		LOG.info("Remove job {} from job leader monitoring.", jobId);

		LeaderRetrievalService leaderRetrievalService = entry.f0;
		JobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = entry.f1;

		leaderRetrievalService.stop();
		jobManagerLeaderListener.stop();
	}
}
 
Example 8
Source File: JobLeaderService.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Stop the job leader services. This implies stopping all leader retrieval services for the
 * different jobs and their leader retrieval listeners.
 *
 * @throws Exception if an error occurs while stopping the service
 */
public void stop() throws Exception {
	LOG.info("Stop job leader service.");

	if (JobLeaderService.State.STARTED == state) {

		for (Tuple2<LeaderRetrievalService, JobLeaderService.JobManagerLeaderListener> leaderRetrievalServiceEntry: jobLeaderServices.values()) {
			LeaderRetrievalService leaderRetrievalService = leaderRetrievalServiceEntry.f0;
			JobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = leaderRetrievalServiceEntry.f1;

			jobManagerLeaderListener.stop();
			leaderRetrievalService.stop();
		}

		jobLeaderServices.clear();
	}

	state = JobLeaderService.State.STOPPED;
}
 
Example 9
Source File: LeaderRetrievalUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
public static InetAddress findConnectingAddress(
		LeaderRetrievalService leaderRetrievalService,
		FiniteDuration timeout) throws LeaderRetrievalException {
	ConnectionUtils.LeaderConnectingAddressListener listener = new ConnectionUtils.LeaderConnectingAddressListener();

	try {
		leaderRetrievalService.start(listener);

		LOG.info("Trying to select the network interface and address to use " +
				"by connecting to the leading JobManager.");

		LOG.info("TaskManager will try to connect for " + timeout +
				" before falling back to heuristics");

		return listener.findConnectingAddress(timeout);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not find the connecting address by " +
				"connecting to the current leader.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 10
Source File: LeaderRetrievalUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the leader akka url and the current leader session ID. The values are stored in a
 * {@link LeaderConnectionInfo} instance.
 *
 * @param leaderRetrievalService Leader retrieval service to retrieve the leader connection
 *                               information
 * @param timeout Timeout when to give up looking for the leader
 * @return LeaderConnectionInfo containing the leader's akka URL and the current leader session
 * ID
 * @throws LeaderRetrievalException
 */
public static LeaderConnectionInfo retrieveLeaderConnectionInfo(
		LeaderRetrievalService leaderRetrievalService,
		FiniteDuration timeout
) throws LeaderRetrievalException {
	LeaderConnectionInfoListener listener = new LeaderConnectionInfoListener();

	try {
		leaderRetrievalService.start(listener);

		Future<LeaderConnectionInfo> connectionInfoFuture = listener.getLeaderConnectionInfoFuture();

		return Await.result(connectionInfoFuture, timeout);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not retrieve the leader address and leader " +
				"session ID.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 11
Source File: LeaderRetrievalUtils.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Retrieves the current leader gateway using the given {@link LeaderRetrievalService}. If the
 * current leader could not be retrieved after the given timeout, then a
 * {@link LeaderRetrievalException} is thrown.
 *
 * @param leaderRetrievalService {@link LeaderRetrievalService} which is used for the leader retrieval
 * @param actorSystem ActorSystem which is used for the {@link LeaderRetrievalListener} implementation
 * @param timeout Timeout value for the retrieval call
 * @return The current leader gateway
 * @throws LeaderRetrievalException If the actor gateway could not be retrieved or the timeout has been exceeded
 */
public static ActorGateway retrieveLeaderGateway(
		LeaderRetrievalService leaderRetrievalService,
		ActorSystem actorSystem,
		FiniteDuration timeout)
	throws LeaderRetrievalException {
	LeaderGatewayListener listener = new LeaderGatewayListener(actorSystem, timeout);

	try {
		leaderRetrievalService.start(listener);

		Future<ActorGateway> actorGatewayFuture = listener.getActorGatewayFuture();

		return Await.result(actorGatewayFuture, timeout);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not retrieve the leader gateway.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 12
Source File: JobLeaderService.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
/**
 * Stop the job leader services. This implies stopping all leader retrieval services for the
 * different jobs and their leader retrieval listeners.
 *
 * @throws Exception if an error occurs while stopping the service
 */
public void stop() throws Exception {
	LOG.info("Stop job leader service.");

	if (JobLeaderService.State.STARTED == state) {

		for (Tuple2<LeaderRetrievalService, JobLeaderService.JobManagerLeaderListener> leaderRetrievalServiceEntry: jobLeaderServices.values()) {
			LeaderRetrievalService leaderRetrievalService = leaderRetrievalServiceEntry.f0;
			JobLeaderService.JobManagerLeaderListener jobManagerLeaderListener = leaderRetrievalServiceEntry.f1;

			jobManagerLeaderListener.stop();
			leaderRetrievalService.stop();
		}

		jobLeaderServices.clear();
	}

	state = JobLeaderService.State.STOPPED;
}
 
Example 13
Source File: ZooKeeperHaServicesTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private void runCleanupTest(
		Configuration configuration,
		TestingBlobStoreService blobStoreService,
		ThrowingConsumer<ZooKeeperHaServices, Exception> zooKeeperHaServicesConsumer) throws Exception {
	try (ZooKeeperHaServices zooKeeperHaServices = new ZooKeeperHaServices(
		ZooKeeperUtils.startCuratorFramework(configuration),
		Executors.directExecutor(),
		configuration,
		blobStoreService)) {

		// create some Zk services to trigger the generation of paths
		final LeaderRetrievalService resourceManagerLeaderRetriever = zooKeeperHaServices.getResourceManagerLeaderRetriever();
		final LeaderElectionService resourceManagerLeaderElectionService = zooKeeperHaServices.getResourceManagerLeaderElectionService();
		final RunningJobsRegistry runningJobsRegistry = zooKeeperHaServices.getRunningJobsRegistry();

		final TestingListener listener = new TestingListener();
		resourceManagerLeaderRetriever.start(listener);
		resourceManagerLeaderElectionService.start(new TestingContender("foobar", resourceManagerLeaderElectionService));
		final JobID jobId = new JobID();
		runningJobsRegistry.setJobRunning(jobId);

		listener.waitForNewLeader(2000L);

		resourceManagerLeaderRetriever.stop();
		resourceManagerLeaderElectionService.stop();
		runningJobsRegistry.clearJob(jobId);

		zooKeeperHaServicesConsumer.accept(zooKeeperHaServices);
	}
}
 
Example 14
Source File: ZooKeeperHaServicesTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void runCleanupTest(
		Configuration configuration,
		TestingBlobStoreService blobStoreService,
		ThrowingConsumer<ZooKeeperHaServices, Exception> zooKeeperHaServicesConsumer) throws Exception {
	try (ZooKeeperHaServices zooKeeperHaServices = new ZooKeeperHaServices(
		ZooKeeperUtils.startCuratorFramework(configuration),
		Executors.directExecutor(),
		configuration,
		blobStoreService)) {

		// create some Zk services to trigger the generation of paths
		final LeaderRetrievalService resourceManagerLeaderRetriever = zooKeeperHaServices.getResourceManagerLeaderRetriever();
		final LeaderElectionService resourceManagerLeaderElectionService = zooKeeperHaServices.getResourceManagerLeaderElectionService();
		final RunningJobsRegistry runningJobsRegistry = zooKeeperHaServices.getRunningJobsRegistry();

		final TestingListener listener = new TestingListener();
		resourceManagerLeaderRetriever.start(listener);
		resourceManagerLeaderElectionService.start(new TestingContender("foobar", resourceManagerLeaderElectionService));
		final JobID jobId = new JobID();
		runningJobsRegistry.setJobRunning(jobId);

		listener.waitForNewLeader(2000L);

		resourceManagerLeaderRetriever.stop();
		resourceManagerLeaderElectionService.stop();
		runningJobsRegistry.clearJob(jobId);

		zooKeeperHaServicesConsumer.accept(zooKeeperHaServices);
	}
}
 
Example 15
Source File: LeaderRetrievalUtils.java    From flink with Apache License 2.0 5 votes vote down vote up
public static InetAddress findConnectingAddress(
		LeaderRetrievalService leaderRetrievalService,
		Duration timeout) throws LeaderRetrievalException {

	ConnectionUtils.LeaderConnectingAddressListener listener = new ConnectionUtils.LeaderConnectingAddressListener();

	try {
		leaderRetrievalService.start(listener);

		LOG.info("Trying to select the network interface and address to use " +
			"by connecting to the leading JobManager.");

		LOG.info("TaskManager will try to connect for " + timeout +
			" before falling back to heuristics");

		return listener.findConnectingAddress(timeout);
	} catch (Exception e) {
		throw new LeaderRetrievalException("Could not find the connecting address by " +
			"connecting to the current leader.", e);
	} finally {
		try {
			leaderRetrievalService.stop();
		} catch (Exception fe) {
			LOG.warn("Could not stop the leader retrieval service.", fe);
		}
	}
}
 
Example 16
Source File: ZooKeeperHaServicesTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private void runCleanupTest(
		Configuration configuration,
		TestingBlobStoreService blobStoreService,
		ThrowingConsumer<ZooKeeperHaServices, Exception> zooKeeperHaServicesConsumer) throws Exception {
	try (ZooKeeperHaServices zooKeeperHaServices = new ZooKeeperHaServices(
		ZooKeeperUtils.startCuratorFramework(configuration),
		Executors.directExecutor(),
		configuration,
		blobStoreService)) {

		// create some Zk services to trigger the generation of paths
		final LeaderRetrievalService resourceManagerLeaderRetriever = zooKeeperHaServices.getResourceManagerLeaderRetriever();
		final LeaderElectionService resourceManagerLeaderElectionService = zooKeeperHaServices.getResourceManagerLeaderElectionService();
		final RunningJobsRegistry runningJobsRegistry = zooKeeperHaServices.getRunningJobsRegistry();

		final TestingListener listener = new TestingListener();
		resourceManagerLeaderRetriever.start(listener);
		resourceManagerLeaderElectionService.start(new TestingContender("foobar", resourceManagerLeaderElectionService));
		final JobID jobId = new JobID();
		runningJobsRegistry.setJobRunning(jobId);

		listener.waitForNewLeader(2000L);

		resourceManagerLeaderRetriever.stop();
		resourceManagerLeaderElectionService.stop();
		runningJobsRegistry.clearJob(jobId);

		zooKeeperHaServicesConsumer.accept(zooKeeperHaServices);
	}
}
 
Example 17
Source File: SingleLeaderElectionServiceTest.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
@Test
public void testShutdown() throws Exception {
	final UUID uuid = UUID.randomUUID();
	final SingleLeaderElectionService service = new SingleLeaderElectionService(executor, uuid);

	// create a leader contender and let it grab leadership
	final LeaderContender contender = mockContender(service);
	service.start(contender);
	verify(contender, times(1)).grantLeadership(uuid);

	// some leader listeners
	final LeaderRetrievalListener listener1 = mock(LeaderRetrievalListener.class);
	final LeaderRetrievalListener listener2 = mock(LeaderRetrievalListener.class);

	LeaderRetrievalService listenerService1 = service.createLeaderRetrievalService();
	LeaderRetrievalService listenerService2 = service.createLeaderRetrievalService();

	listenerService1.start(listener1);
	listenerService2.start(listener2);

	// one listener stops
	listenerService1.stop();

	// shut down the service
	service.shutdown();

	// the leader contender and running listener should get error notifications
	verify(contender, times(1)).handleError(any(Exception.class));
	verify(listener2, times(1)).handleError(any(Exception.class));

	// the stopped listener gets no notification
	verify(listener1, times(0)).handleError(any(Exception.class));

	// should not be possible to start again after shutdown
	try {
		service.start(contender);
		fail("should fail with an exception");
	} catch (IllegalStateException e) {
		// expected
	}

	// no additional leadership grant
	verify(contender, times(1)).grantLeadership(any(UUID.class));
}
 
Example 18
Source File: SingleLeaderElectionServiceTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testShutdown() throws Exception {
	final UUID uuid = UUID.randomUUID();
	final SingleLeaderElectionService service = new SingleLeaderElectionService(executor, uuid);

	// create a leader contender and let it grab leadership
	final LeaderContender contender = mockContender(service);
	service.start(contender);
	verify(contender, times(1)).grantLeadership(uuid);

	// some leader listeners
	final LeaderRetrievalListener listener1 = mock(LeaderRetrievalListener.class);
	final LeaderRetrievalListener listener2 = mock(LeaderRetrievalListener.class);

	LeaderRetrievalService listenerService1 = service.createLeaderRetrievalService();
	LeaderRetrievalService listenerService2 = service.createLeaderRetrievalService();

	listenerService1.start(listener1);
	listenerService2.start(listener2);

	// one listener stops
	listenerService1.stop();

	// shut down the service
	service.shutdown();

	// the leader contender and running listener should get error notifications
	verify(contender, times(1)).handleError(any(Exception.class));
	verify(listener2, times(1)).handleError(any(Exception.class));

	// the stopped listener gets no notification
	verify(listener1, times(0)).handleError(any(Exception.class));

	// should not be possible to start again after shutdown
	try {
		service.start(contender);
		fail("should fail with an exception");
	} catch (IllegalStateException e) {
		// expected
	}

	// no additional leadership grant
	verify(contender, times(1)).grantLeadership(any(UUID.class));
}