Repository navigation
feat: broadcast queue state #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f4c688
feat: broadcast queue state
bithckr 8a24e41
feat: fix lint issue
bithckr 2624044
feat: pin go-test-coverage to v2.19.0
bithckr b26b716
feat: update readme
bithckr bce04f8
feat: pr feedback
bithckr f5a0c0e
feat: fix lint
bithckr ab91d0b
feat: improve test coverage
bithckr bdf2503
feat: resolve vulnerabilities
bithckr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,78 @@ | ||
| // Copyright © 2026, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package recovery | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "log" | ||
| "time" | ||
|
|
||
| amqp "github.com/rabbitmq/amqp091-go" | ||
| ) | ||
|
|
||
| // ConsensusExchange is the fanout exchange every Phorcys instance publishes | ||
| // RecoveryAnnouncements to. Fanout ensures every node subscribed to it — i.e. | ||
| // every other node hosting a replica of the announced queue — receives every | ||
| // announcement regardless of routing key. | ||
| const ConsensusExchange = "phorcys.consensus" | ||
|
|
||
| // RecoveryAnnouncement is what a node publishes to ConsensusExchange after | ||
| // Prepare backs up a queue, so other nodes hosting that queue can compare | ||
| // Term/Index (via RaftPosition.After) and agree on which one calls Execute. | ||
| type RecoveryAnnouncement struct { | ||
| Hostname string `json:"hostname"` | ||
| VHost string `json:"vhost"` | ||
| Queue string `json:"queue"` | ||
| Timestamp time.Time `json:"timestamp"` | ||
| Term uint64 `json:"term"` | ||
| Index uint64 `json:"index"` | ||
| } | ||
|
|
||
| // PublishRecoveryAnnouncement declares ConsensusExchange and publishes ann to | ||
| // it as JSON. | ||
| func PublishRecoveryAnnouncement(ctx context.Context, amqpURL string, ann RecoveryAnnouncement) error { | ||
| conn, err := amqp.Dial(amqpURL) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to connect to broker: %w", err) | ||
| } | ||
| defer func() { _ = conn.Close() }() | ||
|
|
||
| ch, err := conn.Channel() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open channel: %w", err) | ||
| } | ||
| defer func() { _ = ch.Close() }() | ||
|
|
||
| if err := ch.ExchangeDeclare(ConsensusExchange, "fanout", true, false, false, false, nil); err != nil { | ||
| return fmt.Errorf("failed to declare consensus exchange: %w", err) | ||
| } | ||
|
|
||
| body, err := json.Marshal(ann) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal announcement: %w", err) | ||
| } | ||
|
|
||
| pubCtx, cancel := context.WithTimeout(ctx, 10*time.Second) | ||
| defer cancel() | ||
|
|
||
| err = ch.PublishWithContext(pubCtx, | ||
| ConsensusExchange, | ||
| "", // fanout ignores routing key | ||
| false, | ||
| false, | ||
| amqp.Publishing{ | ||
| ContentType: "application/json", | ||
| Timestamp: ann.Timestamp, | ||
| Body: body, | ||
| }, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to publish announcement: %w", err) | ||
| } | ||
|
|
||
| log.Printf("[Consensus] Announced %s/%s: term=%d idx=%d", ann.VHost, ann.Queue, ann.Term, ann.Index) | ||
| return nil | ||
| } |
This file contains hidden or 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,76 @@ | ||
| // Copyright © 2026, SAS Institute Inc., Cary, NC, USA. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package recovery | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestPublishRecoveryAnnouncement_UnreachableBrokerReturnsError(t *testing.T) { | ||
| ann := RecoveryAnnouncement{ | ||
| Hostname: "host1", | ||
| VHost: "/", | ||
| Queue: "orders", | ||
| Timestamp: time.Now(), | ||
| Term: 1, | ||
| Index: 2, | ||
| } | ||
|
|
||
| err := PublishRecoveryAnnouncement(context.Background(), unreachableAMQPURL, ann) | ||
| require.Error(t, err) | ||
| assert.Contains(t, err.Error(), "failed to connect to broker") | ||
| } | ||
|
|
||
| func TestRecoveryAnnouncement_JSONFieldNames(t *testing.T) { | ||
| ann := RecoveryAnnouncement{ | ||
| Hostname: "host1", | ||
| VHost: "/", | ||
| Queue: "orders", | ||
| Timestamp: time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC), | ||
| Term: 7, | ||
| Index: 42, | ||
| } | ||
|
|
||
| body, err := json.Marshal(ann) | ||
| require.NoError(t, err) | ||
|
|
||
| var decoded map[string]any | ||
| require.NoError(t, json.Unmarshal(body, &decoded)) | ||
| assert.Equal(t, "host1", decoded["hostname"]) | ||
| assert.Equal(t, "/", decoded["vhost"]) | ||
| assert.Equal(t, "orders", decoded["queue"]) | ||
| assert.Equal(t, float64(7), decoded["term"]) | ||
| assert.Equal(t, float64(42), decoded["index"]) | ||
| assert.Equal(t, "2026-01-02T03:04:05Z", decoded["timestamp"]) | ||
| } | ||
|
|
||
| func TestRecoveryAnnouncement_JSONRoundTrip(t *testing.T) { | ||
| want := RecoveryAnnouncement{ | ||
| Hostname: "rabbit-node-2", | ||
| VHost: "production", | ||
| Queue: "payments", | ||
| Timestamp: time.Date(2026, 6, 15, 12, 30, 0, 0, time.UTC), | ||
| Term: 3, | ||
| Index: 99, | ||
| } | ||
|
|
||
| body, err := json.Marshal(want) | ||
| require.NoError(t, err) | ||
|
|
||
| var got RecoveryAnnouncement | ||
| require.NoError(t, json.Unmarshal(body, &got)) | ||
| assert.True(t, want.Timestamp.Equal(got.Timestamp)) | ||
| got.Timestamp = want.Timestamp // compare the rest with a plain Equal | ||
| assert.Equal(t, want, got) | ||
| } | ||
|
|
||
| func TestConsensusExchange_Name(t *testing.T) { | ||
| assert.Equal(t, "phorcys.consensus", ConsensusExchange) | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.