-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
index_usage_stats_test.go
312 lines (252 loc) · 9.87 KB
/
index_usage_stats_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server
import (
"context"
gosql "database/sql"
"net/url"
"testing"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/rpc"
"github.com/cockroachdb/cockroach/pkg/security"
"github.com/cockroachdb/cockroach/pkg/server/serverpb"
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/idxusage"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/stretchr/testify/require"
)
func compareTimeHelper(t *testing.T, expected, actual time.Time, delta time.Duration) {
diff := actual.Sub(expected)
require.True(t, diff < delta, "expected delta %s, but found %s", delta, diff)
}
func compareStatsHelper(
t *testing.T, expected, actual roachpb.IndexUsageStatistics, delta time.Duration,
) {
compareTimeHelper(t, expected.LastRead, actual.LastRead, delta)
compareTimeHelper(t, expected.LastWrite, actual.LastWrite, delta)
// We don't perform deep comparison of time.Time. So we set them to a dummy
// value before deep equal.
dummyTime := timeutil.Now()
expected.LastRead = dummyTime
expected.LastWrite = dummyTime
actual.LastRead = dummyTime
actual.LastWrite = dummyTime
require.Equal(t, expected, actual)
}
func createIndexStatsIngestedCallback() (
func(key roachpb.IndexUsageKey),
chan roachpb.IndexUsageKey,
) {
// Create a buffered channel so the callback is non-blocking.
notify := make(chan roachpb.IndexUsageKey, 100)
cb := func(key roachpb.IndexUsageKey) {
notify <- key
}
return cb, notify
}
func waitForStatsIngestion(
t *testing.T,
notify chan roachpb.IndexUsageKey,
expectedKeys map[roachpb.IndexUsageKey]struct{},
expectedEventCnt int,
timeout time.Duration,
) {
var timer timeutil.Timer
eventCnt := 0
timer.Reset(timeout)
for eventCnt < expectedEventCnt {
select {
case key := <-notify:
if _, ok := expectedKeys[key]; ok {
eventCnt++
}
continue
case <-timer.C:
timer.Read = true
t.Fatalf("expected stats ingestion to complete within %s, but it timed out", timeout)
}
}
}
func TestStatusAPIIndexUsage(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)
statsIngestionCb, statsIngestionNotifier := createIndexStatsIngestedCallback()
testCluster := serverutils.StartNewTestCluster(t, 3, base.TestClusterArgs{
ServerArgs: base.TestServerArgs{
Knobs: base.TestingKnobs{
IndexUsageStatsKnobs: &idxusage.TestingKnobs{
OnIndexUsageStatsProcessedCallback: statsIngestionCb,
},
},
},
})
ctx := context.Background()
defer testCluster.Stopper().Stop(ctx)
firstServer := testCluster.Server(0 /* idx */)
firstLocalStatsReader := firstServer.SQLServer().(*sql.Server).GetLocalIndexStatisticsReader()
expectedStatsIndexA := roachpb.IndexUsageStatistics{
TotalReadCount: 2,
LastRead: timeutil.Now(),
}
expectedStatsIndexB := roachpb.IndexUsageStatistics{
TotalReadCount: 2,
LastRead: timeutil.Now(),
}
firstPgURL, firstServerConnCleanup := sqlutils.PGUrl(
t, firstServer.ServingSQLAddr(), "CreateConnections" /* prefix */, url.User(security.RootUser))
defer firstServerConnCleanup()
firstServerSQLConn, err := gosql.Open("postgres", firstPgURL.String())
require.NoError(t, err)
defer func() {
err := firstServerSQLConn.Close()
require.NoError(t, err)
}()
// Create table on the first node.
_, err = firstServerSQLConn.Exec("CREATE TABLE t (k INT PRIMARY KEY, a INT, b INT, c INT, INDEX(a), INDEX(b))")
require.NoError(t, err)
_, err = firstServerSQLConn.Exec("INSERT INTO t VALUES (1, 10, 100, 0), (2, 20, 200, 0), (3, 30, 300, 0)")
require.NoError(t, err)
// We fetch the table ID of the testing table.
rows, err := firstServerSQLConn.Query("SELECT table_id FROM crdb_internal.tables WHERE name = 't'")
require.NoError(t, err)
require.NotNil(t, rows)
defer func() {
err := rows.Close()
require.NoError(t, err)
}()
var tableID int
require.True(t, rows.Next())
err = rows.Scan(&tableID)
require.NoError(t, err)
require.False(t, rows.Next())
indexKeyA := roachpb.IndexUsageKey{
TableID: roachpb.TableID(tableID),
IndexID: 2, // t@t_a_idx
}
indexKeyB := roachpb.IndexUsageKey{
TableID: roachpb.TableID(tableID),
IndexID: 3, // t@t_b_idx
}
err = firstServerSQLConn.Close()
require.NoError(t, err)
firstServerConnCleanup()
// Run some queries on the second node.
secondServer := testCluster.Server(1 /* idx */)
secondLocalStatsReader := secondServer.SQLServer().(*sql.Server).GetLocalIndexStatisticsReader()
secondPgURL, secondServerConnCleanup := sqlutils.PGUrl(
t, secondServer.ServingSQLAddr(), "CreateConnections" /* prefix */, url.User(security.RootUser))
defer secondServerConnCleanup()
secondServerSQLConn, err := gosql.Open("postgres", secondPgURL.String())
require.NoError(t, err)
defer func() {
err := secondServerSQLConn.Close()
require.NoError(t, err)
}()
// Records a non-full scan.
_, err = secondServerSQLConn.Exec("SELECT k, a FROM t WHERE a = 0")
require.NoError(t, err)
// Records an zigzag join.
_, err = secondServerSQLConn.Exec("SELECT k FROM t WHERE a = 10 AND b = 200")
require.NoError(t, err)
// Record a full scan.
_, err = secondServerSQLConn.Exec("SELECT * FROM t@t_b_idx")
require.NoError(t, err)
// Execute a explain query to ensure no index stats is collected.
_, err = secondServerSQLConn.Exec("EXPLAIN SELECT k, a FROM t WHERE a = 0")
require.NoError(t, err)
// When being stressed, there's possibility that it might takes a while for
// the stats payload to be ingested.
// time.Sleep(3 * time.Second)
// Check local node stats.
// Fetch stats reader from each individual
thirdServer := testCluster.Server(2 /* idx */)
thirdLocalStatsReader := thirdServer.SQLServer().(*sql.Server).GetLocalIndexStatisticsReader()
// Wait for the stats to be ingested.
waitForStatsIngestion(t, statsIngestionNotifier, map[roachpb.IndexUsageKey]struct{}{
indexKeyA: {},
indexKeyB: {},
}, /* expectedKeys */ 4 /* expectedEventCnt*/, 5*time.Second /* timeout */)
// First node should have nothing.
stats := firstLocalStatsReader.Get(indexKeyA)
require.Equal(t, roachpb.IndexUsageStatistics{}, stats, "expecting empty stats on node 1, but found %v", stats)
stats = firstLocalStatsReader.Get(indexKeyB)
require.Equal(t, roachpb.IndexUsageStatistics{}, stats, "expecting empty stats on node 1, but found %v", stats)
// Third node should have nothing.
stats = thirdLocalStatsReader.Get(indexKeyA)
require.Equal(t, roachpb.IndexUsageStatistics{}, stats, "expecting empty stats on node 3, but found %v", stats)
stats = thirdLocalStatsReader.Get(indexKeyB)
require.Equal(t, roachpb.IndexUsageStatistics{}, stats, "expecting empty stats on node 1, but found %v", stats)
// Second server should have nonempty local storage.
stats = secondLocalStatsReader.Get(indexKeyA)
compareStatsHelper(t, expectedStatsIndexA, stats, time.Minute)
stats = secondLocalStatsReader.Get(indexKeyB)
compareStatsHelper(t, expectedStatsIndexB, stats, time.Minute)
// Test cluster-wide RPC.
var resp serverpb.IndexUsageStatisticsResponse
err = getStatusJSONProto(thirdServer, "indexusagestatistics", &resp)
require.NoError(t, err)
statsEntries := 0
for _, stats := range resp.Statistics {
// Skip if the table is not what we expected.
if stats.Key.TableID != roachpb.TableID(tableID) {
continue
}
statsEntries++
switch stats.Key.IndexID {
case 2: // t@t_a_idx
compareStatsHelper(t, expectedStatsIndexA, stats.Stats, time.Minute)
case 3: // t@t_b_idx
compareStatsHelper(t, expectedStatsIndexB, stats.Stats, time.Minute)
}
}
require.True(t, statsEntries == 2, "expect to find two stats entries in RPC response, but found %d", statsEntries)
// Test max limit param in RPC endpoint.
rpcContext := newRPCTestContext(thirdServer.(*TestServer), testutils.NewTestBaseContext(security.RootUserName()))
rpcURL := thirdServer.ServingRPCAddr()
conn, err := rpcContext.GRPCDialNode(rpcURL, thirdServer.NodeID(), rpc.DefaultClass).Connect(ctx)
require.NoError(t, err)
client := serverpb.NewStatusClient(conn)
response, err := client.IndexUsageStatistics(ctx, &serverpb.IndexUsageStatisticsRequest{
Max: &serverpb.IndexUsageStatisticsRequest_MaxLimit{MaxLimit: 0},
})
require.NoError(t, err)
require.True(t, len(response.Statistics) == 0, "expected 0 length slice from RPC response, but found: %d", len(response.Statistics))
// Test disabling subsystem.
_, err = secondServerSQLConn.Exec("SET CLUSTER SETTING sql.metrics.index_usage_stats.enabled = false")
require.NoError(t, err)
// Records a non-full scan, but it shouldn't change the stats since we have
// stats collection disabled.
_, err = secondServerSQLConn.Exec("SELECT k, a FROM t WHERE a = 0")
require.NoError(t, err)
err = getStatusJSONProto(thirdServer, "indexusagestatistics", &resp)
require.NoError(t, err)
statsEntries = 0
for _, stats := range resp.Statistics {
// Skip if the table is not what we expected.
if stats.Key.TableID != roachpb.TableID(tableID) {
continue
}
statsEntries++
switch stats.Key.IndexID {
case 2: // t@t_a_idx
compareStatsHelper(t, expectedStatsIndexA, stats.Stats, time.Minute)
case 3: // t@t_b_idx
compareStatsHelper(t, expectedStatsIndexB, stats.Stats, time.Minute)
}
}
require.True(t, statsEntries == 2, "expect to find two stats entries in RPC response, but found %d", statsEntries)
}