From d08a1e3ef6c61965405fc3596dbffbba841c70ae Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Tue, 24 May 2022 14:26:40 -0700 Subject: [PATCH] ignore retention policy errors during migration. - fixes #256 --- .../scrutiny_repository_migrations.go | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/webapp/backend/pkg/database/scrutiny_repository_migrations.go b/webapp/backend/pkg/database/scrutiny_repository_migrations.go index d459ee8..0ac6154 100644 --- a/webapp/backend/pkg/database/scrutiny_repository_migrations.go +++ b/webapp/backend/pkg/database/scrutiny_repository_migrations.go @@ -2,6 +2,7 @@ package database import ( "context" + "errors" "fmt" "github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20201107210306" "github.com/analogj/scrutiny/webapp/backend/pkg/database/migrations/m20220503120000" @@ -9,7 +10,9 @@ import ( "github.com/analogj/scrutiny/webapp/backend/pkg/models/collector" "github.com/analogj/scrutiny/webapp/backend/pkg/models/measurements" "github.com/go-gormigrate/gormigrate/v2" + "github.com/influxdata/influxdb-client-go/v2/api/http" _ "github.com/jinzhu/gorm/dialects/sqlite" + log "github.com/sirupsen/logrus" "gorm.io/gorm" "strconv" "time" @@ -135,7 +138,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { smartTags, smartFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } @@ -145,7 +148,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { tempTags, tempFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } } @@ -164,7 +167,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { smartFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } @@ -174,7 +177,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { tempTags, tempFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } } @@ -192,7 +195,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { smartTags, smartFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } @@ -202,7 +205,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { tempTags, tempFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } } @@ -219,7 +222,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { smartTags, smartFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } @@ -229,7 +232,7 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { tempTags, tempFields, postSmartResults.Date, ctx) - if err != nil { + if ignorePastRetentionPolicyError(err) != nil { return err } } @@ -269,6 +272,21 @@ func (sr *scrutinyRepository) Migrate(ctx context.Context) error { return nil } +// helpers + +//When adding data to influxdb, an error may be returned if the data point is outside the range of the retention policy. +//This function will ignore retention policy errors, and allow the migration to continue. +func ignorePastRetentionPolicyError(err error) error { + var influxDbWriteError *http.Error + if errors.As(err, &influxDbWriteError) { + if influxDbWriteError.StatusCode == 422 { + log.Infoln("ignoring error: attempted to writePoint past retention period duration") + return nil + } + } + return err +} + // Deprecated func m20201107210306_FromPreInfluxDBTempCreatePostInfluxDBTemp(preDevice m20201107210306.Device, preSmartResult m20201107210306.Smart) (error, measurements.SmartTemperature) { //extract temperature data for every datapoint