# cmiyai/3phasecommit architecture

> Architecture diagram of the GitHub repository cmiyai/3phasecommit, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/cmiyai/3phasecommit
- Repository: https://github.com/cmiyai/3phasecommit

## Overview

This repository implements three-phase commit for transactions spanning participant servers. A caller stages reads and writes on the relevant servers, then asks the coordinator to finish the transaction; the coordinator gathers votes, advances participants through pre-commit and commit, or aborts, and reports commit status and read values. Participants hold transaction state and key-value data. Coordinator recovery queries participant states and resumes or rolls back incomplete work. The repository includes a simulated RPC transport and Gob wrapper; config.go is tester support, not part of the runtime map. Relationships for protocol phases are documented in the README; source excerpts show coordinator RPC calls and participant handlers.

Main entry points: [`coordinator.go`](https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go) (Coordinator), [`server.go`](https://github.com/cmiyai/3phasecommit/blob/main/server.go) (Participant handlers), [`labrpc/labrpc.go`](https://github.com/cmiyai/3phasecommit/blob/main/labrpc/labrpc.go) (RPC network), [`labgob/labgob.go`](https://github.com/cmiyai/3phasecommit/blob/main/labgob/labgob.go) (Gob codec)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_coordination["Coordination"]
  node_coordinator["Coordinator<br/>[coordinator.go]"]
  node_recovery["Coordinator recovery<br/>[coordinator.go]"]
  node_response["Transaction result<br/>[coordinator.go]"]
end

subgraph group_participants["Participants"]
  node_participant["Participant handlers<br/>[server.go]"]
  node_txstate["Transaction state<br/>[server.go]"]
  node_kvstore[("Key-value store<br/>[server.go]")]
end

subgraph group_transport["RPC transport"]
  node_rpcnetwork["RPC network<br/>[labrpc.go]"]
  node_rpcdispatch["RPC dispatch<br/>[labrpc.go]"]
end

subgraph group_encoding["RPC encoding"]
  node_gobcodec["Gob codec<br/>[labgob.go]"]
end

node_caller(("Transaction caller"))

node_caller -->|"stages operations"| node_participant
node_caller -->|"starts transaction"| node_coordinator
node_coordinator -->|"prepare, pre-commit, commit, abort, query"| node_participant
node_coordinator -->|"recovers on restart"| node_recovery
node_recovery -->|"queries state"| node_participant
node_recovery -->|"restarts protocol"| node_coordinator
node_coordinator -->|"reports outcome"| node_response
node_participant -->|"updates state"| node_txstate
node_participant -->|"reads and writes"| node_kvstore
node_coordinator -->|"sends RPCs"| node_rpcnetwork
node_rpcnetwork -->|"dispatches requests"| node_rpcdispatch
node_rpcdispatch -->|"invokes handlers"| node_participant
node_rpcnetwork -->|"encodes RPC data"| node_gobcodec
node_caller -->|"receives result"| node_response

click node_coordinator "https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go"
click node_recovery "https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go"
click node_response "https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go"
click node_participant "https://github.com/cmiyai/3phasecommit/blob/main/server.go"
click node_txstate "https://github.com/cmiyai/3phasecommit/blob/main/server.go"
click node_kvstore "https://github.com/cmiyai/3phasecommit/blob/main/server.go"
click node_rpcnetwork "https://github.com/cmiyai/3phasecommit/blob/main/labrpc/labrpc.go"
click node_rpcdispatch "https://github.com/cmiyai/3phasecommit/blob/main/labrpc/labrpc.go"
click node_gobcodec "https://github.com/cmiyai/3phasecommit/blob/main/labgob/labgob.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_coordinator,node_recovery,node_response toneBlue
class node_participant,node_txstate,node_kvstore toneAmber
class node_rpcnetwork,node_rpcdispatch toneMint
class node_gobcodec toneRose
class node_caller toneIndigo
```

## Components

### Coordination

- [Coordinator](https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go): `coordinator.go`
- [Coordinator recovery](https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go): `coordinator.go`
- [Transaction result](https://github.com/cmiyai/3phasecommit/blob/main/coordinator.go): `coordinator.go`

### Participants

- [Participant handlers](https://github.com/cmiyai/3phasecommit/blob/main/server.go): `server.go`
- [Transaction state](https://github.com/cmiyai/3phasecommit/blob/main/server.go): `server.go`
- [Key-value store](https://github.com/cmiyai/3phasecommit/blob/main/server.go): `server.go`

### RPC transport

- [RPC network](https://github.com/cmiyai/3phasecommit/blob/main/labrpc/labrpc.go): `labrpc/labrpc.go`
- [RPC dispatch](https://github.com/cmiyai/3phasecommit/blob/main/labrpc/labrpc.go): `labrpc/labrpc.go`

### RPC encoding

- [Gob codec](https://github.com/cmiyai/3phasecommit/blob/main/labgob/labgob.go): `labgob/labgob.go`

### Other components

- **Transaction caller**

## Connections

- Transaction caller → Participant handlers: stages operations (evidence: `server.go`)
- Transaction caller → Coordinator: starts transaction (evidence: `coordinator.go`)
- Coordinator → Participant handlers: prepare, pre-commit, commit, abort, query (evidence: `coordinator.go`)
- Coordinator → Coordinator recovery: recovers on restart (evidence: `coordinator.go`)
- Coordinator recovery → Participant handlers: queries state (evidence: `coordinator.go`)
- Coordinator recovery → Coordinator: restarts protocol (evidence: `coordinator.go`)
- Coordinator → Transaction result: reports outcome (evidence: `coordinator.go`)
- Participant handlers → Transaction state: updates state (evidence: `server.go`)
- Participant handlers → Key-value store: reads and writes (evidence: `server.go`)
- Coordinator → RPC network: sends RPCs (evidence: `coordinator.go`)
- RPC network → RPC dispatch: dispatches requests (evidence: `labrpc/labrpc.go`)
- RPC dispatch → Participant handlers: invokes handlers (evidence: `labrpc/labrpc.go`)
- RPC network → Gob codec: encodes RPC data (evidence: `labrpc/labrpc.go`)
- Transaction caller → Transaction result: receives result (evidence: `coordinator.go`)
