Skip to content

Commit ed081b9

Browse files
committed
Merged in master
2 parents 95568bb + 5bc6e1f commit ed081b9

File tree

6 files changed

+117
-30
lines changed

6 files changed

+117
-30
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Changed from version 0.10.2 to master
1+
# Changes from version 0.10.2 to master
22

3+
- Support building in a directory other than the source directory. Set `BUILDDIR` (#98).
4+
- Testing a server instance now includes testing for the expected server role.
35
- On linux, also look for `arangod` in `/usr/local/sbin` (#93).
46

57
# Changes from version 0.10.1 to 0.10.2

Makefile

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,29 @@
11
PROJECT := arangodb
2-
SCRIPTDIR := $(shell pwd)
2+
ifndef SCRIPTDIR
3+
SCRIPTDIR := $(shell pwd)
4+
endif
35
ROOTDIR := $(shell cd $(SCRIPTDIR) && pwd)
46
VERSION := $(shell cat $(ROOTDIR)/VERSION)
57
VERSION_MAJOR_MINOR_PATCH := $(shell echo $(VERSION) | cut -f 1 -d '+')
68
VERSION_MAJOR_MINOR := $(shell echo $(VERSION_MAJOR_MINOR_PATCH) | cut -f 1,2 -d '.')
79
VERSION_MAJOR := $(shell echo $(VERSION_MAJOR_MINOR) | cut -f 1 -d '.')
810
COMMIT := $(shell git rev-parse --short HEAD)
9-
DOCKERCLI := $(shell which docker)
11+
MAKEFILE := $(ROOTDIR)/Makefile
1012

11-
GOBUILDDIR := $(SCRIPTDIR)/.gobuild
13+
ifndef NODOCKER
14+
DOCKERCLI := $(shell which docker)
15+
GOBUILDLINKTARGET := ../../../..
16+
else
17+
DOCKERCLI :=
18+
GOBUILDLINKTARGET := $(ROOTDIR)
19+
endif
20+
21+
ifndef BUILDDIR
22+
BUILDDIR := $(ROOTDIR)
23+
endif
24+
GOBUILDDIR := $(BUILDDIR)/.gobuild
1225
SRCDIR := $(SCRIPTDIR)
13-
BINDIR := $(ROOTDIR)/bin
26+
BINDIR := $(BUILDDIR)/bin
1427

1528
ORGPATH := github.com/arangodb-helper
1629
ORGDIR := $(GOBUILDDIR)/src/$(ORGPATH)
@@ -62,10 +75,10 @@ clean:
6275

6376
local:
6477
ifneq ("$(DOCKERCLI)", "")
65-
@${MAKE} -B GOOS=$(shell go env GOHOSTOS) GOARCH=$(shell go env GOHOSTARCH) build-local
78+
@${MAKE} -f $(MAKEFILE) -B GOOS=$(shell go env GOHOSTOS) GOARCH=$(shell go env GOHOSTARCH) build-local
6679
else
67-
@${MAKE} deps
68-
GOPATH=$(GOBUILDDIR) go build -o arangodb $(REPOPATH)
80+
@${MAKE} -f $(MAKEFILE) deps
81+
GOPATH=$(GOBUILDDIR) go build -o $(BUILDDIR)/arangodb $(REPOPATH)
6982
endif
7083

7184
build: $(BIN)
@@ -74,16 +87,16 @@ build-local: build
7487
@ln -sf $(BIN) $(ROOTDIR)/arangodb
7588

7689
binaries: $(GHRELEASE)
77-
@${MAKE} -B GOOS=linux GOARCH=amd64 build
78-
@${MAKE} -B GOOS=darwin GOARCH=amd64 build
79-
@${MAKE} -B GOOS=windows GOARCH=amd64 build
90+
@${MAKE} -f $(MAKEFILE) -B GOOS=linux GOARCH=amd64 build
91+
@${MAKE} -f $(MAKEFILE) -B GOOS=darwin GOARCH=amd64 build
92+
@${MAKE} -f $(MAKEFILE) -B GOOS=windows GOARCH=amd64 build
8093

8194
deps:
82-
@${MAKE} -B -s $(GOBUILDDIR)
95+
@${MAKE} -f $(MAKEFILE) -B SCRIPTDIR=$(SCRIPTDIR) BUILDDIR=$(BUILDDIR) -s $(GOBUILDDIR)
8396

8497
$(GOBUILDDIR):
8598
@mkdir -p $(ORGDIR)
86-
@rm -f $(REPODIR) && ln -s ../../../.. $(REPODIR)
99+
@rm -f $(REPODIR) && ln -s $(GOBUILDLINKTARGET) $(REPODIR)
87100
@rm -f $(GOBUILDDIR)/src/github.com/aktau && ln -s ../../../vendor/github.com/aktau $(GOBUILDDIR)/src/github.com/aktau
88101
@rm -f $(GOBUILDDIR)/src/github.com/dustin && ln -s ../../../vendor/github.com/dustin $(GOBUILDDIR)/src/github.com/dustin
89102
@rm -f $(GOBUILDDIR)/src/github.com/kballard && ln -s ../../../vendor/github.com/kballard $(GOBUILDDIR)/src/github.com/kballard

service/runtime_server_manager.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ type runtimeServerManagerContext interface {
5959
serverHostDir(serverType ServerType) (string, error)
6060

6161
// TestInstance checks the `up` status of an arangod server instance.
62-
TestInstance(ctx context.Context, address string, port int, statusChanged chan StatusItem) (up bool, version string, statusTrail []int, cancelled bool)
62+
TestInstance(ctx context.Context, address string, port int, expectedRole, expectedMode string,
63+
statusChanged chan StatusItem) (up, correctRole bool, version, role, mode string, statusTrail []int, cancelled bool)
6364

6465
// IsLocalSlave returns true if this peer is running as a local slave
6566
IsLocalSlave() bool
@@ -91,13 +92,17 @@ func startArangod(log *logging.Logger, runtimeContext runtimeServerManagerContex
9192
if p != nil {
9293
log.Infof("%s seems to be running already, checking port %d...", serverType, myPort)
9394
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
94-
up, _, _, _ := runtimeContext.TestInstance(ctx, myHostAddress, myPort, nil)
95+
expectedRole, expectedMode := serverType.ExpectedServerRole()
96+
up, correctRole, _, _, _, _, _ := runtimeContext.TestInstance(ctx, myHostAddress, myPort, expectedRole, expectedMode, nil)
9597
cancel()
96-
if up {
98+
if up && correctRole {
9799
log.Infof("%s is already running on %d. No need to start anything.", serverType, myPort)
98100
return p, false, nil
101+
} else if !up {
102+
log.Infof("%s is not up on port %d. Terminating existing process and restarting it...", serverType, myPort)
103+
} else if !correctRole {
104+
log.Infof("%s is not of role '%s' on port %d. Terminating existing process and restarting it...", serverType, expectedRole, myPort)
99105
}
100-
log.Infof("%s is not up on port %d. Terminating existing process and restarting it...", serverType, myPort)
101106
p.Terminate()
102107
}
103108

@@ -219,8 +224,9 @@ func (s *runtimeServerManager) runArangod(ctx context.Context, log *logging.Logg
219224
}
220225
}
221226
}()
222-
if up, version, statusTrail, cancelled := runtimeContext.TestInstance(ctx, myHostAddress, port, statusChanged); !cancelled {
223-
if up {
227+
expectedRole, expectedMode := serverType.ExpectedServerRole()
228+
if up, correctRole, version, role, mode, statusTrail, cancelled := runtimeContext.TestInstance(ctx, myHostAddress, port, expectedRole, expectedMode, statusChanged); !cancelled {
229+
if up && correctRole {
224230
log.Infof("%s up and running (version %s).", serverType, version)
225231
if (serverType == ServerTypeCoordinator && !runtimeContext.IsLocalSlave()) || serverType == ServerTypeSingle || serverType == ServerTypeResilientSingle {
226232
hostPort, err := p.HostPort(port)
@@ -243,8 +249,10 @@ func (s *runtimeServerManager) runArangod(ctx context.Context, log *logging.Logg
243249
s.logMutex.Unlock()
244250
}
245251
}
246-
} else {
252+
} else if !up {
247253
log.Warningf("%s not ready after 5min!: Status trail: %#v", serverType, statusTrail)
254+
} else if !correctRole {
255+
log.Warningf("%s does not have the expected role of '%s,%s' (but '%s,%s'): Status trail: %#v", serverType, expectedRole, expectedMode, role, mode, statusTrail)
248256
}
249257
}
250258
}()

service/server_type.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,21 @@ func (s ServerType) PortOffset() int {
5353
panic(fmt.Sprintf("Unknown ServerType: %s", string(s)))
5454
}
5555
}
56+
57+
// ExpectedServerRole returns the expected `role` & `mode` value resulting from a request to `_admin/server/role`.
58+
func (s ServerType) ExpectedServerRole() (string, string) {
59+
switch s {
60+
case ServerTypeCoordinator:
61+
return "COORDINATOR", ""
62+
case ServerTypeSingle:
63+
return "SINGLE", ""
64+
case ServerTypeResilientSingle:
65+
return "SINGLE", "resilient"
66+
case ServerTypeDBServer:
67+
return "PRIMARY", ""
68+
case ServerTypeAgent:
69+
return "AGENT", ""
70+
default:
71+
panic(fmt.Sprintf("Unknown ServerType: %s", string(s)))
72+
}
73+
}

service/service.go

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,16 @@ type StatusItem struct {
444444
Duration time.Duration
445445
}
446446

447+
type instanceUpInfo struct {
448+
Version string
449+
Role string
450+
Mode string
451+
}
452+
447453
// TestInstance checks the `up` status of an arangod server instance.
448-
func (s *Service) TestInstance(ctx context.Context, address string, port int, statusChanged chan StatusItem) (up bool, version string, statusTrail []int, cancelled bool) {
449-
instanceUp := make(chan string)
454+
func (s *Service) TestInstance(ctx context.Context, address string, port int,
455+
expectedRole, expectedMode string, statusChanged chan StatusItem) (up, correctRole bool, version, role, mode string, statusTrail []int, cancelled bool) {
456+
instanceUp := make(chan instanceUpInfo)
450457
statusCodes := make(chan int)
451458
if statusChanged != nil {
452459
defer close(statusChanged)
@@ -464,7 +471,7 @@ func (s *Service) TestInstance(ctx context.Context, address string, port int, st
464471
},
465472
}
466473
}
467-
makeRequest := func() (string, int, error) {
474+
makeVersionRequest := func() (string, int, error) {
468475
addr := net.JoinHostPort(address, strconv.Itoa(port))
469476
url := fmt.Sprintf("%s://%s/_api/version", scheme, addr)
470477
req, err := http.NewRequest("GET", url, nil)
@@ -491,24 +498,63 @@ func (s *Service) TestInstance(ctx context.Context, address string, port int, st
491498
}
492499
return versionResponse.Version, resp.StatusCode, nil
493500
}
501+
makeRoleRequest := func() (string, string, int, error) {
502+
addr := net.JoinHostPort(address, strconv.Itoa(port))
503+
url := fmt.Sprintf("%s://%s/_admin/server/role", scheme, addr)
504+
req, err := http.NewRequest("GET", url, nil)
505+
if err != nil {
506+
return "", "", -1, maskAny(err)
507+
}
508+
if err := addJwtHeader(req, s.jwtSecret); err != nil {
509+
return "", "", -2, maskAny(err)
510+
}
511+
resp, err := client.Do(req)
512+
if err != nil {
513+
return "", "", -3, maskAny(err)
514+
}
515+
if resp.StatusCode != 200 {
516+
return "", "", resp.StatusCode, maskAny(fmt.Errorf("Invalid status %d", resp.StatusCode))
517+
}
518+
roleResponse := struct {
519+
Role string `json:"role,omitempty"`
520+
Mode string `json:"mode,omitempty"`
521+
}{}
522+
defer resp.Body.Close()
523+
decoder := json.NewDecoder(resp.Body)
524+
if err := decoder.Decode(&roleResponse); err != nil {
525+
return "", "", -4, maskAny(fmt.Errorf("Unexpected role response: %#v", err))
526+
}
527+
return roleResponse.Role, roleResponse.Mode, resp.StatusCode, nil
528+
}
494529

495530
for i := 0; i < 300; i++ {
496-
if version, statusCode, err := makeRequest(); err == nil {
497-
instanceUp <- version
531+
if version, statusCode, err := makeVersionRequest(); err == nil {
532+
if role, mode, statusCode, err := makeRoleRequest(); err == nil {
533+
instanceUp <- instanceUpInfo{
534+
Version: version,
535+
Role: role,
536+
Mode: mode,
537+
}
538+
} else {
539+
statusCodes <- statusCode
540+
}
498541
break
499542
} else {
500543
statusCodes <- statusCode
501544
}
502545
time.Sleep(time.Millisecond * 500)
503546
}
504-
instanceUp <- ""
547+
instanceUp <- instanceUpInfo{}
505548
}()
506549
statusTrail = make([]int, 0, 16)
507550
startTime := time.Now()
508551
for {
509552
select {
510-
case version := <-instanceUp:
511-
return version != "", version, statusTrail, false
553+
case instanceInfo := <-instanceUp:
554+
up = instanceInfo.Version != ""
555+
correctRole = instanceInfo.Role == expectedRole || expectedRole == ""
556+
correctMode := instanceInfo.Mode == expectedMode || expectedMode == ""
557+
return up, correctRole && correctMode, instanceInfo.Version, instanceInfo.Role, instanceInfo.Mode, statusTrail, false
512558
case statusCode := <-statusCodes:
513559
lastStatusCode := math.MinInt32
514560
if len(statusTrail) > 0 {
@@ -525,7 +571,7 @@ func (s *Service) TestInstance(ctx context.Context, address string, port int, st
525571
}
526572
}
527573
case <-ctx.Done():
528-
return false, "", statusTrail, true
574+
return false, false, "", "", "", statusTrail, true
529575
}
530576
}
531577
}

start.go

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

0 commit comments

Comments
 (0)