Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/confighandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type InfluxConfig struct {
Token string
User string
Password string
Buffer time.Duration
}

// AdapterConfig describes device communication parameters
Expand Down
8 changes: 7 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,11 @@ any type is considered valid.
"",
"InfluxDB password (optional)",
)
runCmd.PersistentFlags().Duration(
"influx-buffer",
0,
"InfluxDB write buffer duration. Readings are kept in memory and retried for this long while the database is unavailable, e.g. 1h. 0 keeps the client default of 5 retries within 3 minutes, which holds about 10 seconds of data.",
)

pflags := runCmd.PersistentFlags()

Expand All @@ -166,7 +171,7 @@ any type is considered valid.
bindPFlagsWithPrefix(pflags, "mqtt", "broker", "topic", "user", "password", "clientid", "qos", "retain")

// influx
bindPFlagsWithPrefix(pflags, "influx", "url", "database", "measurement", "organization", "token", "user", "password")
bindPFlagsWithPrefix(pflags, "influx", "url", "database", "measurement", "organization", "token", "user", "password", "buffer")
}

// checkVersion validates if updates are available
Expand Down Expand Up @@ -329,6 +334,7 @@ func run(cmd *cobra.Command, args []string) {
viper.GetString("influx.token"),
viper.GetString("influx.user"),
viper.GetString("influx.password"),
viper.GetDuration("influx.buffer"),
)

tee.AttachRunner(server.NewSnipRunner(influx.Run))
Expand Down
1 change: 1 addition & 0 deletions docs/mbmd_run.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ mbmd run [flags]
If the adapter is a TCP connection (identified by :port), the device type (SUNS) is ignored and
any type is considered valid.
Example: -d SDM:1@/dev/USB11 -d SMA:126@localhost:502
--influx-buffer duration InfluxDB write buffer duration. Readings are kept in memory and retried for this long while the database is unavailable, e.g. 1h. 0 keeps the client default of 5 retries within 3 minutes, which holds about 10 seconds of data.
--influx-database string InfluxDB database
--influx-measurement string InfluxDB measurement (default "data")
--influx-organization string InfluxDB organization
Expand Down
5 changes: 5 additions & 0 deletions mbmd.dist.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ influx:
measurement: mbmd
user:
password:
# how long readings are kept in memory and retried while the database is unavailable (e.g. 1h),
# 0 keeps the client default of 5 retries within 3 minutes, which holds about 10 seconds of data
buffer: 0

# influxdb_v2 config
influx:
Expand All @@ -26,6 +29,8 @@ influx:
measurement: mbmd
organization:
token:
# see influxdb_v1 config
buffer: 0

# adapters are referenced by device
adapters:
Expand Down
16 changes: 15 additions & 1 deletion server/influx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"log"
"math"
"time"

