2018-02-05 16:05:59 -05:00
|
|
|
package logger // import "github.com/docker/docker/daemon/logger"
|
2016-11-14 13:53:53 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
2018-04-02 16:33:36 -04:00
|
|
|
"sync"
|
2016-11-14 13:53:53 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types/plugins/logdriver"
|
|
|
|
protoio "github.com/gogo/protobuf/io"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
2016-11-14 13:53:53 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// mockLoggingPlugin implements the loggingPlugin interface for testing purposes
|
|
|
|
// it only supports a single log stream
|
|
|
|
type mockLoggingPlugin struct {
|
2018-04-02 16:33:36 -04:00
|
|
|
io.WriteCloser
|
|
|
|
inStream io.Reader
|
|
|
|
logs []*logdriver.LogEntry
|
|
|
|
c *sync.Cond
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockLoggingPlugin() *mockLoggingPlugin {
|
|
|
|
r, w := io.Pipe()
|
|
|
|
return &mockLoggingPlugin{
|
|
|
|
WriteCloser: w,
|
|
|
|
inStream: r,
|
|
|
|
logs: []*logdriver.LogEntry{},
|
|
|
|
c: sync.NewCond(new(sync.Mutex)),
|
|
|
|
}
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mockLoggingPlugin) StartLogging(file string, info Info) error {
|
|
|
|
go func() {
|
2018-04-02 16:33:36 -04:00
|
|
|
dec := protoio.NewUint32DelimitedReader(l.inStream, binary.BigEndian, 1e6)
|
|
|
|
for {
|
|
|
|
var msg logdriver.LogEntry
|
|
|
|
if err := dec.ReadMsg(&msg); err != nil {
|
|
|
|
l.c.L.Lock()
|
|
|
|
if l.err == nil {
|
|
|
|
l.err = err
|
|
|
|
}
|
|
|
|
l.c.L.Unlock()
|
|
|
|
|
|
|
|
l.c.Broadcast()
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
l.c.L.Lock()
|
|
|
|
l.logs = append(l.logs, &msg)
|
|
|
|
l.c.L.Unlock()
|
|
|
|
l.c.Broadcast()
|
|
|
|
}
|
|
|
|
|
2016-11-14 13:53:53 -05:00
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mockLoggingPlugin) StopLogging(file string) error {
|
2018-04-02 16:33:36 -04:00
|
|
|
l.c.L.Lock()
|
|
|
|
if l.err == nil {
|
|
|
|
l.err = io.EOF
|
|
|
|
}
|
|
|
|
l.c.L.Unlock()
|
|
|
|
l.c.Broadcast()
|
2016-11-14 13:53:53 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mockLoggingPlugin) Capabilities() (cap Capability, err error) {
|
|
|
|
return Capability{ReadLogs: true}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *mockLoggingPlugin) ReadLogs(info Info, config ReadConfig) (io.ReadCloser, error) {
|
|
|
|
r, w := io.Pipe()
|
2018-04-02 16:33:36 -04:00
|
|
|
|
2016-11-14 13:53:53 -05:00
|
|
|
go func() {
|
2018-04-02 16:33:36 -04:00
|
|
|
var idx int
|
2016-11-14 13:53:53 -05:00
|
|
|
enc := logdriver.NewLogEntryEncoder(w)
|
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
l.c.L.Lock()
|
|
|
|
defer l.c.L.Unlock()
|
2016-11-14 13:53:53 -05:00
|
|
|
for {
|
2018-04-02 16:33:36 -04:00
|
|
|
if l.err != nil {
|
2016-11-14 13:53:53 -05:00
|
|
|
w.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
if idx >= len(l.logs) {
|
|
|
|
if !config.Follow {
|
|
|
|
w.Close()
|
|
|
|
return
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
l.c.Wait()
|
2016-11-14 13:53:53 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
if err := enc.Encode(l.logs[idx]); err != nil {
|
2016-11-14 13:53:53 -05:00
|
|
|
w.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2018-04-02 16:33:36 -04:00
|
|
|
idx++
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return r, nil
|
|
|
|
}
|
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
func (l *mockLoggingPlugin) waitLen(i int) {
|
|
|
|
l.c.L.Lock()
|
|
|
|
defer l.c.L.Unlock()
|
|
|
|
for len(l.logs) < i {
|
|
|
|
l.c.Wait()
|
|
|
|
}
|
|
|
|
}
|
2017-06-18 21:47:45 -04:00
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
func (l *mockLoggingPlugin) check(t *testing.T) {
|
|
|
|
if l.err != nil && l.err != io.EOF {
|
|
|
|
t.Fatal(l.err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMockPluginAdapter(plugin *mockLoggingPlugin) Logger {
|
|
|
|
enc := logdriver.NewLogEntryEncoder(plugin)
|
2016-11-14 13:53:53 -05:00
|
|
|
a := &pluginAdapterWithRead{
|
|
|
|
&pluginAdapter{
|
2018-04-02 16:33:36 -04:00
|
|
|
plugin: plugin,
|
|
|
|
stream: plugin,
|
2016-11-14 13:53:53 -05:00
|
|
|
enc: enc,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
a.plugin.StartLogging("", Info{})
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAdapterReadLogs(t *testing.T) {
|
2018-04-02 16:33:36 -04:00
|
|
|
plugin := newMockLoggingPlugin()
|
|
|
|
l := newMockPluginAdapter(plugin)
|
2016-11-14 13:53:53 -05:00
|
|
|
|
|
|
|
testMsg := []Message{
|
|
|
|
{Line: []byte("Are you the keymaker?"), Timestamp: time.Now()},
|
|
|
|
{Line: []byte("Follow the white rabbit"), Timestamp: time.Now()},
|
|
|
|
}
|
|
|
|
for _, msg := range testMsg {
|
|
|
|
m := msg.copy()
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, l.Log(m))
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|
|
|
|
|
2018-04-02 16:33:36 -04:00
|
|
|
// Wait until messages are read into plugin
|
|
|
|
plugin.waitLen(len(testMsg))
|
|
|
|
|
2016-11-14 13:53:53 -05:00
|
|
|
lr, ok := l.(LogReader)
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, ok, "Logger does not implement LogReader")
|
2016-11-14 13:53:53 -05:00
|
|
|
|
|
|
|
lw := lr.ReadLogs(ReadConfig{})
|
|
|
|
|
|
|
|
for _, x := range testMsg {
|
|
|
|
select {
|
|
|
|
case msg := <-lw.Msg:
|
|
|
|
testMessageEqual(t, &x, msg)
|
2017-06-18 21:47:45 -04:00
|
|
|
case <-time.After(10 * time.Second):
|
2016-11-14 13:53:53 -05:00
|
|
|
t.Fatal("timeout reading logs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case _, ok := <-lw.Msg:
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, !ok, "expected message channel to be closed")
|
2016-11-14 13:53:53 -05:00
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for message channel to close")
|
|
|
|
|
|
|
|
}
|
daemon.ContainerLogs(): fix resource leak on follow
When daemon.ContainerLogs() is called with options.follow=true
(as in "docker logs --follow"), the "loggerutils.followLogs()"
function never returns (even then the logs consumer is gone).
As a result, all the resources associated with it (including
an opened file descriptor for the log file being read, two FDs
for a pipe, and two FDs for inotify watch) are never released.
If this is repeated (such as by running "docker logs --follow"
and pressing Ctrl-C a few times), this results in DoS caused by
either hitting the limit of inotify watches, or the limit of
opened files. The only cure is daemon restart.
Apparently, what happens is:
1. logs producer (a container) is gone, calling (*LogWatcher).Close()
for all its readers (daemon/logger/jsonfilelog/jsonfilelog.go:175).
2. WatchClose() is properly handled by a dedicated goroutine in
followLogs(), cancelling the context.
3. Upon receiving the ctx.Done(), the code in followLogs()
(daemon/logger/loggerutils/logfile.go#L626-L638) keeps to
send messages _synchronously_ (which is OK for now).
4. Logs consumer is gone (Ctrl-C is pressed on a terminal running
"docker logs --follow"). Method (*LogWatcher).Close() is properly
called (see daemon/logs.go:114). Since it was called before and
due to to once.Do(), nothing happens (which is kinda good, as
otherwise it will panic on closing a closed channel).
5. A goroutine (see item 3 above) keeps sending log messages
synchronously to the logWatcher.Msg channel. Since the
channel reader is gone, the channel send operation blocks forever,
and resource cleanup set up in defer statements at the beginning
of followLogs() never happens.
Alas, the fix is somewhat complicated:
1. Distinguish between close from logs producer and logs consumer.
To that effect,
- yet another channel is added to LogWatcher();
- {Watch,}Close() are renamed to {Watch,}ProducerGone();
- {Watch,}ConsumerGone() are added;
*NOTE* that ProducerGone()/WatchProducerGone() pair is ONLY needed
in order to stop ConsumerLogs(follow=true) when a container is stopped;
otherwise we're not interested in it. In other words, we're only
using it in followLogs().
2. Code that was doing (logWatcher*).Close() is modified to either call
ProducerGone() or ConsumerGone(), depending on the context.
3. Code that was waiting for WatchClose() is modified to wait for
either ConsumerGone() or ProducerGone(), or both, depending on the
context.
4. followLogs() are modified accordingly:
- context cancellation is happening on WatchProducerGone(),
and once it's received the FileWatcher is closed and waitRead()
returns errDone on EOF (i.e. log rotation handling logic is disabled);
- due to this, code that was writing synchronously to logWatcher.Msg
can be and is removed as the code above it handles this case;
- function returns once ConsumerGone is received, freeing all the
resources -- this is the bugfix itself.
While at it,
1. Let's also remove the ctx usage to simplify the code a bit.
It was introduced by commit a69a59ffc7e3d ("Decouple removing the
fileWatcher from reading") in order to fix a bug. The bug was actually
a deadlock in fsnotify, and the fix was just a workaround. Since then
the fsnofify bug has been fixed, and a new fsnotify was vendored in.
For more details, please see
https://github.com/moby/moby/pull/27782#issuecomment-416794490
2. Since `(*filePoller).Close()` is fixed to remove all the files
being watched, there is no need to explicitly call
fileWatcher.Remove(name) anymore, so get rid of the extra code.
Should fix https://github.com/moby/moby/issues/37391
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
2018-08-01 00:03:55 -04:00
|
|
|
lw.ProducerGone()
|
2016-11-14 13:53:53 -05:00
|
|
|
|
|
|
|
lw = lr.ReadLogs(ReadConfig{Follow: true})
|
|
|
|
for _, x := range testMsg {
|
|
|
|
select {
|
|
|
|
case msg := <-lw.Msg:
|
|
|
|
testMessageEqual(t, &x, msg)
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout reading logs")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x := Message{Line: []byte("Too infinity and beyond!"), Timestamp: time.Now()}
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, l.Log(x.copy()))
|
2016-11-14 13:53:53 -05:00
|
|
|
|
|
|
|
select {
|
|
|
|
case msg, ok := <-lw.Msg:
|
2017-12-22 16:30:49 -05:00
|
|
|
assert.Check(t, ok, "message channel unexpectedly closed")
|
2016-11-14 13:53:53 -05:00
|
|
|
testMessageEqual(t, &x, msg)
|
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout reading logs")
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Close()
|
|
|
|
select {
|
|
|
|
case msg, ok := <-lw.Msg:
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, !ok, "expected message channel to be closed")
|
|
|
|
assert.Check(t, is.Nil(msg))
|
2016-11-14 13:53:53 -05:00
|
|
|
case <-time.After(10 * time.Second):
|
|
|
|
t.Fatal("timeout waiting for logger to close")
|
|
|
|
}
|
2018-04-02 16:33:36 -04:00
|
|
|
|
|
|
|
plugin.check(t)
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testMessageEqual(t *testing.T, a, b *Message) {
|
2018-03-13 15:28:34 -04:00
|
|
|
assert.Check(t, is.DeepEqual(a.Line, b.Line))
|
|
|
|
assert.Check(t, is.DeepEqual(a.Timestamp.UnixNano(), b.Timestamp.UnixNano()))
|
|
|
|
assert.Check(t, is.Equal(a.Source, b.Source))
|
2016-11-14 13:53:53 -05:00
|
|
|
}
|