aboutsummaryrefslogtreecommitdiff
path: root/weed/mq/offset/sql_storage.go
blob: c3107e5a484fcb3ed08c715a2490d4a4695bf2ab (plain)
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
package offset

import (
	"database/sql"
	"fmt"
	"time"

	"github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
)

// OffsetEntry represents a mapping between Kafka offset and SMQ timestamp
type OffsetEntry struct {
	KafkaOffset  int64
	SMQTimestamp int64
	MessageSize  int32
}

// SQLOffsetStorage implements OffsetStorage using SQL database with _index column
type SQLOffsetStorage struct {
	db *sql.DB
}

// NewSQLOffsetStorage creates a new SQL-based offset storage
func NewSQLOffsetStorage(db *sql.DB) (*SQLOffsetStorage, error) {
	storage := &SQLOffsetStorage{db: db}

	// Initialize database schema
	if err := storage.initializeSchema(); err != nil {
		return nil, fmt.Errorf("failed to initialize schema: %w", err)
	}

	return storage, nil
}

// initializeSchema creates the necessary tables for offset storage
func (s *SQLOffsetStorage) initializeSchema() error {
	// TODO: Create offset storage tables with _index as hidden column
	// ASSUMPTION: Using SQLite-compatible syntax, may need adaptation for other databases

	queries := []string{
		// Partition offset checkpoints table
		// TODO: Add _index as computed column when supported by database
		// ASSUMPTION: Using regular columns for now, _index concept preserved for future enhancement
		`CREATE TABLE IF NOT EXISTS partition_offset_checkpoints (
			partition_key TEXT PRIMARY KEY,
			ring_size INTEGER NOT NULL,
			range_start INTEGER NOT NULL,
			range_stop INTEGER NOT NULL,
			unix_time_ns INTEGER NOT NULL,
			checkpoint_offset INTEGER NOT NULL,
			updated_at INTEGER NOT NULL
		)`,

		// Offset mappings table for detailed tracking
		// TODO: Add _index as computed column when supported by database
		`CREATE TABLE IF NOT EXISTS offset_mappings (
			id INTEGER PRIMARY KEY AUTOINCREMENT,
			partition_key TEXT NOT NULL,
			kafka_offset INTEGER NOT NULL,
			smq_timestamp INTEGER NOT NULL,
			message_size INTEGER NOT NULL,
			created_at INTEGER NOT NULL,
			UNIQUE(partition_key, kafka_offset)
		)`,

		// Indexes for performance
		`CREATE INDEX IF NOT EXISTS idx_partition_offset_checkpoints_partition 
		 ON partition_offset_checkpoints(partition_key)`,

		`CREATE INDEX IF NOT EXISTS idx_offset_mappings_partition_offset 
		 ON offset_mappings(partition_key, kafka_offset)`,

		`CREATE INDEX IF NOT EXISTS idx_offset_mappings_timestamp 
		 ON offset_mappings(partition_key, smq_timestamp)`,
	}

	for _, query := range queries {
		if _, err := s.db.Exec(query); err != nil {
			return fmt.Errorf("failed to execute schema query: %w", err)
		}
	}

	return nil
}

// SaveCheckpoint saves the checkpoint for a partition
func (s *SQLOffsetStorage) SaveCheckpoint(namespace, topicName string, partition *schema_pb.Partition, offset int64) error {
	// Use TopicPartitionKey to ensure each topic has isolated checkpoint storage
	partitionKey := TopicPartitionKey(namespace, topicName, partition)
	now := time.Now().UnixNano()

	// TODO: Use UPSERT for better performance
	// ASSUMPTION: SQLite REPLACE syntax, may need adaptation for other databases
	query := `
		REPLACE INTO partition_offset_checkpoints 
		(partition_key, ring_size, range_start, range_stop, unix_time_ns, checkpoint_offset, updated_at)
		VALUES (?, ?, ?, ?, ?, ?, ?)
	`

	_, err := s.db.Exec(query,
		partitionKey,
		partition.RingSize,
		partition.RangeStart,
		partition.RangeStop,
		partition.UnixTimeNs,
		offset,
		now,
	)

	if err != nil {
		return fmt.Errorf("failed to save checkpoint: %w", err)
	}

	return nil
}

