querylog file -- disable gzip compression

This commit is contained in:
Eugene Bujak 2018-10-09 05:02:16 +03:00
parent 37f6d38c49
commit ca794aed63
1 changed files with 59 additions and 32 deletions

View File

@ -17,6 +17,8 @@ var (
fileWriteLock sync.Mutex
)
const enableGzip = false
func flushToFile(buffer []logEntry) error {
if len(buffer) == 0 {
return nil
@ -42,9 +44,12 @@ func flushToFile(buffer []logEntry) error {
return err
}
filenamegz := queryLogFileName + ".gz"
var zb bytes.Buffer
filename := queryLogFileName
// gzip enabled?
if enableGzip {
filename += ".gz"
zw := gzip.NewWriter(&zb)
zw.Name = queryLogFileName
@ -61,12 +66,15 @@ func flushToFile(buffer []logEntry) error {
log.Printf("Couldn't close gzip writer: %s", err)
return err
}
} else {
zb = b
}
fileWriteLock.Lock()
defer fileWriteLock.Unlock()
f, err := os.OpenFile(filenamegz, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Printf("failed to create file \"%s\": %s", filenamegz, err)
log.Printf("failed to create file \"%s\": %s", filename, err)
return err
}
defer f.Close()
@ -77,7 +85,7 @@ func flushToFile(buffer []logEntry) error {
return err
}
log.Printf("ok \"%s\": %v bytes written", filenamegz, n)
log.Printf("ok \"%s\": %v bytes written", filename, n)
return nil
}
@ -111,8 +119,13 @@ func checkBuffer(buffer []logEntry, b bytes.Buffer) error {
}
func rotateQueryLog() error {
from := queryLogFileName + ".gz"
to := queryLogFileName + ".gz.1"
from := queryLogFileName
to := queryLogFileName + ".1"
if enableGzip {
from = queryLogFileName + ".gz"
to = queryLogFileName + ".gz.1"
}
if _, err := os.Stat(from); os.IsNotExist(err) {
// do nothing, file doesn't exist
@ -143,10 +156,19 @@ func periodicQueryLogRotate() {
func genericLoader(onEntry func(entry *logEntry) error, needMore func() bool, timeWindow time.Duration) error {
now := time.Now()
// read from querylog files, try newest file first
files := []string{
files := []string{}
if enableGzip {
files = []string{
queryLogFileName + ".gz",
queryLogFileName + ".gz.1",
}
} else {
files = []string{
queryLogFileName,
queryLogFileName + ".1",
}
}
// read from all files
for _, file := range files {
@ -158,7 +180,6 @@ func genericLoader(onEntry func(entry *logEntry) error, needMore func() bool, ti
continue
}
trace("Opening file %s", file)
f, err := os.Open(file)
if err != nil {
log.Printf("Failed to open file \"%s\": %s", file, err)
@ -167,6 +188,9 @@ func genericLoader(onEntry func(entry *logEntry) error, needMore func() bool, ti
}
defer f.Close()
var d *json.Decoder
if enableGzip {
trace("Creating gzip reader")
zr, err := gzip.NewReader(f)
if err != nil {
@ -176,7 +200,10 @@ func genericLoader(onEntry func(entry *logEntry) error, needMore func() bool, ti
defer zr.Close()
trace("Creating json decoder")
d := json.NewDecoder(zr)
d = json.NewDecoder(zr)
} else {
d = json.NewDecoder(f)
}
i := 0
// entries on file are in oldest->newest order