Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd/influx): allow setting shard-group durations for buckets via API and CLI #20911

Merged
merged 20 commits into from
Mar 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
a09639d
fix(cmd/influx): make shard duration configurable
Jan 23, 2021
a323bac
fix(cmd/influx): make shard duration configurable from command-line c…
Jan 24, 2021
76cf16b
fix(cmd/influx): make shard duration configurable from command-line c…
Jan 24, 2021
4ab0f73
fix(cmd/influx): added API documentation for shardGroupDuration field
Jan 25, 2021
0aa1927
fix(cmd/influx): shortened shard group duration column title in comma…
Jan 27, 2021
c10caf0
chore: update CHANGELOG
Jan 27, 2021
8ea0af4
fix(influx/cmd): reformatted bucket.go
Jan 27, 2021
cf59685
feat: refactor API based on review suggestions
danxmoran Mar 9, 2021
c17feba
test: fix existing test for updating RP
danxmoran Mar 9, 2021
5b753c8
fix: record ShardGroupDuration in bucket metadata, add backfill migra…
danxmoran Mar 9, 2021
d410020
fix: fix listing buckets with infinite retention
danxmoran Mar 9, 2021
db5da5b
chore: update CHANGELOG
danxmoran Mar 9, 2021
3c85d9d
fix: update input validation to fix tests
danxmoran Mar 10, 2021
93eca2c
test: add more tests for updating shard-group duration.
danxmoran Mar 10, 2021
74ba621
fix: add validation of shard-group duration values
danxmoran Mar 11, 2021
f989f89
test: add more test cases to bucket_service suite
danxmoran Mar 11, 2021
a23c7cb
test: add e2e case for updating both durations at once
danxmoran Mar 11, 2021
d65d99c
test: move HTTP-only validation tests out of common suite
danxmoran Mar 11, 2021
69c1df7
fix: use consistent terminology
danxmoran Mar 11, 2021
eff0ad5
test: add test for shard-group duration migration
danxmoran Mar 11, 2021
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ or `/query` HTTP endpoints.
1. [20827](https://github.com/influxdata/influxdb/pull/20827): Add `--pprof-disabled` option to `influxd` to disable exposing profiling information over HTTP.
1. [20827](https://github.com/influxdata/influxdb/pull/20827): Add `/debug/pprof/all` HTTP endpoint to gather all profiles at once.
1. [20827](https://github.com/influxdata/influxdb/pull/20827): Upgrade `http.pprof-enabled` config in `influxd upgrade`.
1. [20911](https://github.com/influxdata/influxdb/pull/20911): Add support for explicitly setting shard-group durations on buckets. Thanks @hinst!

### Bug Fixes

Expand Down
8 changes: 5 additions & 3 deletions bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Bucket struct {
Description string `json:"description"`
RetentionPolicyName string `json:"rp,omitempty"` // This to support v1 sources
RetentionPeriod time.Duration `json:"retentionPeriod"`
ShardGroupDuration time.Duration `json:"shardGroupDuration"`
CRUDLog
}

Expand Down Expand Up @@ -102,9 +103,10 @@ type BucketService interface {
// BucketUpdate represents updates to a bucket.
// Only fields which are set are updated.
type BucketUpdate struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
RetentionPeriod *time.Duration `json:"retentionPeriod,omitempty"`
Name *string
Description *string
RetentionPeriod *time.Duration
ShardGroupDuration *time.Duration
}

// BucketFilter represents a set of filter that restrict the returned results.
Expand Down
77 changes: 54 additions & 23 deletions cmd/influx/bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ type cmdBucketBuilder struct {

svcFn bucketSVCsFn

id string
hideHeaders bool
json bool
name string
description string
org organization
retention string
id string
hideHeaders bool
json bool
name string
description string
org organization
retention string
shardGroupDuration string
}

func newCmdBucketBuilder(svcsFn bucketSVCsFn, f *globalFlags, opts genericCLIOpts) *cmdBucketBuilder {
Expand Down Expand Up @@ -73,6 +74,8 @@ func (b *cmdBucketBuilder) cmdCreate() *cobra.Command {

cmd.Flags().StringVarP(&b.description, "description", "d", "", "Description of bucket that will be created")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&b.shardGroupDuration, "shard-group-duration", "", "",
"Shard group duration used internally by the storage engine. Not supported by InfluxDB Cloud.")
b.org.register(b.viper, cmd, false)
b.registerPrintFlags(cmd)

Expand All @@ -94,10 +97,16 @@ func (b *cmdBucketBuilder) cmdCreateRunEFn(*cobra.Command, []string) error {
return err
}

shardGroupDuration, err := internal.RawDurationToTimeDuration(b.shardGroupDuration)
if err != nil {
return err
}

bkt := &influxdb.Bucket{
Name: b.name,
Description: b.description,
RetentionPeriod: dur,
Name: b.name,
Description: b.description,
RetentionPeriod: dur,
ShardGroupDuration: shardGroupDuration,
}
bkt.OrgID, err = b.org.getID(orgSVC)
if err != nil {
Expand Down Expand Up @@ -241,16 +250,18 @@ func (b *cmdBucketBuilder) cmdUpdate() *cobra.Command {
Flag: "name",
Short: 'n',
EnvVar: "BUCKET_NAME",
Desc: "New bucket name",
Desc: "New name to set on the bucket",
},
}
opts.mustRegister(b.viper, cmd)

b.registerPrintFlags(cmd)
cmd.Flags().StringVarP(&b.id, "id", "i", "", "The bucket ID (required)")
cmd.Flags().StringVarP(&b.description, "description", "d", "", "Description of bucket that will be created")
cmd.Flags().StringVarP(&b.description, "description", "d", "", "New description to set on the bucket")
cmd.MarkFlagRequired("id")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "Duration bucket will retain data. 0 is infinite. Default is 0.")
cmd.Flags().StringVarP(&b.retention, "retention", "r", "", "New retention duration to set on the bucket. 0 is infinite.")
cmd.Flags().StringVarP(&b.shardGroupDuration, "shard-group-duration", "", "",
"New shard group duration to set on the bucket. 0 will tell the server to pick a value. Not supported by InfluxDB Cloud.")

return cmd
}
Expand All @@ -274,14 +285,22 @@ func (b *cmdBucketBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) er
update.Description = &b.description
}

dur, err := internal.RawDurationToTimeDuration(b.retention)
if err != nil {
return err
}
if dur != 0 {
if b.retention != "" {
dur, err := internal.RawDurationToTimeDuration(b.retention)
if err != nil {
return err
}
update.RetentionPeriod = &dur
}

if b.shardGroupDuration != "" {
sgDur, err := internal.RawDurationToTimeDuration(b.shardGroupDuration)
if err != nil {
return err
}
update.ShardGroupDuration = &sgDur
}

bkt, err := bktSVC.UpdateBucket(context.Background(), id, update)
if err != nil {
return fmt.Errorf("failed to update bucket: %v", err)
Expand Down Expand Up @@ -320,7 +339,7 @@ func (b *cmdBucketBuilder) printBuckets(printOpt bucketPrintOpt) error {

w.HideHeaders(b.hideHeaders)

headers := []string{"ID", "Name", "Retention", "Organization ID"}
headers := []string{"ID", "Name", "Retention", "Shard group duration", "Organization ID"}
if printOpt.deleted {
headers = append(headers, "Deleted")
}
Expand All @@ -331,11 +350,23 @@ func (b *cmdBucketBuilder) printBuckets(printOpt bucketPrintOpt) error {
}

for _, bkt := range printOpt.buckets {
rp := bkt.RetentionPeriod.String()
if bkt.RetentionPeriod == influxdb.InfiniteRetention {
rp = "infinite"
}
sgDur := bkt.ShardGroupDuration.String()
// ShardGroupDuration will be zero if listing buckets from InfluxDB Cloud.
// Show something more useful here in that case.
if bkt.ShardGroupDuration == 0 {
sgDur = "n/a"
}

m := map[string]interface{}{
"ID": bkt.ID.String(),
"Name": bkt.Name,
"Retention": bkt.RetentionPeriod,
"Organization ID": bkt.OrgID.String(),
"ID": bkt.ID.String(),
"Name": bkt.Name,
"Retention": rp,
"Shard group duration": sgDur,
"Organization ID": bkt.OrgID.String(),
}
if printOpt.deleted {
m["Deleted"] = true
Expand Down
25 changes: 25 additions & 0 deletions cmd/influx/bucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ func TestCmdBucket(t *testing.T) {
OrgID: orgID,
},
},
{
name: "with explicit shard-group duration",
flags: []string{
"-r=1h",
"--shard-group-duration=1m",
"-o=org name",
"-n=new name",
},
expectedBucket: influxdb.Bucket{
Name: "new name",
RetentionPeriod: time.Hour,
ShardGroupDuration: time.Minute,
OrgID: orgID,
},
},
}

cmdFn := func(expectedBkt influxdb.Bucket) func(*globalFlags, genericCLIOpts) *cobra.Command {
Expand Down Expand Up @@ -408,6 +423,16 @@ func TestCmdBucket(t *testing.T) {
RetentionPeriod: durPtr(time.Minute),
},
},
{
name: "shard-group duration",
flags: []string{
"-i=" + influxdb.ID(3).String(),
"--shard-group-duration=1m",
},
expected: influxdb.BucketUpdate{
ShardGroupDuration: durPtr(time.Minute),
},
},
}

cmdFn := func(expectedUpdate influxdb.BucketUpdate) func(*globalFlags, genericCLIOpts) *cobra.Command {
Expand Down
4 changes: 2 additions & 2 deletions cmd/influxd/launcher/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func (t *TemporaryEngine) CreateBucket(ctx context.Context, b *influxdb.Bucket)
return t.engine.CreateBucket(ctx, b)
}

func (t *TemporaryEngine) UpdateBucketRetentionPeriod(ctx context.Context, bucketID influxdb.ID, d time.Duration) error {
return t.engine.UpdateBucketRetentionPeriod(ctx, bucketID, d)
func (t *TemporaryEngine) UpdateBucketRetentionPolicy(ctx context.Context, bucketID influxdb.ID, upd *influxdb.BucketUpdate) error {
return t.engine.UpdateBucketRetentionPolicy(ctx, bucketID, upd)
}

// DeleteBucket deletes a bucket from the time-series data.
Expand Down
135 changes: 125 additions & 10 deletions cmd/influxd/launcher/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"testing"
"time"

"github.com/dustin/go-humanize"
"github.com/google/go-cmp/cmp"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/cmd/influxd/launcher"
"github.com/influxdata/influxdb/v2/http"
"github.com/influxdata/influxdb/v2/pkg/testing/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -200,15 +200,130 @@ func TestLauncher_DeleteWithPredicate(t *testing.T) {
}

func TestLauncher_UpdateRetentionPolicy(t *testing.T) {
l := launcher.RunAndSetupNewLauncherOrFail(ctx, t)
defer l.ShutdownOrFail(t, ctx)
durPtr := func(d time.Duration) *time.Duration {
return &d
}

bucket, err := l.BucketService(t).FindBucket(ctx, influxdb.BucketFilter{ID: &l.Bucket.ID})
require.NoError(t, err)
require.NotNil(t, bucket)
testCases := []struct {
name string
initRp time.Duration
initSgd time.Duration
derivedSgd *time.Duration
newRp *time.Duration
newSgd *time.Duration
expectInitErr bool
expectUpdateErr bool
}{
{
name: "infinite to 1w",
derivedSgd: durPtr(humanize.Week),
newRp: durPtr(humanize.Week),
},
{
name: "1w to 1d",
initRp: humanize.Week,
derivedSgd: durPtr(humanize.Day),
newRp: durPtr(humanize.Day),
},
{
name: "1d to 1h",
initRp: humanize.Day,
derivedSgd: durPtr(time.Hour),
newRp: durPtr(time.Hour),
},
{
name: "infinite, update shard duration",
initSgd: humanize.Month,
derivedSgd: durPtr(humanize.Month),
newSgd: durPtr(humanize.Week),
},
{
name: "1w, update shard duration",
initRp: humanize.Week,
initSgd: humanize.Week,
newSgd: durPtr(time.Hour),
},
{
name: "1d, update shard duration",
initRp: humanize.Day,
initSgd: 3 * time.Hour,
newSgd: durPtr(1*time.Hour + 30*time.Minute),
},
{
name: "infinite, update both retention and shard duration",
derivedSgd: durPtr(humanize.Week),
newRp: durPtr(time.Hour),
newSgd: durPtr(time.Hour),
},
{
name: "init shard duration larger than RP",
initRp: time.Hour,
initSgd: humanize.Day,
expectInitErr: true,
},
{
name: "updated shard duration larger than RP",
initRp: humanize.Day,
initSgd: time.Hour,
newSgd: durPtr(humanize.Week),
expectUpdateErr: true,
},
}

newRetentionPeriod := 1 * time.Hour
bucket, err = l.BucketService(t).UpdateBucket(ctx, bucket.ID, influxdb.BucketUpdate{RetentionPeriod: &newRetentionPeriod})
require.NoError(t, err)
assert.Equal(t, bucket.RetentionPeriod, newRetentionPeriod)
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

l := launcher.RunAndSetupNewLauncherOrFail(ctx, t)
defer l.ShutdownOrFail(t, ctx)
bucketService := l.BucketService(t)

bucket := &influxdb.Bucket{
OrgID: l.Org.ID,
RetentionPeriod: tc.initRp,
ShardGroupDuration: tc.initSgd,
}
err := bucketService.CreateBucket(ctx, bucket)
if tc.expectInitErr {
require.Error(t, err)
return
}
require.NoError(t, err)
defer bucketService.DeleteBucket(ctx, bucket.ID)

bucket, err = bucketService.FindBucketByID(ctx, bucket.ID)
require.NoError(t, err)

expectedSgd := tc.initSgd
if tc.derivedSgd != nil {
expectedSgd = *tc.derivedSgd
}
require.Equal(t, tc.initRp, bucket.RetentionPeriod)
require.Equal(t, expectedSgd, bucket.ShardGroupDuration)

bucket, err = bucketService.UpdateBucket(ctx, bucket.ID, influxdb.BucketUpdate{
RetentionPeriod: tc.newRp,
ShardGroupDuration: tc.newSgd,
})
if tc.expectUpdateErr {
require.Error(t, err)
return
}
require.NoError(t, err)

bucket, err = bucketService.FindBucketByID(ctx, bucket.ID)
require.NoError(t, err)

expectedRp := tc.initRp
if tc.newRp != nil {
expectedRp = *tc.newRp
}
if tc.newSgd != nil {
expectedSgd = *tc.newSgd
}
require.Equal(t, expectedRp, bucket.RetentionPeriod)
require.Equal(t, expectedSgd, bucket.ShardGroupDuration)
})
}
}
Loading