// LoadCheckpoint loads the checkpoint for a partition
func (s *SQLOffsetStorage) LoadCheckpoint(namespace, topicName string, partition *schema_pb.Partition) (int64, error) {
	// Use TopicPartitionKey to match SaveCheckpoint
	partitionKey := TopicPartitionKey(namespace, topicName, partition)

	query := `
		SELECT checkpoint_offset 
		FROM partition_offset_checkpoints 
		WHERE partition_key = ?
	`

	var checkpointOffset int64
	err := s.db.QueryRow(query, partitionKey).Scan(&checkpointOffset)

	if err == sql.ErrNoRows {
		return -1, fmt.Errorf("no checkpoint found")
	}

	if err != nil {
		return -1, fmt.Errorf("failed to load checkpoint: %w", err)
	}

	return checkpointOffset, nil
}

// GetHighestOffset finds the highest offset in storage for a partition
func (s *SQLOffsetStorage) GetHighestOffset(namespace, topicName string, partition *schema_pb.Partition) (int64, error) {
	// Use TopicPartitionKey to match SaveCheckpoint
	partitionKey := TopicPartitionKey(namespace, topicName, partition)

	// TODO: Use _index column for efficient querying
	// ASSUMPTION: kafka_offset represents the sequential offset we're tracking
	query := `
		SELECT MAX(kafka_offset) 
		FROM offset_mappings 
		WHERE partition_key = ?
	`

	var highestOffset sql.NullInt64
	err := s.db.QueryRow(query, partitionKey).Scan(&highestOffset)

	if err != nil {
		return -1, fmt.Errorf("failed to get highest offset: %w", err)
	}

	if !highestOffset.Valid {
		return -1, fmt.Errorf("no records found")
	}

	return highestOffset.Int64, nil
}

// SaveOffsetMapping stores an offset mapping (extends OffsetStorage interface)
func (s *SQLOffsetStorage) SaveOffsetMapping(partitionKey string, kafkaOffset, smqTimestamp int64, size int32) error {
	now := time.Now().UnixNano()

	// TODO: Handle duplicate key conflicts gracefully
	// ASSUMPTION: Using INSERT OR REPLACE for conflict resolution
	query := `
		INSERT OR REPLACE INTO offset_mappings 
		(partition_key, kafka_offset, smq_timestamp, message_size, created_at)
		VALUES (?, ?, ?, ?, ?)
	`

	_, err := s.db.Exec(query, partitionKey, kafkaOffset, smqTimestamp, size, now)
	if err != nil {
		return fmt.Errorf("failed to save offset mapping: %w", err)
	}

	return nil
}

// LoadOffsetMappings retrieves all offset mappings for a partition
func (s *SQLOffsetStorage) LoadOffsetMappings(partitionKey string) ([]OffsetEntry, error) {
	// TODO: Add pagination for large result sets
	// ASSUMPTION: Loading all mappings for now, should be paginated in production
	query := `
		SELECT kafka_offset, smq_timestamp, message_size
		FROM offset_mappings 
		WHERE partition_key = ?
		ORDER BY kafka_offset ASC
	`

	rows, err := s.db.Query(query, partitionKey)
	if err != nil {
		return nil, fmt.Errorf("failed to query offset mappings: %w", err)
	}
	defer rows.Close()

	var entries []OffsetEntry
	for rows.Next() {
		var entry OffsetEntry
		err := rows.Scan(&entry.KafkaOffset, &entry.SMQTimestamp, &entry.MessageSize)
		if err != nil {
			return nil, fmt.Errorf("failed to scan offset entry: %w", err)
		}
		entries = append(entries, entry)
	}

	if err := rows.Err(); err != nil {
		return nil, fmt.Errorf("error iterating offset mappings: %w", err)
	}

	return entries, nil
}

// GetOffsetMappingsByRange retrieves offset mappings within a specific range
func (s *SQLOffsetStorage) GetOffsetMappingsByRange(partitionKey string, startOffset, endOffset int64) ([]OffsetEntry, error) {
	// TODO: Use _index column for efficient range queries
	query := `
		SELECT kafka_offset, smq_timestamp, message_size
		FROM offset_mappings 
		WHERE partition_key = ? AND kafka_offset >= ? AND kafka_offset <= ?
		ORDER BY kafka_offset ASC
	`

	rows, err := s.db.Query(query, partitionKey, startOffset, endOffset)
	if err != nil {
		return nil, fmt.Errorf("failed to query offset range: %w", err)
	}
	defer rows.Close()

	var entries []OffsetEntry
	for rows.Next() {
		var entry OffsetEntry
		err := rows.Scan(&entry.KafkaOffset, &entry.SMQTimestamp, &entry.MessageSize)
		if err != nil {
			return nil, fmt.Errorf("failed to scan offset entry: %w", err)
		}
		entries = append(entries, entry)
	}

	return entries, nil
}

