Skip to content

Commit 2f95ba4

Browse files
committed
Show "Your resilient single server can now be accessed with ..." message only on leader
1 parent 1cf0cbf commit 2f95ba4

File tree

4 files changed

+50
-18
lines changed

4 files changed

+50
-18
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changes from version 0.10.4 to master
22

3+
- For `activefailover` deployments, the message where to reach your database is now only shown for the current leader.
34
- Added `--log.dir` option to configure a custom directory to which all log files will be written.
45
- It is no longer allowed to use `log.file` as a passthrough option.
56
- Added `--starter.host` option, to bind the HTTP server to a specific network interface instead of the default `0.0.0.0`.

service/runtime_server_manager.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type runtimeServerManagerContext interface {
7575

7676
// TestInstance checks the `up` status of an arangod server instance.
7777
TestInstance(ctx context.Context, serverType ServerType, address string, port int,
78-
statusChanged chan StatusItem) (up, correctRole bool, version, role, mode string, statusTrail []int, cancelled bool)
78+
statusChanged chan StatusItem) (up, correctRole bool, version, role, mode string, isLeader bool, statusTrail []int, cancelled bool)
7979

8080
// IsLocalSlave returns true if this peer is running as a local slave
8181
IsLocalSlave() bool
@@ -116,7 +116,7 @@ func startServer(ctx context.Context, log *logging.Logger, runtimeContext runtim
116116
if p != nil {
117117
log.Infof("%s seems to be running already, checking port %d...", serverType, myPort)
118118
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
119-
up, correctRole, _, _, _, _, _ := runtimeContext.TestInstance(ctx, serverType, myHostAddress, myPort, nil)
119+
up, correctRole, _, _, _, _, _, _ := runtimeContext.TestInstance(ctx, serverType, myHostAddress, myPort, nil)
120120
cancel()
121121
if up && correctRole {
122122
log.Infof("%s is already running on %d. No need to start anything.", serverType, myPort)
@@ -271,7 +271,7 @@ func (s *runtimeServerManager) runServer(ctx context.Context, log *logging.Logge
271271
}
272272
}
273273
}()
274-
if up, correctRole, version, role, mode, statusTrail, cancelled := runtimeContext.TestInstance(ctx, serverType, myHostAddress, port, statusChanged); !cancelled {
274+
if up, correctRole, version, role, mode, isLeader, statusTrail, cancelled := runtimeContext.TestInstance(ctx, serverType, myHostAddress, port, statusChanged); !cancelled {
275275
if up && correctRole {
276276
log.Infof("%s up and running (version %s).", serverType, version)
277277
if (serverType == ServerTypeCoordinator && !runtimeContext.IsLocalSlave()) || serverType == ServerTypeSingle || serverType == ServerTypeResilientSingle {
@@ -289,10 +289,12 @@ func (s *runtimeServerManager) runServer(ctx context.Context, log *logging.Logge
289289
} else if serverType == ServerTypeResilientSingle {
290290
what = "resilient single server"
291291
}
292-
s.logMutex.Lock()
293-
log.Infof("Your %s can now be accessed with a browser at `%s://%s:%d` or", what, urlSchemes.Browser, ip, hostPort)
294-
log.Infof("using `arangosh --server.endpoint %s://%s:%d`.", urlSchemes.ArangoSH, ip, hostPort)
295-
s.logMutex.Unlock()
292+
if serverType != ServerTypeResilientSingle || isLeader {
293+
s.logMutex.Lock()
294+
log.Infof("Your %s can now be accessed with a browser at `%s://%s:%d` or", what, urlSchemes.Browser, ip, hostPort)
295+
log.Infof("using `arangosh --server.endpoint %s://%s:%d`.", urlSchemes.ArangoSH, ip, hostPort)
296+
s.logMutex.Unlock()
297+
}
296298
runtimeContext.removeRecoveryFile()
297299
}
298300
}

service/service.go

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -542,14 +542,15 @@ type StatusItem struct {
542542
}
543543

544544
type instanceUpInfo struct {
545-
Version string
546-
Role string
547-
Mode string
545+
Version string
546+
Role string
547+
Mode string
548+
IsLeader bool
548549
}
549550

550551
// TestInstance checks the `up` status of an arangod server instance.
551552
func (s *Service) TestInstance(ctx context.Context, serverType ServerType, address string, port int,
552-
statusChanged chan StatusItem) (up, correctRole bool, version, role, mode string, statusTrail []int, cancelled bool) {
553+
statusChanged chan StatusItem) (up, correctRole bool, version, role, mode string, isLeader bool, statusTrail []int, cancelled bool) {
553554
instanceUp := make(chan instanceUpInfo)
554555
statusCodes := make(chan int)
555556
if statusChanged != nil {
@@ -666,14 +667,42 @@ func (s *Service) TestInstance(ctx context.Context, serverType ServerType, addre
666667
}
667668
return roleResponse.Role, roleResponse.Mode, resp.StatusCode, nil
668669
}
670+
makeIsLeaderRequest := func() (bool, error) {
671+
if serverType != ServerTypeResilientSingle {
672+
return false, nil
673+
}
674+
addr := net.JoinHostPort(address, strconv.Itoa(port))
675+
url := fmt.Sprintf("%s://%s/_api/database", scheme, addr)
676+
req, err := http.NewRequest("GET", url, nil)
677+
if err != nil {
678+
return false, maskAny(err)
679+
}
680+
if err := addJwtHeader(req, s.jwtSecret); err != nil {
681+
return false, maskAny(err)
682+
}
683+
resp, err := client.Do(req)
684+
if err != nil {
685+
return false, maskAny(err)
686+
}
687+
if resp.StatusCode == 200 {
688+
return true, nil
689+
}
690+
if resp.StatusCode == 503 && resp.Header.Get("X-Arango-Endpoint") != "" {
691+
return false, nil
692+
}
693+
return false, maskAny(fmt.Errorf("Invalid status %d", resp.StatusCode))
694+
}
669695

670696
for i := 0; i < 300; i++ {
671697
if version, statusCode, err := makeVersionRequest(); err == nil {
672698
if role, mode, statusCode, err := makeRoleRequest(); err == nil {
673-
instanceUp <- instanceUpInfo{
674-
Version: version,
675-
Role: role,
676-
Mode: mode,
699+
if isLeader, err := makeIsLeaderRequest(); err == nil {
700+
instanceUp <- instanceUpInfo{
701+
Version: version,
702+
Role: role,
703+
Mode: mode,
704+
IsLeader: isLeader,
705+
}
677706
}
678707
} else {
679708
statusCodes <- statusCode
@@ -695,7 +724,7 @@ func (s *Service) TestInstance(ctx context.Context, serverType ServerType, addre
695724
up = instanceInfo.Version != ""
696725
correctRole = instanceInfo.Role == expectedRole || expectedRole == ""
697726
correctMode := instanceInfo.Mode == expectedMode || expectedMode == ""
698-
return up, correctRole && correctMode, instanceInfo.Version, instanceInfo.Role, instanceInfo.Mode, statusTrail, false
727+
return up, correctRole && correctMode, instanceInfo.Version, instanceInfo.Role, instanceInfo.Mode, instanceInfo.IsLeader, statusTrail, false
699728
case statusCode := <-statusCodes:
700729
lastStatusCode := math.MinInt32
701730
if len(statusTrail) > 0 {
@@ -712,7 +741,7 @@ func (s *Service) TestInstance(ctx context.Context, serverType ServerType, addre
712741
}
713742
}
714743
case <-ctx.Done():
715-
return false, false, "", "", "", statusTrail, true
744+
return false, false, "", "", "", false, statusTrail, true
716745
}
717746
}
718747
}

start.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func cmdStartRun(cmd *cobra.Command, args []string) {
137137
allUp := true
138138
for _, server := range list.Servers {
139139
ctx, cancel := context.WithTimeout(rootCtx, time.Second)
140-
up, _, _, _, _, _, _ := service.TestInstance(ctx, svc.ServerType(server.Type), server.IP, server.Port, nil)
140+
up, _, _, _, _, _, _, _ := service.TestInstance(ctx, svc.ServerType(server.Type), server.IP, server.Port, nil)
141141
cancel()
142142
if !up {
143143
allUp = false

0 commit comments

Comments
 (0)