-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclickhouse.go
More file actions
486 lines (432 loc) · 16.8 KB
/
clickhouse.go
File metadata and controls
486 lines (432 loc) · 16.8 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package testutil
import (
"context"
"fmt"
"io"
"log/slog"
"sync/atomic"
"time"
"github.com/scality/log-courier/pkg/clickhouse"
"github.com/scality/log-courier/pkg/logcourier"
)
// ==========================================
// Test Schema Definitions
// ==========================================
// These schemas mirror the Federation ClickHouse setup but without replication.
// Federation schemas are maintained in roles/run-s3-analytics-clickhouse/
//
// Key differences from Federation:
// - MergeTree instead of ReplicatedMergeTree (no replication)
const schemaAccessLogsLocal = `
CREATE TABLE IF NOT EXISTS %s.access_logs
(
timestamp DateTime,
insertedAt DateTime DEFAULT now(),
hostname LowCardinality(String),
startTime DateTime64(3),
requester String,
operation String,
requestURI String,
errorCode String,
objectSize UInt64,
totalTime Float32,
turnAroundTime Float32,
referer String,
userAgent String,
versionId String,
signatureVersion LowCardinality(String),
cipherSuite LowCardinality(String),
authenticationType LowCardinality(String),
hostHeader String,
tlsVersion LowCardinality(String),
aclRequired LowCardinality(String),
bucketOwner String,
bucketName String,
req_id String,
bytesSent UInt64,
clientIP String,
httpCode UInt16,
objectKey String,
logFormatVersion LowCardinality(String),
loggingEnabled Bool,
loggingTargetBucket String,
loggingTargetPrefix String,
awsAccessKeyID String,
raftSessionID UInt16
)
ENGINE = MergeTree()
PARTITION BY toStartOfInterval(insertedAt, INTERVAL 1 HOUR)
ORDER BY (raftSessionID, bucketName, insertedAt, timestamp, req_id)
`
const schemaAccessLogsFederated = `
CREATE TABLE IF NOT EXISTS %s.access_logs_federated AS %s.access_logs
ENGINE = Distributed(workbench_cluster, %s, access_logs, raftSessionID)
`
const schemaOffsetsLocal = `
CREATE TABLE IF NOT EXISTS %s.offsets
(
bucketName String,
raftSessionID UInt16,
lastProcessedInsertedAt DateTime,
lastProcessedTimestamp DateTime64(3),
lastProcessedReqId String
)
ENGINE = MergeTree()
ORDER BY (bucketName, raftSessionID)
`
const schemaOffsetsFederated = `
CREATE TABLE IF NOT EXISTS %s.offsets_federated AS %s.offsets
ENGINE = Distributed(workbench_cluster, %s, offsets, raftSessionID)
`
// ==========================================
// Test Helper
// ==========================================
// ClickHouseTestHelper provides utilities for testing with ClickHouse
type ClickHouseTestHelper struct {
Clients []*clickhouse.Client
DatabaseName string
Hosts []string
}
// Client returns the primary client (first shard) for backward compatibility
// This allows existing tests to continue using helper.Client
func (h *ClickHouseTestHelper) Client() *clickhouse.Client {
if len(h.Clients) == 0 {
return nil
}
return h.Clients[0]
}
// NewClickHouseTestHelper creates a new test helper with a unique database
func NewClickHouseTestHelper(ctx context.Context) (*ClickHouseTestHelper, error) {
// Reset config to ensure clean state for each test (prevents config pollution between tests)
logcourier.ConfigSpec.Reset()
err := logcourier.ConfigSpec.LoadConfiguration("", "", nil)
if err != nil {
return nil, fmt.Errorf("failed to load config: %w", err)
}
// Create a no-op logger for tests
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
hosts := logcourier.ConfigSpec.GetStringSlice("clickhouse.url")
if len(hosts) == 0 {
return nil, fmt.Errorf("no ClickHouse hosts configured")
}
// Generate unique database name for test isolation
dbName := fmt.Sprintf("logs_test_%d", time.Now().UnixNano())
// Create clients for each shard
clients := make([]*clickhouse.Client, len(hosts))
for i, host := range hosts {
cfg := clickhouse.Config{
Hosts: []string{host}, // Single host per client
Username: logcourier.ConfigSpec.GetString("clickhouse.username"),
Password: logcourier.ConfigSpec.GetString("clickhouse.password"),
Timeout: time.Duration(logcourier.ConfigSpec.GetInt("clickhouse.timeout-seconds")) * time.Second,
Logger: logger,
Settings: map[string]interface{}{
"insert_distributed_sync": 1, // Enable synchronous inserts for tests
},
}
client, err := clickhouse.NewClient(ctx, cfg)
if err != nil {
// Clean up any already-created clients
for j := 0; j < i; j++ {
_ = clients[j].Close()
}
return nil, fmt.Errorf("failed to connect to ClickHouse shard %d (%s): %w", i+1, host, err)
}
clients[i] = client
}
return &ClickHouseTestHelper{
Clients: clients,
DatabaseName: dbName,
Hosts: hosts,
}, nil
}
// SetupSchema creates distributed schema on all shards
func (h *ClickHouseTestHelper) SetupSchema(ctx context.Context) error {
// Verify we have multiple shards configured - panic if not to catch misconfiguration immediately
if len(h.Clients) < 2 {
panic(fmt.Sprintf("FATAL: distributed setup requires at least 2 shards, but only %d configured (hosts: %v)", len(h.Clients), h.Hosts))
}
// Create database on all shards
for i, client := range h.Clients {
if err := client.Exec(ctx, fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s", h.DatabaseName)); err != nil {
return fmt.Errorf("failed to create database on shard %d: %w", i+1, err)
}
}
// Verify database exists on all shards before proceeding
// This ensures metadata is synchronized across the cluster
for i, client := range h.Clients {
var dbExists uint64
query := fmt.Sprintf("SELECT count() FROM system.databases WHERE name = '%s'", h.DatabaseName)
if err := client.QueryRow(ctx, query).Scan(&dbExists); err != nil {
return fmt.Errorf("failed to verify database on shard %d: %w", i+1, err)
}
if dbExists == 0 {
return fmt.Errorf("database %s not found on shard %d after creation", h.DatabaseName, i+1)
}
}
// Create local tables on all shards
if err := h.createLocalAccessLogsTable(ctx); err != nil {
return err
}
if err := h.verifyTableExists(ctx, "access_logs"); err != nil {
return err
}
if err := h.createLocalOffsetsTable(ctx); err != nil {
return err
}
if err := h.verifyTableExists(ctx, "offsets"); err != nil {
return err
}
// Create distributed tables on all shards
if err := h.createDistributedAccessLogsTable(ctx); err != nil {
return err
}
if err := h.verifyTableExists(ctx, "access_logs_federated"); err != nil {
return err
}
if err := h.createDistributedOffsetsTable(ctx); err != nil {
return err
}
if err := h.verifyTableExists(ctx, "offsets_federated"); err != nil {
return err
}
// Verify distributed tables are actually queryable via cluster connections
// This ensures metadata is visible not just via direct connections but also
// via the cluster network path that distributed queries use
if err := h.verifyDistributedTableAccessible(ctx); err != nil {
return err
}
return nil
}
func (h *ClickHouseTestHelper) createLocalAccessLogsTable(ctx context.Context) error {
sql := fmt.Sprintf(schemaAccessLogsLocal, h.DatabaseName)
for i, client := range h.Clients {
if err := client.Exec(ctx, sql); err != nil {
return fmt.Errorf("failed to create local access_logs table on shard %d: %w", i+1, err)
}
}
return nil
}
func (h *ClickHouseTestHelper) createLocalOffsetsTable(ctx context.Context) error {
sql := fmt.Sprintf(schemaOffsetsLocal, h.DatabaseName)
for i, client := range h.Clients {
if err := client.Exec(ctx, sql); err != nil {
return fmt.Errorf("failed to create local offsets table on shard %d: %w", i+1, err)
}
}
return nil
}
func (h *ClickHouseTestHelper) createDistributedAccessLogsTable(ctx context.Context) error {
sql := fmt.Sprintf(schemaAccessLogsFederated, h.DatabaseName, h.DatabaseName, h.DatabaseName)
for i, client := range h.Clients {
if err := client.Exec(ctx, sql); err != nil {
return fmt.Errorf("failed to create distributed access_logs_federated table on shard %d: %w", i+1, err)
}
}
return nil
}
func (h *ClickHouseTestHelper) createDistributedOffsetsTable(ctx context.Context) error {
sql := fmt.Sprintf(schemaOffsetsFederated, h.DatabaseName, h.DatabaseName, h.DatabaseName)
for i, client := range h.Clients {
if err := client.Exec(ctx, sql); err != nil {
return fmt.Errorf("failed to create distributed offsets_federated table on shard %d: %w", i+1, err)
}
}
return nil
}
// verifyTableExists ensures a table exists on all shards
// This ensures metadata is synchronized across the cluster
func (h *ClickHouseTestHelper) verifyTableExists(ctx context.Context, tableName string) error {
for i, client := range h.Clients {
var tableExists uint64
query := fmt.Sprintf("SELECT count() FROM system.tables WHERE database = '%s' AND name = '%s'", h.DatabaseName, tableName)
if err := client.QueryRow(ctx, query).Scan(&tableExists); err != nil {
return fmt.Errorf("failed to verify table %s on shard %d: %w", tableName, i+1, err)
}
if tableExists == 0 {
return fmt.Errorf("table %s.%s not found on shard %d after creation", h.DatabaseName, tableName, i+1)
}
}
return nil
}
// verifyDistributedTableAccessible ensures distributed tables can actually query
// the local tables on all shards via cluster connections. This catches metadata
// visibility issues that might not be detected by direct shard queries.
func (h *ClickHouseTestHelper) verifyDistributedTableAccessible(ctx context.Context) error {
// Simple verification: try to query the distributed table from each shard
for i, client := range h.Clients {
query := fmt.Sprintf("SELECT count() FROM %s.access_logs_federated", h.DatabaseName)
var count uint64
if err := client.QueryRow(ctx, query).Scan(&count); err != nil {
return fmt.Errorf("shard %d cannot query access_logs_federated: %w", i+1, err)
}
query = fmt.Sprintf("SELECT count() FROM %s.offsets_federated", h.DatabaseName)
if err := client.QueryRow(ctx, query).Scan(&count); err != nil {
return fmt.Errorf("shard %d cannot query offsets_federated: %w", i+1, err)
}
}
return nil
}
// TeardownSchema drops all test tables and database from all shards
func (h *ClickHouseTestHelper) TeardownSchema(ctx context.Context) error {
for i, client := range h.Clients {
if err := client.Exec(ctx, fmt.Sprintf("DROP DATABASE IF EXISTS %s", h.DatabaseName)); err != nil {
return fmt.Errorf("failed to drop database on shard %d: %w", i+1, err)
}
}
return nil
}
// TestLogRecord represents a minimal test log record for insertion
type TestLogRecord struct {
Timestamp time.Time
BucketName string
ReqID string
Action string
ObjectKey string
BytesSent uint64
RaftSessionID uint16
HttpCode uint16
LoggingEnabled bool
}
// InsertTestLog inserts a test log record into the federated table
// The Distributed engine will route to the correct shard based on raftSessionID
func (h *ClickHouseTestHelper) InsertTestLog(ctx context.Context, log TestLogRecord) error {
query := fmt.Sprintf(`
INSERT INTO %s.%s
(timestamp, insertedAt, startTime, requester, operation, requestURI, errorCode,
objectSize, totalTime, turnAroundTime, referer, userAgent, versionId,
signatureVersion, cipherSuite, authenticationType, hostHeader, tlsVersion,
aclRequired, bucketOwner, bucketName, req_id, bytesSent, clientIP, httpCode,
objectKey, loggingEnabled, loggingTargetBucket, loggingTargetPrefix, raftSessionID)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, h.DatabaseName, clickhouse.TableAccessLogsFederated)
return h.Clients[0].Exec(ctx, query,
log.Timestamp, // timestamp
time.Now(), // insertedAt
log.Timestamp, // startTime
"", // requester
log.Action, // operation
"", // requestURI
"", // errorCode
uint64(0), // objectSize
float32(0), // totalTime
float32(0), // turnAroundTime
"", // referer
"", // userAgent
"", // versionId
"", // signatureVersion
"", // cipherSuite
"", // authenticationType
"", // hostHeader
"", // tlsVersion
"", // aclRequired
"", // bucketOwner
log.BucketName, // bucketName
log.ReqID, // req_id
log.BytesSent, // bytesSent
"", // clientIP
log.HttpCode, // httpCode
log.ObjectKey, // objectKey
log.LoggingEnabled, // loggingEnabled
"", // loggingTargetBucket
"", // loggingTargetPrefix
log.RaftSessionID, // raftSessionID
)
}
// Close closes all shard clients
func (h *ClickHouseTestHelper) Close() error {
var errs []error
for i, client := range h.Clients {
if client != nil {
if err := client.Close(); err != nil {
errs = append(errs, fmt.Errorf("failed to close client %d: %w", i+1, err))
}
}
}
if len(errs) > 0 {
return fmt.Errorf("errors closing clients: %v", errs)
}
return nil
}
// InsertTestLogWithTargetBucket inserts a test log record with logging target bucket/prefix
// The Distributed engine will route to the correct shard based on raftSessionID
func (h *ClickHouseTestHelper) InsertTestLogWithTargetBucket(ctx context.Context, log TestLogRecord, targetBucket, targetPrefix string) error {
query := fmt.Sprintf(`
INSERT INTO %s.%s
(timestamp, insertedAt, startTime, requester, operation, requestURI, errorCode,
objectSize, totalTime, turnAroundTime, referer, userAgent, versionId,
signatureVersion, cipherSuite, authenticationType, hostHeader, tlsVersion,
aclRequired, bucketOwner, bucketName, req_id, bytesSent, clientIP, httpCode,
objectKey, loggingEnabled, loggingTargetBucket, loggingTargetPrefix, raftSessionID)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
`, h.DatabaseName, clickhouse.TableAccessLogsFederated)
return h.Clients[0].Exec(ctx, query,
log.Timestamp, // timestamp
time.Now(), // insertedAt
log.Timestamp, // startTime
"", // requester
log.Action, // operation
"", // requestURI
"", // errorCode
uint64(0), // objectSize
float32(0), // totalTime
float32(0), // turnAroundTime
"", // referer
"", // userAgent
"", // versionId
"", // signatureVersion
"", // cipherSuite
"", // authenticationType
"", // hostHeader
"", // tlsVersion
"", // aclRequired
"", // bucketOwner
log.BucketName, // bucketName
log.ReqID, // req_id
log.BytesSent, // bytesSent
"", // clientIP
log.HttpCode, // httpCode
log.ObjectKey, // objectKey
log.LoggingEnabled, // loggingEnabled
targetBucket, // loggingTargetBucket
targetPrefix, // loggingTargetPrefix
log.RaftSessionID, // raftSessionID
)
}
// FailingOffsetManager wraps an OffsetManager and injects failures for testing
type FailingOffsetManager struct {
manager logcourier.OffsetManagerInterface
commitCount atomic.Int64
failUntilCount int64
}
// NewFailingOffsetManager creates a new failing offset manager wrapper
func NewFailingOffsetManager(manager logcourier.OffsetManagerInterface, failUntilCount int64) *FailingOffsetManager {
return &FailingOffsetManager{
manager: manager,
failUntilCount: failUntilCount,
}
}
// CommitOffset wraps the underlying manager's CommitOffset and fails until failUntilCount
func (f *FailingOffsetManager) CommitOffset(ctx context.Context, bucket string, raftSessionID uint16, offset logcourier.Offset) error {
count := f.commitCount.Add(1)
if count <= f.failUntilCount {
return fmt.Errorf("simulated offset commit failure (attempt %d)", count)
}
return f.manager.CommitOffset(ctx, bucket, raftSessionID, offset)
}
// CommitOffsetsBatch wraps the underlying manager's CommitOffsetsBatch and fails until failUntilCount
func (f *FailingOffsetManager) CommitOffsetsBatch(ctx context.Context, commits []logcourier.OffsetCommit) error {
count := f.commitCount.Add(1)
if count <= f.failUntilCount {
return fmt.Errorf("simulated offset commit failure (attempt %d)", count)
}
return f.manager.CommitOffsetsBatch(ctx, commits)
}
// GetOffset delegates to the underlying manager
func (f *FailingOffsetManager) GetOffset(ctx context.Context, bucket string, raftSessionID uint16) (logcourier.Offset, error) {
return f.manager.GetOffset(ctx, bucket, raftSessionID)
}
// GetCommitCount returns the total number of commit attempts
func (f *FailingOffsetManager) GetCommitCount() int64 {
return f.commitCount.Load()
}