Reduce database size

...and it's still not a breaking change!
fix #42

Benchmark: using config/heavy-load.yml
From 8.1MB to 4.8MB → 40% of size reduction!

75% of size reduction if we cumulate both optimizations, waow!
This commit is contained in:
ppom 2023-10-21 12:00:00 +02:00
parent b56ccffd3b
commit d5d73f3e6f
4 changed files with 16 additions and 2 deletions

View file

@ -187,7 +187,7 @@ func MatchesManager() {
matchesManagerHandleFlush(fo)
case pft = <-matchesC:
entry := LogEntry{pft.t, pft.p, pft.f.stream.name, pft.f.name, 0, false}
entry := LogEntry{pft.t, 0, pft.p, pft.f.stream.name, pft.f.name, 0, false}
entry.Exec = matchesManagerHandleMatch(pft)

View file

@ -163,6 +163,7 @@ func rotateDB(c *Conf, logDec *gob.Decoder, flushDec *gob.Decoder, logEnc *gob.E
flushes[PSF{entry.Pattern, entry.Stream, entry.Filter}] = entry.T
}
lastTimeCpt := int64(0)
now := time.Now()
for {
var entry LogEntry
@ -207,6 +208,12 @@ func rotateDB(c *Conf, logDec *gob.Decoder, flushDec *gob.Decoder, logEnc *gob.E
continue
}
// restore time
if entry.T.IsZero() {
entry.T = time.Unix(entry.S, lastTimeCpt)
}
lastTimeCpt++
// store matches
if !entry.Exec && entry.T.Add(filter.retryDuration).Unix() > now.Unix() {
if startup {
@ -229,6 +236,7 @@ func rotateDB(c *Conf, logDec *gob.Decoder, flushDec *gob.Decoder, logEnc *gob.E
}
func encodeOrFatal(enc *gob.Encoder, entry LogEntry, writeSF2int map[SF]int, writeCounter *int) {
// Stream/Filter reduction
sf, ok := writeSF2int[SF{entry.Stream, entry.Filter}]
if ok {
entry.SF = sf
@ -239,6 +247,11 @@ func encodeOrFatal(enc *gob.Encoder, entry LogEntry, writeSF2int map[SF]int, wri
writeSF2int[SF{entry.Stream, entry.Filter}] = *writeCounter
*writeCounter++
}
// Time reduction
if !entry.T.IsZero() {
entry.S = entry.T.Unix()
entry.T = time.Time{}
}
err := enc.Encode(entry)
if err != nil {
logger.Fatalln("Failed to write to new DB:", err)

View file

@ -99,7 +99,7 @@ func SocketManager(streams map[string]*Stream) {
case Show:
response.ClientStatus = genClientStatus(actions, matches, &actionsLock, &matchesLock)
case Flush:
le := LogEntry{time.Now(), request.Pattern, "", "", 0, false}
le := LogEntry{time.Now(), 0, request.Pattern, "", "", 0, false}
matchesC := FlushMatchOrder{request.Pattern, make(chan MatchesMap)}
actionsC := FlushActionOrder{request.Pattern, make(chan ActionsMap)}
flushToMatchesC <- matchesC

View file

@ -62,6 +62,7 @@ type Action struct {
type LogEntry struct {
T time.Time
S int64
Pattern string
Stream, Filter string
SF int