Skip to content

Commit d339009

Browse files
committed
fix: alert event table replacing/orderby key
1 parent 2a2d81a commit d339009

4 files changed

Lines changed: 31 additions & 18 deletions

File tree

server/ingester/event/dbwriter/alert_event_column_block.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ type AlertEventBlock struct {
4747
ColUserId proto.ColUInt32
4848
// New columns
4949
ColEventId proto.ColStr
50-
ColStartTime proto.ColUInt64
51-
ColEndTime proto.ColUInt64
50+
ColStartTime proto.ColDateTime
51+
ColEndTime proto.ColDateTime
5252
ColDuration proto.ColUInt32
5353
ColState proto.ColUInt32
5454
ColAlertTime proto.ColUInt64
@@ -155,8 +155,8 @@ func (n *AlertEventStore) AppendToColumnBlock(b ckdb.CKColumnBlock) {
155155
block.ColTeamId.Append(n.TeamID)
156156
block.ColUserId.Append(n.UserId)
157157
block.ColEventId.Append(n.EventId)
158-
block.ColStartTime.Append(n.StartTime)
159-
block.ColEndTime.Append(n.EndTime)
158+
ckdb.AppendColDateTime(&block.ColStartTime, n.StartTime)
159+
ckdb.AppendColDateTime(&block.ColEndTime, n.EndTime)
160160
block.ColDuration.Append(n.Duration)
161161
block.ColState.Append(n.State)
162162
block.ColAlertTime.Append(n.AlertTime)

server/ingester/event/dbwriter/alert_event_writer.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ type AlertEventStore struct {
7676

7777
// New fields for AlertEvent
7878
EventId string
79-
StartTime uint64
80-
EndTime uint64
79+
StartTime uint32
80+
EndTime uint32
8181
Duration uint32
8282
State uint32
8383
AlertTime uint64
@@ -118,8 +118,8 @@ func AlertEventColumns() []*ckdb.Column {
118118

119119
// New columns
120120
ckdb.NewColumn("event_id", ckdb.String),
121-
ckdb.NewColumn("start_time", ckdb.UInt64),
122-
ckdb.NewColumn("end_time", ckdb.UInt64),
121+
ckdb.NewColumn("start_time", ckdb.DateTime),
122+
ckdb.NewColumn("end_time", ckdb.DateTime),
123123
ckdb.NewColumn("duration", ckdb.UInt32),
124124
ckdb.NewColumn("state", ckdb.UInt32),
125125
ckdb.NewColumn("alert_time", ckdb.UInt64),
@@ -204,12 +204,24 @@ func (e *AlertEventStore) GenerateNewFlowTags(cache *flow_tag.FlowTagCache) {
204204
}
205205

206206
// GenAlertEventCKTable creates the alert_event table definition.
207-
// Engine: ReplacingMergeTree(_id), ORDER BY (time, event_id).
207+
//
208+
// Design rationale:
209+
// - Engine: ReplacingMergeTree(time) — for the same ORDER BY key, keep the row
210+
// with the highest _id (i.e., the most recently written row).
211+
// - ORDER BY (event_id): dedup key is event_id alone, guaranteeing globally
212+
// unique event_id. ClickHouse ReplacingMergeTree dedup key == ORDER BY key,
213+
// so time cannot be added to ORDER BY without making it part of the dedup key.
214+
// - PARTITION BY toYYYYMM(time) (TimeFuncDay): provides time-based partition
215+
// pruning for fast time-range queries without requiring time in ORDER BY.
216+
// Queries with a time filter skip entire month partitions automatically.
208217
func GenAlertEventCKTable(cluster, storagePolicy, ckdbType string, ttl int, coldStorage *ckdb.ColdStorage) *ckdb.Table {
209218
table := common.ALERT_EVENT.TableName()
210219
timeKey := "time"
211220
engine := ckdb.ReplacingMergeTree
212-
orderKeys := []string{"time", "event_id"}
221+
// Dedup key is event_id only — guarantees at most one row per event_id
222+
// within each daily partition (same event_id always has the same time,
223+
// so it always lands in the same partition → globally unique event_id).
224+
orderKeys := []string{"event_id"}
213225

214226
return &ckdb.Table{
215227
Version: basecommon.CK_VERSION,
@@ -219,9 +231,9 @@ func GenAlertEventCKTable(cluster, storagePolicy, ckdbType string, ttl int, cold
219231
GlobalName: table,
220232
Columns: AlertEventColumns(),
221233
TimeKey: timeKey,
222-
ReplacingKey: "_id",
234+
ReplacingKey: "time",
223235
TTL: ttl,
224-
PartitionFunc: DefaultPartition,
236+
PartitionFunc: DefaultAlertEventPartition,
225237
Engine: engine,
226238
Cluster: cluster,
227239
StoragePolicy: storagePolicy,

server/ingester/event/dbwriter/event.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ import (
3636
)
3737

3838
const (
39-
DefaultPartition = ckdb.TimeFuncTwelveHour
40-
DefaultFileEventPartition = ckdb.TimeFuncHour
41-
IO_EVENT_TYPE_READ = "read"
42-
IO_EVENT_TYPE_WRITE = "write"
39+
DefaultPartition = ckdb.TimeFuncTwelveHour
40+
DefaultAlertEventPartition = ckdb.TimeFuncDay
41+
DefaultFileEventPartition = ckdb.TimeFuncHour
42+
IO_EVENT_TYPE_READ = "read"
43+
IO_EVENT_TYPE_WRITE = "write"
4344
)
4445

4546
type SignalSource uint8

server/ingester/event/decoder/decoder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,8 @@ func (d *Decoder) writeAlertEvent(event *alert_event.AlertEvent) {
487487

488488
// New fields
489489
s.EventId = event.GetEventId()
490-
s.StartTime = event.GetStartTime()
491-
s.EndTime = event.GetEndTime()
490+
s.StartTime = uint32(event.GetStartTime())
491+
s.EndTime = uint32(event.GetEndTime())
492492
s.Duration = event.GetDuration()
493493
s.State = event.GetState()
494494
s.AlertTime = event.GetAlertTime()

0 commit comments

Comments
 (0)