2019-08-26 11:54:38 +03:00
package querylog
2018-12-05 14:03:41 +03:00
import (
2019-10-22 15:51:48 +03:00
"bufio"
2018-12-05 14:03:41 +03:00
"bytes"
"compress/gzip"
"encoding/json"
2019-09-16 17:07:18 +03:00
"io"
2018-12-05 14:03:41 +03:00
"os"
2019-10-22 15:51:48 +03:00
"strings"
2018-12-05 14:03:41 +03:00
"time"
2019-02-25 16:44:22 +03:00
"github.com/AdguardTeam/golibs/log"
2018-12-05 14:03:41 +03:00
)
const enableGzip = false
2019-09-16 17:07:18 +03:00
const maxEntrySize = 1000
2018-12-05 14:03:41 +03:00
2019-02-11 14:22:36 +03:00
// flushLogBuffer flushes the current buffer to file and resets the current buffer
2019-05-15 13:11:36 +03:00
func ( l * queryLog ) flushLogBuffer ( fullFlush bool ) error {
l . fileFlushLock . Lock ( )
defer l . fileFlushLock . Unlock ( )
2019-02-10 20:47:43 +03:00
// flush remainder to file
2019-09-27 18:58:57 +03:00
l . bufferLock . Lock ( )
needFlush := len ( l . buffer ) >= logBufferCap
2019-05-15 13:11:36 +03:00
if ! needFlush && ! fullFlush {
2019-09-27 18:58:57 +03:00
l . bufferLock . Unlock ( )
2019-05-15 13:11:36 +03:00
return nil
}
2019-09-27 18:58:57 +03:00
flushBuffer := l . buffer
l . buffer = nil
2019-05-15 13:11:36 +03:00
l . flushPending = false
2019-09-27 18:58:57 +03:00
l . bufferLock . Unlock ( )
2019-02-10 20:47:43 +03:00
err := l . flushToFile ( flushBuffer )
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Saving querylog to file failed: %s" , err )
2019-02-10 20:47:43 +03:00
return err
}
return nil
}
// flushToFile saves the specified log entries to the query log file
func ( l * queryLog ) flushToFile ( buffer [ ] * logEntry ) error {
2018-12-05 14:03:41 +03:00
if len ( buffer ) == 0 {
2019-05-08 10:43:47 +03:00
log . Debug ( "querylog: there's nothing to write to a file" )
2018-12-05 14:03:41 +03:00
return nil
}
start := time . Now ( )
var b bytes . Buffer
e := json . NewEncoder ( & b )
for _ , entry := range buffer {
err := e . Encode ( entry )
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Failed to marshal entry: %s" , err )
2018-12-05 14:03:41 +03:00
return err
}
}
elapsed := time . Since ( start )
2019-02-25 16:44:22 +03:00
log . Debug ( "%d elements serialized via json in %v: %d kB, %v/entry, %v/entry" , len ( buffer ) , elapsed , b . Len ( ) / 1024 , float64 ( b . Len ( ) ) / float64 ( len ( buffer ) ) , elapsed / time . Duration ( len ( buffer ) ) )
2018-12-05 14:03:41 +03:00
2019-10-24 14:19:13 +03:00
var err error
2018-12-05 14:03:41 +03:00
var zb bytes . Buffer
2019-02-10 20:47:43 +03:00
filename := l . logFile
2018-12-05 14:03:41 +03:00
// gzip enabled?
if enableGzip {
filename += ".gz"
zw := gzip . NewWriter ( & zb )
2019-02-10 20:47:43 +03:00
zw . Name = l . logFile
2018-12-05 14:03:41 +03:00
zw . ModTime = time . Now ( )
_ , err = zw . Write ( b . Bytes ( ) )
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Couldn't compress to gzip: %s" , err )
2018-12-05 14:03:41 +03:00
zw . Close ( )
return err
}
if err = zw . Close ( ) ; err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Couldn't close gzip writer: %s" , err )
2018-12-05 14:03:41 +03:00
return err
}
} else {
zb = b
}
2019-09-27 18:58:57 +03:00
l . fileWriteLock . Lock ( )
defer l . fileWriteLock . Unlock ( )
2018-12-05 14:03:41 +03:00
f , err := os . OpenFile ( filename , os . O_WRONLY | os . O_CREATE | os . O_APPEND , 0644 )
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "failed to create file \"%s\": %s" , filename , err )
2018-12-05 14:03:41 +03:00
return err
}
defer f . Close ( )
n , err := f . Write ( zb . Bytes ( ) )
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Couldn't write to file: %s" , err )
2018-12-05 14:03:41 +03:00
return err
}
2019-02-25 16:44:22 +03:00
log . Debug ( "ok \"%s\": %v bytes written" , filename , n )
2018-12-05 14:03:41 +03:00
return nil
}
2019-09-27 18:58:57 +03:00
func ( l * queryLog ) rotate ( ) error {
2019-02-10 20:47:43 +03:00
from := l . logFile
to := l . logFile + ".1"
2018-12-05 14:03:41 +03:00
if enableGzip {
2019-02-10 20:47:43 +03:00
from = l . logFile + ".gz"
to = l . logFile + ".gz.1"
2018-12-05 14:03:41 +03:00
}
if _ , err := os . Stat ( from ) ; os . IsNotExist ( err ) {
// do nothing, file doesn't exist
return nil
}
err := os . Rename ( from , to )
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Failed to rename querylog: %s" , err )
2018-12-05 14:03:41 +03:00
return err
}
2019-02-25 16:44:22 +03:00
log . Debug ( "Rotated from %s to %s successfully" , from , to )
2018-12-05 14:03:41 +03:00
return nil
}
2019-09-27 18:58:57 +03:00
func ( l * queryLog ) periodicRotate ( ) {
for range time . Tick ( time . Duration ( l . conf . Interval ) * 24 * time . Hour ) {
err := l . rotate ( )
2018-12-05 14:03:41 +03:00
if err != nil {
2019-02-25 16:44:22 +03:00
log . Error ( "Failed to rotate querylog: %s" , err )
2018-12-05 14:03:41 +03:00
// do nothing, continue rotating
}
}
}
2019-08-26 11:54:38 +03:00
// Reader is the DB reader context
type Reader struct {
2019-09-16 17:07:18 +03:00
ql * queryLog
f * os . File
2019-10-22 15:51:48 +03:00
reader * bufio . Reader // reads file line by line
2019-09-16 17:07:18 +03:00
now time . Time
validFrom int64 // UNIX time (ns)
olderThan int64 // UNIX time (ns)
2019-08-26 11:54:38 +03:00
files [ ] string
ifile int
2019-09-16 17:07:18 +03:00
limit uint64
count uint64 // counter for returned elements
latest bool // return the latest entries
filePrepared bool
searching bool // we're seaching for an entry with exact time stamp
fseeker fileSeeker // file seeker object
fpos uint64 // current file offset
nSeekRequests uint32 // number of Seek() requests made (finding a new line doesn't count)
2019-08-26 11:54:38 +03:00
}
2019-09-16 17:07:18 +03:00
type fileSeeker struct {
target uint64 // target value
pos uint64 // current offset, may be adjusted by user for increased accuracy
lastpos uint64 // the last offset returned
lo uint64 // low boundary offset
hi uint64 // high boundary offset
}
// OpenReader - return reader object
2019-08-26 11:54:38 +03:00
func ( l * queryLog ) OpenReader ( ) * Reader {
r := Reader { }
r . ql = l
r . now = time . Now ( )
2019-09-27 18:58:57 +03:00
r . validFrom = r . now . Unix ( ) - int64 ( l . conf . Interval * 24 * 60 * 60 )
2019-09-16 17:07:18 +03:00
r . validFrom *= 1000000000
r . files = [ ] string {
r . ql . logFile ,
r . ql . logFile + ".1" ,
}
2019-08-26 11:54:38 +03:00
return & r
}
2019-09-16 17:07:18 +03:00
// Close - close the reader
2019-08-26 11:54:38 +03:00
func ( r * Reader ) Close ( ) {
elapsed := time . Since ( r . now )
var perunit time . Duration
if r . count > 0 {
perunit = elapsed / time . Duration ( r . count )
}
2019-09-16 17:07:18 +03:00
log . Debug ( "querylog: read %d entries in %v, %v/entry, seek-reqs:%d" ,
r . count , elapsed , perunit , r . nSeekRequests )
2019-08-26 11:54:38 +03:00
if r . f != nil {
r . f . Close ( )
}
}
2019-09-16 17:07:18 +03:00
// BeginRead - start reading
// olderThan: stop returning entries when an entry with this time is reached
// count: minimum number of entries to return
func ( r * Reader ) BeginRead ( olderThan time . Time , count uint64 ) {
r . olderThan = olderThan . UnixNano ( )
r . latest = olderThan . IsZero ( )
r . limit = count
if r . latest {
r . olderThan = r . now . UnixNano ( )
}
r . filePrepared = false
r . searching = false
}
// BeginReadPrev - start reading the previous data chunk
func ( r * Reader ) BeginReadPrev ( olderThan time . Time , count uint64 ) {
r . olderThan = olderThan . UnixNano ( )
r . latest = olderThan . IsZero ( )
r . limit = count
if r . latest {
r . olderThan = r . now . UnixNano ( )
}
off := r . fpos - maxEntrySize * ( r . limit + 1 )
if int64 ( off ) < maxEntrySize {
off = 0
}
2019-10-02 15:41:14 +03:00
r . fpos = off
2019-09-16 17:07:18 +03:00
log . Debug ( "QueryLog: seek: %x" , off )
_ , err := r . f . Seek ( int64 ( off ) , io . SeekStart )
if err != nil {
log . Error ( "file.Seek: %s: %s" , r . files [ r . ifile ] , err )
return
}
r . nSeekRequests ++
r . seekToNewLine ( )
r . fseeker . pos = r . fpos
r . filePrepared = true
r . searching = false
}
// Perform binary seek
// Return 0: success; 1: seek reqiured; -1: error
func ( fs * fileSeeker ) seekBinary ( cur uint64 ) int32 {
log . Debug ( "QueryLog: seek: tgt=%x cur=%x, %x: [%x..%x]" , fs . target , cur , fs . pos , fs . lo , fs . hi )
off := uint64 ( 0 )
if fs . pos >= fs . lo && fs . pos < fs . hi {
if cur == fs . target {
return 0
} else if cur < fs . target {
fs . lo = fs . pos + 1
} else {
fs . hi = fs . pos
}
off = fs . lo + ( fs . hi - fs . lo ) / 2
} else {
// we didn't find another entry from the last file offset: now return the boundary beginning
off = fs . lo
}
if off == fs . lastpos {
return - 1
}
fs . lastpos = off
fs . pos = off
return 1
}
// Seek to a new line
func ( r * Reader ) seekToNewLine ( ) bool {
2019-10-22 15:51:48 +03:00
r . reader = bufio . NewReader ( r . f )
b , err := r . reader . ReadBytes ( '\n' )
2019-09-16 17:07:18 +03:00
if err != nil {
2019-10-22 15:51:48 +03:00
r . reader = nil
2019-09-16 17:07:18 +03:00
log . Error ( "QueryLog: file.Read: %s: %s" , r . files [ r . ifile ] , err )
return false
}
2019-10-22 15:51:48 +03:00
off := len ( b )
2019-09-16 17:07:18 +03:00
r . fpos += uint64 ( off )
log . Debug ( "QueryLog: seek: %x (+%d)" , r . fpos , off )
return true
}
// Open a file
func ( r * Reader ) openFile ( ) bool {
var err error
fn := r . files [ r . ifile ]
r . f , err = os . Open ( fn )
if err != nil {
if ! os . IsNotExist ( err ) {
log . Error ( "QueryLog: Failed to open file \"%s\": %s" , fn , err )
}
return false
}
return true
}
// Seek to the needed position
func ( r * Reader ) prepareRead ( ) bool {
fn := r . files [ r . ifile ]
fi , err := r . f . Stat ( )
if err != nil {
log . Error ( "QueryLog: file.Stat: %s: %s" , fn , err )
return false
}
fsize := uint64 ( fi . Size ( ) )
off := uint64 ( 0 )
if r . latest {
// read data from the end of file
off = fsize - maxEntrySize * ( r . limit + 1 )
if int64 ( off ) < maxEntrySize {
off = 0
}
2019-10-02 15:41:14 +03:00
r . fpos = off
2019-09-16 17:07:18 +03:00
log . Debug ( "QueryLog: seek: %x" , off )
_ , err = r . f . Seek ( int64 ( off ) , io . SeekStart )
if err != nil {
log . Error ( "QueryLog: file.Seek: %s: %s" , fn , err )
return false
}
} else {
// start searching in file: we'll read the first chunk of data from the middle of file
r . searching = true
r . fseeker = fileSeeker { }
r . fseeker . target = uint64 ( r . olderThan )
r . fseeker . hi = fsize
rc := r . fseeker . seekBinary ( 0 )
r . fpos = r . fseeker . pos
if rc == 1 {
_ , err = r . f . Seek ( int64 ( r . fpos ) , io . SeekStart )
if err != nil {
log . Error ( "QueryLog: file.Seek: %s: %s" , fn , err )
return false
}
}
2019-08-26 11:54:38 +03:00
}
2019-09-16 17:07:18 +03:00
r . nSeekRequests ++
if ! r . seekToNewLine ( ) {
return false
}
r . fseeker . pos = r . fpos
return true
2019-08-26 11:54:38 +03:00
}
2019-09-16 17:07:18 +03:00
// Next - return the next entry or nil if reading is finished
2019-08-26 11:54:38 +03:00
func ( r * Reader ) Next ( ) * logEntry { // nolint
for {
// open file if needed
if r . f == nil {
if r . ifile == len ( r . files ) {
return nil
}
2019-09-16 17:07:18 +03:00
if ! r . openFile ( ) {
2019-08-26 11:54:38 +03:00
r . ifile ++
continue
}
}
2019-09-16 17:07:18 +03:00
if ! r . filePrepared {
if ! r . prepareRead ( ) {
return nil
}
r . filePrepared = true
}
2019-10-22 15:51:48 +03:00
// open decoder
b , err := r . reader . ReadBytes ( '\n' )
if err != nil {
2019-09-16 17:07:18 +03:00
return nil
2019-08-26 11:54:38 +03:00
}
2019-10-22 15:51:48 +03:00
strReader := strings . NewReader ( string ( b ) )
jd := json . NewDecoder ( strReader )
2019-08-26 11:54:38 +03:00
// read data
var entry logEntry
2019-10-22 15:51:48 +03:00
err = jd . Decode ( & entry )
2019-08-26 11:54:38 +03:00
if err != nil {
2019-10-22 15:51:48 +03:00
log . Debug ( "QueryLog: Failed to decode: %s" , err )
continue
2019-08-26 11:54:38 +03:00
}
2019-09-16 17:07:18 +03:00
t := entry . Time . UnixNano ( )
if r . searching {
2019-08-26 11:54:38 +03:00
2019-10-22 15:51:48 +03:00
r . reader = nil
2019-09-16 17:07:18 +03:00
rr := r . fseeker . seekBinary ( uint64 ( t ) )
r . fpos = r . fseeker . pos
if rr < 0 {
log . Error ( "QueryLog: File seek error: can't find the target entry: %s" , r . files [ r . ifile ] )
return nil
} else if rr == 0 {
// We found the target entry.
// We'll start reading the previous chunk of data.
r . searching = false
off := r . fpos - ( maxEntrySize * ( r . limit + 1 ) )
if int64 ( off ) < maxEntrySize {
off = 0
}
r . fpos = off
}
2019-08-26 11:54:38 +03:00
2019-09-16 17:07:18 +03:00
_ , err = r . f . Seek ( int64 ( r . fpos ) , io . SeekStart )
if err != nil {
log . Error ( "QueryLog: file.Seek: %s: %s" , r . files [ r . ifile ] , err )
return nil
}
r . nSeekRequests ++
2019-08-26 11:54:38 +03:00
2019-09-16 17:07:18 +03:00
if ! r . seekToNewLine ( ) {
return nil
}
r . fseeker . pos = r . fpos
2019-08-26 11:54:38 +03:00
continue
}
2019-09-16 17:07:18 +03:00
if t < r . validFrom {
2019-08-26 11:54:38 +03:00
continue
}
2019-09-16 17:07:18 +03:00
if t >= r . olderThan {
return nil
2019-08-26 11:54:38 +03:00
}
2019-09-16 17:07:18 +03:00
r . count ++
return & entry
2019-08-26 11:54:38 +03:00
}
2019-09-16 17:07:18 +03:00
}
2019-08-26 11:54:38 +03:00
2019-09-16 17:07:18 +03:00
// Total returns the total number of items
func ( r * Reader ) Total ( ) int {
return 0
2019-08-26 11:54:38 +03:00
}