|
| 1 | +package logstorage |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +// asyncTaskType identifies the type of background (asynchronous) task attached to a partition. |
| 12 | +// More types can be added in the future (e.g. compaction, ttl, schema-changes). |
| 13 | +type asyncTaskType int |
| 14 | + |
| 15 | +const ( |
| 16 | + asyncTaskNone asyncTaskType = iota // no task |
| 17 | + asyncTaskDelete // delete rows matching a query |
| 18 | +) |
| 19 | + |
| 20 | +// Status field tracks the outcome of the task. |
| 21 | +type asyncTaskStatus int |
| 22 | + |
| 23 | +const ( |
| 24 | + taskPending asyncTaskStatus = iota |
| 25 | + taskSuccess |
| 26 | + taskError |
| 27 | +) |
| 28 | + |
| 29 | +type asyncTask struct { |
| 30 | + Type asyncTaskType `json:"type"` |
| 31 | + TenantIDs []TenantID `json:"tenantIDs,omitempty"` // affected tenants (empty slice = all) |
| 32 | + Query string `json:"query,omitempty"` // serialized LogSQL query |
| 33 | + Seq uint64 `json:"seq,omitempty"` // monotonically increasing *global* sequence |
| 34 | + |
| 35 | + // Status tracks the last execution state; omitted from JSON when zero (pending) to |
| 36 | + // preserve compatibility with tasks created before this field existed. |
| 37 | + Status asyncTaskStatus `json:"status,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +type asyncTasks struct { |
| 41 | + pt *partition |
| 42 | + |
| 43 | + mu sync.Mutex |
| 44 | + ts []asyncTask |
| 45 | + currentSeq atomic.Uint64 |
| 46 | +} |
| 47 | + |
| 48 | +func newAsyncTasks(pt *partition, tasks []asyncTask) *asyncTasks { |
| 49 | + ast := &asyncTasks{ |
| 50 | + pt: pt, |
| 51 | + ts: tasks, |
| 52 | + } |
| 53 | + return ast |
| 54 | +} |
| 55 | + |
| 56 | +func (at *asyncTasks) updatePending() asyncTask { |
| 57 | + var result asyncTask |
| 58 | + |
| 59 | + at.mu.Lock() |
| 60 | + for i := range at.ts { |
| 61 | + task := at.ts[i] |
| 62 | + if task.Status == taskPending { |
| 63 | + result = task |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + at.mu.Unlock() |
| 68 | + |
| 69 | + at.currentSeq.Store(result.Seq) |
| 70 | + return result |
| 71 | +} |
| 72 | + |
| 73 | +// unmarshalAsyncTasks converts JSON data back to async tasks |
| 74 | +func unmarshalAsyncTasks(data []byte) ([]asyncTask, error) { |
| 75 | + if len(data) == 0 { |
| 76 | + return nil, nil |
| 77 | + } |
| 78 | + |
| 79 | + var tasks []asyncTask |
| 80 | + if err := json.Unmarshal(data, &tasks); err != nil { |
| 81 | + return nil, fmt.Errorf("unmarshal async tasks: %w", err) |
| 82 | + } |
| 83 | + return tasks, nil |
| 84 | +} |
| 85 | + |
| 86 | +// markResolvedSync updates task status and persists the change to disk. |
| 87 | +// It holds the internal mutex only for in‐memory modification; the slow fs write |
| 88 | +// is executed after the lock is released to avoid blocking other readers. |
| 89 | +func (at *asyncTasks) markResolvedSync(seq uint64, status asyncTaskStatus) { |
| 90 | + var updated bool |
| 91 | + |
| 92 | + at.mu.Lock() |
| 93 | + for i := len(at.ts) - 1; i >= 0; i-- { |
| 94 | + if at.ts[i].Seq == seq && at.ts[i].Status == taskPending { |
| 95 | + at.ts[i].Status = status |
| 96 | + updated = true |
| 97 | + break |
| 98 | + } |
| 99 | + } |
| 100 | + at.mu.Unlock() |
| 101 | + |
| 102 | + if updated { |
| 103 | + at.pt.mustSaveAsyncTasks() |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +// addDeleteTask appends a delete task to the partition's task list |
| 108 | +func (at *asyncTasks) addDeleteTaskSync(tenantIDs []TenantID, q *Query, seq uint64) uint64 { |
| 109 | + task := asyncTask{ |
| 110 | + Seq: seq, |
| 111 | + Type: asyncTaskDelete, |
| 112 | + TenantIDs: append([]TenantID(nil), tenantIDs...), |
| 113 | + Query: q.String(), |
| 114 | + Status: taskPending, |
| 115 | + } |
| 116 | + |
| 117 | + at.mu.Lock() |
| 118 | + at.ts = append(at.ts, task) |
| 119 | + at.mu.Unlock() |
| 120 | + |
| 121 | + at.pt.mustSaveAsyncTasks() |
| 122 | + return seq |
| 123 | +} |
| 124 | + |
| 125 | +// taskSeq provides unique, monotonically increasing sequence numbers for async tasks. |
| 126 | +var taskSeq atomic.Uint64 |
| 127 | + |
| 128 | +func init() { |
| 129 | + // Initialise with current unix-nano in order to minimise collision with seqs that may be present on disk. |
| 130 | + taskSeq.Store(uint64(time.Now().UnixNano())) |
| 131 | +} |
0 commit comments