Java Code Examples for org.apache.flink.util.ShutdownHookUtil#addShutdownHook()

The following examples show how to use org.apache.flink.util.ShutdownHookUtil#addShutdownHook() . 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: FileChannelManagerImplTest.java    From flink with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception{
	boolean callerHasHook = Boolean.parseBoolean(args[0]);
	String tmpDirectory = args[1];
	String signalFilePath = args[2];

	FileChannelManager manager = new FileChannelManagerImpl(new String[]{tmpDirectory}, DIR_NAME_PREFIX);

	if (callerHasHook) {
		// Verifies the case that both FileChannelManager and its upper component
		// have registered shutdown hooks, like in IOManager.
		ShutdownHookUtil.addShutdownHook(() -> manager.close(), "Caller", LOG);
	}

	// Signals the main process to execute the kill action.
	new File(signalFilePath).createNewFile();

	// Blocks the process to wait to be killed.
	Thread.sleep(3 * TEST_TIMEOUT.toMillis());
}
 
Example 2
Source File: TaskExecutorLocalStateStoresManager.java    From flink with Apache License 2.0 6 votes vote down vote up
public TaskExecutorLocalStateStoresManager(
	boolean localRecoveryEnabled,
	@Nonnull File[] localStateRootDirectories,
	@Nonnull Executor discardExecutor) throws IOException {

	this.taskStateStoresByAllocationID = new HashMap<>();
	this.localRecoveryEnabled = localRecoveryEnabled;
	this.localStateRootDirectories = localStateRootDirectories;
	this.discardExecutor = discardExecutor;
	this.lock = new Object();
	this.closed = false;

	for (File localStateRecoveryRootDir : localStateRootDirectories) {

		if (!localStateRecoveryRootDir.exists()
			&& !localStateRecoveryRootDir.mkdirs()
			// we double check for exists in case another task created the directory concurrently.
			&& !localStateRecoveryRootDir.exists()) {
			throw new IOException("Could not create root directory for local recovery: " +
				localStateRecoveryRootDir);
		}
	}

	// register a shutdown hook
	this.shutdownHook = ShutdownHookUtil.addShutdownHook(this::shutdown, getClass().getSimpleName(), LOG);
}
 
Example 3
Source File: AbstractBlobCache.java    From flink with Apache License 2.0 5 votes vote down vote up
public AbstractBlobCache(
		final Configuration blobClientConfig,
		final BlobView blobView,
		final Logger logger,
		@Nullable final InetSocketAddress serverAddress) throws IOException {

	this.log = checkNotNull(logger);
	this.blobClientConfig = checkNotNull(blobClientConfig);
	this.blobView = checkNotNull(blobView);
	this.readWriteLock = new ReentrantReadWriteLock();

	// configure and create the storage directory
	this.storageDir = BlobUtils.initLocalStorageDirectory(blobClientConfig);
	log.info("Created BLOB cache storage directory " + storageDir);

	// configure the number of fetch retries
	final int fetchRetries = blobClientConfig.getInteger(BlobServerOptions.FETCH_RETRIES);
	if (fetchRetries >= 0) {
		this.numFetchRetries = fetchRetries;
	} else {
		log.warn("Invalid value for {}. System will attempt no retries on failed fetch operations of BLOBs.",
			BlobServerOptions.FETCH_RETRIES.key());
		this.numFetchRetries = 0;
	}

	// Add shutdown hook to delete storage directory
	shutdownHook = ShutdownHookUtil.addShutdownHook(this, getClass().getSimpleName(), log);

	this.serverAddress = serverAddress;
}
 
Example 4
Source File: ContextEnvironment.java    From flink with Apache License 2.0 5 votes vote down vote up
private JobExecutionResult getJobExecutionResult(final JobClient jobClient) throws Exception {
	checkNotNull(jobClient);

	JobExecutionResult jobExecutionResult;
	if (getConfiguration().getBoolean(DeploymentOptions.ATTACHED)) {
		CompletableFuture<JobExecutionResult> jobExecutionResultFuture =
				jobClient.getJobExecutionResult(getUserCodeClassLoader());

		if (getConfiguration().getBoolean(DeploymentOptions.SHUTDOWN_IF_ATTACHED)) {
			Thread shutdownHook = ShutdownHookUtil.addShutdownHook(
					() -> {
						// wait a smidgen to allow the async request to go through before
						// the jvm exits
						jobClient.cancel().get(1, TimeUnit.SECONDS);
					},
					ContextEnvironment.class.getSimpleName(),
					LOG);
			jobExecutionResultFuture.whenComplete((ignored, throwable) ->
					ShutdownHookUtil.removeShutdownHook(
						shutdownHook, ContextEnvironment.class.getSimpleName(), LOG));
		}

		jobExecutionResult = jobExecutionResultFuture.get();
		System.out.println(jobExecutionResult);
	} else {
		jobExecutionResult = new DetachedJobExecutionResult(jobClient.getJobID());
	}

	return jobExecutionResult;
}
 
