-
Notifications
You must be signed in to change notification settings - Fork 2.5k
WIP!: maintnofications for cluster #3601
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
Draft
ndyakov
wants to merge
8
commits into
master
Choose a base branch
from
ndyakov/CAE-1625-maint-cluster
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,088
−31
Draft
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a247360
smigrating/smigrated intro
ndyakov 4631320
proper notification format
ndyakov b3a3bdd
Update maintnotifications/manager.go
ndyakov d7a246b
cascading smigrated will trigger multiple reloads
ndyakov 391cee9
process once per client / seqid
ndyakov b635202
Merge branch 'master' into ndyakov/CAE-1625-maint-cluster
ndyakov cf2d5d3
fix flaky tests
ndyakov 3c6dce8
Merge branch 'master' into ndyakov/CAE-1625-maint-cluster
ndyakov 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,169 @@ | ||
| package redis | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync/atomic" | ||
| "testing" | ||
|
|
||
| "github.com/redis/go-redis/v9/maintnotifications" | ||
| ) | ||
|
|
||
| // TestClusterClientSMigratedCallback tests that ClusterClient sets up SMIGRATED callback on node clients | ||
| func TestClusterClientSMigratedCallback(t *testing.T) { | ||
| t.Run("CallbackSetupWithMaintNotifications", func(t *testing.T) { | ||
| // Track if state reload was called | ||
| var reloadCalled atomic.Bool | ||
|
|
||
| // Create cluster options with maintnotifications enabled | ||
| opt := &ClusterOptions{ | ||
| Addrs: []string{"localhost:7000"}, // Dummy address | ||
| MaintNotificationsConfig: &maintnotifications.Config{ | ||
| Mode: maintnotifications.ModeEnabled, | ||
| }, | ||
| // Use custom NewClient to track when nodes are created | ||
| NewClient: func(opt *Options) *Client { | ||
| client := NewClient(opt) | ||
| return client | ||
| }, | ||
| } | ||
|
|
||
| // Create cluster client | ||
| cluster := NewClusterClient(opt) | ||
| defer cluster.Close() | ||
|
|
||
| // Manually trigger node creation by calling GetOrCreate | ||
| // This simulates what happens during normal cluster operations | ||
| node, err := cluster.nodes.GetOrCreate("localhost:7000") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create node: %v", err) | ||
| } | ||
|
|
||
| // Get the maintnotifications manager from the node client | ||
| manager := node.Client.GetMaintNotificationsManager() | ||
| if manager == nil { | ||
| t.Skip("MaintNotifications manager not initialized (expected if not connected to real Redis)") | ||
| return | ||
| } | ||
|
|
||
| // Set up cluster state reload callback for testing | ||
| var receivedHostPort string | ||
| var receivedSlotRanges []string | ||
| manager.SetClusterStateReloadCallback(func(ctx context.Context, hostPort string, slotRanges []string) { | ||
| reloadCalled.Store(true) | ||
| receivedHostPort = hostPort | ||
| receivedSlotRanges = slotRanges | ||
| }) | ||
|
|
||
| // Trigger the callback (this is what SMIGRATED notification would do) | ||
| ctx := context.Background() | ||
| testHostPort := "127.0.0.1:6379" | ||
| testSlotRanges := []string{"1234", "5000-6000"} | ||
| manager.TriggerClusterStateReload(ctx, testHostPort, testSlotRanges) | ||
|
|
||
| // Verify callback was called | ||
| if !reloadCalled.Load() { | ||
| t.Error("Cluster state reload callback should have been called") | ||
| } | ||
|
|
||
| // Verify host:port was passed correctly | ||
| if receivedHostPort != testHostPort { | ||
| t.Errorf("Expected host:port %s, got %s", testHostPort, receivedHostPort) | ||
| } | ||
|
|
||
| // Verify slot ranges were passed correctly | ||
| if len(receivedSlotRanges) != len(testSlotRanges) { | ||
| t.Errorf("Expected %d slot ranges, got %d", len(testSlotRanges), len(receivedSlotRanges)) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("NoCallbackWithoutMaintNotifications", func(t *testing.T) { | ||
| // Create cluster options WITHOUT maintnotifications | ||
| opt := &ClusterOptions{ | ||
| Addrs: []string{"localhost:7000"}, // Dummy address | ||
| // MaintNotificationsConfig is nil | ||
| } | ||
|
|
||
| // Create cluster client | ||
| cluster := NewClusterClient(opt) | ||
| defer cluster.Close() | ||
|
|
||
| // The OnNewNode callback should not be registered when MaintNotificationsConfig is nil | ||
| // This test just verifies that the cluster client doesn't panic | ||
| }) | ||
| } | ||
|
|
||
| // TestClusterClientSMigratedIntegration tests SMIGRATED notification handling in cluster context | ||
| func TestClusterClientSMigratedIntegration(t *testing.T) { | ||
| t.Run("SMigratedTriggersStateReload", func(t *testing.T) { | ||
| // This test verifies the integration between SMIGRATED notification and cluster state reload | ||
| // We verify that the callback is properly set up to call cluster.state.LazyReload() | ||
|
|
||
| // Create cluster options with maintnotifications enabled | ||
| opt := &ClusterOptions{ | ||
| Addrs: []string{"localhost:7000"}, | ||
| MaintNotificationsConfig: &maintnotifications.Config{ | ||
| Mode: maintnotifications.ModeEnabled, | ||
| }, | ||
| } | ||
|
|
||
| // Create cluster client | ||
| cluster := NewClusterClient(opt) | ||
| defer cluster.Close() | ||
|
|
||
| // Create a node | ||
| node, err := cluster.nodes.GetOrCreate("localhost:7000") | ||
| if err != nil { | ||
| t.Fatalf("Failed to create node: %v", err) | ||
| } | ||
|
|
||
| // Get the maintnotifications manager | ||
| manager := node.Client.GetMaintNotificationsManager() | ||
| if manager == nil { | ||
| t.Skip("MaintNotifications manager not initialized (expected if not connected to real Redis)") | ||
| return | ||
| } | ||
|
|
||
| // Verify that the callback is set by checking it's not nil | ||
| // We can't directly test LazyReload being called without a real cluster, | ||
| // but we can verify the callback mechanism works | ||
| var callbackWorks atomic.Bool | ||
| var receivedHostPort string | ||
| var receivedSlotRanges []string | ||
| manager.SetClusterStateReloadCallback(func(ctx context.Context, hostPort string, slotRanges []string) { | ||
| callbackWorks.Store(true) | ||
| receivedHostPort = hostPort | ||
| receivedSlotRanges = slotRanges | ||
| }) | ||
|
|
||
| ctx := context.Background() | ||
| testHostPort := "127.0.0.1:7000" | ||
| testSlotRanges := []string{"5678"} | ||
| manager.TriggerClusterStateReload(ctx, testHostPort, testSlotRanges) | ||
|
|
||
| if !callbackWorks.Load() { | ||
| t.Error("Callback mechanism should work") | ||
| } | ||
|
|
||
| if receivedHostPort != testHostPort { | ||
| t.Errorf("Expected host:port %s, got %s", testHostPort, receivedHostPort) | ||
| } | ||
|
|
||
| if len(receivedSlotRanges) != 1 || receivedSlotRanges[0] != "5678" { | ||
| t.Errorf("Expected slot ranges [5678], got %v", receivedSlotRanges) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| // TestSMigratingAndSMigratedConstants verifies the SMIGRATING and SMIGRATED constants are exported | ||
| func TestSMigratingAndSMigratedConstants(t *testing.T) { | ||
| // This test verifies that the SMIGRATING and SMIGRATED constants are properly defined | ||
| // and accessible from the maintnotifications package | ||
| if maintnotifications.NotificationSMigrating != "SMIGRATING" { | ||
| t.Errorf("Expected NotificationSMigrating to be 'SMIGRATING', got: %s", maintnotifications.NotificationSMigrating) | ||
| } | ||
|
|
||
| if maintnotifications.NotificationSMigrated != "SMIGRATED" { | ||
| t.Errorf("Expected NotificationSMigrated to be 'SMIGRATED', got: %s", maintnotifications.NotificationSMigrated) | ||
| } | ||
| } | ||
|
|
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
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
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
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.