-
Notifications
You must be signed in to change notification settings - Fork 8
/
open-cluster.sh
executable file
·133 lines (106 loc) · 2.39 KB
/
open-cluster.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
set -o errexit
cargo build
kill() {
if [ "$(uname)" = "Darwin" ]; then
SERVICE='raft-key-value'
if pgrep -xq -- "${SERVICE}"; then
pkill -f "${SERVICE}"
fi
else
set +e # killall will error if finds no process to kill
killall raft-key-value
set -e
fi
}
rpc() {
local uri=$1
local body="$2"
echo '---'" rpc(:$uri, $body)"
{
if [ ".$body" = "." ]; then
curl --silent "127.0.0.1:$uri"
else
curl --silent "127.0.0.1:$uri" -H "Content-Type: application/json" -d "$body"
fi
} | {
if type jq > /dev/null 2>&1; then
jq
else
cat
fi
}
echo
echo
}
export RUST_LOG=debug
export RAFT_HEARTBEAT_INTERVAL=100
echo "Killing all running raft-key-value"
kill
sleep 1
echo "Start 3 uninitialized raft-key-value servers..."
nohup ./target/debug/raft-key-value --id 1 --http-addr 127.0.0.1:21001 > n1.log &
sleep 1
echo "Server 1 started"
nohup ./target/debug/raft-key-value --id 2 --http-addr 127.0.0.1:21002 > n2.log &
sleep 1
echo "Server 2 started"
nohup ./target/debug/raft-key-value --id 3 --http-addr 127.0.0.1:21003 > n3.log &
sleep 1
echo "Server 3 started"
sleep 1
echo "Initialize server 1 as a single-node cluster"
sleep 2
echo
#rpc 21001/init '{}'
#echo "Server 1 is a leader now"
sleep 2
echo "Get metrics from the leader"
sleep 2
echo
rpc 21001/metrics
sleep 1
echo "Adding node 2 and node 3 as learners, to receive log from leader node 1"
sleep 1
echo
rpc 21001/add-learner '[2, "127.0.0.1:21002"]'
echo "Node 2 added as leaner"
sleep 1
echo
rpc 21001/add-learner '[3, "127.0.0.1:21003"]'
echo "Node 3 added as leaner"
sleep 1
echo "Get metrics from the leader, after adding 2 learners"
sleep 2
echo
rpc 21001/metrics
sleep 1
echo "Changing membership from [1] to 3 nodes cluster: [1, 2, 3]"
echo
rpc 21001/change-membership '[1, 2, 3]'
sleep 1
echo "Membership changed"
sleep 1
echo "Get metrics from the leader again"
sleep 1
echo
rpc 21001/metrics
sleep 1
echo "Write data on leader"
sleep 1
echo
rpc 21001/write '{"Set":{"key":"foo","value":"bar"}}'
sleep 1
echo "Data written"
sleep 1
echo "Read on every node, including the leader"
sleep 1
echo "Read from node 1"
echo
rpc 21001/read '"foo"'
echo "Read from node 2"
echo
rpc 21002/read '"foo"'
echo "Read from node 3"
echo
rpc 21003/read '"foo"'