-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Byron Ruth <[email protected]>
- Loading branch information
Showing
10 changed files
with
174 additions
and
134 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
17 changes: 10 additions & 7 deletions
17
...cases/faux-active-active/cli/n1-edit.json → ...e-cases/active-active-kv/cli/n1-edit.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
17 changes: 10 additions & 7 deletions
17
...cases/faux-active-active/cli/n2-edit.json → ...e-cases/active-active-kv/cli/n2-edit.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
] | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
58ba5f8
There was a problem hiding this comment.
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