Skip to content

Commit dce477f

Browse files
committed
Merged changes from master
2 parents 03377df + 84ffd2e commit dce477f

File tree

6 files changed

+54
-48
lines changed

6 files changed

+54
-48
lines changed

service/arangodb.go

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,11 @@ const (
117117

118118
// A helper function:
119119

120-
func normalizeHost(address string) string {
121-
host, _, err := net.SplitHostPort(address)
122-
if err != nil {
123-
host = strings.Split(address, ":")[0]
124-
}
120+
func normalizeHostName(host string) string {
125121
if ip := net.ParseIP(host); ip != nil {
126122
if ip.IsLoopback() {
127-
return "127.0.0.1"
123+
return "localhost"
128124
}
129-
} else if host == "localhost" {
130-
return "127.0.0.1"
131125
}
132126
return host
133127
}
@@ -141,9 +135,9 @@ func (s *Service) testInstance(ctx context.Context, address string, port int) (u
141135
instanceUp := make(chan bool)
142136
go func() {
143137
client := &http.Client{Timeout: time.Second * 10}
144-
145138
makeRequest := func() error {
146-
url := fmt.Sprintf("http://%s:%d/_api/version", address, port)
139+
addr := net.JoinHostPort(address, strconv.Itoa(port))
140+
url := fmt.Sprintf("http://%s/_api/version", addr)
147141
req, err := http.NewRequest("GET", url, nil)
148142
if err != nil {
149143
return maskAny(err)
@@ -178,24 +172,6 @@ func (s *Service) testInstance(ctx context.Context, address string, port int) (u
178172
}
179173
}
180174

181-
var confFileTemplate = `# ArangoDB configuration file
182-
#
183-
# Documentation:
184-
# https://docs.arangodb.com/Manual/Administration/Configuration/
185-
#
186-
187-
[server]
188-
endpoint = tcp://0.0.0.0:%s
189-
threads = %d
190-
%s
191-
192-
[log]
193-
level = %s
194-
195-
[javascript]
196-
v8-contexts = %d
197-
`
198-
199175
func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress string, myPort string, mode string) (args []string, configVolumes []Volume) {
200176
hostConfFileName := filepath.Join(myHostDir, "arangod.conf")
201177
containerConfFileName := filepath.Join(myContainerDir, "arangod.conf")
@@ -226,7 +202,7 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
226202
serverSection := &configSection{
227203
Name: "server",
228204
Settings: map[string]string{
229-
"endpoint": fmt.Sprintf("tcp://0.0.0.0:%s", myPort),
205+
"endpoint": fmt.Sprintf("tcp://[::]:%s", myPort),
230206
"threads": threads,
231207
"authentication": "false",
232208
},
@@ -279,11 +255,12 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
279255
if s.ServerThreads != 0 {
280256
args = append(args, "--server.threads", strconv.Itoa(s.ServerThreads))
281257
}
258+
myTCPURL := "tcp://" + net.JoinHostPort(myAddress, myPort)
282259
switch mode {
283260
case "agent":
284261
args = append(args,
285262
"--agency.activate", "true",
286-
"--agency.my-address", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
263+
"--agency.my-address", myTCPURL,
287264
"--agency.size", strconv.Itoa(s.AgencySize),
288265
"--agency.supervision", "true",
289266
"--foxx.queues", "false",
@@ -293,23 +270,23 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
293270
if p.HasAgent && p.ID != s.ID {
294271
args = append(args,
295272
"--agency.endpoint",
296-
fmt.Sprintf("tcp://%s:%d", p.Address, s.MasterPort+p.PortOffset+portOffsetAgent),
273+
fmt.Sprintf("tcp://%s", net.JoinHostPort(p.Address, strconv.Itoa(s.MasterPort+p.PortOffset+portOffsetAgent))),
297274
)
298275
}
299276
}
300277
case "dbserver":
301278
args = append(args,
302-
"--cluster.my-address", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
279+
"--cluster.my-address", myTCPURL,
303280
"--cluster.my-role", "PRIMARY",
304-
"--cluster.my-local-info", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
281+
"--cluster.my-local-info", myTCPURL,
305282
"--foxx.queues", "false",
306283
"--server.statistics", "true",
307284
)
308285
case "coordinator":
309286
args = append(args,
310-
"--cluster.my-address", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
287+
"--cluster.my-address", myTCPURL,
311288
"--cluster.my-role", "COORDINATOR",
312-
"--cluster.my-local-info", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
289+
"--cluster.my-local-info", myTCPURL,
313290
"--foxx.queues", "true",
314291
"--server.statistics", "true",
315292
)
@@ -319,7 +296,7 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
319296
p := s.myPeers.Peers[i]
320297
args = append(args,
321298
"--cluster.agency-endpoint",
322-
fmt.Sprintf("tcp://%s:%d", p.Address, s.MasterPort+p.PortOffset+portOffsetAgent),
299+
fmt.Sprintf("tcp://%s", net.JoinHostPort(p.Address, strconv.Itoa(s.MasterPort+p.PortOffset+portOffsetAgent))),
323300
)
324301
}
325302
}

service/peers.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
package service
22

3+
import (
4+
"fmt"
5+
"net"
6+
"strconv"
7+
"strings"
8+
)
9+
310
type Peer struct {
411
ID string // Unique of of the peer
512
Address string // IP address of arangodb peer server
@@ -9,6 +16,13 @@ type Peer struct {
916
HasAgent bool // If set, this peer is running an agent
1017
}
1118

19+
// CreateStarterURL creates a URL to the relative path to the starter on this peer.
20+
func (p Peer) CreateStarterURL(relPath string) string {
21+
addr := net.JoinHostPort(p.Address, strconv.Itoa(p.Port))
22+
relPath = strings.TrimPrefix(relPath, "/")
23+
return fmt.Sprintf("http://%s/%s", addr, relPath)
24+
}
25+
1226
// Peer information.
1327
// When this type (or any of the types used in here) is changed, increase `SetupConfigVersion`.
1428
type peers struct {

service/runner_docker.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"fmt"
55
"io/ioutil"
6+
"net"
67
"os"
78
"path/filepath"
89
"strconv"
@@ -243,7 +244,7 @@ func (r *dockerRunner) CreateStartArangodbCommand(index int, masterIP string, ma
243244
addr := masterIP
244245
hostPort := 4000 + (portOffsetIncrement * (index - 1))
245246
if masterPort != "" {
246-
addr = addr + ":" + masterPort
247+
addr = net.JoinHostPort(addr, masterPort)
247248
masterPortI, _ := strconv.Atoi(masterPort)
248249
hostPort = masterPortI + (portOffsetIncrement * (index - 1))
249250
}

service/runner_process.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"fmt"
55
"io/ioutil"
6+
"net"
67
"os"
78
"os/exec"
89
"path/filepath"
@@ -80,7 +81,7 @@ func (r *processRunner) CreateStartArangodbCommand(index int, masterIP string, m
8081
}
8182
addr := masterIP
8283
if masterPort != "" {
83-
addr = addr + ":" + masterPort
84+
addr = net.JoinHostPort(addr, masterPort)
8485
}
8586
return fmt.Sprintf("arangodb --dataDir=./db%d --join %s", index, addr)
8687
}

service/server.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ import (
55
"fmt"
66
"io"
77
"io/ioutil"
8+
"net"
89
"net/http"
910
"os"
1011
"path/filepath"
12+
"strconv"
1113
)
1214

1315
type HelloRequest struct {
1416
SlaveID string // Unique ID of the slave
15-
SlaveAddress string // Address used to reach the slave (if empty, this will be derived from the request)
17+
SlaveAddress string // IP address used to reach the slave (if empty, this will be derived from the request)
1618
SlavePort int // Port used to reach the slave
1719
DataDir string // Directory used for data by this slave
1820
}
@@ -58,7 +60,7 @@ func (s *Service) startHTTPServer() {
5860
s.log.Fatalf("Failed to get HTTP port info: %#v", err)
5961
}
6062
addr := fmt.Sprintf("0.0.0.0:%d", containerPort)
61-
s.log.Infof("Listening on %s (%s:%d)", addr, s.OwnAddress, hostPort)
63+
s.log.Infof("Listening on %s (%s)", addr, net.JoinHostPort(s.OwnAddress, strconv.Itoa(hostPort)))
6264
if err := http.ListenAndServe(addr, nil); err != nil {
6365
s.log.Errorf("Failed to listen on %s: %v", addr, err)
6466
}
@@ -77,7 +79,7 @@ func (s *Service) helloHandler(w http.ResponseWriter, r *http.Request) {
7779
header := w.Header()
7880
if len(s.myPeers.Peers) > 0 {
7981
master := s.myPeers.Peers[0]
80-
header.Add("Location", fmt.Sprintf("http://%s:%d/hello", master.Address, master.Port))
82+
header.Add("Location", master.CreateStarterURL("/hello"))
8183
w.WriteHeader(http.StatusTemporaryRedirect)
8284
} else {
8385
writeError(w, http.StatusBadRequest, "No master known.")
@@ -87,7 +89,12 @@ func (s *Service) helloHandler(w http.ResponseWriter, r *http.Request) {
8789

8890
// Learn my own address (if needed)
8991
if len(s.myPeers.Peers) == 0 {
90-
myself := normalizeHost(r.Host)
92+
host, _, err := net.SplitHostPort(r.Host)
93+
if err != nil {
94+
writeError(w, http.StatusBadRequest, fmt.Sprintf("Cannot derive own host address: %v", err))
95+
return
96+
}
97+
myself := normalizeHostName(host)
9198
_, hostPort, _ := s.getHTTPServerPort()
9299
s.myPeers.Peers = []Peer{
93100
Peer{
@@ -118,9 +125,14 @@ func (s *Service) helloHandler(w http.ResponseWriter, r *http.Request) {
118125

119126
slaveAddr := req.SlaveAddress
120127
if slaveAddr == "" {
121-
slaveAddr = normalizeHost(r.RemoteAddr)
128+
host, _, err := net.SplitHostPort(r.RemoteAddr)
129+
if err != nil {
130+
writeError(w, http.StatusBadRequest, "SlaveAddress must be set.")
131+
return
132+
}
133+
slaveAddr = normalizeHostName(host)
122134
} else {
123-
slaveAddr = normalizeHost(slaveAddr)
135+
slaveAddr = normalizeHostName(slaveAddr)
124136
}
125137
slavePort := req.SlavePort
126138

service/slave.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ func (s *Service) startSlave(peerAddress string, runner Runner) {
1818
masterPort, _ = strconv.Atoi(port)
1919
}
2020
for {
21-
s.log.Infof("Contacting master %s:%d...", peerAddress, masterPort)
21+
masterAddr := net.JoinHostPort(peerAddress, strconv.Itoa(masterPort))
22+
s.log.Infof("Contacting master %s...", masterAddr)
2223
_, hostPort, err := s.getHTTPServerPort()
2324
if err != nil {
2425
s.log.Fatalf("Failed to get HTTP server port: %#v", err)
@@ -31,7 +32,7 @@ func (s *Service) startSlave(peerAddress string, runner Runner) {
3132
})
3233
buf := bytes.Buffer{}
3334
buf.Write(b)
34-
r, e := http.Post(fmt.Sprintf("http://%s:%d/hello", peerAddress, masterPort), "application/json", &buf)
35+
r, e := http.Post(fmt.Sprintf("http://%s/hello", masterAddr), "application/json", &buf)
3536
if e != nil {
3637
s.log.Infof("Cannot start because of error from master: %v", e)
3738
time.Sleep(time.Second)
@@ -76,7 +77,7 @@ func (s *Service) startSlave(peerAddress string, runner Runner) {
7677
}
7778
time.Sleep(time.Second)
7879
master := s.myPeers.Peers[0]
79-
r, err := http.Get(fmt.Sprintf("http://%s:%d/hello", master.Address, master.Port))
80+
r, err := http.Get(master.CreateStarterURL("/hello"))
8081
if err != nil {
8182
s.log.Errorf("Failed to connect to master: %v", err)
8283
time.Sleep(time.Second * 2)
@@ -97,7 +98,7 @@ func (s *Service) sendMasterGoodbye() error {
9798
// I'm the master, do nothing
9899
return nil
99100
}
100-
u := fmt.Sprintf("http://%s:%d/goodbye", master.Address, master.Port)
101+
u := master.CreateStarterURL("/goodbye")
101102
s.log.Infof("Saying goodbye to master at %s", u)
102103
req := GoodbyeRequest{SlaveID: s.ID}
103104
data, err := json.Marshal(req)

0 commit comments

Comments
 (0)