A Go implementation of a distributed task runtime for a cluster of one coordinator and multiple workers. It provides worker discovery and heartbeats, capacity-aware pull scheduling, leased task execution, retries, map/reduce jobs, checkpointed state, coordinator gossip, and Raft-backed high availability.
- Dynamic worker registration, heartbeats, and graceful deregistration
- Worker failure detection (
ALIVE,SUSPECT, andDEAD) - Capacity-aware task assignment using CPU and memory reservations
- Worker-affinity preferences and bounded queue backpressure
- Pull-based scheduling with 100 ms worker polling
- Lease-based task reassignment and configurable retry limits
- Protection against stale or duplicate task completions
- Dependency-gated tasks and distributed MapReduce-style jobs
- Atomic file checkpoints and optional synchronous mirrored checkpoints
- SWIM-style coordinator gossip discovery
- Raft-backed scheduler state and leader-only mutations
flowchart LR
C[ctl operator client] -->|gRPC| COORD[Coordinator]
W1[Worker 1] -->|Register, heartbeat, pull task| COORD
W2[Worker 2] -->|Register, heartbeat, pull task| COORD
W3[Worker 3] -->|Register, heartbeat, pull task| COORD
COORD --> REG[Worker registry]
COORD --> SCH[Capacity-aware scheduler]
SCH --> TASKS[(Tasks, jobs, leases)]
SCH --> STORE[(File / replicated / Raft store)]
sequenceDiagram
participant CTL as ctl
participant Coord as Coordinator
participant S as Scheduler
participant Worker
CTL->>Coord: SubmitTask or SubmitMapReduce
Coord->>S: Validate and enqueue
S-->>Coord: Task/job ID
Coord-->>CTL: Task/job ID
Worker->>Coord: RequestTask(worker ID)
Coord->>S: Assign by capacity and affinity
S-->>Coord: Leased task
Coord-->>Worker: Operation and numbers
Worker->>Coord: CompleteTask(result/error)
Coord->>S: Validate worker and lease
S-->>Coord: Completed or failed
flowchart TD
subgraph Coordinator process
GRPC[gRPC service]
REG[Membership registry]
FD[Failure detector]
SCH[Scheduler]
STORE[Store abstraction]
GRPC --> REG
GRPC --> SCH
FD --> REG
FD --> SCH
SCH --> STORE
end
subgraph Worker process
RPC[gRPC client]
HB[Heartbeat loop]
POLL[Task poll loop]
EXEC[Task executor]
RPC --> HB
RPC --> POLL
POLL --> EXEC
end
- Workers send heartbeats every 3 seconds.
- A worker becomes
SUSPECTafter 5 seconds without a heartbeat. - A worker becomes
DEADafter 10 seconds. - Every assigned task has a 15-second lease.
- If a worker dies or a lease expires, the task is requeued until the configured maximum of 3 attempts is reached.
- Completion requests must come from the worker currently holding the task lease. A stale completion for another worker is rejected.
- At startup from a checkpoint, tasks that were running are reset to
PENDINGand requeued.
| Area | Technology |
|---|---|
| Language | Go 1.26 |
| RPC | gRPC and Protocol Buffers |
| Membership | HashiCorp Memberlist / SWIM-style gossip |
| Consensus | HashiCorp Raft |
| Raft storage | BoltDB-backed Raft logs and snapshots |
| State persistence | JSON file checkpoints or Raft state machine |
| Local concurrency | Go mutexes and condition-free pull polling |
| Testing | Go testing package |
The Protocol Buffer contract is in proto/membership.proto. Generated Go code
is stored in gen/membership/v1/.
.
├── cmd/
│ ├── coordinator/ Coordinator executable
│ ├── worker/ Worker executable
│ └── ctl/ Command-line operator client
├── gen/membership/v1/ Generated gRPC and protobuf Go code
├── internal/
│ ├── coordinator/ gRPC service and failure detector
│ ├── coordination/ In-memory leader lease primitive
│ ├── gossip/ Memberlist coordinator discovery
│ ├── ha/ Raft store and snapshot state machine
│ ├── membership/ Worker registry and status tracking
│ ├── scheduler/ Tasks, jobs, leases, capacity, and persistence
│ └── worker/ Worker registration, heartbeat, and execution client
├── proto/
│ └── membership.proto Public gRPC API definition
├── go.mod
├── go.sum
└── plan.txt Original implementation roadmap
The worker supports these operations:
| Operation | Behavior |
|---|---|
SUM |
Sums the supplied numbers |
MAP_IDENTITY |
Sums the values without transformation |
MAP_SQUARE |
Sums the squares of the supplied values |
REDUCE_SUM |
Sums dependency results produced by map tasks |
A task may declare:
- Required CPU cores
- Required memory in MB
- Preferred worker IDs for data locality or affinity
- A list of task dependencies
MapReduce submissions partition the input into map tasks and automatically
add a REDUCE_SUM task. The reducer is blocked until every map task completes.
- Go 1.26 or later
- Git
- Windows PowerShell or a compatible shell
Verify the toolchain:
go versionFrom the repository root:
go mod download
go mod tidyGo dependencies are declared in go.mod; no external database is required for
the default runtime.
Start one coordinator:
go run ./cmd/coordinatorThe coordinator listens on :50051.
In separate terminals, start one or more workers:
go run ./cmd/worker worker-1
go run ./cmd/worker worker-2
go run ./cmd/worker worker-3Each worker in the current executable registers with 4 CPU cores and 4096 MB of memory.
List workers and current reserved capacity:
go run ./cmd/ctl workersSubmit independent sums:
go run ./cmd/ctl submit-sum 10 20 30Submit a map/reduce job:
go run ./cmd/ctl submit-mapreduce 3 MAP_SQUARE 1 2 3 4 5 6The arguments are <partition-count> <MAP_IDENTITY|MAP_SQUARE> <numbers...>.
The command prints a job ID. Inspect it with:
go run ./cmd/ctl job <job-id>Use another coordinator address with:
go run ./cmd/ctl -address localhost:50051 workersSet DCR_STATE_PATH before starting the coordinator:
$env:DCR_STATE_PATH = ".\state\scheduler.json"
go run ./cmd/coordinatorEvery scheduler mutation attempts to replace the JSON checkpoint atomically. Restarts reload the checkpoint and requeue interrupted running tasks.
Set a second path to synchronously mirror each snapshot:
$env:DCR_STATE_PATH = ".\state\primary\scheduler.json"
$env:DCR_REPLICA_STATE_PATH = ".\state\replica\scheduler.json"
go run ./cmd/coordinatorOn startup, DCR_STATE_PATH is tried first, then the replica. This is a
two-copy durability mechanism, not a Raft quorum.
The HA mode requires a stable Raft node ID, a TCP transport address, and a dedicated data directory for each coordinator.
Example first-node setup:
$env:DCR_RAFT_NODE_ID = "coordinator-1"
$env:DCR_RAFT_BIND = "127.0.0.1:7001"
$env:DCR_RAFT_DATA_DIR = ".\raft-1"
$env:DCR_RAFT_BOOTSTRAP = "true"
go run ./cmd/coordinatorStart a second coordinator with a separate gRPC port. The executable currently
always listens on :50051, so host-level port mapping or separate machines
must be used for additional coordinator processes.
Optional gossip configuration associates coordinator peers:
$env:DCR_GOSSIP_NAME = "coordinator-1"
$env:DCR_GOSSIP_BIND = "127.0.0.1:7946"
$env:DCR_GOSSIP_JOIN = "127.0.0.1:7947"Gossip is used for peer discovery. Raft is responsible for the elected
coordinator and committed scheduler state. Mutating gRPC requests are rejected
on followers with an Unavailable response.
Do not set DCR_STATE_PATH and DCR_RAFT_NODE_ID together. The coordinator
exits if both modes are configured.
Run the complete unit test suite:
go test ./...Run a package or test repeatedly while developing:
go test ./internal/scheduler
go test ./internal/ha
go test ./internal/coordinator -run Test -count=1The public gRPC service is membership.MembershipService:
RegisterHeartbeatDeregisterListWorkersSubmitTaskRequestTaskCompleteTaskSubmitMapReduceGetJob
The protobuf contract and generated gRPC client/server code live in
proto/membership.proto and gen/membership/v1/.