Skip to content

Commit 826a9df

Browse files
committed
perf: use explicit column lists in system.local/peers queries
Replace SELECT * with explicit column lists in qrySystemLocal, qrySystemPeers, and qrySystemPeersV2 to reduce the amount of data transferred from the server during topology discovery and control connection setup. Only columns actually consumed by hostInfoFromMap are now fetched. Add isScyllaConn() to ConnInterface so ring_describer can choose the correct peers query: Scylla omits preferred_ip (column does not exist), while Cassandra includes it. Remove dead DSE-specific case branches (workload, graph, dse_version) from hostInfoFromMap, since these columns are no longer queried. Deprecate Graph(), WorkLoad(), and DSEVersion() methods on HostInfo with doc comments, preserving backward API compatibility. Fix a stale comment in awaitSchemaAgreement that listed the scan column order incorrectly. This mirrors the same optimization done in the Python driver (scylladb/python-driver#528).
1 parent d15f511 commit 826a9df

File tree

4 files changed

+41
-90
lines changed

4 files changed

+41
-90
lines changed

conn.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ type ConnInterface interface {
171171
querySystem(ctx context.Context, query string, values ...interface{}) *Iter
172172
getIsSchemaV2() bool
173173
setSchemaV2(s bool)
174+
isScyllaConn() bool
174175
getScyllaSupported() ScyllaConnectionFeatures
175176
}
176177

@@ -1868,10 +1869,11 @@ func (c *Conn) querySystem(ctx context.Context, query string, values ...interfac
18681869
return c.executeQuery(ctx, q)
18691870
}
18701871

1871-
const qrySystemPeers = "SELECT * FROM system.peers"
1872-
const qrySystemPeersV2 = "SELECT * FROM system.peers_v2"
1872+
const qrySystemPeers = "SELECT peer, data_center, host_id, rack, release_version, rpc_address, schema_version, tokens FROM system.peers"
1873+
const qrySystemPeersCassandra = "SELECT peer, data_center, host_id, preferred_ip, rack, release_version, rpc_address, schema_version, tokens FROM system.peers"
1874+
const qrySystemPeersV2 = "SELECT peer, data_center, host_id, native_address, native_port, preferred_ip, rack, release_version, schema_version, tokens FROM system.peers_v2"
18731875

1874-
const qrySystemLocal = "SELECT * FROM system.local WHERE key='local'"
1876+
const qrySystemLocal = "SELECT broadcast_address, cluster_name, data_center, host_id, listen_address, partitioner, rack, release_version, rpc_address, schema_version, tokens FROM system.local WHERE key='local'"
18751877

18761878
func getSchemaAgreement(queryLocalSchemasRows []string, querySystemPeersRows []schemaAgreementHost, logger StdLogger) (err error) {
18771879
versions := make(map[string]struct{})
@@ -1936,7 +1938,7 @@ func (c *Conn) awaitSchemaAgreement(ctx context.Context) error {
19361938
} else {
19371939
iter = c.querySystem(ctx, "SELECT host_id, data_center, rack, schema_version, rpc_address FROM system.peers")
19381940
}
1939-
// data_center, rack, host_id, schema_version, rpc_address
1941+
// Scan order: host_id, data_center, rack, schema_version, rpc_address/preferred_ip
19401942
var hosts []schemaAgreementHost
19411943
var tmp schemaAgreementHost
19421944
for iter.Scan(&tmp.HostID, &tmp.DataCenter, &tmp.Rack, &tmp.SchemaVersion, &tmp.RPCAddress) {

host_source.go

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,6 @@ type HostInfo struct {
254254
// that we are thread safe use a mutex to access all fields.
255255
mu sync.RWMutex
256256
state nodeState
257-
graph bool
258257
}
259258

260259
func (h *HostInfo) Equal(host *HostInfo) bool {
@@ -392,21 +391,24 @@ func (h *HostInfo) HostID() string {
392391
return h.hostId
393392
}
394393

394+
// Deprecated: WorkLoad is a DSE-specific field that is no longer queried
395+
// from system tables. It will always return "" for hosts discovered via
396+
// the driver. Only populated if set explicitly via HostInfoBuilder.
395397
func (h *HostInfo) WorkLoad() string {
396-
h.mu.RLock()
397-
defer h.mu.RUnlock()
398398
return h.workload
399399
}
400400

401+
// Deprecated: Graph is a DSE-specific field that is no longer queried
402+
// from system tables. It will always return false for hosts discovered via
403+
// the driver.
401404
func (h *HostInfo) Graph() bool {
402-
h.mu.RLock()
403-
defer h.mu.RUnlock()
404-
return h.graph
405+
return false
405406
}
406407

408+
// Deprecated: DSEVersion is a DSE-specific field that is no longer queried
409+
// from system tables. It will always return "" for hosts discovered via
410+
// the driver. Only populated if set explicitly via HostInfoBuilder.
407411
func (h *HostInfo) DSEVersion() string {
408-
h.mu.RLock()
409-
defer h.mu.RUnlock()
410412
return h.dseVersion
411413
}
412414

@@ -697,26 +699,11 @@ func hostInfoFromMap(row map[string]interface{}, defaultPort int) (*HostInfo, er
697699
return nil, fmt.Errorf(assertErrorMsg, "native_port")
698700
}
699701
host.port = native_port
700-
case "workload":
701-
host.workload, ok = value.(string)
702-
if !ok {
703-
return nil, fmt.Errorf(assertErrorMsg, "workload")
704-
}
705-
case "graph":
706-
host.graph, ok = value.(bool)
707-
if !ok {
708-
return nil, fmt.Errorf(assertErrorMsg, "graph")
709-
}
710702
case "tokens":
711703
host.tokens, ok = value.([]string)
712704
if !ok {
713705
return nil, fmt.Errorf(assertErrorMsg, "tokens")
714706
}
715-
case "dse_version":
716-
host.dseVersion, ok = value.(string)
717-
if !ok {
718-
return nil, fmt.Errorf(assertErrorMsg, "dse_version")
719-
}
720707
case "schema_version":
721708
schemaVersion, ok := value.(UUID)
722709
if !ok {

ring_describer.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,10 @@ func (r *ringDescriber) getClusterPeerInfo(localHost *HostInfo, c ConnInterface)
4848
var iter *Iter
4949
if c.getIsSchemaV2() {
5050
iter = c.querySystem(context.TODO(), qrySystemPeersV2)
51-
} else {
51+
} else if c.isScyllaConn() {
5252
iter = c.querySystem(context.TODO(), qrySystemPeers)
53+
} else {
54+
iter = c.querySystem(context.TODO(), qrySystemPeersCassandra)
5355
}
5456

5557
if iter == nil {

ring_describer_test.go

Lines changed: 22 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -97,18 +97,8 @@ func (*mockConnection) executeQuery(ctx context.Context, qry *Query) *Iter { ret
9797
var systemLocalResultMetadata = resultMetadata{
9898
flags: 0,
9999
pagingState: []byte{},
100-
actualColCount: 18,
100+
actualColCount: 11,
101101
columns: []ColumnInfo{{
102-
Keyspace: "system",
103-
Table: "local",
104-
Name: "key",
105-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
106-
}, {
107-
Keyspace: "system",
108-
Table: "local",
109-
Name: "bootstrapped",
110-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
111-
}, {
112102
Keyspace: "system",
113103
Table: "local",
114104
Name: "broadcast_address",
@@ -118,21 +108,11 @@ var systemLocalResultMetadata = resultMetadata{
118108
Table: "local",
119109
Name: "cluster_name",
120110
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
121-
}, {
122-
Keyspace: "system",
123-
Table: "local",
124-
Name: "cql_version",
125-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
126111
}, {
127112
Keyspace: "system",
128113
Table: "local",
129114
Name: "data_center",
130115
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
131-
}, {
132-
Keyspace: "system",
133-
Table: "local",
134-
Name: "gossip_generation",
135-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeInt},
136116
}, {
137117
Keyspace: "system",
138118
Table: "local",
@@ -143,11 +123,6 @@ var systemLocalResultMetadata = resultMetadata{
143123
Table: "local",
144124
Name: "listen_address",
145125
TypeInfo: NativeType{proto: protoVersion4, typ: TypeInet},
146-
}, {
147-
Keyspace: "system",
148-
Table: "local",
149-
Name: "native_protocol_version",
150-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
151126
}, {
152127
Keyspace: "system",
153128
Table: "local",
@@ -173,11 +148,6 @@ var systemLocalResultMetadata = resultMetadata{
173148
Table: "local",
174149
Name: "schema_version",
175150
TypeInfo: NativeType{proto: protoVersion4, typ: TypeUUID},
176-
}, {
177-
Keyspace: "system",
178-
Table: "local",
179-
Name: "supported_features",
180-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
181151
}, {
182152
Keyspace: "system",
183153
Table: "local",
@@ -186,71 +156,56 @@ var systemLocalResultMetadata = resultMetadata{
186156
NativeType: NativeType{proto: protoVersion4, typ: TypeSet},
187157
Elem: NativeType{proto: protoVersion4, typ: TypeVarchar},
188158
},
189-
}, {
190-
Keyspace: "system",
191-
Table: "local",
192-
Name: "truncated_at",
193-
TypeInfo: CollectionType{
194-
NativeType: NativeType{proto: protoVersion4, typ: TypeMap},
195-
196-
Key: NativeType{proto: protoVersion4, typ: TypeUUID},
197-
Elem: NativeType{proto: protoVersion4, typ: TypeBlob},
198-
},
199159
}},
200160
}
201161

202162
var systemPeersResultMetadata = resultMetadata{
203163
flags: 0,
204164
pagingState: []byte{},
205-
actualColCount: 10,
165+
actualColCount: 9,
206166
columns: []ColumnInfo{{
207167
Keyspace: "system",
208-
Table: "local",
168+
Table: "peers",
209169
Name: "peer",
210170
TypeInfo: NativeType{proto: protoVersion4, typ: TypeInet},
211171
}, {
212172
Keyspace: "system",
213-
Table: "local",
173+
Table: "peers",
214174
Name: "data_center",
215175
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
216176
}, {
217177
Keyspace: "system",
218-
Table: "local",
178+
Table: "peers",
219179
Name: "host_id",
220180
TypeInfo: NativeType{proto: protoVersion4, typ: TypeUUID},
221181
}, {
222182
Keyspace: "system",
223-
Table: "local",
183+
Table: "peers",
224184
Name: "preferred_ip",
225185
TypeInfo: NativeType{proto: protoVersion4, typ: TypeInet},
226186
}, {
227187
Keyspace: "system",
228-
Table: "local",
188+
Table: "peers",
229189
Name: "rack",
230190
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
231191
}, {
232192
Keyspace: "system",
233-
Table: "local",
193+
Table: "peers",
234194
Name: "release_version",
235195
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
236196
}, {
237197
Keyspace: "system",
238-
Table: "local",
198+
Table: "peers",
239199
Name: "rpc_address",
240200
TypeInfo: NativeType{proto: protoVersion4, typ: TypeInet},
241201
}, {
242202
Keyspace: "system",
243-
Table: "local",
203+
Table: "peers",
244204
Name: "schema_version",
245205
TypeInfo: NativeType{proto: protoVersion4, typ: TypeUUID},
246206
}, {
247207
Keyspace: "system",
248-
Table: "local",
249-
Name: "supported_features",
250-
TypeInfo: NativeType{proto: protoVersion4, typ: TypeVarchar},
251-
}, {
252-
Keyspace: "system",
253-
Table: "local",
208+
Table: "peers",
254209
Name: "tokens",
255210
TypeInfo: CollectionType{
256211
NativeType: NativeType{proto: protoVersion4, typ: TypeSet},
@@ -260,18 +215,22 @@ var systemPeersResultMetadata = resultMetadata{
260215
}
261216

262217
func (*mockConnection) querySystem(ctx context.Context, query string, values ...interface{}) *Iter {
263-
localData := []interface{}{"local", "COMPLETED", net.IPv4(192, 168, 100, 12), "", "3.3.1", "datacenter1", 1733834239, ParseUUIDMust("045859a7-6b9f-4efd-a5e7-acd64a295e13"), net.IPv4(192, 168, 100, 12), "4", "org.apache.cassandra.dht.Murmur3Partitioner", "rack1", "3.0.8", net.IPv4(192, 168, 100, 12), ParseUUIDMust("daf4df2c-b708-11ef-5c25-3004361afd71"), "", []string{}, map[UUID]byte{}}
264-
peerData1 := []interface{}{net.IPv4(192, 168, 100, 13), "datacenter1", ParseUUIDMust("b953309f-6e68-41f2-baf5-0e60da317a9c"), net.IP{}, "rack1", "3.0.8", net.IPv4(192, 168, 100, 13), ParseUUIDMust("b6ed5bde-b318-11ef-8f58-aeba19e31273"), "", []string{"-1032311531684407545", "-1112089412567859825"}}
265-
peerData2 := []interface{}{net.IPv4(192, 168, 100, 14), "datacenter1", ParseUUIDMust("8269e111-ea38-44bd-a73f-9d3d12cfaf78"), net.IP{}, "rack1", "3.0.8", net.IPv4(192, 168, 100, 14), ParseUUIDMust("b6ed5bde-b318-11ef-8f58-aeba19e31273"), "", []string{}}
266-
267-
if query == "SELECT * FROM system.local WHERE key='local'" {
218+
// Column order matches the explicit SELECT in qrySystemLocal:
219+
// broadcast_address, cluster_name, data_center, host_id, listen_address, partitioner, rack, release_version, rpc_address, schema_version, tokens
220+
localData := []interface{}{net.IPv4(192, 168, 100, 12), "", "datacenter1", ParseUUIDMust("045859a7-6b9f-4efd-a5e7-acd64a295e13"), net.IPv4(192, 168, 100, 12), "org.apache.cassandra.dht.Murmur3Partitioner", "rack1", "3.0.8", net.IPv4(192, 168, 100, 12), ParseUUIDMust("daf4df2c-b708-11ef-5c25-3004361afd71"), []string{}}
221+
// Column order matches the explicit SELECT in qrySystemPeersCassandra:
222+
// peer, data_center, host_id, preferred_ip, rack, release_version, rpc_address, schema_version, tokens
223+
peerData1 := []interface{}{net.IPv4(192, 168, 100, 13), "datacenter1", ParseUUIDMust("b953309f-6e68-41f2-baf5-0e60da317a9c"), net.IP{}, "rack1", "3.0.8", net.IPv4(192, 168, 100, 13), ParseUUIDMust("b6ed5bde-b318-11ef-8f58-aeba19e31273"), []string{"-1032311531684407545", "-1112089412567859825"}}
224+
peerData2 := []interface{}{net.IPv4(192, 168, 100, 14), "datacenter1", ParseUUIDMust("8269e111-ea38-44bd-a73f-9d3d12cfaf78"), net.IP{}, "rack1", "3.0.8", net.IPv4(192, 168, 100, 14), ParseUUIDMust("b6ed5bde-b318-11ef-8f58-aeba19e31273"), []string{}}
225+
226+
if query == qrySystemLocal {
268227
return &Iter{
269228
meta: systemLocalResultMetadata,
270229
framer: &mock.MockFramer{Data: marshalMetadataMust(systemLocalResultMetadata, localData)},
271230
numRows: 1,
272231
next: nil,
273232
}
274-
} else if query == "SELECT * FROM system.peers" {
233+
} else if query == qrySystemPeersCassandra {
275234
return &Iter{
276235
meta: systemPeersResultMetadata,
277236
framer: &mock.MockFramer{Data: append(marshalMetadataMust(systemPeersResultMetadata, peerData1), marshalMetadataMust(systemPeersResultMetadata, peerData2)...)},
@@ -284,6 +243,7 @@ func (*mockConnection) querySystem(ctx context.Context, query string, values ...
284243

285244
func (*mockConnection) getIsSchemaV2() bool { return false }
286245
func (*mockConnection) setSchemaV2(s bool) {}
246+
func (*mockConnection) isScyllaConn() bool { return false }
287247
func (*mockConnection) getScyllaSupported() ScyllaConnectionFeatures {
288248
return ScyllaConnectionFeatures{}
289249
}

0 commit comments

Comments
 (0)