Broadcast

Updated at:

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 preProcess phase if the JavaProcessor is used.

  • Run business logic on each worker through the process method.

  • Aggregate execution results from all workers in the postProcess phase 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:

  1. preProcess -- Called once on each worker before the process method is called. Use this phase for resource initialization, such as preparing caches or database connections.

  2. process -- Called on all workers in the group. Each worker runs this method independently and returns a ProcessResult.

  3. postProcess -- Called once on each worker after all workers call the process method. Use this phase to aggregate results: context.getTaskResults() returns a map of each worker's output, and context.getTaskStatuses() returns a map of each worker's completion status. The postProcess return value can be ingested into workflows.

Note: Java jobs require JavaProcessor 1.0.7 or later.

JavaProcessor interface

MethodRequiredSignatureDescription
processYespublic ProcessResult process(JobContext context) throws ExceptionRuns on all workers. Returns each worker's result.
preProcessNopublic void preProcess(JobContext context)Runs once on each worker before process. Use for resource initialization.
postProcessNopublic 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:

MethodReturn typeDescription
getShardingNum()intTotal number of shards.
getShardingId()intIndex of the current worker.
getTaskId()longID 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));
    }

}