Skip to content

Commit bc30a7e

Browse files
committed
Prefer connection address over system table addresses
There is an unintended change in 2.0-rc1 causing the driver to prefer system table addresses over the connection address which breaks deployments that rely on this behavior from 1.x. This patch fixes this and keeps the behavior the same as it was in 1.x. Patch by João Reis; reviewed by TBD for CASSGO-91
1 parent 5c60751 commit bc30a7e

File tree

3 files changed

+84
-10
lines changed

3 files changed

+84
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7676

7777
- Driver closes connection when timeout occurs (CASSGO-87)
7878
- Do not set beta protocol flag when using v5 (CASSGO-88)
79+
- Driver is using system table ip addresses over the connection address (CASSGO-91)
7980

8081
#### 2.0.0-rc1
8182

docker_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//go:build all || cassandra
2+
// +build all cassandra
3+
4+
/*
5+
* Licensed to the Apache Software Foundation (ASF) under one
6+
* or more contributor license agreements. See the NOTICE file
7+
* distributed with this work for additional information
8+
* regarding copyright ownership. The ASF licenses this file
9+
* to you under the Apache License, Version 2.0 (the
10+
* "License"); you may not use this file except in compliance
11+
* with the License. You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
*/
21+
/*
22+
* Content before git sha 34fdeebefcbf183ed7f916f931aa0586fdaa1b40
23+
* Copyright (c) 2016, The Gocql authors,
24+
* provided under the BSD-3-Clause License.
25+
* See the NOTICE file distributed with this work for additional information.
26+
*/
27+
28+
package gocql
29+
30+
import (
31+
"fmt"
32+
"os/exec"
33+
"strings"
34+
"testing"
35+
"time"
36+
37+
"github.com/stretchr/testify/assert"
38+
)
39+
40+
// This test tests that gocql is able to connect to a C* node provisioned with Docker
41+
// This is useful to make sure we don't break common testing configurations
42+
func TestDocker(t *testing.T) {
43+
version := "3.11.11"
44+
randomUuid := MustRandomUUID().String()
45+
err := exec.Command("docker", "run", "-d", "--name", randomUuid, "-p", "9080:9042", fmt.Sprintf("cassandra:%s", version)).Run()
46+
defer exec.Command("docker", "rm", "-f", randomUuid).Run()
47+
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
52+
cluster := NewCluster("localhost:9080")
53+
cluster.Logger = NewLogger(LogLevelDebug)
54+
55+
timer := time.After(60 * time.Second)
56+
var session *Session
57+
done := false
58+
for !done {
59+
select {
60+
case <-timer:
61+
t.Fatalf("timed out, last err: %v", err)
62+
default:
63+
session, err = cluster.CreateSession()
64+
if err == nil {
65+
done = true
66+
} else if strings.Contains(err.Error(), "unable to discover protocol version") {
67+
time.Sleep(5 * time.Second)
68+
} else {
69+
t.Fatal(err)
70+
}
71+
}
72+
}
73+
74+
defer session.Close()
75+
var parsedVersion string
76+
err = session.Query("SELECT release_version FROM system.local").Scan(&parsedVersion)
77+
if err != nil {
78+
t.Fatalf("failed to query: %s", err)
79+
}
80+
81+
assert.Equal(t, version, parsedVersion)
82+
}

host_source.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -680,16 +680,7 @@ func newHostInfoFromRow(s *Session, defaultAddr net.IP, defaultPort int, row map
680680
}
681681
}
682682

683-
// Determine the connect address from available addresses
684-
if validIpAddr(host.rpcAddress) {
685-
host.connectAddress = host.rpcAddress
686-
} else if validIpAddr(host.preferredIP) {
687-
host.connectAddress = host.preferredIP
688-
} else if validIpAddr(host.broadcastAddress) {
689-
host.connectAddress = host.broadcastAddress
690-
} else if validIpAddr(host.peer) {
691-
host.connectAddress = host.peer
692-
}
683+
host.connectAddress, _ = host.connectAddressLocked()
693684

694685
if s != nil && s.cfg.AddressTranslator != nil {
695686
ip, port := s.cfg.translateAddressPort(host.ConnectAddress(), host.port, s.logger)

0 commit comments

Comments
 (0)