Broadcast
The broadcast execution model dispatches a single job to all workers in a group simultaneously. Each worker runs the job independently. The job succeeds only when every worker completes it; if any worker fails, the entire job fails.
Use broadcast when you need to run the same task on every node in a cluster, such as clearing caches, executing O&M scripts, or aggregating data.
Use cases
Batch O&M operations
Run a script on all workers at a scheduled time.
Clear the cache on all workers at a scheduled time.
Dynamically enable a service on all workers, collect the execution results from one worker, and update databases.
Data aggregation
Initialize Redis caches or databases in the
preProcessphase if the JavaProcessor is used.Run business logic on each worker through the
processmethod.Aggregate execution results from all workers in the
postProcessphase and update caches or databases.
How broadcast works
The broadcast model supports both script jobs and Java jobs. Java jobs provide two additional lifecycle methods -- preProcess and postProcess -- that enable resource setup and teardown around the main execution.
For Java jobs, the broadcast model follows a three-phase lifecycle:
preProcess-- Called once on each worker before theprocessmethod is called. Use this phase for resource initialization, such as preparing caches or database connections.process-- Called on all workers in the group. Each worker runs this method independently and returns aProcessResult.postProcess-- Called once on each worker after all workers call theprocessmethod. Use this phase to aggregate results:context.getTaskResults()returns a map of each worker's output, andcontext.getTaskStatuses()returns a map of each worker's completion status. ThepostProcessreturn value can be ingested into workflows.
Note: Java jobs require JavaProcessor 1.0.7 or later.
JavaProcessor interface
| Method | Required | Signature | Description |
|---|---|---|---|
process | Yes | public ProcessResult process(JobContext context) throws Exception | Runs on all workers. Returns each worker's result. |
preProcess | No | public void preProcess(JobContext context) | Runs once on each worker before process. Use for resource initialization. |
postProcess | No | public ProcessResult postProcess(JobContext context) | Runs once on each worker after all workers complete process. Use for result aggregation. |
JobContext methods
The JobContext object provides the following methods for broadcast jobs:
| Method | Return type | Description |
|---|---|---|
getShardingNum() | int | Total number of shards. |
getShardingId() | int | Index of the current worker. |
getTaskId() | long | ID of the current task. |
getTaskResults() | Map<Long, String> | Results from all workers. Available in postProcess. |
getTaskStatuses() | Map<Long, TaskStatus> | Completion status of each worker. Available in postProcess. |
Example: aggregate values from all workers
This broadcast job generates a random value on each worker and sums the results of successful workers in postProcess.
@Component
public class TestBroadcastJob extends JavaProcessor {
/**
* Only one worker calls the method.
*/
@Override
public void preProcess(JobContext context) {
System.out.println("TestBroadcastJob.preProcess");
}
/**
* All workers call the method.
*/
@Override
public ProcessResult process(JobContext context) throws Exception {
int value = new Random().nextInt(10);
System.out.println("Total number of shards=" + context.getShardingNum() + ", Shard index=" + context.getShardingId() + ", "
+ "taskId=" + context.getTaskId() + ", value=" + value);
return new ProcessResult(true, String.valueOf(value));
}
/**
* Only one worker calls the method.
*/
@Override
public ProcessResult postProcess(JobContext context) {
System.out.println("TestBroadcastJob.postProcess");
Map<Long, String> allTaskResults = context.getTaskResults();
Map<Long, TaskStatus> allTaskStatuses = context.getTaskStatuses();
int num = 0;
for (Entry<Long, String> entry : allTaskResults.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
if (allTaskStatuses.get(entry.getKey()).equals(TaskStatus.SUCCESS)) {
num += Integer.valueOf(entry.getValue());
}
}
System.out.println("TestBroadcastJob.postProcess(), num=" + num);
return new ProcessResult(true, String.valueOf(num));
}
}