-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-cluster.sh
executable file
·145 lines (113 loc) · 3.06 KB
/
test-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
134
135
136
137
138
139
140
141
142
143
144
145
#!/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 port=$1
local method=$2
local body="$3"
local isApiService="$4"
local cmd="grpcurl -plaintext -proto ./proto/management_service.proto -d $body -import-path ./proto localhost:$port raftd.ManagementService/$method"
if [ "$isApiService" = "true" ]; then
cmd="grpcurl -plaintext -proto ./proto/api_service.proto -d $body -import-path ./proto localhost:$port raftd.ApiService/$method"
fi
echo '---'" rpc(127.0.0.1:$port/$method, $body)"
{
time $cmd
} | {
if type jq > /dev/null 2>&1; then
jq 'if has("data") then .data |= fromjson else . end'
else
cat
fi
}
echo
echo
}
export RUST_LOG=trace
export RUST_BACKTRACE=full
echo "Killing all running raft-key-value"
kill
sleep 1
echo "Start 5 uninitialized raft-key-value servers..."
nohup ./target/debug/raft-key-value --id 1 --addr 127.0.0.1:5051 > n1.log &
sleep 1
echo "Server 1 started"
nohup ./target/debug/raft-key-value --id 2 --addr 127.0.0.1:5052 > n2.log &
sleep 1
echo "Server 2 started"
nohup ./target/debug/raft-key-value --id 3 --addr 127.0.0.1:5053 > n3.log &
sleep 1
echo "Server 3 started"
sleep 1
nohup ./target/debug/raft-key-value --id 4 --addr 127.0.0.1:5054 > n4.log &
sleep 1
echo "Server 4 started"
sleep 1
nohup ./target/debug/raft-key-value --id 5 --addr 127.0.0.1:5055 > n5.log &
sleep 1
echo "Server 5 started"
sleep 1
echo "Initialize servers 1,2,3 as a 3-nodes cluster"
sleep 2
echo
rpc 5051 Init '{"nodes":[{"node_id":"1","rpc_addr":"127.0.0.1:5051"},{"node_id":"2","rpc_addr":"127.0.0.1:5052"},{"node_id":"3","rpc_addr":"127.0.0.1:5053"}]}'
echo "Server 1 is a leader now"
sleep 2
echo "Get metrics from the leader"
sleep 2
echo
rpc 5051 Metrics '{}'
sleep 1
echo "Adding node 4 and node 5 as learners, to receive log from leader node 1"
sleep 1
echo
rpc 5051 AddLearner '{"node":{"node_id":"4","rpc_addr":"127.0.0.1:5054"}}'
echo "Node 4 added as learner"
sleep 1
echo
rpc 5051 AddLearner '{"node":{"node_id":"5","rpc_addr":"127.0.0.1:5055"}}'
echo "Node 5 added as learner"
sleep 1
echo "Get metrics from the leader, after adding 2 learners"
sleep 2
echo
rpc 5051 Metrics '{}'
sleep 1
echo "Changing membership from [1, 2, 3] to 5 nodes cluster: [1, 2, 3, 4, 5]"
echo
rpc 5051 ChangeMembership '{"members":["1","2","3","4","5"],"retain":true}'
sleep 1
echo 'Membership changed to [1, 2, 3, 4, 5]'
sleep 1
echo "Get metrics from the leader again"
sleep 1
echo
rpc 5051 Metrics '{}'
sleep 1
echo "Write foo=zoo on node-1"
sleep 1
echo
rpc 5051 Set '{"key":"foo","value":"zoo"}' true
sleep 1
echo "Data written"
sleep 1
echo "Read foo=zoo from node-2"
sleep 1
echo "Read from node 2"
echo
rpc 5052 Get '{"key":"foo"}' true
echo
echo "Killing all nodes..."
kill