From 4cf778b28f9bd8eaf83f1537ab7825da45a30138 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Sun, 10 Mar 2019 03:39:33 +0000 Subject: [PATCH] journald/read: avoid piling up open files If we take a long time to process log messages, and during that time journal file rotation occurs, the journald client library will keep those rotated files open until sd_journal_process() is called. By periodically calling sd_journal_process() during the processing loop we shrink the window of time a client instance has open file descriptors for rotated (deleted) journal files. This code is modelled after that of journalctl [1]; the above explanation as well as the value of 1024 is taken from there. [v2: fix CErr() argument] [1] https://github.com/systemd/systemd/blob/dc16327c48d/src/journal/journalctl.c#L2676 Signed-off-by: Kir Kolyshkin (cherry picked from commit b73fb8fd5d521081c92b5c2cce334c21b2e0ff5f) Signed-off-by: Kir Kolyshkin Upstream-commit: 48160719f2c5734b513aec7cedbbca0c72f4a085 Component: engine --- components/engine/daemon/logger/journald/read.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/components/engine/daemon/logger/journald/read.go b/components/engine/daemon/logger/journald/read.go index 3dfa08b0a4..36240a832a 100644 --- a/components/engine/daemon/logger/journald/read.go +++ b/components/engine/daemon/logger/journald/read.go @@ -113,6 +113,7 @@ import ( "github.com/coreos/go-systemd/journal" "github.com/docker/docker/api/types/backend" "github.com/docker/docker/daemon/logger" + "github.com/sirupsen/logrus" ) func (s *journald) Close() error { @@ -205,6 +206,16 @@ drain: }: shown++ } + // Call sd_journal_process() periodically during the processing loop + // to close any opened file descriptors for rotated (deleted) journal files. + if shown%1024 == 0 { + if ret := C.sd_journal_process(j); ret < 0 { + // log a warning but ignore it for now + logrus.WithField("container", s.vars["CONTAINER_ID_FULL"]). + WithField("error", CErr(ret)). + Warn("journald: error processing journal") + } + } } // If we're at the end of the journal, we're done (for now). if C.sd_journal_next(j) <= 0 {