Skip to content

Commit 9cc380f

Browse files
committed
in_tail: fix data loss on shutdown and prevent DB resurrection
Unprocessed buffered data is lost on shutdown because the file offset is saved ahead of processing. This patch rewinds the offset by the remaining buffer length on exit, ensuring data is re-read on restart. Also, to prevent resurrecting deleted files in the DB, db_id is reset to 0 upon deletion. The offset update logic now checks db_id > 0 to ensure only active files are updated. Signed-off-by: jinyong.choi <[email protected]>
1 parent 0296ff2 commit 9cc380f

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

plugins/in_tail/tail.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
#define FLB_TAIL_STATIC 0 /* Data is being consumed through read(2) */
3434
#define FLB_TAIL_EVENT 1 /* Data is being consumed through inotify */
3535

36+
/* Database */
37+
#define FLB_TAIL_DB_ID_NONE 0 /* File not in database or deleted */
38+
3639
/* Config */
3740
#define FLB_TAIL_CHUNK "32768" /* buffer chunk = 32KB */
3841
#define FLB_TAIL_REFRESH 60 /* refresh every 60 seconds */

plugins/in_tail/tail_db.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ int flb_tail_db_file_delete(struct flb_tail_file *file,
363363
}
364364

365365
flb_plg_debug(ctx->ins, "db: file deleted from database: %s", file->name);
366+
file->db_id = FLB_TAIL_DB_ID_NONE;
366367
return 0;
367368
}
368369

plugins/in_tail/tail_file.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1274,7 +1274,7 @@ int flb_tail_file_append(char *path, struct stat *st, int mode,
12741274
file->dmode_lastline = flb_sds_create_size(ctx->docker_mode == FLB_TRUE ? 20000 : 0);
12751275
file->dmode_firstline = false;
12761276
#ifdef FLB_HAVE_SQLDB
1277-
file->db_id = 0;
1277+
file->db_id = FLB_TAIL_DB_ID_NONE;
12781278
#endif
12791279
file->skip_next = FLB_FALSE;
12801280
file->skip_warn = FLB_FALSE;
@@ -1448,6 +1448,38 @@ void flb_tail_file_remove(struct flb_tail_file *file)
14481448
flb_plg_debug(ctx->ins, "inode=%"PRIu64" removing file name %s",
14491449
file->inode, file->name);
14501450

1451+
if (file->buf_len > 0) {
1452+
if (file->decompression_context == NULL) {
1453+
/*
1454+
* If there is data in the buffer, it means it was not processed.
1455+
* We must rewind the offset to ensure this data is re-read on restart.
1456+
*/
1457+
off_t old_offset = file->offset;
1458+
1459+
if (file->offset > file->buf_len) {
1460+
file->offset -= file->buf_len;
1461+
} else {
1462+
file->offset = 0;
1463+
}
1464+
1465+
flb_plg_debug(ctx->ins, "inode=%"PRIu64" rewind offset for %s: "
1466+
"old=%"PRId64" new=%"PRId64" (buf_len=%lu)",
1467+
file->inode, file->name, old_offset, file->offset,
1468+
(unsigned long)file->buf_len);
1469+
1470+
#ifdef FLB_HAVE_SQLDB
1471+
if (ctx->db && file->db_id > FLB_TAIL_DB_ID_NONE) {
1472+
flb_tail_db_file_offset(file, ctx);
1473+
}
1474+
#endif
1475+
}
1476+
else {
1477+
flb_plg_warn(ctx->ins, "inode=%"PRIu64" cannot rewind compressed file %s; "
1478+
"%lu decompressed bytes in buffer may be lost on restart",
1479+
file->inode, file->name, (unsigned long)file->buf_len);
1480+
}
1481+
}
1482+
14511483
if (file->decompression_context != NULL) {
14521484
flb_decompression_context_destroy(file->decompression_context);
14531485
}

0 commit comments

Comments
 (0)