Skip to content

Commit

Permalink
Add example of leader failure
Browse files Browse the repository at this point in the history
Signed-off-by: Byron Ruth <b@devel.io>
  • Loading branch information
bruth committed Nov 4, 2022
1 parent 881727d commit aca9bdb
Show file tree
Hide file tree
Showing 4 changed files with 195 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/jetstream/leader-failure/cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM natsio/nats-box:0.13.2

RUN apk add bash curl

COPY --from=nats:2.9.5 /nats-server /usr/local/bin/

COPY . .

ENTRYPOINT ["bash"]

CMD ["main.sh"]

177 changes: 177 additions & 0 deletions examples/jetstream/leader-failure/cli/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
#!/bin/sh

set -euo pipefail

# Define the system account to be included by all configurations.
cat <<- EOF > sys.conf
accounts: {
SYS: {
users: [{user: sys, password: sys}]
}
APP: {
jetstream: true,
users: [{user: app, password: app}]
}
}
system_account: SYS
EOF

# Create the *east* and *west* server configurations.
# A requirement of JetStream is to have a cluster block defined
# since this is how available storage resources are determined
# for placement of streams and consumers.
#
# In a production deployment, at least three nodes per cluster
# are recommended which supports creating R3 streams. In this
# test setup, only R1 streams can be created since streams replicas
# do not cross gateway connections.
cat <<- EOF > n1.conf
port: 4222
http_port: 8222
server_name: n1
include sys.conf
jetstream: {
store_dir: "./n1"
}
cluster: {
name: c1,
port: 6222,
routes: [
"nats-route://0.0.0.0:6222",
"nats-route://0.0.0.0:6223",
"nats-route://0.0.0.0:6224",
],
}
EOF

cat <<- EOF > n2.conf
port: 4223
http_port: 8223
server_name: n2
include sys.conf
jetstream: {
store_dir: "./n2"
}
cluster: {
name: c1,
port: 6223,
routes: [
"nats-route://0.0.0.0:6222",
"nats-route://0.0.0.0:6223",
"nats-route://0.0.0.0:6224",
],
}
EOF

cat <<- EOF > n3.conf
port: 4224
http_port: 8224
server_name: n3
include sys.conf
jetstream: {
store_dir: "./n3"
}
cluster: {
name: c1,
port: 6224,
routes: [
"nats-route://0.0.0.0:6222",
"nats-route://0.0.0.0:6223",
"nats-route://0.0.0.0:6224",
],
}
EOF


# Start the servers and sleep for a few seconds to startup.
nats-server -c n1.conf &
N1_PID=$!

nats-server -c n2.conf &
N2_PID=$!

nats-server -c n3.conf &
N3_PID=$!

sleep 3

# Save and select the default context to use all seed servers.
nats context save \
--server "nats://localhost:4222,nats://localhost:4223,nats://localhost:4224" \
--user app \
--password app \
app

nats context select app


# Show the server list which will indicate the clusters and
# gateway connections as well as the JetStream server report.
nats --user sys --password sys server list
nats --user sys --password sys server report jetstream

# Add a stream.
nats stream add \
--retention=limits \
--storage=file \
--replicas=3 \
--discard=old \
--dupe-window=2m \
--max-age=-1 \
--max-msgs=-1 \
--max-bytes=-1 \
--max-msg-size=-1 \
--max-msgs-per-subject=-1 \
--max-consumers=-1 \
--allow-rollup \
--no-deny-delete \
--no-deny-purge \
--subjects="orders" \
ORDERS

# Publish some messages.
nats req orders --count=100 'Message {{Count}}'

# Force a step down of the leader.
nats stream cluster step-down ORDERS

# Report the new leader.
nats --user sys --password sys server report jetstream

# Publish more messages.
nats req orders --count=100 'Message {{Count}}'

nats stream list

# Now determine the leader and kill the server.
LEADER=$(nats stream info ORDERS --json | jq -r '.cluster.leader')
echo "$LEADER is the leader"

case $LEADER in
"n1")
kill $N1_PID
;;
"n2")
kill $N2_PID
;;
"n3")
kill $N3_PID
;;
esac

# Publish more messages.
nats req orders --count=100 'Message {{Count}}'

nats stream list

nats --user sys --password sys server report jetstream
4 changes: 4 additions & 0 deletions examples/jetstream/leader-failure/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version: '3.9'
services:
app:
image: ${IMAGE_TAG}
2 changes: 2 additions & 0 deletions examples/jetstream/leader-failure/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
title: Leader Failure
description: |-

1 comment on commit aca9bdb

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for nats-by-example ready!

✅ Preview
https://nats-by-example-kq17cku2d-connecteverything.vercel.app

Built with commit aca9bdb.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.