Skip to content

Commit

Permalink
Use integer Raft node IDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
erikgrinaker committed Nov 18, 2023
1 parent b6b48e0 commit 56fd065
Show file tree
Hide file tree
Showing 49 changed files with 404 additions and 434 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/clusters/*/toydb-?/data
/clusters/*/toydb*/data
/data
/target
.vscode/
Expand Down
44 changes: 22 additions & 22 deletions clusters/docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,62 @@ networks:
ipam:
driver: default
config:
- subnet: 172.20.0.0/24
- subnet: 172.20.0.0/24

services:
toydb-a: &toydb
toydb1: &toydb
build: ..
environment:
- TOYDB_LOG_LEVEL
- TOYDB_LOG_LEVEL
volumes:
- ./toydb-a/toydb.yaml:/etc/toydb.yaml
- ./toydb-a/data:/var/lib/toydb
- ./toydb1/toydb.yaml:/etc/toydb.yaml
- ./toydb1/data:/var/lib/toydb
networks:
toydb:
ipv4_address: 172.20.0.101
ports:
- 9601:9605
- 9601:9605

toydb-b:
toydb2:
<<: *toydb
volumes:
- ./toydb-b/toydb.yaml:/etc/toydb.yaml
- ./toydb-b/data:/var/lib/toydb
- ./toydb2/toydb.yaml:/etc/toydb.yaml
- ./toydb2/data:/var/lib/toydb
networks:
toydb:
ipv4_address: 172.20.0.102
ports:
- 9602:9605
- 9602:9605

toydb-c:
toydb3:
<<: *toydb
volumes:
- ./toydb-c/toydb.yaml:/etc/toydb.yaml
- ./toydb-c/data:/var/lib/toydb
- ./toydb3/toydb.yaml:/etc/toydb.yaml
- ./toydb3/data:/var/lib/toydb
networks:
toydb:
ipv4_address: 172.20.0.103
ports:
- 9603:9605
- 9603:9605

toydb-d:
toydb4:
<<: *toydb
volumes:
- ./toydb-d/toydb.yaml:/etc/toydb.yaml
- ./toydb-d/data:/var/lib/toydb
- ./toydb4/toydb.yaml:/etc/toydb.yaml
- ./toydb4/data:/var/lib/toydb
networks:
toydb:
ipv4_address: 172.20.0.104
ports:
- 9604:9605
- 9604:9605

toydb-e:
toydb5:
<<: *toydb
volumes:
- ./toydb-e/toydb.yaml:/etc/toydb.yaml
- ./toydb-e/data:/var/lib/toydb
- ./toydb5/toydb.yaml:/etc/toydb.yaml
- ./toydb5/data:/var/lib/toydb
networks:
toydb:
ipv4_address: 172.20.0.105
ports:
- 9605:9605
- 9605:9605
7 changes: 0 additions & 7 deletions clusters/docker/toydb-a/toydb.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions clusters/docker/toydb-b/toydb.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions clusters/docker/toydb-c/toydb.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions clusters/docker/toydb-d/toydb.yaml

This file was deleted.

7 changes: 0 additions & 7 deletions clusters/docker/toydb-e/toydb.yaml

This file was deleted.

File renamed without changes.
7 changes: 7 additions & 0 deletions clusters/docker/toydb1/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id: 1
data_dir: /var/lib/toydb
peers:
2: 172.20.0.102
3: 172.20.0.103
4: 172.20.0.104
5: 172.20.0.105
File renamed without changes.
7 changes: 7 additions & 0 deletions clusters/docker/toydb2/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
id: 2
data_dir: /var/lib/toydb
peers:
1: 172.20.0.101
3: 172.20.0.103
4: 172.20.0.104
5: 172.20.0.105
File renamed without changes.
7 changes: 7 additions & 0 deletions clusters/docker/toydb3/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
3: toydb-c
data_dir: /var/lib/toydb
peers:
1: 172.20.0.101
2: 172.20.0.102
4: 172.20.0.104
5: 172.20.0.105
File renamed without changes.
7 changes: 7 additions & 0 deletions clusters/docker/toydb4/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
4: toydb-d
data_dir: /var/lib/toydb
peers:
1: 172.20.0.101
2: 172.20.0.102
3: 172.20.0.103
5: 172.20.0.105
File renamed without changes.
7 changes: 7 additions & 0 deletions clusters/docker/toydb5/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
5: toydb-e
data_dir: /var/lib/toydb
peers:
1: 172.20.0.101
2: 172.20.0.102
3: 172.20.0.103
4: 172.20.0.104
4 changes: 2 additions & 2 deletions clusters/local/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set -euo pipefail

cargo build --release --bin toydb

for ID in a b c d e; do
(cargo run -q --release -- -c toydb-$ID/toydb.yaml 2>&1 | sed -e "s/\\(.*\\)/toydb-$ID \\1/g") &
for ID in 1 2 3 4 5; do
(cargo run -q --release -- -c toydb$ID/toydb.yaml 2>&1 | sed -e "s/\\(.*\\)/toydb$ID \\1/g") &
done

trap 'kill $(jobs -p)' EXIT
Expand Down
10 changes: 0 additions & 10 deletions clusters/local/toydb-a/toydb.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions clusters/local/toydb-b/toydb.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions clusters/local/toydb-c/toydb.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions clusters/local/toydb-d/toydb.yaml

