Repository navigation
node: keep router replacements within attachment target #833
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
Open
shin0343
wants to merge
1
commit into
google:main
Choose a base branch
from
shin0343:fix/router-replacement-redial
base: main
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.
Open
Changes from all commits
Commits
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,221 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package node | ||
|
|
||
| import ( | ||
| "context" | ||
| "slices" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/google/agentmesh/api" | ||
| "github.com/libp2p/go-libp2p/core/network" | ||
| "github.com/libp2p/go-libp2p/core/peer" | ||
| ) | ||
|
|
||
| // The replacement authenticates while the old router's redial is in flight. | ||
| // Letting that redial finish must not leave the node holding K+1 routers. | ||
| func TestRouterReplacementDuringRedial(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| old := newFakeRouter(t, "/ip4/127.0.0.1/tcp/0") | ||
| kept := newFakeRouterWithKey(t, old.cpPub, old.cpPriv, "/ip4/127.0.0.1/tcp/0") | ||
| spare := newFakeRouterWithKey(t, old.cpPub, old.cpPriv, "/ip4/127.0.0.1/tcp/0") | ||
| node := old.startNode(t, ctx, append(slices.Clone(old.routerAddrs), kept.routerAddrs...), func(o *Options) { | ||
| o.Routers = 2 | ||
| o.RouterRedialDelay = time.Millisecond | ||
| o.MonitorBootstrap = time.Hour | ||
| }) | ||
| node.setRouterCatalog(candidatesFromAddrs(ctx, append(append(slices.Clone(old.routerAddrs), kept.routerAddrs...), spare.routerAddrs...))) | ||
| waitForRouterState(t, ctx, "both initial attachments", func() bool { return node.AttachedRouters() == 2 }) | ||
|
|
||
| started, resume := make(chan struct{}), make(chan struct{}) | ||
| var startedOnce, resumeOnce sync.Once | ||
| defer resumeOnce.Do(func() { close(resume) }) | ||
| old.h.SetStreamHandler(api.AuthProtocolID, func(s network.Stream) { | ||
| startedOnce.Do(func() { close(started) }) | ||
| select { | ||
| case <-resume: | ||
| old.admit(s) | ||
| case <-ctx.Done(): | ||
| _ = s.Reset() | ||
| } | ||
| }) | ||
| dropFromRouter(t, old.h, node) | ||
| select { | ||
| case <-started: | ||
| case <-ctx.Done(): | ||
| t.Fatal("old router's redial never reached its handshake") | ||
| } | ||
| if got := node.topUpRouters(ctx); got != 2 { | ||
| t.Fatalf("top-up holds %d routers, want 2", got) | ||
| } | ||
| if node.isAttached(old.h.ID()) { | ||
| t.Error("replaced router is still held while its redial is pending") | ||
| } | ||
| resumeOnce.Do(func() { close(resume) }) | ||
| waitForRouterState(t, ctx, "old router's redial to finish", func() bool { | ||
| node.mu.Lock() | ||
| defer node.mu.Unlock() | ||
| return !node.redialing[old.h.ID()] | ||
| }) | ||
| got := node.attachedRouters() | ||
| if len(got) != 2 || !slices.Contains(got, kept.h.ID()) || !slices.Contains(got, spare.h.ID()) { | ||
| t.Errorf("routers after redial = %v, want kept %s and spare %s", got, kept.h.ID(), spare.h.ID()) | ||
| } | ||
| if node.connMgr.IsProtected(old.h.ID(), routerTag) { | ||
| t.Error("replaced router is still protected by the attachment tag") | ||
| } | ||
| node.mu.Lock() | ||
| relays := slices.Clone(node.currentRelays) | ||
| node.mu.Unlock() | ||
| if len(relays) != 2 { | ||
| t.Errorf("AutoRelay candidates = %v, want the two remaining attachments", relays) | ||
| } | ||
| for _, relay := range relays { | ||
| if relay.ID == old.h.ID() { | ||
| t.Error("replaced router is still offered to AutoRelay") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A delayed top-up and concurrent top-ups can finish after another path has | ||
| // already filled the target. Admission must check the live count atomically. | ||
| func TestFinishDialAndAttachKeepsRouterLimit(t *testing.T) { | ||
| for _, lost := range []bool{false, true} { | ||
| name := "already full" | ||
| if lost { | ||
| name = "concurrent replacements" | ||
| } | ||
| t.Run(name, func(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| kept := newFakeRouter(t, "/ip4/127.0.0.1/tcp/0") | ||
| one := newFakeRouterWithKey(t, kept.cpPub, kept.cpPriv, "/ip4/127.0.0.1/tcp/0") | ||
| two := newFakeRouterWithKey(t, kept.cpPub, kept.cpPriv, "/ip4/127.0.0.1/tcp/0") | ||
| node := kept.startNode(t, ctx, kept.routerAddrs, func(o *Options) { | ||
| o.Routers = 1 | ||
| o.RouterRedialDelay = -1 | ||
| o.MonitorBootstrap = time.Hour | ||
| }) | ||
| node.setRouterCatalog(candidatesFromAddrs(ctx, append(append(slices.Clone(kept.routerAddrs), one.routerAddrs...), two.routerAddrs...))) | ||
| if lost { | ||
| dropFromRouter(t, kept.h, node) | ||
| waitForRouterState(t, ctx, "old session to clear", func() bool { | ||
| node.mu.Lock() | ||
| defer node.mu.Unlock() | ||
| return !node.authenticatedRouters[kept.h.ID()] | ||
| }) | ||
| } | ||
| identity, err := node.loadIdentityForAuth() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for _, router := range []*fakeRouter{one, two} { | ||
| if err := node.authRouter(ctx, peer.AddrInfo{ID: router.h.ID(), Addrs: router.h.Addrs()}, identity); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
| node.mu.Lock() | ||
| if node.dialing == nil { | ||
| node.dialing = make(map[peer.ID]bool) | ||
| } | ||
| node.dialing[one.h.ID()] = true | ||
| node.dialing[two.h.ID()] = true | ||
| node.mu.Unlock() | ||
|
|
||
| ready := make(chan struct{}) | ||
| var attaching sync.WaitGroup | ||
| for _, id := range []peer.ID{one.h.ID(), two.h.ID()} { | ||
| attaching.Go(func() { | ||
| <-ready | ||
| node.finishDialAndAttach(id) | ||
| }) | ||
| } | ||
| close(ready) | ||
| attaching.Wait() | ||
| got := node.attachedRouters() | ||
| if len(got) != 1 { | ||
| t.Fatalf("routers = %v, want exactly one", got) | ||
| } | ||
| if !lost && got[0] != kept.h.ID() { | ||
| t.Errorf("router = %s, want existing live router %s", got[0], kept.h.ID()) | ||
| } | ||
| if lost && got[0] == kept.h.ID() { | ||
| t.Errorf("router = %s, want one of the replacements", got[0]) | ||
| } | ||
| node.mu.Lock() | ||
| pending := len(node.dialing) | ||
| node.mu.Unlock() | ||
| if pending != 0 { | ||
| t.Errorf("pending router dials = %d, want 0", pending) | ||
| } | ||
| for _, id := range []peer.ID{kept.h.ID(), one.h.ID(), two.h.ID()} { | ||
| want := id == got[0] | ||
| if node.isAttached(id) != want || node.connMgr.IsProtected(id, routerTag) != want { | ||
| t.Errorf("router %s attachment/protection must be %v", id, want) | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestFinishDialAndAttachRetainsRedialUntilTargetIsFull(t *testing.T) { | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| old := newFakeRouter(t, "/ip4/127.0.0.1/tcp/0") | ||
| one := newFakeRouterWithKey(t, old.cpPub, old.cpPriv, "/ip4/127.0.0.1/tcp/0") | ||
| two := newFakeRouterWithKey(t, old.cpPub, old.cpPriv, "/ip4/127.0.0.1/tcp/0") | ||
| node := old.startNode(t, ctx, old.routerAddrs, func(o *Options) { | ||
| o.Routers = 2 | ||
| o.RouterRedialDelay = -1 | ||
| o.MonitorBootstrap = time.Hour | ||
| }) | ||
| all := append(append(slices.Clone(old.routerAddrs), one.routerAddrs...), two.routerAddrs...) | ||
| node.setRouterCatalog(candidatesFromAddrs(ctx, all)) | ||
| dropFromRouter(t, old.h, node) | ||
| waitForRouterState(t, ctx, "old router's session to clear", func() bool { | ||
| node.mu.Lock() | ||
| defer node.mu.Unlock() | ||
| return !node.authenticatedRouters[old.h.ID()] | ||
| }) | ||
| identity, err := node.loadIdentityForAuth() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for i, router := range []*fakeRouter{one, two} { | ||
| if err := node.authRouter(ctx, peer.AddrInfo{ID: router.h.ID(), Addrs: router.h.Addrs()}, identity); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| node.finishDialAndAttach(router.h.ID()) | ||
| if got := node.isAttached(old.h.ID()); got != (i == 0) { | ||
| t.Errorf("old router held = %v after replacement %d, want %v", got, i+1, i == 0) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func waitForRouterState(t *testing.T, ctx context.Context, description string, ready func() bool) { | ||
| t.Helper() | ||
| tick := time.NewTicker(time.Millisecond) | ||
| defer tick.Stop() | ||
| for !ready() { | ||
| select { | ||
| case <-ctx.Done(): | ||
| t.Fatalf("waiting for %s: %v", description, ctx.Err()) | ||
| case <-tick.C: | ||
| } | ||
| } | ||
| } | ||
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.
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.
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.
The standard library
sync.WaitGroupdoes not have aGomethod. This will cause a compilation error. Useattaching.Add(1),defer attaching.Done(), and a standardgostatement instead.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.
sync.WaitGroup.Gowas added in Go 1.25: https://pkg.go.dev/sync#WaitGroup.Go. This repository requires Go 1.26.6 ingo.mod, so this method is available on its supported toolchain. I also reran all three router-replacement regression tests under-racewith Go 1.27.3; they compile and pass. Keeping the standard-library helper here.