Flink Streaming
Job Management
Programming
Troubleshooting
Software Development

How to stop a flink streaming job from program

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Apache Flink is a powerful framework used for both batch and stream processing, capable of handling stateful computations on data streams. Sometimes, while working with streaming data, it becomes necessary to programmatically stop a Flink job. This could be due to various reasons such as a logic trigger reached within the data, resource optimization, or a need to upgrade the processing pipeline. This article explains the methods to stop a Flink streaming job programmatically, providing technical explanations and examples.

Flink’s management of jobs is centralized around the JobManager, which coordinates and superviles tasks execution managed by TaskManagers. To interact and manage jobs, Flink provides a REST API, CLI, and a Web UI, where stopping a job is a fundamental operation.

Method 1: Using the REST API

The Flink REST API provides endpoints for managing jobs. To stop a job, you can send a request to the job's cancellation endpoint. Here’s how you can do it in a program:

python
1import requests
2
3def stop_flink_job(job_id, flink_host='localhost', port=8081):
4    """
5    Stop a Flink job using the Flink REST API.
6
7    Args:
8    job_id (str): The Flink job ID.
9    flink_host (str, optional): The host on which Flink's JobManager runs.
10    port (int, optional): The port on which Flink's REST API is available.
11    """
12    url = f"http://{flink_host}:{port}/jobs/{job_id}"
13    response = requests.patch(f"{url}/yarn-cancel")
14    if response.status_code == 200:
15        print("Job stopped successfully")
16    else:
17        print("Failed to stop job")

This function utilizes the PATCH method on the /jobs/{jobId}/yarn-cancel endpoint, which is commonly used when Flink runs on YARN. Note that the specific endpoint might vary based on the setup (like standalone or Mesos), and security features might require additional headers or authentication methods.

Method 2: Triggering a Cancellation from within a Job

Flink doesn't support directly stopping a job from within its own execution context, due to the risk of inconsistency and partial state updates. However, you can achieve a controlled shutdown by implementing a custom mechanism:

  1. Control Source: Use a Broadcast stream that sends control messages (e.g., STOP) which can be read by all operations.
  2. Custom Operator: Implement a ProcessFunction that listens to these control messages and leverages the GlobalJobParameters to initiate a shutdown.

Here is a conceptual example using the ControlSource:

java
DataStream<String> control = env.addSource(new ControlSource());
control.broadcast().connect(dataStream).process(new StopFunction());

And the StopFunction might look like this:

java
1public static class StopFunction extends CoProcessFunction<String, ControlMessage, String> {
2    @Override
3    public void processElement1(String value, Context ctx, Collector<String> out) throws Exception {
4        out.collect(value);
5    }
6
7    @Override
8    public void processElement2(ControlMessage value, Context ctx, Collector<String> out) throws Exception {
9        if (value.getMessage().equals("STOP")) {
10            // Logic to initiate a savepoint and stop the job
11        }
12    }
13}

Flink’s command-line interface also provides the ability to stop a job. You can execute this command from within your program using system calls:

bash
flink cancel <job_id>

In Python, you could use:

python
import subprocess
subprocess.run(["flink", "cancel", "job_id"])

Key Points Summary

MethodUse CaseProsCons
REST APIRemote job managementProgrammatic access, precise job controlNeeds setup of REST API, security considerations
Internal Trigger (within the job)Controlled shutdown on specific conditionsGranular control over job logicComplex implementation, unofficial method
CLIExternal scripts or system callsEasy to use, direct command-line accessRequires command-line access, less granularity

Conclusion

Stopping a Flink job programmatically can be achieved through various methods, each with its own advantages and use cases. The choice depends largely on the precise needs of the application, such as whether there is access to Flink’s REST API, the need for precision in controlling the job's lifecycle, or simplicity and directness of using CLI commands. Proper understanding and testing of the chosen method are essential to ensure job integrity and data consistency.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.