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

fix(onboarding): deprecate misleading retentionPeriodHrs key #20798

Merged
merged 5 commits into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
1. [20754](https://github.com/influxdata/influxdb/pull/20754): Update references to docs site to use current URLs.
1. [20773](https://github.com/influxdata/influxdb/pull/20773): Fix data race in TSM engine when inspecting tombstone stats.
1. [20797](https://github.com/influxdata/influxdb/pull/20797): Fix data race in TSM cache. Thanks @StoneYunZhao!
1. [20798](https://github.com/influxdata/influxdb/pull/20798): Deprecate misleading `retentionPeriodHrs` key in onboarding API.

## v2.0.4 [2021-02-08]

Expand Down
20 changes: 10 additions & 10 deletions authorizer/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ func TestOnboardingValidation(t *testing.T) {
ts := authorizer.NewTaskService(zaptest.NewLogger(t), mockTaskService(3, 2, 1))

r, err := onboard.OnboardInitialUser(context.Background(), &influxdb.OnboardingRequest{
User: "Setec Astronomy",
Password: "too many secrets",
Org: "thing",
Bucket: "holder",
RetentionPeriod: 1,
User: "Setec Astronomy",
Password: "too many secrets",
Org: "thing",
Bucket: "holder",
RetentionPeriodSeconds: 1,
})
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -126,11 +126,11 @@ func TestValidations(t *testing.T) {
svc, onboard := setup(t)

r, err := onboard.OnboardInitialUser(context.Background(), &influxdb.OnboardingRequest{
User: "Setec Astronomy",
Password: "too many secrets",
Org: "thing",
Bucket: "holder",
RetentionPeriod: 1,
User: "Setec Astronomy",
Password: "too many secrets",
Org: "thing",
Bucket: "holder",
RetentionPeriodSeconds: 1,
})
if err != nil {
t.Fatal(err)
Expand Down
25 changes: 15 additions & 10 deletions cmd/influx/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,20 +226,25 @@ func onboardingRequest(ui *input.UI) (*influxdb.OnboardingRequest, error) {
}

req := &influxdb.OnboardingRequest{
User: setupFlags.username,
Password: setupFlags.password,
Org: setupFlags.org,
Bucket: setupFlags.bucket,
RetentionPeriod: influxdb.InfiniteRetention,
Token: setupFlags.token,
User: setupFlags.username,
Password: setupFlags.password,
Org: setupFlags.org,
Bucket: setupFlags.bucket,
RetentionPeriodSeconds: influxdb.InfiniteRetention,
Token: setupFlags.token,
}

if setupFlags.retention != "" {
dur, err := internal2.RawDurationToTimeDuration(setupFlags.retention)
if err != nil {
return nil, err
}
req.RetentionPeriod = dur
secs := dur / time.Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better here to use the Duration.Seconds() and Duration.Nanoseconds() to hide the internal representation in the Duration? The modulo will have to change, if you do this, of course.

nanos := dur % time.Second
if nanos > 0 {
return nil, fmt.Errorf("retention policy %q is too precise, must be divisible by 1s", setupFlags.retention)
}
req.RetentionPeriodSeconds = int64(secs)
}

if setupFlags.force {
Expand Down Expand Up @@ -268,16 +273,16 @@ func onboardingRequest(ui *input.UI) (*influxdb.OnboardingRequest, error) {
"Please type your retention period in hours.\nOr press ENTER for infinite", infiniteStr)
rp, err := strconv.Atoi(rpStr)
if rp >= 0 && err == nil {
req.RetentionPeriod = time.Duration(rp) * time.Hour
req.RetentionPeriodSeconds = int64((time.Duration(rp) * time.Hour).Seconds())
break
}
}
}

if confirmed := internal2.GetConfirm(ui, func() string {
rp := "infinite"
if req.RetentionPeriod > 0 {
rp = fmt.Sprintf("%d hrs", req.RetentionPeriod/time.Hour)
if req.RetentionPeriodSeconds > 0 {
rp = (time.Duration(req.RetentionPeriodSeconds) * time.Second).String()
}
return fmt.Sprintf(`
You have entered:
Expand Down
10 changes: 5 additions & 5 deletions cmd/influxd/upgrade/security_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,11 @@ func TestUpgradeSecurity(t *testing.T) {

// onboard admin
oReq := &influxdb.OnboardingRequest{
User: "admin",
Password: "12345678",
Org: "testers",
Bucket: "def",
RetentionPeriod: influxdb.InfiniteRetention,
User: "admin",
Password: "12345678",
Org: "testers",
Bucket: "def",
RetentionPeriodSeconds: influxdb.InfiniteRetention,
}
oResp, err := setupAdmin(ctx, v2, oReq)
require.NoError(t, err)
Expand Down
25 changes: 15 additions & 10 deletions cmd/influxd/upgrade/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,25 @@ func onboardingRequest(ui *input.UI, options *options) (*influxdb.OnboardingRequ
}

req := &influxdb.OnboardingRequest{
User: options.target.userName,
Password: options.target.password,
Org: options.target.orgName,
Bucket: options.target.bucket,
RetentionPeriod: influxdb.InfiniteRetention,
Token: options.target.token,
User: options.target.userName,
Password: options.target.password,
Org: options.target.orgName,
Bucket: options.target.bucket,
RetentionPeriodSeconds: influxdb.InfiniteRetention,
Token: options.target.token,
}

if options.target.retention != "" {
dur, err := internal.RawDurationToTimeDuration(options.target.retention)
if err != nil {
return nil, err
}
req.RetentionPeriod = dur
secs := dur / time.Second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, can you use the Duration.Seconds() call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds like a good idea, I'll play with the math

nanos := dur % time.Second
if nanos > 0 {
return nil, fmt.Errorf("retention policy %q is too precise, must be divisible by 1s", options.target.retention)
}
req.RetentionPeriodSeconds = int64(secs)
}

if options.force {
Expand Down Expand Up @@ -72,16 +77,16 @@ func onboardingRequest(ui *input.UI, options *options) (*influxdb.OnboardingRequ
"Please type your retention period in hours.\nOr press ENTER for infinite", infiniteStr)
rp, err := strconv.Atoi(rpStr)
if rp >= 0 && err == nil {
req.RetentionPeriod = time.Duration(rp) * time.Hour
req.RetentionPeriodSeconds = int64((time.Duration(rp) * time.Hour).Seconds())
break
}
}
}

if confirmed := internal.GetConfirm(ui, func() string {
rp := "infinite"
if req.RetentionPeriod > 0 {
rp = fmt.Sprintf("%d hrs", req.RetentionPeriod/time.Hour)
if req.RetentionPeriodSeconds > 0 {
rp = (time.Duration(req.RetentionPeriodSeconds) * time.Second).String()
}
return fmt.Sprintf(`
You have entered:
Expand Down
6 changes: 6 additions & 0 deletions http/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11143,8 +11143,14 @@ components:
type: string
bucket:
type: string
retentionPeriodSeconds:
type: integer
retentionPeriodHrs:
type: integer
deprecated: true
description: >
Retention period *in nanoseconds* for the new bucket. This key's name has been misleading since OSS 2.0 GA,
please transition to use `retentionPeriodSeconds`
required:
- username
- org
Expand Down
20 changes: 14 additions & 6 deletions onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ type OnboardingResults struct {
// OnboardingRequest is the request
// to setup defaults.
type OnboardingRequest struct {
User string `json:"username"`
Password string `json:"password"`
Org string `json:"org"`
Bucket string `json:"bucket"`
RetentionPeriod time.Duration `json:"retentionPeriodHrs,omitempty"`
Token string `json:"token,omitempty"`
User string `json:"username"`
Password string `json:"password"`
Org string `json:"org"`
Bucket string `json:"bucket"`
RetentionPeriodSeconds int64 `json:"retentionPeriodSeconds,omitempty"`
RetentionPeriodHours time.Duration `json:"retentionPeriodHrs,omitempty"`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self-review: I meant to name this RetentionPeriodDeprecated to emphasize that it's not actually hours

Token string `json:"token,omitempty"`
}

func (r *OnboardingRequest) Valid() error {
Expand All @@ -59,3 +60,10 @@ func (r *OnboardingRequest) Valid() error {
}
return nil
}

func (r *OnboardingRequest) RetentionPeriod() time.Duration {
if r.RetentionPeriodSeconds > 0 {
return time.Duration(r.RetentionPeriodSeconds) * time.Second
}
return r.RetentionPeriodHours
}
2 changes: 1 addition & 1 deletion tenant/service_onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (s *OnboardService) onboardUser(ctx context.Context, req *influxdb.Onboardi
OrgID: org.ID,
Name: req.Bucket,
Type: influxdb.BucketTypeUser,
RetentionPeriod: req.RetentionPeriod,
RetentionPeriod: req.RetentionPeriod(),
}

if err := s.service.CreateBucket(ctx, ub); err != nil {
Expand Down
42 changes: 37 additions & 5 deletions tenant/service_onboarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/stretchr/testify/require"

influxdb "github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/authorization"
icontext "github.com/influxdata/influxdb/v2/context"
"github.com/influxdata/influxdb/v2/kv"
Expand Down Expand Up @@ -186,12 +186,44 @@ func TestOnboardService_RetentionPolicy(t *testing.T) {
UserID: 123,
})

var retention int64 = 72 * 3600 // 72h
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
User: "name",
Org: "name",
Bucket: "name",
RetentionPeriodSeconds: retention,
})

if err != nil {
t.Fatal(err)
}

assert.Equal(t, onboard.Bucket.RetentionPeriod, time.Duration(retention) * time.Second, "Retention policy should pass through")
}

func TestOnboardService_RetentionPolicyDeprecated(t *testing.T) {
s, _, _ := NewTestInmemStore(t)
storage := tenant.NewStore(s)
ten := tenant.NewService(storage)

authStore, err := authorization.NewStore(s)
require.NoError(t, err)

authSvc := authorization.NewService(authStore, ten)

// we will need an auth service as well
svc := tenant.NewOnboardService(ten, authSvc)

ctx := icontext.SetAuthorizer(context.Background(), &influxdb.Authorization{
UserID: 123,
})

retention := 72 * time.Hour
onboard, err := svc.OnboardInitialUser(ctx, &influxdb.OnboardingRequest{
User: "name",
Org: "name",
Bucket: "name",
RetentionPeriod: retention,
User: "name",
Org: "name",
Bucket: "name",
RetentionPeriodHours: retention,
})

if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions testing/onboarding.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ func OnboardInitialUser(
},
args: args{
request: &platform.OnboardingRequest{
User: "admin",
Org: "org1",
Bucket: "bucket1",
Password: "password1",
RetentionPeriod: time.Hour * 24 * 7, // 1 week
User: "admin",
Org: "org1",
Bucket: "bucket1",
Password: "password1",
RetentionPeriodSeconds: 3600 * 24 * 7, // 1 week
},
},
wants: wants{
Expand Down
12 changes: 6 additions & 6 deletions tests/pipeline_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func NewPipeline(tb testing.TB, opts ...launcher.OptSetter) *Pipeline {

// setup default operator
res := p.Launcher.OnBoardOrFail(tb, &influxdb.OnboardingRequest{
User: DefaultUsername,
Password: DefaultPassword,
Org: DefaultOrgName,
Bucket: DefaultBucketName,
RetentionPeriod: 0, // infinite retention period
Token: OperToken,
User: DefaultUsername,
Password: DefaultPassword,
Org: DefaultOrgName,
Bucket: DefaultBucketName,
RetentionPeriodSeconds: influxdb.InfiniteRetention,
Token: OperToken,
})

p.DefaultOrgID = res.Org.ID
Expand Down