Example 5
Source File: AbstractBlobCache.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
public AbstractBlobCache(
		final Configuration blobClientConfig,
		final BlobView blobView,
		final Logger logger,
		@Nullable final InetSocketAddress serverAddress) throws IOException {

	this.log = checkNotNull(logger);
	this.blobClientConfig = checkNotNull(blobClientConfig);
	this.blobView = checkNotNull(blobView);
	this.readWriteLock = new ReentrantReadWriteLock();

	// configure and create the storage directory
	this.storageDir = BlobUtils.initLocalStorageDirectory(blobClientConfig);
	log.info("Created BLOB cache storage directory " + storageDir);

	// configure the number of fetch retries
	final int fetchRetries = blobClientConfig.getInteger(BlobServerOptions.FETCH_RETRIES);
	if (fetchRetries >= 0) {
		this.numFetchRetries = fetchRetries;
	} else {
		log.warn("Invalid value for {}. System will attempt no retries on failed fetch operations of BLOBs.",
			BlobServerOptions.FETCH_RETRIES.key());
		this.numFetchRetries = 0;
	}

	// Add shutdown hook to delete storage directory
	shutdownHook = ShutdownHookUtil.addShutdownHook(this, getClass().getSimpleName(), log);

	this.serverAddress = serverAddress;
}
 
Example 6
Source File: FileCache.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Thread createShutdownHook(final FileCache cache, final Logger logger) {

		return ShutdownHookUtil.addShutdownHook(
			cache::shutdown,
			FileCache.class.getSimpleName(),
			logger
		);
	}
 
Example 7
Source File: FileCache.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
private static Thread createShutdownHook(final FileCache cache, final Logger logger) {

		return ShutdownHookUtil.addShutdownHook(
			cache::shutdown,
			FileCache.class.getSimpleName(),
			logger
		);
	}
 
Example 8
Source File: StreamContextEnvironment.java    From flink with Apache License 2.0 5 votes vote down vote up
private JobExecutionResult getJobExecutionResult(final JobClient jobClient) throws Exception {
	checkNotNull(jobClient);

	JobExecutionResult jobExecutionResult;
	if (getConfiguration().getBoolean(DeploymentOptions.ATTACHED)) {
		CompletableFuture<JobExecutionResult> jobExecutionResultFuture =
				jobClient.getJobExecutionResult(getUserClassloader());

		if (getConfiguration().getBoolean(DeploymentOptions.SHUTDOWN_IF_ATTACHED)) {
			Thread shutdownHook = ShutdownHookUtil.addShutdownHook(
					() -> {
						// wait a smidgen to allow the async request to go through before
						// the jvm exits
						jobClient.cancel().get(1, TimeUnit.SECONDS);
					},
					StreamContextEnvironment.class.getSimpleName(),
					LOG);
			jobExecutionResultFuture.whenComplete((ignored, throwable) ->
					ShutdownHookUtil.removeShutdownHook(
						shutdownHook, StreamContextEnvironment.class.getSimpleName(), LOG));
		}

		jobExecutionResult = jobExecutionResultFuture.get();
		System.out.println(jobExecutionResult);
	} else {
		jobExecutionResult = new DetachedJobExecutionResult(jobClient.getJobID());
	}

	return jobExecutionResult;
}
 
