Skip to content

Commit 84ffd2e

Browse files
authored
Merge pull request #17 from arangodb-helper/ipv6
Various IPv6 fixes
2 parents 25c0a36 + 3227e85 commit 84ffd2e

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

service/arangodb.go

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

117117
// A helper function:
118118

119-
func normalizeHost(address string) string {
120-
host, _, err := net.SplitHostPort(address)
121-
if err != nil {
122-
host = strings.Split(address, ":")[0]
123-
}
119+
func normalizeHostName(host string) string {
124120
if ip := net.ParseIP(host); ip != nil {
125121
if ip.IsLoopback() {
126-
return "127.0.0.1"
122+
return "localhost"
127123
}
128-
} else if host == "localhost" {
129-
return "127.0.0.1"
130124
}
131125
return host
132126
}
@@ -141,7 +135,8 @@ func testInstance(ctx context.Context, address string, port int) (up, cancelled
141135
go func() {
142136
client := &http.Client{Timeout: time.Second * 10}
143137
for i := 0; i < 300; i++ {
144-
url := fmt.Sprintf("http://%s:%d/_api/version", address, port)
138+
addr := net.JoinHostPort(address, strconv.Itoa(port))
139+
url := fmt.Sprintf("http://%s/_api/version", addr)
145140
r, e := client.Get(url)
146141
if e == nil && r != nil && r.StatusCode == 200 {
147142
instanceUp <- true
@@ -166,7 +161,7 @@ var confFileTemplate = `# ArangoDB configuration file
166161
#
167162
168163
[server]
169-
endpoint = tcp://0.0.0.0:%s
164+
endpoint = tcp://[::]:%s
170165
threads = %d
171166
172167
[log]
@@ -223,11 +218,12 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
223218
if s.ServerThreads != 0 {
224219
args = append(args, "--server.threads", strconv.Itoa(s.ServerThreads))
225220
}
221+
myTCPURL := "tcp://" + net.JoinHostPort(myAddress, myPort)
226222
switch mode {
227223
case "agent":
228224
args = append(args,
229225
"--agency.activate", "true",
230-
"--agency.my-address", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
226+
"--agency.my-address", myTCPURL,
231227
"--agency.size", strconv.Itoa(s.AgencySize),
232228
"--agency.supervision", "true",
233229
"--foxx.queues", "false",
@@ -237,23 +233,23 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
237233
if p.HasAgent && p.ID != s.ID {
238234
args = append(args,
239235
"--agency.endpoint",
240-
fmt.Sprintf("tcp://%s:%d", p.Address, s.MasterPort+p.PortOffset+portOffsetAgent),
236+
fmt.Sprintf("tcp://%s", net.JoinHostPort(p.Address, strconv.Itoa(s.MasterPort+p.PortOffset+portOffsetAgent))),
241237
)
242238
}
243239
}
244240
case "dbserver":
245241
args = append(args,
246-
"--cluster.my-address", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
242+
"--cluster.my-address", myTCPURL,
247243
"--cluster.my-role", "PRIMARY",
248-
"--cluster.my-local-info", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
244+
"--cluster.my-local-info", myTCPURL,
249245
"--foxx.queues", "false",
250246
"--server.statistics", "true",
251247
)
252248
case "coordinator":
253249
args = append(args,
254-
"--cluster.my-address", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
250+
"--cluster.my-address", myTCPURL,
255251
"--cluster.my-role", "COORDINATOR",
256-
"--cluster.my-local-info", fmt.Sprintf("tcp://%s:%s", myAddress, myPort),
252+
"--cluster.my-local-info", myTCPURL,
257253
"--foxx.queues", "true",
258254
"--server.statistics", "true",
259255
)
@@ -263,7 +259,7 @@ func (s *Service) makeBaseArgs(myHostDir, myContainerDir string, myAddress strin
263259
p := s.myPeers.Peers[i]
264260
args = append(args,
265261
"--cluster.agency-endpoint",
266-
fmt.Sprintf("tcp://%s:%d", p.Address, s.MasterPort+p.PortOffset+portOffsetAgent),
262+
fmt.Sprintf("tcp://%s", net.JoinHostPort(p.Address, strconv.Itoa(s.MasterPort+p.PortOffset+portOffsetAgent))),
267263
)
268264
}
269265
}

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)