Skip to content

Commit 1d13f9b

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-45
1 parent 30207ca commit 1d13f9b

File tree

6 files changed

+38
-84
lines changed

6 files changed

+38
-84
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ 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+
55+
- Fix DisableInitialHostLookup flag ignored when querying system.peers (PR update) (CASSGO-5)
56+
5357
### Fixed
5458
- Cassandra version unmarshal fix (CASSGO-49)
5559

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: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,14 +1904,12 @@ func (c *Conn) awaitSchemaAgreement(ctx context.Context) (err error) {
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+
rows, err := iter.SliceMap()
1910+
if err != nil {
1911+
goto cont
1912+
}
19151913

19161914
for _, row := range rows {
19171915
h, err := NewHostInfo(c.host.ConnectAddress(), c.session.cfg.Port)
@@ -1927,12 +1925,11 @@ func (c *Conn) awaitSchemaAgreement(ctx context.Context) (err error) {
19271925
continue
19281926
}
19291927

1930-
versions[host.schemaVersion] = struct{}{}
1931-
}
1928+
versions[host.schemaVersion] = struct{}{}
1929+
}
19321930

1933-
if err = iter.Close(); err != nil {
1934-
goto cont
1935-
}
1931+
if err = iter.Close(); err != nil {
1932+
goto cont
19361933
}
19371934

19381935
iter = c.query(ctx, localSchemas)

disable_host_lookup_test.go

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,39 @@
11
package gocql
22

33
import (
4-
"fmt"
5-
"log"
4+
"net"
65
"testing"
7-
"time"
86
)
97

10-
func TestDisableHostLookup(t *testing.T) {
8+
func TestDisableHostLookupRingRefresh(t *testing.T) {
119
cluster := createCluster()
12-
cluster.DisableHostLookup = true
10+
cluster.DisableInitialHostLookup = true
1311

1412
cluster.NumConns = 1
1513
session := createSessionFromCluster(cluster, t)
1614
defer session.Close()
1715

18-
if err := createTable(session, "CREATE TABLE IF NOT EXISTS gocql_test.test_table (id int primary key)"); err != nil {
19-
t.Fatal(err)
20-
}
21-
if err := session.Query("insert into gocql_test.test_table (id) values (?)", 123).Exec(); err != nil {
22-
t.Error(err)
23-
return
24-
}
25-
26-
go simulateHeartbeatFailure(session)
27-
28-
queryCycles := 20
29-
for ; queryCycles > 0; queryCycles-- {
30-
time.Sleep(2 * time.Second)
31-
queryTestKeyspace(session)
32-
triggerSchemaChange(session)
33-
}
34-
}
35-
36-
func queryTestKeyspace(session *Session) {
37-
iter := session.Query("SELECT * FROM gocql_test.test_table").Iter()
38-
39-
var id string
40-
for iter.Scan(&id) {
41-
fmt.Printf("id: %s\n", id)
42-
}
43-
if err := iter.Close(); err != nil {
44-
log.Printf("error querying: %v", err)
45-
}
46-
}
47-
48-
func triggerSchemaChange(session *Session) {
49-
// Create a keyspace to trigger schema agreement
50-
err := session.Query(`CREATE KEYSPACE IF NOT EXISTS test_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1}`).Exec()
51-
if err != nil {
52-
log.Printf("unable to create keyspace: %v", err)
53-
}
16+
oldHosts := make(map[string]*HostInfo)
5417

55-
// Alter the keyspace to trigger schema agreement
56-
err = session.Query(`ALTER KEYSPACE test_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 2}`).Exec()
57-
if err != nil {
58-
log.Printf("unable to alter keyspace: %v", err)
18+
for key, host := range session.ring.hosts {
19+
host.broadcastAddress = net.ParseIP("10.10.10.10")
20+
oldHosts[key] = host
5921
}
6022

61-
// Drop the keyspace after schema agreement
62-
err = session.Query(`DROP KEYSPACE IF EXISTS test_keyspace`).Exec()
23+
// if DisableInitialHostLookup == true - host.broadcastAddress must not be updated.
24+
err := session.refreshRing()
6325
if err != nil {
64-
log.Printf("unable to drop keyspace: %v", err)
26+
t.Fatal(err)
6527
}
66-
}
6728

68-
func simulateHeartbeatFailure(session *Session) {
69-
time.Sleep(20 * time.Second) // simulate 20 seconds of normal operation
70-
log.Println("Simulating heartbeat failure...")
71-
cluster := session.cfg
72-
session.Close() // close the session to simulate a heartbeat failure
29+
for key, host := range session.ring.hosts {
30+
oldHost, ok := oldHosts[key]
31+
if !ok {
32+
t.Fatalf("old host not found for key: %s", key)
33+
}
7334

74-
time.Sleep(10 * time.Second) // wait for 10 seconds to simulate downtime
75-
76-
// Reconnect
77-
log.Println("Reconnecting...")
78-
newSession, err := cluster.CreateSession()
79-
if err != nil {
80-
log.Fatalf("unable to create session: %v", err)
35+
if !oldHost.broadcastAddress.Equal(host.broadcastAddress) {
36+
t.Fatalf("broadcast addresses do not match for key: %s", key)
37+
}
8138
}
82-
*session = *newSession
83-
log.Println("Reconnected")
8439
}

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)