2019-08-26 09:54:38 +01:00
|
|
|
package querylog
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2024-11-21 17:19:39 +00:00
|
|
|
"context"
|
2018-12-05 11:03:41 +00:00
|
|
|
"encoding/json"
|
2021-10-29 11:43:08 +01:00
|
|
|
"fmt"
|
2018-12-05 11:03:41 +00:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
2024-10-02 19:00:15 +01:00
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
2021-05-24 15:28:11 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
2024-11-21 17:19:39 +00:00
|
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
|
|
"github.com/c2h5oh/datasize"
|
2018-12-05 11:03:41 +00:00
|
|
|
)
|
|
|
|
|
2023-03-31 15:39:04 +01:00
|
|
|
// flushLogBuffer flushes the current buffer to file and resets the current
|
|
|
|
// buffer.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) flushLogBuffer(ctx context.Context) (err error) {
|
2023-10-03 18:50:43 +01:00
|
|
|
defer func() { err = errors.Annotate(err, "flushing log buffer: %w") }()
|
2024-11-21 17:19:39 +00:00
|
|
|
|
2019-05-15 11:11:36 +01:00
|
|
|
l.fileFlushLock.Lock()
|
|
|
|
defer l.fileFlushLock.Unlock()
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
b, err := l.encodeEntries(ctx)
|
2023-10-03 18:50:43 +01:00
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return err
|
|
|
|
}
|
2023-03-31 15:39:04 +01:00
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
return l.flushToFile(ctx, b)
|
2019-02-10 17:47:43 +00:00
|
|
|
}
|
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
// encodeEntries returns JSON encoded log entries, logs estimated time, clears
|
|
|
|
// the log buffer.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) encodeEntries(ctx context.Context) (b *bytes.Buffer, err error) {
|
2023-10-03 18:50:43 +01:00
|
|
|
l.bufferLock.Lock()
|
|
|
|
defer l.bufferLock.Unlock()
|
2023-03-31 15:39:04 +01:00
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
bufLen := l.buffer.Len()
|
|
|
|
if bufLen == 0 {
|
|
|
|
return nil, errors.Error("nothing to write to a file")
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2023-03-31 15:39:04 +01:00
|
|
|
|
2018-12-05 11:03:41 +00:00
|
|
|
start := time.Now()
|
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
b = &bytes.Buffer{}
|
|
|
|
e := json.NewEncoder(b)
|
|
|
|
|
|
|
|
l.buffer.Range(func(entry *logEntry) (cont bool) {
|
2021-05-24 15:28:11 +01:00
|
|
|
err = e.Encode(entry)
|
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
// Don't wrap the error since it's informative enough as is.
|
|
|
|
return nil, err
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
size := b.Len()
|
2018-12-05 11:03:41 +00:00
|
|
|
elapsed := time.Since(start)
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"serialized elements via json",
|
|
|
|
"count", bufLen,
|
|
|
|
"elapsed", elapsed,
|
|
|
|
"size", datasize.ByteSize(size),
|
|
|
|
"size_per_entry", datasize.ByteSize(float64(size)/float64(bufLen)),
|
|
|
|
"time_per_entry", elapsed/time.Duration(bufLen),
|
|
|
|
)
|
2018-12-05 11:03:41 +00:00
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
l.buffer.Clear()
|
|
|
|
l.flushPending = false
|
2018-12-05 11:03:41 +00:00
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// flushToFile saves the encoded log entries to the query log file.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) flushToFile(ctx context.Context, b *bytes.Buffer) (err error) {
|
2019-09-27 16:58:57 +01:00
|
|
|
l.fileWriteLock.Lock()
|
|
|
|
defer l.fileWriteLock.Unlock()
|
2023-10-03 18:50:43 +01:00
|
|
|
|
|
|
|
filename := l.logFile
|
|
|
|
|
2024-12-03 15:26:00 +00:00
|
|
|
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, aghos.DefaultPermFile)
|
2018-12-05 11:03:41 +00:00
|
|
|
if err != nil {
|
2023-10-03 18:50:43 +01:00
|
|
|
return fmt.Errorf("creating file %q: %w", filename, err)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2023-10-03 18:50:43 +01:00
|
|
|
|
2021-05-24 15:28:11 +01:00
|
|
|
defer func() { err = errors.WithDeferred(err, f.Close()) }()
|
2018-12-05 11:03:41 +00:00
|
|
|
|
2023-10-03 18:50:43 +01:00
|
|
|
n, err := f.Write(b.Bytes())
|
2018-12-05 11:03:41 +00:00
|
|
|
if err != nil {
|
2023-10-03 18:50:43 +01:00
|
|
|
return fmt.Errorf("writing to file %q: %w", filename, err)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(ctx, "flushed to file", "file", filename, "size", datasize.ByteSize(n))
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) rotate(ctx context.Context) error {
|
2019-02-10 17:47:43 +00:00
|
|
|
from := l.logFile
|
|
|
|
to := l.logFile + ".1"
|
2018-12-05 11:03:41 +00:00
|
|
|
|
|
|
|
err := os.Rename(from, to)
|
|
|
|
if err != nil {
|
2021-01-27 15:32:13 +00:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(ctx, "no log to rotate")
|
2021-10-29 11:43:08 +01:00
|
|
|
|
2021-01-27 15:32:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-29 11:43:08 +01:00
|
|
|
return fmt.Errorf("failed to rename old file: %w", err)
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(ctx, "renamed log file", "from", from, "to", to)
|
2021-01-27 15:32:13 +00:00
|
|
|
|
2018-12-05 11:03:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) readFileFirstTimeValue(ctx context.Context) (first time.Time, err error) {
|
2021-07-01 16:50:28 +01:00
|
|
|
var f *os.File
|
|
|
|
f, err = os.Open(l.logFile)
|
2020-09-02 17:42:26 +01:00
|
|
|
if err != nil {
|
2021-07-01 16:50:28 +01:00
|
|
|
return time.Time{}, err
|
2020-09-02 17:42:26 +01:00
|
|
|
}
|
|
|
|
|
2021-07-01 16:50:28 +01:00
|
|
|
defer func() { err = errors.WithDeferred(err, f.Close()) }()
|
|
|
|
|
|
|
|
buf := make([]byte, 512)
|
|
|
|
var r int
|
|
|
|
r, err = f.Read(buf)
|
2020-09-02 17:42:26 +01:00
|
|
|
if err != nil {
|
2021-07-01 16:50:28 +01:00
|
|
|
return time.Time{}, err
|
2020-09-02 17:42:26 +01:00
|
|
|
}
|
|
|
|
|
2021-07-01 16:50:28 +01:00
|
|
|
val := readJSONValue(string(buf[:r]), `"T":"`)
|
2020-09-02 17:42:26 +01:00
|
|
|
t, err := time.Parse(time.RFC3339Nano, val)
|
|
|
|
if err != nil {
|
2021-07-01 16:50:28 +01:00
|
|
|
return time.Time{}, err
|
2020-09-02 17:42:26 +01:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(ctx, "oldest log entry", "entry_time", val)
|
2021-07-01 16:50:28 +01:00
|
|
|
|
|
|
|
return t, nil
|
2020-09-02 17:42:26 +01:00
|
|
|
}
|
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) periodicRotate(ctx context.Context) {
|
|
|
|
defer slogutil.RecoverAndLog(ctx, l.logger)
|
2021-07-01 16:50:28 +01:00
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
l.checkAndRotate(ctx)
|
2021-11-09 14:17:57 +00:00
|
|
|
|
|
|
|
// rotationCheckIvl is the period of time between checking the need for
|
|
|
|
// rotating log files. It's smaller of any available rotation interval to
|
|
|
|
// increase time accuracy.
|
|
|
|
//
|
|
|
|
// See https://github.com/AdguardTeam/AdGuardHome/issues/3823.
|
|
|
|
const rotationCheckIvl = 1 * time.Hour
|
|
|
|
|
|
|
|
rotations := time.NewTicker(rotationCheckIvl)
|
2021-10-29 11:43:08 +01:00
|
|
|
defer rotations.Stop()
|
|
|
|
|
|
|
|
for range rotations.C {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.checkAndRotate(ctx)
|
2021-11-09 14:17:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-29 11:43:08 +01:00
|
|
|
|
2021-11-09 14:17:57 +00:00
|
|
|
// checkAndRotate rotates log files if those are older than the specified
|
|
|
|
// rotation interval.
|
2024-11-21 17:19:39 +00:00
|
|
|
func (l *queryLog) checkAndRotate(ctx context.Context) {
|
2023-04-03 14:29:07 +01:00
|
|
|
var rotationIvl time.Duration
|
|
|
|
func() {
|
|
|
|
l.confMu.RLock()
|
|
|
|
defer l.confMu.RUnlock()
|
|
|
|
|
|
|
|
rotationIvl = l.conf.RotationIvl
|
|
|
|
}()
|
2023-02-13 15:15:33 +00:00
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
oldest, err := l.readFileFirstTimeValue(ctx)
|
2021-11-09 14:17:57 +00:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.ErrorContext(ctx, "reading oldest record for rotation", slogutil.KeyError, err)
|
2021-07-01 16:50:28 +01:00
|
|
|
|
2021-11-09 14:17:57 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-29 11:43:08 +01:00
|
|
|
|
2023-04-03 14:29:07 +01:00
|
|
|
if rotTime, now := oldest.Add(rotationIvl), time.Now(); rotTime.After(now) {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(
|
|
|
|
ctx,
|
|
|
|
"not rotating",
|
|
|
|
"now", now.Format(time.RFC3339),
|
|
|
|
"rotate_time", rotTime.Format(time.RFC3339),
|
2021-11-09 14:17:57 +00:00
|
|
|
)
|
2021-10-29 11:43:08 +01:00
|
|
|
|
2021-11-09 14:17:57 +00:00
|
|
|
return
|
|
|
|
}
|
2021-10-29 11:43:08 +01:00
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
err = l.rotate(ctx)
|
2021-11-09 14:17:57 +00:00
|
|
|
if err != nil {
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.ErrorContext(ctx, "rotating", slogutil.KeyError, err)
|
2020-09-02 17:42:26 +01:00
|
|
|
|
2021-11-09 14:17:57 +00:00
|
|
|
return
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|
2021-11-09 14:17:57 +00:00
|
|
|
|
2024-11-21 17:19:39 +00:00
|
|
|
l.logger.DebugContext(ctx, "rotated successfully")
|
2018-12-05 11:03:41 +00:00
|
|
|
}
|