Skip to content

Commit

Permalink
Update to use leaf nodes
Browse files Browse the repository at this point in the history
Signed-off-by: Byron Ruth <[email protected]>
  • Loading branch information
bruth committed Feb 22, 2023
1 parent 6bd2b4f commit 58ba5f8
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 134 deletions.
19 changes: 19 additions & 0 deletions examples/use-cases/active-active-kv/cli/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM golang:1.19-alpine3.17 AS build

RUN apk update && apk add git

RUN go install github.com/nats-io/natscli/nats@main
RUN go install github.com/nats-io/nats-server/v2@dev

FROM alpine:3.17

RUN apk add bash curl

COPY --from=build /go/bin/nats-server /usr/local/bin/
COPY --from=build /go/bin/nats /usr/local/bin/

COPY . .

ENTRYPOINT ["bash"]

CMD ["main.sh"]
130 changes: 130 additions & 0 deletions examples/use-cases/active-active-kv/cli/main.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/bin/sh

set -euo pipefail

# Define configuration for two nodes.
cat <<- EOF > n1.conf
port: 4222
http_port: 8222
server_name: n1
mappings: {
"\$KV.mykv.>" : "\$KV.mykv.n1.>"
}
jetstream: {
domain: n1
store_dir: "./n1"
}
leafnodes: {
port: 7422
remotes = [
{
url: "nats-leaf://localhost:7423"
}
]
}
EOF

cat <<- EOF > n2.conf
port: 4223
http_port: 8223
server_name: n2
mappings: {
"\$KV.mykv.>" : "\$KV.mykv.n2.>"
}
jetstream: {
domain: n2
store_dir: "./n2"
}
leafnodes: {
port: 7423
remotes = [
{
url: "nats-leaf://localhost:7422"
}
]
}
EOF


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

nats-server -c n2.conf > /dev/null 2>&1 &
N2_PID=$!

sleep 3

# Wait until the servers are healthy.
curl --fail --silent \
--retry 10 \
--retry-delay 1 \
http://localhost:8222/healthz > /dev/null

curl --fail --silent \
--retry 10 \
--retry-delay 1 \
http://localhost:8223/healthz > /dev/null

# Save two contexts, one for each leaf.
nats context save n1 \
--server nats://localhost:4222

nats context save n2 \
--server nats://localhost:4223

# Create a KV on each leaf.
nats --context=n1 kv add mykv --history=10
nats --context=n2 kv add mykv --history=10

# Edit to source from one another with a subject transform.
# e.g. $KV.mykv.> -> $KV.mykv.> and vice versa for the other.
nats --context=n1 stream edit --force --config n1-edit.json KV_mykv
nats --context=n2 stream edit --force --config n2-edit.json KV_mykv

# Put one value each.
nats --context=n1 kv put mykv foo ''
nats --context=n2 kv put mykv foo ''

echo 'Messages in n1 KV...'
nats --context=n1 stream view KV_mykv

echo 'Messages in n2 KV...'
nats --context=n2 stream view KV_mykv

# Report the streams. Each should have two messages.
nats --context=n1 stream report
nats --context=n2 stream report

# Take n1 down.
nats-server --signal=quit=$N1_PID

# Put two more keys in n2.
nats --context=n2 kv put mykv baz ''
nats --context=n2 kv put mykv qux ''

# Report on n2, which should not have four messages.
nats --context=n2 stream report
nats --context=n2 stream view KV_mykv

# Turn n1 back on.
nats-server -c n1.conf & #> /dev/null 2>&1 &
N1_PID=$!

sleep 2

# Wait for healthy.
curl --fail --silent \
--retry 10 \
--retry-delay 1 \
http://localhost:8222/healthz > /dev/null

# Let n1 catch up with messages from n2
nats --context=n1 stream report
nats --context=n1 stream view KV_mykv
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
{
"name": "KV_n1",
"name": "KV_mykv",
"subjects": [
"$KV.n1.\u003e"
"$KV.mykv.\u003e"
],
"placement": {},
"retention": "limits",
"max_consumers": -1,
"max_msgs_per_subject": 5,
"max_msgs_per_subject": 10,
"max_msgs": -1,
"max_bytes": -1,
"max_age": 0,
"max_msg_size": -1,
"storage": "file",
"discard": "new",
"num_replicas": 1,
"duplicate_window": 12000000000,
"duplicate_window": 120000000000,
"sealed": false,
"deny_delete": true,
"deny_purge": false,
"allow_rollup_hdrs": true,
"allow_direct": true,
"sources": [
{
"name": "KV_n2",
"filter_subject": "$KV.n2.\u003e",
"subject_transform_dest": "$KV.n1.\u003e"
"name": "KV_mykv",
"filter_subject": "$KV.mykv.n2.\u003e",
"external": {
"api": "$JS.n2.API"
}
}
]
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
{
"name": "KV_n2",
"name": "KV_mykv",
"subjects": [
"$KV.n2.\u003e"
"$KV.mykv.\u003e"
],
"placement": {},
"retention": "limits",
"max_consumers": -1,
"max_msgs_per_subject": 5,
"max_msgs_per_subject": 10,
"max_msgs": -1,
"max_bytes": -1,
"max_age": 0,
"max_msg_size": -1,
"storage": "file",
"discard": "new",
"num_replicas": 1,
"duplicate_window": 12000000000,
"duplicate_window": 120000000000,
"sealed": false,
"deny_delete": true,
"deny_purge": false,
"allow_rollup_hdrs": true,
"allow_direct": true,
"sources": [
{
"name": "KV_n1",
"filter_subject": "$KV.n1.\u003e",
"subject_transform_dest": "$KV.n2.\u003e"
"name": "KV_mykv",
"filter_subject": "$KV.mykv.n1.\u003e",
"external": {
"api": "$JS.n1.API"
}
}
]
}
5 changes: 5 additions & 0 deletions examples/use-cases/active-active-kv/meta.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
title: Active-active KV
description: |-
This example shows how to model two KV streams, placed on
separate nodes (on a two-node cluster) that will source
entries from each other.
76 changes: 0 additions & 76 deletions examples/use-cases/faux-active-active/cli/main.sh

This file was deleted.

21 changes: 0 additions & 21 deletions examples/use-cases/faux-active-active/cli/n1.json

This file was deleted.

21 changes: 0 additions & 21 deletions examples/use-cases/faux-active-active/cli/n2.json

This file was deleted.

2 changes: 0 additions & 2 deletions examples/use-cases/faux-active-active/meta.yaml

This file was deleted.

1 comment on commit 58ba5f8

@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-ndrbhig1h-connecteverything.vercel.app

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

Please sign in to comment.