|
| 1 | +package kubernetescollector |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "slices" |
| 9 | + "strings" |
| 10 | + "sync" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/VictoriaMetrics/VictoriaLogs/lib/logstorage" |
| 14 | + "github.com/VictoriaMetrics/VictoriaMetrics/lib/fs" |
| 15 | + "github.com/VictoriaMetrics/VictoriaMetrics/lib/logger" |
| 16 | +) |
| 17 | + |
| 18 | +// checkpointsDB manages persistent log file reading state checkpoints. |
| 19 | +// It saves reading positions to disk to enable resuming log collection |
| 20 | +// after vlagent restarts without data loss or duplication. |
| 21 | +// |
| 22 | +// The caller is responsible for closing checkpointsDB via stop() method |
| 23 | +// when it's no longer needed. |
| 24 | +type checkpointsDB struct { |
| 25 | + checkpointsPath string |
| 26 | + |
| 27 | + checkpoints map[string]checkpoint |
| 28 | + checkpointsLock sync.Mutex |
| 29 | + |
| 30 | + wg sync.WaitGroup |
| 31 | + stopCh chan struct{} |
| 32 | +} |
| 33 | + |
| 34 | +// startCheckpointsDB starts a checkpointsDB instance. |
| 35 | +// The caller must call stop() when the checkpointsDB is no longer needed. |
| 36 | +func startCheckpointsDB(path string) (*checkpointsDB, error) { |
| 37 | + checkpoints, err := readCheckpoints(path) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + checkpointsMap := make(map[string]checkpoint) |
| 43 | + for _, cp := range checkpoints { |
| 44 | + checkpointsMap[cp.Path] = cp |
| 45 | + } |
| 46 | + |
| 47 | + db := &checkpointsDB{ |
| 48 | + checkpointsPath: path, |
| 49 | + checkpoints: checkpointsMap, |
| 50 | + stopCh: make(chan struct{}), |
| 51 | + } |
| 52 | + |
| 53 | + db.startPeriodicSyncCheckpoints() |
| 54 | + |
| 55 | + return db, nil |
| 56 | +} |
| 57 | + |
| 58 | +// checkpoint represents a persistent snapshot of a log file reading state. |
| 59 | +// |
| 60 | +// The checkpoint is saved to disk to enable resuming log collection from the exact |
| 61 | +// position after vlagent restarts, preventing: |
| 62 | +// 1. Log duplication when logs are re-read from the beginning. |
| 63 | +// 2. Log loss when a log file was rotated while vlagent was down. |
| 64 | +// In this case we should find the rotated file. |
| 65 | +// |
| 66 | +// checkpoint includes pod metadata (common and stream fields) to allow immediate log processing |
| 67 | +// without waiting for the Kubernetes API server to provide pod information. |
| 68 | +type checkpoint struct { |
| 69 | + Path string `json:"path"` |
| 70 | + Inode uint64 `json:"inode"` |
| 71 | + Offset int64 `json:"offset"` |
| 72 | + CommonFields []logstorage.Field `json:"commonFields"` |
| 73 | +} |
| 74 | + |
| 75 | +func (db *checkpointsDB) set(cp checkpoint) { |
| 76 | + db.checkpointsLock.Lock() |
| 77 | + defer db.checkpointsLock.Unlock() |
| 78 | + |
| 79 | + db.checkpoints[cp.Path] = cp |
| 80 | +} |
| 81 | + |
| 82 | +func (db *checkpointsDB) get(path string) (checkpoint, bool) { |
| 83 | + db.checkpointsLock.Lock() |
| 84 | + defer db.checkpointsLock.Unlock() |
| 85 | + |
| 86 | + cp, ok := db.checkpoints[path] |
| 87 | + return cp, ok |
| 88 | +} |
| 89 | + |
| 90 | +func (db *checkpointsDB) getAll() []checkpoint { |
| 91 | + db.checkpointsLock.Lock() |
| 92 | + defer db.checkpointsLock.Unlock() |
| 93 | + |
| 94 | + cps := make([]checkpoint, 0, len(db.checkpoints)) |
| 95 | + for _, cp := range db.checkpoints { |
| 96 | + cps = append(cps, cp) |
| 97 | + } |
| 98 | + |
| 99 | + return cps |
| 100 | +} |
| 101 | + |
| 102 | +func (db *checkpointsDB) delete(path string) { |
| 103 | + db.checkpointsLock.Lock() |
| 104 | + defer db.checkpointsLock.Unlock() |
| 105 | + |
| 106 | + delete(db.checkpoints, path) |
| 107 | +} |
| 108 | + |
| 109 | +func (db *checkpointsDB) mustSync() { |
| 110 | + cps := db.getAll() |
| 111 | + |
| 112 | + slices.SortFunc(cps, func(a, b checkpoint) int { |
| 113 | + return strings.Compare(a.Path, b.Path) |
| 114 | + }) |
| 115 | + |
| 116 | + data, err := json.MarshalIndent(cps, "", "\t") |
| 117 | + if err != nil { |
| 118 | + logger.Panicf("BUG: cannot marshal checkpoints: %s", err) |
| 119 | + } |
| 120 | + |
| 121 | + fs.MustWriteAtomic(db.checkpointsPath, data, true) |
| 122 | +} |
| 123 | + |
| 124 | +func readCheckpoints(path string) ([]checkpoint, error) { |
| 125 | + data, err := os.ReadFile(path) |
| 126 | + if err != nil { |
| 127 | + if errors.Is(err, os.ErrNotExist) { |
| 128 | + return nil, nil |
| 129 | + } |
| 130 | + return nil, fmt.Errorf("cannot read file checkpoints: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + if len(data) == 0 { |
| 134 | + return nil, nil |
| 135 | + } |
| 136 | + |
| 137 | + var checkpoints []checkpoint |
| 138 | + if err := json.Unmarshal(data, &checkpoints); err != nil { |
| 139 | + return nil, fmt.Errorf("cannot unmarshal file checkpoints from %q: %w", path, err) |
| 140 | + } |
| 141 | + |
| 142 | + return checkpoints, nil |
| 143 | +} |
| 144 | + |
| 145 | +// startPeriodicFlushCheckpoints periodically persists in-memory checkpoints to disk. |
| 146 | +// |
| 147 | +// It complements the explicit sync performed on graceful stop, |
| 148 | +// ensuring regular persistence even when the process is killed. |
| 149 | +func (db *checkpointsDB) startPeriodicSyncCheckpoints() { |
| 150 | + db.wg.Add(1) |
| 151 | + go func() { |
| 152 | + defer db.wg.Done() |
| 153 | + |
| 154 | + ticker := time.NewTicker(1 * time.Minute) |
| 155 | + defer ticker.Stop() |
| 156 | + |
| 157 | + for { |
| 158 | + select { |
| 159 | + case <-ticker.C: |
| 160 | + db.mustSync() |
| 161 | + case <-db.stopCh: |
| 162 | + db.mustSync() |
| 163 | + return |
| 164 | + } |
| 165 | + } |
| 166 | + }() |
| 167 | +} |
| 168 | + |
| 169 | +func (db *checkpointsDB) stop() { |
| 170 | + close(db.stopCh) |
| 171 | + db.wg.Wait() |
| 172 | +} |
0 commit comments