all: imp code

This commit is contained in:
Stanislav Chzhen 2023-10-02 17:43:09 +03:00
parent 21e51fcbe4
commit 35a45c7dc5
5 changed files with 96 additions and 59 deletions

View File

@ -29,24 +29,35 @@ func (rb *RingBuffer[T]) Append(e T) {
}
}
// Range calls cb for each element of the buffer.
func (rb *RingBuffer[T]) Range(cb func(T)) {
// Range calls cb for each element of the buffer. If cb returns false it stops.
func (rb *RingBuffer[T]) Range(cb func(T) (cont bool)) {
cur := rb.cur
if !rb.full {
for _, e := range rb.buf[:cur] {
cb(e)
}
rb.rangeNonFull(cb)
return
}
for _, e := range rb.buf[cur:] {
cb(e)
if !cb(e) {
return
}
}
for _, e := range rb.buf[:cur] {
cb(e)
if !cb(e) {
return
}
}
}
// rangeNonFull iterates over non-full buffer.
func (rb *RingBuffer[T]) rangeNonFull(cb func(T) (cont bool)) {
for _, e := range rb.buf[:rb.cur] {
if !cb(e) {
return
}
}
}

View File

@ -11,8 +11,10 @@ func TestRingBuffer(t *testing.T) {
b := aghalg.NewRingBuffer[int](5)
elements := func() (es []int) {
b.Range(func(e int) {
b.Range(func(e int) (cont bool) {
es = append(es, e)
return true
})
return es

View File

@ -195,13 +195,13 @@ func newLogEntry(params *AddParams) (entry *logEntry) {
// Add implements the [QueryLog] interface for *queryLog.
func (l *queryLog) Add(params *AddParams) {
var isEnabled bool
var isEnabled, fileIsEnabled bool
var memSize uint32
func() {
l.confMu.RLock()
defer l.confMu.RUnlock()
isEnabled = l.conf.Enabled
isEnabled, fileIsEnabled = l.conf.Enabled, l.conf.FileEnabled
memSize = l.conf.MemSize
}()
@ -222,22 +222,19 @@ func (l *queryLog) Add(params *AddParams) {
entry := newLogEntry(params)
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
l.buffer.Append(entry)
needFlush := false
func() {
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
l.buffer.Append(entry)
if !l.flushPending {
needFlush = l.buffer.Len() >= int(memSize)
if needFlush {
l.flushPending = true
}
}
}()
if !l.flushPending && fileIsEnabled {
needFlush = l.buffer.Len() >= int(memSize)
}
if needFlush {
l.flushPending = true
go func() {
flushErr := l.flushLogBuffer()
if flushErr != nil {

View File

@ -17,64 +17,84 @@ func (l *queryLog) flushLogBuffer() (err error) {
l.fileFlushLock.Lock()
defer l.fileFlushLock.Unlock()
err = l.flushToFile()
defer func() {
err = errors.Annotate(err, "flushing log buffer: %w")
}()
return errors.Annotate(err, "writing to file: %w")
b, err := l.encodeEntries()
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
err = l.flushToFile(b)
if err != nil {
// Don't wrap the error since it's informative enough as is.
return err
}
return nil
}
// flushToFile saves the specified log entries to the query log file
func (l *queryLog) flushToFile() (err error) {
if l.buffer.Len() == 0 {
log.Debug("querylog: nothing to write to a file")
// encodeEntries returns JSON encoded log entries, logs estimated time, clears
// the log buffer.
func (l *queryLog) encodeEntries() (b *bytes.Buffer, err error) {
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
return nil
bufLen := l.buffer.Len()
if bufLen == 0 {
return nil, errors.Error("nothing to write to a file")
}
start := time.Now()
var b bytes.Buffer
e := json.NewEncoder(&b)
b = new(bytes.Buffer)
e := json.NewEncoder(b)
func() {
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
l.buffer.Range(func(entry *logEntry) (cont bool) {
if err = e.Encode(entry); err != nil {
return false
}
l.buffer.Range(func(entry *logEntry) {
err = e.Encode(entry)
})
l.flushPending = false
}()
return true
})
if err != nil {
log.Error("Failed to marshal entry: %s", err)
return err
// Don't wrap the error since it's informative enough as is.
return nil, err
}
defer l.buffer.Clear()
elapsed := time.Since(start)
bufLen := l.buffer.Len()
log.Debug("%d elements serialized via json in %v: %d kB, %v/entry, %v/entry", bufLen, elapsed, b.Len()/1024, float64(b.Len())/float64(bufLen), elapsed/time.Duration(bufLen))
var zb bytes.Buffer
filename := l.logFile
zb = b
l.buffer.Clear()
l.flushPending = false
return b, nil
}
// flushToFile saves the encoded log entries to the query log file.
func (l *queryLog) flushToFile(b *bytes.Buffer) (err error) {
l.fileWriteLock.Lock()
defer l.fileWriteLock.Unlock()
defer func() {
err = errors.Annotate(err, "writing to file: %w")
}()
filename := l.logFile
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o644)
if err != nil {
log.Error("failed to create file \"%s\": %s", filename, err)
return err
return fmt.Errorf("failed to create file \"%s\": %s", filename, err)
}
defer func() { err = errors.WithDeferred(err, f.Close()) }()
n, err := f.Write(zb.Bytes())
n, err := f.Write(b.Bytes())
if err != nil {
log.Error("Couldn't write to file: %s", err)
return err
return fmt.Errorf("couldn't write to file: %s", err)
}
log.Debug("querylog: ok \"%s\": %v bytes written", filename, n)

View File

@ -51,14 +51,12 @@ func (l *queryLog) searchMemory(params *searchParams, cache clientCache) (entrie
l.bufferLock.Lock()
defer l.bufferLock.Unlock()
// Go through the buffer in the reverse order, from newer to older.
var err error
l.buffer.Range(func(entry *logEntry) {
l.buffer.Range(func(entry *logEntry) (cont bool) {
// A shallow clone is enough, since the only thing that this loop
// modifies is the client field.
e := entry.shallowClone()
var err error
e.client, err = l.client(e.ClientID, e.IP.String(), cache)
if err != nil {
msg := "querylog: enriching memory record at time %s" +
@ -71,8 +69,17 @@ func (l *queryLog) searchMemory(params *searchParams, cache clientCache) (entrie
if params.match(e) {
entries = append(entries, e)
}
return true
})
// Reverse order of the entries, from newer to older.
//
// TODO(s.chzhen): Use slices.Reverse in Go 1.21.
for i, j := 0, len(entries)-1; i < j; i, j = i+1, j-1 {
entries[i], entries[j] = entries[j], entries[i]
}
return entries, l.buffer.Len()
}