This file was deleted.

10 changes: 0 additions & 10 deletions clusters/local/toydb-e/toydb.yaml

This file was deleted.

File renamed without changes.
10 changes: 10 additions & 0 deletions clusters/local/toydb1/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 1
data_dir: toydb1/data
sync: false
listen_sql: 0.0.0.0:9601
listen_raft: 0.0.0.0:9701
peers:
2: 127.0.0.1:9702
3: 127.0.0.1:9703
4: 127.0.0.1:9704
5: 127.0.0.1:9705
File renamed without changes.
10 changes: 10 additions & 0 deletions clusters/local/toydb2/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 2
data_dir: toydb2/data
sync: false
listen_sql: 0.0.0.0:9602
listen_raft: 0.0.0.0:9702
peers:
1: 127.0.0.1:9701
3: 127.0.0.1:9703
4: 127.0.0.1:9704
5: 127.0.0.1:9705
File renamed without changes.
10 changes: 10 additions & 0 deletions clusters/local/toydb3/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 3
data_dir: toydb-c/data
sync: false
listen_sql: 0.0.0.0:9603
listen_raft: 0.0.0.0:9703
peers:
1: 127.0.0.1:9701
2: 127.0.0.1:9702
4: 127.0.0.1:9704
5: 127.0.0.1:9705
File renamed without changes.
10 changes: 10 additions & 0 deletions clusters/local/toydb4/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 4
data_dir: toydb-d/data
sync: false
listen_sql: 0.0.0.0:9604
listen_raft: 0.0.0.0:9704
peers:
1: 127.0.0.1:9701
2: 127.0.0.1:9702
3: 127.0.0.1:9703
5: 127.0.0.1:9705
File renamed without changes.
10 changes: 10 additions & 0 deletions clusters/local/toydb5/toydb.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
id: 5
data_dir: toydb-e/data
sync: false
listen_sql: 0.0.0.0:9605
listen_raft: 0.0.0.0:9705
peers:
1: 127.0.0.1:9701
2: 127.0.0.1:9702
3: 127.0.0.1:9703
4: 127.0.0.1:9704
2 changes: 1 addition & 1 deletion config/toydb.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The node ID, peer ID/address map (empty for single node), and log level.
id: toydb
id: 1
peers: {}
log_level: INFO

Expand Down
16 changes: 8 additions & 8 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ To start a five-node cluster on the local machine (requires a working

```
$ (cd clusters/local && ./run.sh)
toydb-b 19:06:28 [ INFO] Listening on 0.0.0.0:9602 (SQL) and 0.0.0.0:9702 (Raft)
toydb-b 19:06:28 [ERROR] Failed connecting to Raft peer 127.0.0.1:9705: Connection refused
toydb-e 19:06:28 [ INFO] Listening on 0.0.0.0:9605 (SQL) and 0.0.0.0:9705 (Raft)
toydb2 19:06:28 [ INFO] Listening on 0.0.0.0:9602 (SQL) and 0.0.0.0:9702 (Raft)
toydb2 19:06:28 [ERROR] Failed connecting to Raft peer 127.0.0.1:9705: Connection refused
toydb5 19:06:28 [ INFO] Listening on 0.0.0.0:9605 (SQL) and 0.0.0.0:9705 (Raft)
[...]
toydb-e 19:06:29 [ INFO] Voting for toydb-d in term 1 election
toydb-c 19:06:29 [ INFO] Voting for toydb-d in term 1 election
toydb-d 19:06:29 [ INFO] Won election for term 1, becoming leader
toydb5 19:06:29 [ INFO] Voting for toydb-d in term 1 election
toydb3 19:06:29 [ INFO] Voting for toydb-d in term 1 election
toydb4 19:06:29 [ INFO] Won election for term 1, becoming leader
```

In a separate terminal, start a `toysql` client and check the server status:
Expand All @@ -37,9 +37,9 @@ $ cargo run --release --bin toysql
Connected to toyDB node "toydb-e". Enter !help for instructions.
toydb> !status
Server: toydb-e (leader toydb-d in term 1 with 5 nodes)
Server: 5 (leader 4 in term 1 with 5 nodes)
Raft log: 1 committed, 0 applied, 0.000 MB (hybrid storage)
Node logs: toydb-a:1 toydb-b:1 toydb-c:1 toydb-d:1 toydb-e:1
Node logs: 1:1 2:1 3:1 4:1 5:1
SQL txns: 0 active, 0 total (bitcask storage)
```

Expand Down
6 changes: 3 additions & 3 deletions src/bin/toydb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async fn main() -> Result<()> {
name => return Err(Error::Config(format!("Unknown SQL storage engine {}", name))),
};

Server::new(&cfg.id, cfg.peers, raft_log, raft_state)
Server::new(cfg.id, cfg.peers, raft_log, raft_state)
.await?
.listen(&cfg.listen_sql, &cfg.listen_raft)
.await?
Expand All @@ -69,8 +69,8 @@ async fn main() -> Result<()> {

#[derive(Debug, Deserialize)]
struct Config {
id: String,
peers: HashMap<String, String>,
id: raft::NodeID,
peers: HashMap<raft::NodeID, String>,
listen_sql: String,
listen_raft: String,
log_level: String,
Expand Down
Loading

0 comments on commit 56fd065

Please sign in to comment.