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

View File

@ -99,7 +99,7 @@ func SocketManager(streams map[string]*Stream) {
case Show: case Show:
response.ClientStatus = genClientStatus(actions, matches, &actionsLock, &matchesLock) response.ClientStatus = genClientStatus(actions, matches, &actionsLock, &matchesLock)
case Flush: 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)} matchesC := FlushMatchOrder{request.Pattern, make(chan MatchesMap)}
actionsC := FlushActionOrder{request.Pattern, make(chan ActionsMap)} actionsC := FlushActionOrder{request.Pattern, make(chan ActionsMap)}
flushToMatchesC <- matchesC flushToMatchesC <- matchesC

View File

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