2023-09-06 02:00:33 +02:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"time"
|
2023-10-12 12:00:00 +02:00
|
|
|
|
|
|
|
"framagit.org/ppom/reaction/logger"
|
2023-09-06 02:00:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-09-09 19:32:23 +02:00
|
|
|
logDBName = "./reaction-matches.db"
|
|
|
|
logDBNewName = "./reaction-matches.new.db"
|
|
|
|
flushDBName = "./reaction-flushes.db"
|
2023-09-06 02:00:33 +02:00
|
|
|
)
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
func openDB(path string) (bool, *ReadDB) {
|
2023-09-06 02:00:33 +02:00
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
2023-09-09 19:32:23 +02:00
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.WARN, "No DB found at %s. It's ok if this is the first time reaction is running.\n", path)
|
2023-09-09 19:32:23 +02:00
|
|
|
return true, nil
|
|
|
|
}
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to open DB:", err)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
2023-09-09 19:32:23 +02:00
|
|
|
return false, &ReadDB{file, gob.NewDecoder(file)}
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
func createDB(path string) *WriteDB {
|
2023-09-06 02:00:33 +02:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to create DB:", err)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
2023-09-09 19:32:23 +02:00
|
|
|
return &WriteDB{file, gob.NewEncoder(file)}
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 20:42:47 +02:00
|
|
|
func DatabaseManager(c *Conf) {
|
2023-09-09 19:32:23 +02:00
|
|
|
logDB, flushDB := c.RotateDB(true)
|
2023-09-22 23:18:08 +02:00
|
|
|
close(startupMatchesC)
|
2023-09-09 19:32:23 +02:00
|
|
|
c.manageLogs(logDB, flushDB)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
func (c *Conf) manageLogs(logDB *WriteDB, flushDB *WriteDB) {
|
2023-10-20 12:00:00 +02:00
|
|
|
cpt := 0
|
|
|
|
writeSF2int := make(map[SF]int)
|
|
|
|
writeCpt := 1
|
2023-09-06 02:00:33 +02:00
|
|
|
for {
|
2023-09-09 19:32:23 +02:00
|
|
|
select {
|
2023-09-25 20:42:42 +02:00
|
|
|
case entry := <-flushToDatabaseC:
|
2023-09-09 19:32:23 +02:00
|
|
|
flushDB.enc.Encode(entry)
|
2023-09-09 23:38:53 +02:00
|
|
|
case entry := <-logsC:
|
2023-10-20 12:00:00 +02:00
|
|
|
encodeOrFatal(logDB.enc, entry, writeSF2int, &writeCpt)
|
2023-09-09 19:32:23 +02:00
|
|
|
cpt++
|
|
|
|
// let's say 100 000 entries ~ 10 MB
|
2023-10-20 12:00:00 +02:00
|
|
|
if cpt == 500_000 {
|
2023-09-09 19:32:23 +02:00
|
|
|
cpt = 0
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.INFO, "Rotating database...")
|
2023-09-09 19:32:23 +02:00
|
|
|
logDB.file.Close()
|
|
|
|
flushDB.file.Close()
|
|
|
|
logDB, flushDB = c.RotateDB(false)
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.INFO, "Rotated database")
|
2023-09-09 19:32:23 +02:00
|
|
|
}
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
func (c *Conf) RotateDB(startup bool) (*WriteDB, *WriteDB) {
|
2023-09-06 02:00:33 +02:00
|
|
|
var (
|
2023-09-09 19:32:23 +02:00
|
|
|
doesntExist bool
|
|
|
|
err error
|
|
|
|
logReadDB *ReadDB
|
|
|
|
flushReadDB *ReadDB
|
|
|
|
logWriteDB *WriteDB
|
|
|
|
flushWriteDB *WriteDB
|
2023-09-06 02:00:33 +02:00
|
|
|
)
|
2023-09-09 19:32:23 +02:00
|
|
|
doesntExist, logReadDB = openDB(logDBName)
|
|
|
|
if doesntExist {
|
|
|
|
return createDB(logDBName), createDB(flushDBName)
|
|
|
|
}
|
|
|
|
doesntExist, flushReadDB = openDB(flushDBName)
|
|
|
|
if doesntExist {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Println(logger.WARN, "Strange! No flushes db, opening /dev/null instead")
|
2023-09-09 19:32:23 +02:00
|
|
|
doesntExist, flushReadDB = openDB("/dev/null")
|
|
|
|
if doesntExist {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Opening dummy /dev/null failed")
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
logWriteDB = createDB(logDBNewName)
|
2023-09-06 02:00:33 +02:00
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
rotateDB(c, logReadDB.dec, flushReadDB.dec, logWriteDB.enc, startup)
|
2023-09-06 02:00:33 +02:00
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
err = logReadDB.file.Close()
|
2023-09-06 02:00:33 +02:00
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to close old DB:", err)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// It should be ok to rename an open file
|
2023-09-09 19:32:23 +02:00
|
|
|
err = os.Rename(logDBNewName, logDBName)
|
2023-09-06 02:00:33 +02:00
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to replace old DB with new one:", err)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
err = os.Remove(flushDBName)
|
2023-09-24 16:00:10 +02:00
|
|
|
if err != nil && !errors.Is(err, os.ErrNotExist) {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to delete old DB:", err)
|
2023-09-09 19:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
flushWriteDB = createDB(flushDBName)
|
|
|
|
return logWriteDB, flushWriteDB
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
func rotateDB(c *Conf, logDec *gob.Decoder, flushDec *gob.Decoder, logEnc *gob.Encoder, startup bool) {
|
2023-10-20 12:00:00 +02:00
|
|
|
// This mapping is a space optimization feature
|
|
|
|
// It permits to compress stream+filter to a small number (which is a byte in gob)
|
|
|
|
// We do this only for matches, not for flushes
|
|
|
|
readSF2int := make(map[int]SF)
|
|
|
|
writeSF2int := make(map[SF]int)
|
|
|
|
writeCounter := 1
|
2023-09-06 02:00:33 +02:00
|
|
|
// This extra code is made to warn only one time for each non-existant filter
|
|
|
|
discardedEntries := make(map[SF]int)
|
|
|
|
malformedEntries := 0
|
|
|
|
defer func() {
|
|
|
|
for sf, t := range discardedEntries {
|
|
|
|
if t > 0 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.WARN, "info discarded %v times from the DBs: stream/filter not found: %s.%s\n", t, sf.s, sf.f)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if malformedEntries > 0 {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Printf(logger.WARN, "%v malformed entries discarded from the DBs\n", malformedEntries)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-09-09 19:32:23 +02:00
|
|
|
// pattern, stream, fitler → last flush
|
2024-02-24 11:42:31 +01:00
|
|
|
flushes := make(map[*PSF]time.Time)
|
2023-09-09 19:32:23 +02:00
|
|
|
for {
|
2023-10-20 12:00:00 +02:00
|
|
|
var entry LogEntry
|
|
|
|
var filter *Filter
|
2023-09-09 19:32:23 +02:00
|
|
|
// decode entry
|
2023-10-20 12:00:00 +02:00
|
|
|
err := flushDec.Decode(&entry)
|
2023-09-06 02:00:33 +02:00
|
|
|
if err != nil {
|
2023-09-09 19:32:23 +02:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
malformedEntries++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve related filter
|
|
|
|
if entry.Stream != "" || entry.Filter != "" {
|
|
|
|
if stream := c.Streams[entry.Stream]; stream != nil {
|
|
|
|
filter = stream.Filters[entry.Filter]
|
|
|
|
}
|
|
|
|
if filter == nil {
|
|
|
|
discardedEntries[SF{entry.Stream, entry.Filter}]++
|
|
|
|
continue
|
|
|
|
}
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
2023-09-09 19:32:23 +02:00
|
|
|
|
|
|
|
// store
|
2024-02-24 11:42:31 +01:00
|
|
|
flushes[&PSF{entry.Pattern, entry.Stream, entry.Filter}] = entry.T
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 12:00:00 +02:00
|
|
|
lastTimeCpt := int64(0)
|
2023-09-06 02:00:33 +02:00
|
|
|
now := time.Now()
|
|
|
|
for {
|
2023-10-20 12:00:00 +02:00
|
|
|
var entry LogEntry
|
|
|
|
var filter *Filter
|
2023-09-06 02:00:33 +02:00
|
|
|
|
|
|
|
// decode entry
|
2023-10-20 12:00:00 +02:00
|
|
|
err := logDec.Decode(&entry)
|
2023-09-06 02:00:33 +02:00
|
|
|
if err != nil {
|
|
|
|
if err == io.EOF {
|
2023-09-09 19:32:23 +02:00
|
|
|
break
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
malformedEntries++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-10-20 12:00:00 +02:00
|
|
|
// retrieve related stream & filter
|
|
|
|
if entry.Stream == "" && entry.Filter == "" {
|
2023-10-20 12:00:00 +02:00
|
|
|
sf, ok := readSF2int[entry.SF]
|
|
|
|
if !ok {
|
2023-10-20 12:00:00 +02:00
|
|
|
discardedEntries[SF{"", ""}]++
|
|
|
|
continue
|
|
|
|
}
|
2023-10-20 12:00:00 +02:00
|
|
|
entry.Stream = sf.s
|
|
|
|
entry.Filter = sf.f
|
|
|
|
}
|
|
|
|
if stream := c.Streams[entry.Stream]; stream != nil {
|
|
|
|
filter = stream.Filters[entry.Filter]
|
|
|
|
}
|
|
|
|
if filter == nil {
|
|
|
|
discardedEntries[SF{entry.Stream, entry.Filter}]++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if entry.SF != 0 {
|
|
|
|
readSF2int[entry.SF] = SF{entry.Stream, entry.Filter}
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-01 12:00:00 +02:00
|
|
|
// check if it hasn't been flushed
|
2024-02-24 11:42:31 +01:00
|
|
|
lastGlobalFlush := flushes[&PSF{entry.Pattern, "", ""}].Unix()
|
|
|
|
lastLocalFlush := flushes[&PSF{entry.Pattern, entry.Stream, entry.Filter}].Unix()
|
2023-10-01 12:00:00 +02:00
|
|
|
entryTime := entry.T.Unix()
|
|
|
|
if lastLocalFlush > entryTime || lastGlobalFlush > entryTime {
|
|
|
|
continue
|
2023-09-09 19:32:23 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 12:00:00 +02:00
|
|
|
// restore time
|
|
|
|
if entry.T.IsZero() {
|
|
|
|
entry.T = time.Unix(entry.S, lastTimeCpt)
|
|
|
|
}
|
|
|
|
lastTimeCpt++
|
|
|
|
|
2023-09-06 02:00:33 +02:00
|
|
|
// store matches
|
|
|
|
if !entry.Exec && entry.T.Add(filter.retryDuration).Unix() > now.Unix() {
|
|
|
|
if startup {
|
2023-09-09 23:38:53 +02:00
|
|
|
startupMatchesC <- PFT{entry.Pattern, filter, entry.T}
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 12:00:00 +02:00
|
|
|
encodeOrFatal(logEnc, entry, writeSF2int, &writeCounter)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// replay executions
|
|
|
|
if entry.Exec && entry.T.Add(*filter.longuestActionDuration).Unix() > now.Unix() {
|
|
|
|
if startup {
|
2023-10-01 12:00:00 +02:00
|
|
|
flushToMatchesC <- FlushMatchOrder{entry.Pattern, nil}
|
2023-09-22 18:09:31 +02:00
|
|
|
filter.sendActions(entry.Pattern, entry.T)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
|
2023-10-20 12:00:00 +02:00
|
|
|
encodeOrFatal(logEnc, entry, writeSF2int, &writeCounter)
|
2023-09-06 02:00:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-09 19:32:23 +02:00
|
|
|
|
2023-10-20 12:00:00 +02:00
|
|
|
func encodeOrFatal(enc *gob.Encoder, entry LogEntry, writeSF2int map[SF]int, writeCounter *int) {
|
2023-10-21 12:00:00 +02:00
|
|
|
// Stream/Filter reduction
|
2023-10-20 12:00:00 +02:00
|
|
|
sf, ok := writeSF2int[SF{entry.Stream, entry.Filter}]
|
|
|
|
if ok {
|
|
|
|
entry.SF = sf
|
|
|
|
entry.Stream = ""
|
|
|
|
entry.Filter = ""
|
|
|
|
} else {
|
|
|
|
entry.SF = *writeCounter
|
|
|
|
writeSF2int[SF{entry.Stream, entry.Filter}] = *writeCounter
|
|
|
|
*writeCounter++
|
|
|
|
}
|
2023-10-21 12:00:00 +02:00
|
|
|
// Time reduction
|
|
|
|
if !entry.T.IsZero() {
|
|
|
|
entry.S = entry.T.Unix()
|
|
|
|
entry.T = time.Time{}
|
|
|
|
}
|
2023-09-09 19:32:23 +02:00
|
|
|
err := enc.Encode(entry)
|
|
|
|
if err != nil {
|
2023-10-12 12:00:00 +02:00
|
|
|
logger.Fatalln("Failed to write to new DB:", err)
|
2023-09-09 19:32:23 +02:00
|
|
|
}
|
|
|
|
}
|