2018-02-05 16:05:59 -05:00
|
|
|
package jsonfilelog // import "github.com/docker/docker/daemon/logger/jsonfilelog"
|
2015-10-08 16:55:28 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-05 12:39:28 -04:00
|
|
|
"context"
|
2015-10-08 16:55:28 -04:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2016-07-11 16:31:42 -04:00
|
|
|
|
2017-07-18 22:01:20 -04:00
|
|
|
"github.com/docker/docker/api/types/backend"
|
2015-10-08 16:55:28 -04:00
|
|
|
"github.com/docker/docker/daemon/logger"
|
2017-09-25 15:57:45 -04:00
|
|
|
"github.com/docker/docker/daemon/logger/jsonfilelog/jsonlog"
|
2018-04-05 12:39:28 -04:00
|
|
|
"github.com/docker/docker/daemon/logger/loggerutils"
|
|
|
|
"github.com/docker/docker/pkg/tailfile"
|
2015-10-08 16:55:28 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// ReadLogs implements the logger's LogReader interface for the logs
|
|
|
|
// created by this driver.
|
|
|
|
func (l *JSONFileLogger) ReadLogs(config logger.ReadConfig) *logger.LogWatcher {
|
2022-03-02 18:16:47 -05:00
|
|
|
return l.writer.ReadLogs(config)
|
2015-10-08 16:55:28 -04:00
|
|
|
}
|
|
|
|
|
2017-07-18 11:54:03 -04:00
|
|
|
func decodeLogLine(dec *json.Decoder, l *jsonlog.JSONLog) (*logger.Message, error) {
|
|
|
|
l.Reset()
|
|
|
|
if err := dec.Decode(l); err != nil {
|
2016-07-11 16:31:42 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-18 11:54:03 -04:00
|
|
|
var attrs []backend.LogAttr
|
|
|
|
if len(l.Attrs) != 0 {
|
|
|
|
attrs = make([]backend.LogAttr, 0, len(l.Attrs))
|
|
|
|
for k, v := range l.Attrs {
|
|
|
|
attrs = append(attrs, backend.LogAttr{Key: k, Value: v})
|
2016-07-11 16:31:42 -04:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 11:54:03 -04:00
|
|
|
msg := &logger.Message{
|
|
|
|
Source: l.Stream,
|
|
|
|
Timestamp: l.Created,
|
|
|
|
Line: []byte(l.Log),
|
|
|
|
Attrs: attrs,
|
|
|
|
}
|
|
|
|
return msg, nil
|
2016-07-11 16:31:42 -04:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:24:31 -04:00
|
|
|
type decoder struct {
|
daemon/logger: follow LogFile without file watches
File watches have been a source of complexity and unreliability in the
LogFile follow implementation, especially when combined with file
rotation. File change events can be unreliably delivered, especially on
Windows, and the polling fallback adds latency. Following across
rotations has never worked reliably on Windows. Without synchronization
between the log writer and readers, race conditions abound: readers can
read from the file while a log entry is only partially written, leading
to decode errors and necessitating retries.
In addition to the complexities stemming from file watches, the LogFile
follow implementation had complexity from needing to handle file
truncations, and (due to a now-fixed bug in the polling file watcher
implementation) evictions to unlock the log file so it could be rotated.
Log files are now always rotated, never truncated, so these situations
no longer need to be handled by the follow code.
Rewrite the LogFile follow implementation in terms of waiting until
LogFile notifies it that a new message has been written to the log file.
The LogFile informs the follower of the file offset of the last complete
write so that the follower knows not to read past that, preventing it
from attempting to decode partial messages and making retries
unnecessary. Synchronization between LogFile and its followers is used
at critical points to prevent missed notifications of writes and races
between file rotations and the follower opening files for read.
Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-02-09 14:49:23 -05:00
|
|
|
rdr io.Reader
|
|
|
|
dec *json.Decoder
|
|
|
|
jl *jsonlog.JSONLog
|
2020-04-08 15:24:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) Reset(rdr io.Reader) {
|
|
|
|
d.rdr = rdr
|
|
|
|
d.dec = nil
|
|
|
|
if d.jl != nil {
|
|
|
|
d.jl.Reset()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) Close() {
|
|
|
|
d.dec = nil
|
|
|
|
d.rdr = nil
|
|
|
|
d.jl = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *decoder) Decode() (msg *logger.Message, err error) {
|
|
|
|
if d.dec == nil {
|
|
|
|
d.dec = json.NewDecoder(d.rdr)
|
|
|
|
}
|
|
|
|
if d.jl == nil {
|
|
|
|
d.jl = &jsonlog.JSONLog{}
|
|
|
|
}
|
daemon/logger: follow LogFile without file watches
File watches have been a source of complexity and unreliability in the
LogFile follow implementation, especially when combined with file
rotation. File change events can be unreliably delivered, especially on
Windows, and the polling fallback adds latency. Following across
rotations has never worked reliably on Windows. Without synchronization
between the log writer and readers, race conditions abound: readers can
read from the file while a log entry is only partially written, leading
to decode errors and necessitating retries.
In addition to the complexities stemming from file watches, the LogFile
follow implementation had complexity from needing to handle file
truncations, and (due to a now-fixed bug in the polling file watcher
implementation) evictions to unlock the log file so it could be rotated.
Log files are now always rotated, never truncated, so these situations
no longer need to be handled by the follow code.
Rewrite the LogFile follow implementation in terms of waiting until
LogFile notifies it that a new message has been written to the log file.
The LogFile informs the follower of the file offset of the last complete
write so that the follower knows not to read past that, preventing it
from attempting to decode partial messages and making retries
unnecessary. Synchronization between LogFile and its followers is used
at critical points to prevent missed notifications of writes and races
between file rotations and the follower opening files for read.
Signed-off-by: Cory Snider <csnider@mirantis.com>
2022-02-09 14:49:23 -05:00
|
|
|
return decodeLogLine(d.dec, d.jl)
|
2021-03-15 17:25:53 -04:00
|
|
|
}
|
|
|
|
|
2020-04-08 15:24:31 -04:00
|
|
|
// decodeFunc is used to create a decoder for the log file reader
|
|
|
|
func decodeFunc(rdr io.Reader) loggerutils.Decoder {
|
|
|
|
return &decoder{
|
|
|
|
rdr: rdr,
|
|
|
|
dec: nil,
|
|
|
|
jl: nil,
|
2015-10-08 16:55:28 -04:00
|
|
|
}
|
|
|
|
}
|
2018-04-05 12:39:28 -04:00
|
|
|
|
|
|
|
func getTailReader(ctx context.Context, r loggerutils.SizeReaderAt, req int) (io.Reader, int, error) {
|
|
|
|
return tailfile.NewTailReader(ctx, r, req)
|
|
|
|
}
|