Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

7 Commits

Folders and files

Repository files navigation

Distributed Computing Runtime

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.

Features

  • Dynamic worker registration, heartbeats, and graceful deregistration
  • Worker failure detection (ALIVE, SUSPECT, and DEAD)
  • 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

Architecture

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)]
Loading

Request flow

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
Loading

Runtime components

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
Loading

Failure and recovery model

  • Workers send heartbeats every 3 seconds.
  • A worker becomes SUSPECT after 5 seconds without a heartbeat.
  • A worker becomes DEAD after 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 PENDING and requeued.

Tech Stack

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/.

Project Layout

.
├── 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

Primitives and Operations

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.

Setup

Prerequisites

  • Go 1.26 or later
  • Git
  • Windows PowerShell or a compatible shell

Verify the toolchain:

go version

Install dependencies

From the repository root:

go mod download
go mod tidy

Go dependencies are declared in go.mod; no external database is required for the default runtime.

Run locally

Start one coordinator:

go run ./cmd/coordinator

The 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-3

Each worker in the current executable registers with 4 CPU cores and 4096 MB of memory.

Inspect and submit work

List workers and current reserved capacity:

go run ./cmd/ctl workers

Submit independent sums:

go run ./cmd/ctl submit-sum 10 20 30

Submit a map/reduce job:

go run ./cmd/ctl submit-mapreduce 3 MAP_SQUARE 1 2 3 4 5 6

The 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 workers

Persistence Modes

Single-node file checkpoint

Set DCR_STATE_PATH before starting the coordinator:

$env:DCR_STATE_PATH = ".\state\scheduler.json"
go run ./cmd/coordinator

Every scheduler mutation attempts to replace the JSON checkpoint atomically. Restarts reload the checkpoint and requeue interrupted running tasks.

Mirrored file checkpoint

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/coordinator

On startup, DCR_STATE_PATH is tried first, then the replica. This is a two-copy durability mechanism, not a Raft quorum.

Raft High Availability

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/coordinator

Start 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.

Testing

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=1

API

The public gRPC service is membership.MembershipService:

  • Register
  • Heartbeat
  • Deregister
  • ListWorkers
  • SubmitTask
  • RequestTask
  • CompleteTask
  • SubmitMapReduce
  • GetJob

The protobuf contract and generated gRPC client/server code live in proto/membership.proto and gen/membership/v1/.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages