Skip to content

Commit

Permalink
* querylog: fix end of log handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ainar-g committed Nov 10, 2020
1 parent 979874c commit 27b13a4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 9 deletions.
8 changes: 8 additions & 0 deletions internal/agherr/agherr.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ import (
"strings"
)

// Error is the constant error type.
type Error string

// Error implements the error interface for Error.
func (err Error) Error() (msg string) {
return string(err)
}

// manyError is an error containing several wrapped errors. It is created to be
// a simpler version of the API provided by github.com/joomcode/errorx.
type manyError struct {
Expand Down
6 changes: 6 additions & 0 deletions internal/querylog/qlog_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ import (
"sync"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/agherr"
"github.com/AdguardTeam/golibs/log"
)

// ErrSeekNotFound is returned from the Seek method
// if we failed to find the desired record
var ErrSeekNotFound = errors.New("Seek not found the record")

// ErrEndOfLog is returned when the end of the current log is reached.
var ErrEndOfLog agherr.Error = "end of log"

// TODO: Find a way to grow buffer instead of relying on this value when reading strings
const maxEntrySize = 16 * 1024

Expand Down Expand Up @@ -106,6 +110,8 @@ func (q *QLogFile) Seek(timestamp int64) (int64, int, error) {
// the scope is too narrow and we won't find anything anymore
log.Error("querylog: didn't find timestamp:%v", timestamp)
return 0, depth, ErrSeekNotFound
} else if lineIdx == end && lineEndIdx == end {
return 0, depth, ErrEndOfLog
}

// Save the last found idx
Expand Down
12 changes: 8 additions & 4 deletions internal/querylog/qlog_reader.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package querylog

import (
"errors"
"io"

"github.com/AdguardTeam/AdGuardHome/internal/agherr"
Expand Down Expand Up @@ -52,11 +53,14 @@ func (r *QLogReader) Seek(timestamp int64) error {
for i := len(r.qFiles) - 1; i >= 0; i-- {
q := r.qFiles[i]
_, _, err := q.Seek(timestamp)
if err == nil {
// Our search is finished, we found the element we were looking for
// Update currentFile only, position is already set properly in the QLogFile
if err == nil || errors.Is(err, ErrEndOfLog) {
// Our search is finished, and we either found the
// element we were looking for or reached the end of the
// log. Update currentFile only, position is already
// set properly in QLogFile.
r.currentFile = i
return nil

return err
}
}

Expand Down
13 changes: 8 additions & 5 deletions internal/querylog/querylog_search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package querylog

import (
"errors"
"io"
"time"

Expand Down Expand Up @@ -45,6 +46,7 @@ func (l *queryLog) search(params *searchParams) ([]*logEntry, time.Time) {
// remove extra records
entries = entries[:totalLimit]
}

if params.offset > 0 {
if len(entries) > params.offset {
entries = entries[params.offset:]
Expand All @@ -53,11 +55,9 @@ func (l *queryLog) search(params *searchParams) ([]*logEntry, time.Time) {
oldest = time.Time{}
}
}
if len(entries) == totalLimit {
// change the "oldest" value here.
// we cannot use the "oldest" we got from "searchFiles" anymore
// because after adding in-memory records and removing extra records
// the situation has changed

if len(entries) > 0 && len(entries) <= totalLimit {
// Update oldest after merging in the memory buffer.
oldest = entries[len(entries)-1].Time
}

Expand Down Expand Up @@ -95,6 +95,9 @@ func (l *queryLog) searchFiles(params *searchParams) ([]*logEntry, time.Time, in
// The one that was specified in the "oldest" param is not needed,
// we need only the one next to it
_, err = r.ReadNext()
} else if errors.Is(err, ErrEndOfLog) {
// We've reached the end of the log.
return entries, time.Time{}, 0
}
}

Expand Down

0 comments on commit 27b13a4

Please sign in to comment.