Example 9
Source File: ProcessPythonEnvironmentManager.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void open() throws Exception {
	baseDirectory = createBaseDirectory(tmpDirectories);
	archivesDirectory = String.join(File.separator, baseDirectory, PYTHON_ARCHIVES_DIR);
	requirementsDirectory = String.join(File.separator, baseDirectory, PYTHON_REQUIREMENTS_DIR);
	filesDirectory = String.join(File.separator, baseDirectory, PYTHON_FILES_DIR);

	File baseDirectoryFile = new File(baseDirectory);
	if (!baseDirectoryFile.exists() && !baseDirectoryFile.mkdir()) {
		throw new IOException(
			"Could not create the base directory: " + baseDirectory);
	}
	shutdownHook = ShutdownHookUtil.addShutdownHook(
		this, ProcessPythonEnvironmentManager.class.getSimpleName(), LOG);
}
 
Example 10
Source File: AbstractBlobCache.java    From flink with Apache License 2.0 5 votes vote down vote up
public AbstractBlobCache(
		final Configuration blobClientConfig,
		final BlobView blobView,
		final Logger logger,
		@Nullable final InetSocketAddress serverAddress) throws IOException {

	this.log = checkNotNull(logger);
	this.blobClientConfig = checkNotNull(blobClientConfig);
	this.blobView = checkNotNull(blobView);
	this.readWriteLock = new ReentrantReadWriteLock();

	// configure and create the storage directory
	this.storageDir = BlobUtils.initLocalStorageDirectory(blobClientConfig);
	log.info("Created BLOB cache storage directory " + storageDir);

	// configure the number of fetch retries
	final int fetchRetries = blobClientConfig.getInteger(BlobServerOptions.FETCH_RETRIES);
	if (fetchRetries >= 0) {
		this.numFetchRetries = fetchRetries;
	} else {
		log.warn("Invalid value for {}. System will attempt no retries on failed fetch operations of BLOBs.",
			BlobServerOptions.FETCH_RETRIES.key());
		this.numFetchRetries = 0;
	}

	// Add shutdown hook to delete storage directory
	shutdownHook = ShutdownHookUtil.addShutdownHook(this, getClass().getSimpleName(), log);

	this.serverAddress = serverAddress;
}
 
Example 11
Source File: FileCache.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Thread createShutdownHook(final FileCache cache, final Logger logger) {

		return ShutdownHookUtil.addShutdownHook(
			cache::shutdown,
			FileCache.class.getSimpleName(),
			logger
		);
	}
 
Example 12
Source File: BlobServer.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiates a new BLOB server and binds it to a free network port.
 *
 * @param config Configuration to be used to instantiate the BlobServer
 * @param blobStore BlobStore to store blobs persistently
 *
 * @throws IOException
 * 		thrown if the BLOB server cannot bind to a free network port or if the
 * 		(local or distributed) file storage cannot be created or is not usable
 */
public BlobServer(Configuration config, BlobStore blobStore) throws IOException {
	this.blobServiceConfiguration = checkNotNull(config);
	this.blobStore = checkNotNull(blobStore);
	this.readWriteLock = new ReentrantReadWriteLock();

	// configure and create the storage directory
	this.storageDir = BlobUtils.initLocalStorageDirectory(config);
	LOG.info("Created BLOB server storage directory {}", storageDir);

	// configure the maximum number of concurrent connections
	final int maxConnections = config.getInteger(BlobServerOptions.FETCH_CONCURRENT);
	if (maxConnections >= 1) {
		this.maxConnections = maxConnections;
	}
	else {
		LOG.warn("Invalid value for maximum connections in BLOB server: {}. Using default value of {}",
				maxConnections, BlobServerOptions.FETCH_CONCURRENT.defaultValue());
		this.maxConnections = BlobServerOptions.FETCH_CONCURRENT.defaultValue();
	}

	// configure the backlog of connections
	int backlog = config.getInteger(BlobServerOptions.FETCH_BACKLOG);
	if (backlog < 1) {
		LOG.warn("Invalid value for BLOB connection backlog: {}. Using default value of {}",
				backlog, BlobServerOptions.FETCH_BACKLOG.defaultValue());
		backlog = BlobServerOptions.FETCH_BACKLOG.defaultValue();
	}

	// Initializing the clean up task
	this.cleanupTimer = new Timer(true);

	this.cleanupInterval = config.getLong(BlobServerOptions.CLEANUP_INTERVAL) * 1000;
	this.cleanupTimer
		.schedule(new TransientBlobCleanupTask(blobExpiryTimes, readWriteLock.writeLock(),
			storageDir, LOG), cleanupInterval, cleanupInterval);

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

	//  ----------------------- start the server -------------------

	final String serverPortRange = config.getString(BlobServerOptions.PORT);
	final Iterator<Integer> ports = NetUtils.getPortRangeFromString(serverPortRange);

	final ServerSocketFactory socketFactory;
	if (SSLUtils.isInternalSSLEnabled(config) && config.getBoolean(BlobServerOptions.SSL_ENABLED)) {
		try {
			socketFactory = SSLUtils.createSSLServerSocketFactory(config);
		}
		catch (Exception e) {
			throw new IOException("Failed to initialize SSL for the blob server", e);
		}
	}
	else {
		socketFactory = ServerSocketFactory.getDefault();
	}

	final int finalBacklog = backlog;
	final String bindHost = config.getOptional(JobManagerOptions.BIND_HOST).orElseGet(NetUtils::getWildcardIPAddress);

	this.serverSocket = NetUtils.createSocketFromPorts(ports,
			(port) -> socketFactory.createServerSocket(port, finalBacklog, InetAddress.getByName(bindHost)));

	if (serverSocket == null) {
		throw new IOException("Unable to open BLOB Server in specified port range: " + serverPortRange);
	}

	// start the server thread
	setName("BLOB Server listener at " + getPort());
	setDaemon(true);

	if (LOG.isInfoEnabled()) {
		LOG.info("Started BLOB server at {}:{} - max concurrent requests: {} - max backlog: {}",
				serverSocket.getInetAddress().getHostAddress(), getPort(), maxConnections, backlog);
	}
}
 