// GetPartitionStats returns statistics about a partition's offset usage
func (s *SQLOffsetStorage) GetPartitionStats(partitionKey string) (*PartitionStats, error) {
	query := `
		SELECT 
			COUNT(*) as record_count,
			MIN(kafka_offset) as earliest_offset,
			MAX(kafka_offset) as latest_offset,
			SUM(message_size) as total_size,
			MIN(created_at) as first_record_time,
			MAX(created_at) as last_record_time
		FROM offset_mappings 
		WHERE partition_key = ?
	`

	var stats PartitionStats
	var earliestOffset, latestOffset sql.NullInt64
	var totalSize sql.NullInt64
	var firstRecordTime, lastRecordTime sql.NullInt64

	err := s.db.QueryRow(query, partitionKey).Scan(
		&stats.RecordCount,
		&earliestOffset,
		&latestOffset,
		&totalSize,
		&firstRecordTime,
		&lastRecordTime,
	)

	if err != nil {
		return nil, fmt.Errorf("failed to get partition stats: %w", err)
	}

	stats.PartitionKey = partitionKey

	if earliestOffset.Valid {
		stats.EarliestOffset = earliestOffset.Int64
	} else {
		stats.EarliestOffset = -1
	}

	if latestOffset.Valid {
		stats.LatestOffset = latestOffset.Int64
		stats.HighWaterMark = latestOffset.Int64 + 1
	} else {
		stats.LatestOffset = -1
		stats.HighWaterMark = 0
	}

	if firstRecordTime.Valid {
		stats.FirstRecordTime = firstRecordTime.Int64
	}

	if lastRecordTime.Valid {
		stats.LastRecordTime = lastRecordTime.Int64
	}

	if totalSize.Valid {
		stats.TotalSize = totalSize.Int64
	}

	return &stats, nil
}

// CleanupOldMappings removes offset mappings older than the specified time
func (s *SQLOffsetStorage) CleanupOldMappings(olderThanNs int64) error {
	// TODO: Add configurable cleanup policies
	// ASSUMPTION: Simple time-based cleanup, could be enhanced with retention policies
	query := `
		DELETE FROM offset_mappings 
		WHERE created_at < ?
	`

	result, err := s.db.Exec(query, olderThanNs)
	if err != nil {
		return fmt.Errorf("failed to cleanup old mappings: %w", err)
	}

	rowsAffected, _ := result.RowsAffected()
	if rowsAffected > 0 {
		// Log cleanup activity
		fmt.Printf("Cleaned up %d old offset mappings\n", rowsAffected)
	}

	return nil
}

// Close closes the database connection
func (s *SQLOffsetStorage) Close() error {
	if s.db != nil {
		return s.db.Close()
	}
	return nil
}

// PartitionStats provides statistics about a partition's offset usage
type PartitionStats struct {
	PartitionKey    string
	RecordCount     int64
	EarliestOffset  int64
	LatestOffset    int64
	HighWaterMark   int64
	TotalSize       int64
	FirstRecordTime int64
	LastRecordTime  int64
}

// GetAllPartitions returns a list of all partitions with offset data
func (s *SQLOffsetStorage) GetAllPartitions() ([]string, error) {
	query := `
		SELECT DISTINCT partition_key 
		FROM offset_mappings 
		ORDER BY partition_key
	`

	rows, err := s.db.Query(query)
	if err != nil {
		return nil, fmt.Errorf("failed to get all partitions: %w", err)
	}
	defer rows.Close()

	var partitions []string
	for rows.Next() {
		var partitionKey string
		if err := rows.Scan(&partitionKey); err != nil {
			return nil, fmt.Errorf("failed to scan partition key: %w", err)
		}
		partitions = append(partitions, partitionKey)
	}

	return partitions, nil
}

// Vacuum performs database maintenance operations
func (s *SQLOffsetStorage) Vacuum() error {
	// TODO: Add database-specific optimization commands
	// ASSUMPTION: SQLite VACUUM command, may need adaptation for other databases
	_, err := s.db.Exec("VACUUM")
	if err != nil {
		return fmt.Errorf("failed to vacuum database: %w", err)
	}

	return nil
}