Skip to content

Commit f5b6f8a

Browse files
committed
Fix DisableInitialHostLookup flag ignored when querying system.peers(PR update)
This commit provides update for the 'Modified DisableInitialHostLookup flag name and logic' refreshRing() does NOOP when 'DisableInitialHostLookup' is true. system.peers can be queried when flag is true for the schema version retrieving. patch by Oleksandr Luzhniy; for CASSGO-5
1 parent 30207ca commit f5b6f8a

File tree

7 files changed

+56
-107
lines changed

7 files changed

+56
-107
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
5050

5151
- Refactoring hostpool package test and Expose HostInfo creation (CASSGO-59)
5252

53+
- Fix DisableInitialHostLookup flag ignored when querying system.peers (CASSGO-5)
54+
5355
### Fixed
5456
- Cassandra version unmarshal fix (CASSGO-49)
5557

cassandra_test.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import (
4444
"time"
4545
"unicode"
4646

47-
inf "gopkg.in/inf.v0"
47+
"gopkg.in/inf.v0"
4848

4949
"github.com/stretchr/testify/require"
5050
)
@@ -108,6 +108,40 @@ func TestUseStatementError(t *testing.T) {
108108
}
109109
}
110110

111+
// TestDisableHostLookupRingRefresh checks that session.ring will not be updated if cluster.DisableInitialHostLookup == true
112+
func TestDisableHostLookupRingRefresh(t *testing.T) {
113+
cluster := createCluster()
114+
cluster.DisableInitialHostLookup = true
115+
116+
cluster.NumConns = 1
117+
session := createSessionFromCluster(cluster, t)
118+
defer session.Close()
119+
120+
oldHosts := make(map[string]*HostInfo)
121+
122+
for key, host := range session.ring.hosts {
123+
host.broadcastAddress = net.ParseIP("10.10.10.10")
124+
oldHosts[key] = host
125+
}
126+
127+
// if DisableInitialHostLookup == true - host.broadcastAddress must not be updated.
128+
err := session.refreshRing()
129+
if err != nil {
130+
t.Fatal(err)
131+
}
132+
133+
for key, host := range session.ring.hosts {
134+
oldHost, ok := oldHosts[key]
135+
if !ok {
136+
t.Fatalf("old host not found for key: %s", key)
137+
}
138+
139+
if !oldHost.broadcastAddress.Equal(host.broadcastAddress) {
140+
t.Fatalf("broadcast addresses do not match for key: %s", key)
141+
}
142+
}
143+
}
144+
111145
// TestInvalidKeyspace checks that an invalid keyspace will return promptly and without a flood of connections
112146
func TestInvalidKeyspace(t *testing.T) {
113147
cluster := createCluster()

cluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,12 @@ type ClusterConfig struct {
195195
// set to 10.0.0.1 which is what will be used to connect to.
196196
IgnorePeerAddr bool
197197

198-
// If DisableHostLookup then the driver will not attempt to get host info
198+
// If DisableInitialHostLookup then the driver will not attempt to get host info
199199
// from the system.peers table, this will mean that the driver will connect to
200200
// hosts supplied and will not attempt to lookup the hosts information, this will
201201
// mean that data_center, rack and token information will not be available and as
202202
// such host filtering and token aware query routing will not be available.
203-
DisableHostLookup bool
203+
DisableInitialHostLookup bool
204204

205205
// Configure events the driver will register for
206206
Events struct {

conn.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,20 +1898,20 @@ func (c *Conn) querySystemLocal(ctx context.Context) *Iter {
18981898
func (c *Conn) awaitSchemaAgreement(ctx context.Context) (err error) {
18991899
const localSchemas = "SELECT schema_version FROM system.local WHERE key='local'"
19001900

1901-
versions := make(map[string]struct{})
1901+
var versions map[string]struct{}
19021902
var schemaVersion string
19031903

19041904
endDeadline := time.Now().Add(c.session.cfg.MaxWaitSchemaAgreement)
19051905

19061906
for time.Now().Before(endDeadline) {
1907-
iter := &Iter{}
1908-
if !c.session.cfg.DisableHostLookup {
1909-
iter = c.querySystemPeers(ctx, c.host.version)
1907+
iter := c.querySystemPeers(ctx, c.host.version)
19101908

1911-
rows, err := iter.SliceMap()
1912-
if err != nil {
1913-
goto cont
1914-
}
1909+
versions = make(map[string]struct{})
1910+
1911+
rows, err := iter.SliceMap()
1912+
if err != nil {
1913+
goto cont
1914+
}
19151915

19161916
for _, row := range rows {
19171917
h, err := NewHostInfo(c.host.ConnectAddress(), c.session.cfg.Port)
@@ -1927,12 +1927,11 @@ func (c *Conn) awaitSchemaAgreement(ctx context.Context) (err error) {
19271927
continue
19281928
}
19291929

1930-
versions[host.schemaVersion] = struct{}{}
1931-
}
1930+
versions[host.schemaVersion] = struct{}{}
1931+
}
19321932

1933-
if err = iter.Close(); err != nil {
1934-
goto cont
1935-
}
1933+
if err = iter.Close(); err != nil {
1934+
goto cont
19361935
}
19371936

19381937
iter = c.query(ctx, localSchemas)

disable_host_lookup_test.go

Lines changed: 0 additions & 84 deletions
This file was deleted.

host_source.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -665,11 +665,6 @@ func (r *ringDescriber) getClusterPeerInfo(localHost *HostInfo) ([]*HostInfo, er
665665
return nil, errNoControl
666666
}
667667

668-
// Check if host lookup is disabled
669-
if r.session.cfg.DisableHostLookup {
670-
return []*HostInfo{}, nil
671-
}
672-
673668
var peers []*HostInfo
674669
iter := r.session.control.withConnHost(func(ch *connHost) *Iter {
675670
return ch.conn.querySystemPeers(context.TODO(), localHost.version)
@@ -752,6 +747,9 @@ func (s *Session) refreshRing() error {
752747
}
753748

754749
func refreshRing(r *ringDescriber) error {
750+
if r.session.cfg.DisableInitialHostLookup {
751+
return nil
752+
}
755753
hosts, partitioner, err := r.GetHosts()
756754
if err != nil {
757755
return err

session.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func (s *Session) init() error {
241241
return err
242242
}
243243

244-
if !s.cfg.DisableHostLookup {
244+
if !s.cfg.DisableInitialHostLookup {
245245
var partitioner string
246246
newHosts, partitioner, err := s.hostSource.GetHosts()
247247
if err != nil {
@@ -344,7 +344,7 @@ func (s *Session) init() error {
344344
// cluster is using the newer system schema or not... however, if control
345345
// connection is disable, we really have no choice, so we just make our
346346
// best guess...
347-
if !s.cfg.disableControlConn && s.cfg.DisableHostLookup {
347+
if !s.cfg.disableControlConn && s.cfg.DisableInitialHostLookup {
348348
newer, _ := checkSystemSchema(s.control)
349349
s.useSystemSchema = newer
350350
} else {

0 commit comments

Comments
 (0)