Example 13
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 14
Source File: ClusterEntrypoint.java    From flink with Apache License 2.0 4 votes vote down vote up
protected ClusterEntrypoint(Configuration configuration) {
	this.configuration = generateClusterConfiguration(configuration);
	this.terminationFuture = new CompletableFuture<>();

	shutDownHook = ShutdownHookUtil.addShutdownHook(this::cleanupDirectories, getClass().getSimpleName(), LOG);
}
 
Example 15
Source File: BlobServer.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiates a new BLOB server and binds it to a free network port.
 *
 * @param config Configuration to be used to instantiate the BlobServer
 * @param blobStore BlobStore to store blobs persistently
 *
 * @throws IOException
 * 		thrown if the BLOB server cannot bind to a free network port or if the
 * 		(local or distributed) file storage cannot be created or is not usable
 */
public BlobServer(Configuration config, BlobStore blobStore) throws IOException {
	this.blobServiceConfiguration = checkNotNull(config);
	this.blobStore = checkNotNull(blobStore);
	this.readWriteLock = new ReentrantReadWriteLock();

	// configure and create the storage directory
	this.storageDir = BlobUtils.initLocalStorageDirectory(config);
	LOG.info("Created BLOB server storage directory {}", storageDir);

	// configure the maximum number of concurrent connections
	final int maxConnections = config.getInteger(BlobServerOptions.FETCH_CONCURRENT);
	if (maxConnections >= 1) {
		this.maxConnections = maxConnections;
	}
	else {
		LOG.warn("Invalid value for maximum connections in BLOB server: {}. Using default value of {}",
				maxConnections, BlobServerOptions.FETCH_CONCURRENT.defaultValue());
		this.maxConnections = BlobServerOptions.FETCH_CONCURRENT.defaultValue();
	}

	// configure the backlog of connections
	int backlog = config.getInteger(BlobServerOptions.FETCH_BACKLOG);
	if (backlog < 1) {
		LOG.warn("Invalid value for BLOB connection backlog: {}. Using default value of {}",
				backlog, BlobServerOptions.FETCH_BACKLOG.defaultValue());
		backlog = BlobServerOptions.FETCH_BACKLOG.defaultValue();
	}

	// Initializing the clean up task
	this.cleanupTimer = new Timer(true);

	this.cleanupInterval = config.getLong(BlobServerOptions.CLEANUP_INTERVAL) * 1000;
	this.cleanupTimer
		.schedule(new TransientBlobCleanupTask(blobExpiryTimes, readWriteLock.writeLock(),
			storageDir, LOG), cleanupInterval, cleanupInterval);

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

	//  ----------------------- start the server -------------------

	final String serverPortRange = config.getString(BlobServerOptions.PORT);
	final Iterator<Integer> ports = NetUtils.getPortRangeFromString(serverPortRange);

	final ServerSocketFactory socketFactory;
	if (SSLUtils.isInternalSSLEnabled(config) && config.getBoolean(BlobServerOptions.SSL_ENABLED)) {
		try {
			socketFactory = SSLUtils.createSSLServerSocketFactory(config);
		}
		catch (Exception e) {
			throw new IOException("Failed to initialize SSL for the blob server", e);
		}
	}
	else {
		socketFactory = ServerSocketFactory.getDefault();
	}

	final int finalBacklog = backlog;
	this.serverSocket = NetUtils.createSocketFromPorts(ports,
			(port) -> socketFactory.createServerSocket(port, finalBacklog));

	if (serverSocket == null) {
		throw new IOException("Unable to open BLOB Server in specified port range: " + serverPortRange);
	}

	// start the server thread
	setName("BLOB Server listener at " + getPort());
	setDaemon(true);

	if (LOG.isInfoEnabled()) {
		LOG.info("Started BLOB server at {}:{} - max concurrent requests: {} - max backlog: {}",
				serverSocket.getInetAddress().getHostAddress(), getPort(), maxConnections, backlog);
	}
}
 
