Skip to content

Commit 33db08e

Browse files
authored
Merge pull request #631 from nats-io/fix_connect_urls_notification_with_explicit_routes
[FIXED] Cluster toplogy change possibly not sent to clients
2 parents 3b58264 + aeca31c commit 33db08e

File tree

3 files changed

+62
-3
lines changed

3 files changed

+62
-3
lines changed

server/route.go

+3
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,9 @@ func (s *Server) addRoute(c *client, info *Info) (bool, bool) {
564564
remote.mu.Lock()
565565
// r will be not nil if c.route.didSolicit was true
566566
if r != nil {
567+
// If we upgrade to solicited, we still want to keep the remote's
568+
// connectURLs. So transfer those.
569+
r.connectURLs = remote.route.connectURLs
567570
remote.route = r
568571
}
569572
// This is to mitigate the issue where both sides add the route

server/routes_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -728,3 +728,58 @@ func TestRoutesToEachOther(t *testing.T) {
728728
t.Log("Was not able to get duplicate route this time!")
729729
}
730730
}
731+
732+
func TestConnectULRsWithRoutesToEachOther(t *testing.T) {
733+
optsA := DefaultOptions()
734+
optsA.Host = "127.0.0.1"
735+
optsA.Cluster.Port = 7246
736+
optsA.Routes = RoutesFromStr("nats://127.0.0.1:7247")
737+
738+
optsB := DefaultOptions()
739+
optsB.Host = "127.0.0.1"
740+
optsB.Cluster.Port = 7247
741+
optsB.Routes = RoutesFromStr("nats://127.0.0.1:7246")
742+
743+
// Start servers with go routines to increase change of
744+
// each server connecting to each other at the same time.
745+
srvA := New(optsA)
746+
defer srvA.Shutdown()
747+
748+
srvB := New(optsB)
749+
defer srvB.Shutdown()
750+
751+
go srvA.Start()
752+
go srvB.Start()
753+
754+
// Wait for cluster to be formed
755+
checkClusterFormed(t, srvA, srvB)
756+
757+
// Connect to serverB
758+
url := fmt.Sprintf("nats://%s", srvB.Addr().String())
759+
nc, err := nats.Connect(url)
760+
if err != nil {
761+
t.Fatalf("Error on connect: %v", err)
762+
}
763+
defer nc.Close()
764+
ds := nc.Servers()
765+
if len(ds) != 2 {
766+
t.Fatalf("Expected 2 servers, got %v", ds)
767+
}
768+
769+
// Shutdown server A and make sure that we are notfied
770+
// that server A is no longer running.
771+
srvA.Shutdown()
772+
timeout := time.Now().Add(5 * time.Second)
773+
ok := false
774+
for time.Now().Before(timeout) {
775+
ds = nc.Servers()
776+
if len(ds) == 1 {
777+
ok = true
778+
break
779+
}
780+
time.Sleep(50 * time.Millisecond)
781+
}
782+
if !ok {
783+
t.Fatalf("List of servers should be only 1, got %v", ds)
784+
}
785+
}

server/server.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,11 @@ func (s *Server) logPid() error {
255255
func (s *Server) Start() {
256256
s.Noticef("Starting nats-server version %s", VERSION)
257257
s.Debugf("Go build version %s", s.info.GoVersion)
258-
if gitCommit == "" {
259-
gitCommit = "not set"
258+
gc := gitCommit
259+
if gc == "" {
260+
gc = "not set"
260261
}
261-
s.Noticef("Git commit [%s]", gitCommit)
262+
s.Noticef("Git commit [%s]", gc)
262263

263264
// Avoid RACE between Start() and Shutdown()
264265
s.mu.Lock()

0 commit comments

Comments
 (0)