2021-06-09 06:03:31 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-09-08 09:08:51 +02:00
|
|
|
"context"
|
2021-06-09 06:03:31 +02:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-08-20 11:45:34 +02:00
|
|
|
"os/signal"
|
2021-08-20 12:54:11 +02:00
|
|
|
"sync"
|
2021-08-20 11:45:34 +02:00
|
|
|
"syscall"
|
2021-08-31 15:17:36 +02:00
|
|
|
"time"
|
2021-10-07 14:59:07 +02:00
|
|
|
|
|
|
|
"github.com/influxdata/line-protocol/v2/lineprotocol"
|
2021-06-09 06:03:31 +02:00
|
|
|
)
|
|
|
|
|
2021-08-31 15:17:36 +02:00
|
|
|
type MetricConfig struct {
|
|
|
|
Frequency int64 `json:"frequency"`
|
|
|
|
Aggregation string `json:"aggregation"`
|
|
|
|
Scope string `json:"scope"`
|
2021-06-09 06:03:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:45:34 +02:00
|
|
|
type Config struct {
|
2021-09-13 12:28:33 +02:00
|
|
|
Metrics map[string]MetricConfig `json:"metrics"`
|
|
|
|
RetentionInMemory int `json:"retention-in-memory"`
|
|
|
|
Nats string `json:"nats"`
|
2021-09-20 09:27:31 +02:00
|
|
|
JwtPublicKey string `json:"jwt-public-key"`
|
2021-09-13 12:28:33 +02:00
|
|
|
Checkpoints struct {
|
|
|
|
Interval int `json:"interval"`
|
|
|
|
RootDir string `json:"directory"`
|
|
|
|
Restore int `json:"restore"`
|
|
|
|
} `json:"checkpoints"`
|
|
|
|
Archive struct {
|
|
|
|
Interval int `json:"interval"`
|
|
|
|
RootDir string `json:"directory"`
|
|
|
|
} `json:"archive"`
|
2021-06-09 06:03:31 +02:00
|
|
|
}
|
|
|
|
|
2021-08-31 15:17:36 +02:00
|
|
|
var conf Config
|
|
|
|
var memoryStore *MemoryStore = nil
|
2021-09-07 09:28:41 +02:00
|
|
|
var lastCheckpoint time.Time
|
2021-06-09 06:03:31 +02:00
|
|
|
|
|
|
|
func loadConfiguration(file string) Config {
|
|
|
|
var config Config
|
|
|
|
configFile, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
2021-09-20 09:27:31 +02:00
|
|
|
defer configFile.Close()
|
2021-06-09 06:03:31 +02:00
|
|
|
jsonParser := json.NewDecoder(configFile)
|
|
|
|
jsonParser.Decode(&config)
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2021-10-07 14:59:07 +02:00
|
|
|
func handleLine(dec *lineprotocol.Decoder) {
|
|
|
|
for dec.Next() {
|
|
|
|
measurement, err := dec.Measurement()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-08-20 11:45:34 +02:00
|
|
|
|
2021-10-07 14:59:07 +02:00
|
|
|
var cluster, host, typeName, typeId string
|
|
|
|
for {
|
|
|
|
key, val, err := dec.NextTag()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if key == nil {
|
|
|
|
break
|
|
|
|
}
|
2021-08-20 11:45:34 +02:00
|
|
|
|
2021-10-07 14:59:07 +02:00
|
|
|
switch string(key) {
|
|
|
|
case "cluster":
|
|
|
|
cluster = string(val)
|
|
|
|
case "host":
|
|
|
|
host = string(val)
|
|
|
|
case "type":
|
|
|
|
typeName = string(val)
|
|
|
|
case "type-id":
|
|
|
|
typeId = string(val)
|
|
|
|
default:
|
|
|
|
log.Fatalf("Unkown tag: '%s' (value: '%s')", string(key), string(val))
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 11:45:34 +02:00
|
|
|
|
2021-10-07 14:59:07 +02:00
|
|
|
selector := make([]string, 0, 3)
|
|
|
|
selector = append(selector, cluster)
|
|
|
|
selector = append(selector, host)
|
|
|
|
if len(typeId) > 0 {
|
|
|
|
selector = append(selector, typeName+typeId)
|
|
|
|
}
|
|
|
|
|
|
|
|
var value Float
|
|
|
|
for {
|
|
|
|
key, val, err := dec.NextField()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if key == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(key) != "value" {
|
|
|
|
log.Fatalf("Unkown field: '%s' (value: %#v)", string(key), val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val.Kind() == lineprotocol.Float {
|
|
|
|
value = Float(val.FloatV())
|
|
|
|
} else if val.Kind() == lineprotocol.Int {
|
|
|
|
value = Float(val.IntV())
|
|
|
|
} else {
|
|
|
|
log.Fatalf("Unsupported value type in message: %s", val.Kind().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t, err := dec.Time(lineprotocol.Second, time.Now())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := memoryStore.Write(selector, t.Unix(), []Metric{
|
|
|
|
{Name: string(measurement), Value: value},
|
|
|
|
}); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-08-24 10:40:26 +02:00
|
|
|
}
|
2021-08-20 11:45:34 +02:00
|
|
|
}
|
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
func intervals(wg *sync.WaitGroup, ctx context.Context) {
|
2021-09-13 13:40:39 +02:00
|
|
|
wg.Add(3)
|
2021-09-13 12:28:33 +02:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
d := time.Duration(conf.RetentionInMemory) * time.Second
|
2021-09-13 13:40:39 +02:00
|
|
|
if d <= 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-09-13 12:28:33 +02:00
|
|
|
ticks := time.Tick(d / 2)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticks:
|
|
|
|
log.Println("Freeing up memory...")
|
|
|
|
t := time.Now().Add(-d)
|
|
|
|
freed, err := memoryStore.Free(Selector{}, t.Unix())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Freeing up memory failed: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
log.Printf("%d buffers freed\n", freed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
lastCheckpoint = time.Now()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
d := time.Duration(conf.Checkpoints.Interval) * time.Second
|
2021-09-13 13:40:39 +02:00
|
|
|
if d <= 0 {
|
|
|
|
return
|
|
|
|
}
|
2021-09-13 12:28:33 +02:00
|
|
|
ticks := time.Tick(d)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticks:
|
2021-09-13 13:40:39 +02:00
|
|
|
log.Println("Checkpoint creation started...")
|
2021-09-13 12:28:33 +02:00
|
|
|
now := time.Now()
|
|
|
|
n, err := memoryStore.ToCheckpoint(conf.Checkpoints.RootDir,
|
|
|
|
lastCheckpoint.Unix(), now.Unix())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Checkpoint creation failed: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
log.Printf("Checkpoint finished (%d files)\n", n)
|
|
|
|
lastCheckpoint = now
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-09-13 13:40:39 +02:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
d := time.Duration(conf.Archive.Interval) * time.Second
|
|
|
|
if d <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ticks := time.Tick(d)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-ticks:
|
|
|
|
log.Println("Start zipping and deleting old checkpoints...")
|
|
|
|
t := time.Now().Add(-d)
|
|
|
|
err := ArchiveCheckpoints(conf.Checkpoints.RootDir, conf.Archive.RootDir, t.Unix())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Archiving failed: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
log.Println("Archiving checkpoints completed!")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-09-13 12:28:33 +02:00
|
|
|
}
|
|
|
|
|
2021-08-20 11:45:34 +02:00
|
|
|
func main() {
|
2021-08-31 15:17:36 +02:00
|
|
|
startupTime := time.Now()
|
2021-08-20 11:45:34 +02:00
|
|
|
conf = loadConfiguration("config.json")
|
2021-08-31 15:17:36 +02:00
|
|
|
memoryStore = NewMemoryStore(conf.Metrics)
|
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
restoreFrom := startupTime.Add(-time.Duration(conf.Checkpoints.Restore))
|
|
|
|
files, err := memoryStore.FromCheckpoint(conf.Checkpoints.RootDir, restoreFrom.Unix())
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Loading checkpoints failed: %s\n", err.Error())
|
|
|
|
} else {
|
|
|
|
log.Printf("Checkpoints loaded (%d files)\n", files)
|
2021-06-09 06:03:31 +02:00
|
|
|
}
|
|
|
|
|
2021-09-08 09:08:51 +02:00
|
|
|
ctx, shutdown := context.WithCancel(context.Background())
|
|
|
|
|
2021-08-31 15:17:36 +02:00
|
|
|
var wg sync.WaitGroup
|
2021-08-20 11:45:34 +02:00
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
go func() {
|
2021-09-20 09:27:31 +02:00
|
|
|
<-sigs
|
2021-08-31 15:17:36 +02:00
|
|
|
log.Println("Shuting down...")
|
2021-09-08 09:08:51 +02:00
|
|
|
shutdown()
|
2021-08-20 12:54:11 +02:00
|
|
|
}()
|
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
intervals(&wg, ctx)
|
2021-09-07 09:28:41 +02:00
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
wg.Add(2)
|
2021-08-20 12:54:11 +02:00
|
|
|
|
|
|
|
go func() {
|
2021-09-08 09:08:51 +02:00
|
|
|
err := StartApiServer(":8080", ctx)
|
2021-08-31 15:17:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2021-08-20 12:54:11 +02:00
|
|
|
wg.Done()
|
2021-08-20 11:45:34 +02:00
|
|
|
}()
|
|
|
|
|
2021-08-31 15:17:36 +02:00
|
|
|
go func() {
|
2021-09-13 12:28:33 +02:00
|
|
|
// err := ReceiveNats(conf.Nats, handleLine, runtime.NumCPU()-1, ctx)
|
|
|
|
err := ReceiveNats(conf.Nats, handleLine, 1, ctx)
|
|
|
|
|
2021-08-31 15:17:36 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
}()
|
2021-08-20 12:54:11 +02:00
|
|
|
|
|
|
|
wg.Wait()
|
2021-08-31 15:17:36 +02:00
|
|
|
|
2021-09-13 12:28:33 +02:00
|
|
|
log.Printf("Writing to '%s'...\n", conf.Checkpoints.RootDir)
|
|
|
|
files, err = memoryStore.ToCheckpoint(conf.Checkpoints.RootDir, lastCheckpoint.Unix(), time.Now().Unix())
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Writing checkpoint failed: %s\n", err.Error())
|
2021-08-31 15:17:36 +02:00
|
|
|
}
|
2021-09-13 12:28:33 +02:00
|
|
|
log.Printf("Done! (%d files written)\n", files)
|
2021-06-09 06:03:31 +02:00
|
|
|
}
|