Example 16
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 17
Source File: RemoteEnvironment.java    From flink with Apache License 2.0 4 votes vote down vote up
private void installShutdownHook() {
	if (shutdownHook == null) {
		this.shutdownHook = ShutdownHookUtil.addShutdownHook(this::dispose, getClass().getSimpleName(), LOG);
	}
}
 
Example 18
Source File: ClusterEntrypoint.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
protected ClusterEntrypoint(Configuration configuration) {
	this.configuration = generateClusterConfiguration(configuration);
	this.terminationFuture = new CompletableFuture<>();

	shutDownHook = ShutdownHookUtil.addShutdownHook(this::cleanupDirectories, getClass().getSimpleName(), LOG);
}
 
Example 19
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 20
Source File: PythonStreamer.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
private void startPython() throws IOException {
	String tmpDir = config.getString(PythonOptions.DATA_TMP_DIR);
	if (tmpDir == null) {
		tmpDir = System.getProperty("java.io.tmpdir");
	}
	File outputFile = new File(tmpDir, envID + "_" + setID + this.function.getRuntimeContext().getIndexOfThisSubtask() + "_output");
	File inputFile = new File(tmpDir, envID + "_" + setID + this.function.getRuntimeContext().getIndexOfThisSubtask() + "_input)");

	sender.open(inputFile);
	receiver.open(outputFile);

	String path = function.getRuntimeContext().getDistributedCache().getFile(FLINK_PYTHON_DC_ID).getAbsolutePath();

	String planPath = path + FLINK_PYTHON_PLAN_NAME;

	String pythonBinaryPath = config.getString(PythonOptions.PYTHON_BINARY_PATH);

	String arguments = config.getString(PLAN_ARGUMENTS_KEY, "");
	process = Runtime.getRuntime().exec(pythonBinaryPath + " -O -B " + planPath + arguments);
	outPrinter = new Thread(new StreamPrinter(process.getInputStream()));
	outPrinter.start();
	errorPrinter = new Thread(new StreamPrinter(process.getErrorStream(), msg));
	errorPrinter.start();

	shutdownThread = ShutdownHookUtil.addShutdownHook(
		() -> destroyProcess(process),
		getClass().getSimpleName(),
		LOG);

	OutputStream processOutput = process.getOutputStream();
	processOutput.write("operator\n".getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write((envID + "\n").getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write((setID + "\n").getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write(("" + server.getLocalPort() + "\n").getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write((this.function.getRuntimeContext().getIndexOfThisSubtask() + "\n")
		.getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write(((config.getLong(PythonOptions.MMAP_FILE_SIZE) << 10) + "\n").getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write((inputFile + "\n").getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.write((outputFile + "\n").getBytes(ConfigConstants.DEFAULT_CHARSET));
	processOutput.flush();

	while (true) {
		try {
			socket = server.accept();
			break;
		} catch (SocketTimeoutException ignored) {
			checkPythonProcessHealth();
		}
	}
	in = new DataInputStream(socket.getInputStream());
	out = new DataOutputStream(socket.getOutputStream());
}