influxdb "github.com/influxdata/influxdb-client-go/v2"
Expand All @@ -25,13 +26,26 @@ func NewInfluxClient(
token string,
user string,
password string,
buffer time.Duration,
) *Influx {
// InfluxDB v1 compatibility
if token == "" && user != "" {
token = fmt.Sprintf("%s:%s", user, password)
}

client := influxdb.NewClient(url, token)
options := influxdb.DefaultOptions()

// retry indefinitely while the database is unavailable, keeping the batches
// of the last `buffer` duration. The client counts its retry queue in batches
// (RetryBufferLimit/BatchSize) and every flush interval produces one batch.
if buffer > 0 {
batches := uint(buffer / (time.Duration(options.FlushInterval()) * time.Millisecond))
options.SetRetryBufferLimit(max(batches, 1) * options.BatchSize()).
SetMaxRetries(math.MaxUint32).
SetMaxRetryTime(math.MaxUint32)
}

client := influxdb.NewClientWithOptions(url, token, options)

if database == "" {
log.Fatal("influx: missing database")
Expand Down
152 changes: 152 additions & 0 deletions server/influx_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package server

import (
"io"
"math"
"net/http"
"net/http/httptest"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/volkszaehler/mbmd/meters"
)

func TestInfluxBufferOptions(t *testing.T) {
tc := []struct {
buffer time.Duration
batches uint
maxRetries uint
retryTime uint
}{
// client defaults: 50,000 / 5,000 = 10 batches, 5 retries within 3 minutes
{0, 10, 5, 180_000},
// one batch per 1s flush interval, retried indefinitely
{time.Hour, 3600, math.MaxUint32, math.MaxUint32},
{10 * time.Minute, 600, math.MaxUint32, math.MaxUint32},
// shorter than one flush interval still keeps one batch
{500 * time.Millisecond, 1, math.MaxUint32, math.MaxUint32},
}

for _, tc := range tc {
t.Run(tc.buffer.String(), func(t *testing.T) {
influx := NewInfluxClient("http://localhost:8086", "db", "mbmd", "org", "", "", "", tc.buffer)
defer influx.client.Close()

opts := influx.client.Options().WriteOptions()

if got := opts.RetryBufferLimit() / opts.BatchSize(); got != tc.batches {
t.Errorf("retry buffer holds %d batches, want %d", got, tc.batches)
}
if got := opts.MaxRetries(); got != tc.maxRetries {
t.Errorf("max retries %d, want %d", got, tc.maxRetries)
}
if got := opts.MaxRetryTime(); got != tc.retryTime {
t.Errorf("max retry time %d, want %d", got, tc.retryTime)
}
})
}
}

// fakeInflux counts written lines and fails all writes while down is set
type fakeInflux struct {
down atomic.Bool
mu sync.Mutex
lines int
}

func (f *fakeInflux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if f.down.Load() {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
body, _ := io.ReadAll(r.Body)
f.mu.Lock()
f.lines += len(strings.Split(strings.TrimSpace(string(body)), "\n"))
f.mu.Unlock()
w.WriteHeader(http.StatusNoContent)
}

func (f *fakeInflux) count() int {
f.mu.Lock()
defer f.mu.Unlock()
return f.lines
}

// simulateOutage writes points during a 20s database outage, which is longer
// than the ~10s the client's default retry buffer covers. It keeps writing
// after the database is back until all points arrived or wait has passed,
// and returns the number of points sent and received.
func simulateOutage(t *testing.T, buffer, wait time.Duration) (sent, received int) {
fake := &fakeInflux{}
srv := httptest.NewServer(fake)
defer srv.Close()

influx := NewInfluxClient(srv.URL, "db", "mbmd", "org", "", "", "", buffer)

in := make(chan QuerySnip)
done := make(chan struct{})
go func() {
influx.Run(in)
close(done)
}()

send := func(value float64) {
in <- QuerySnip{
Device: "test",
MeasurementResult: meters.MeasurementResult{Measurement: meters.Power, Value: value},
}
sent++
}

fake.down.Store(true)

const points = 100
for i := range points {
send(float64(i))
time.Sleep(200 * time.Millisecond)
}

fake.down.Store(false)

// keep data flowing, the client only retries when it writes new batches
deadline := time.Now().Add(wait)
for fake.count() < sent && time.Now().Before(deadline) {
send(-1)
time.Sleep(time.Second)
}

close(in)
<-done

return sent, fake.count()
}

// TestInfluxOutage verifies that points written while the database is down
// are delivered once it is back.
func TestInfluxOutage(t *testing.T) {
if testing.Short() {
t.Skip("slow")
}
t.Parallel()

// the wait covers the client's exponential backoff of up to 125s between retries
if sent, received := simulateOutage(t, time.Hour, 3*time.Minute); received != sent {
t.Errorf("received %d of %d points", received, sent)
}
}

// TestInfluxOutageDefault verifies that without a buffer the client keeps its
// default behaviour and drops data during a longer outage.
func TestInfluxOutageDefault(t *testing.T) {
if testing.Short() {
t.Skip("slow")
}
t.Parallel()

if sent, received := simulateOutage(t, 0, time.Minute); received >= sent {
t.Errorf("received %d of %d points, expected data loss without buffer", received, sent)
}
}
Loading