diff --git a/CHANGELOG.md b/CHANGELOG.md index bfc40a3633d..6df4896454e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Bugfixes - [#1917](https://github.com/influxdb/influxdb/pull/1902): Creating Infinite Retention Policy Failed. - [#1758](https://github.com/influxdb/influxdb/pull/1758): Add Graphite Integration Test. +- [#1929](https://github.com/influxdb/influxdb/pull/1929): Default Retention Policy incorrectly auto created. ### Features - [#1902](https://github.com/influxdb/influxdb/pull/1902): Enforce retention policies to have a minimum duration. diff --git a/server.go b/server.go index bb48fd5f449..9c2649a2f81 100644 --- a/server.go +++ b/server.go @@ -766,10 +766,11 @@ func (s *Server) applyCreateDatabase(m *messaging.Message) (err error) { if s.RetentionAutoCreate { // Create the default retention policy. - db.policies[c.Name] = &RetentionPolicy{ - Name: DefaultRetentionPolicyName, - Duration: 0, - ReplicaN: 1, + db.policies[DefaultRetentionPolicyName] = &RetentionPolicy{ + Name: DefaultRetentionPolicyName, + Duration: 0, + ShardGroupDuration: calculateShardGroupDuration(0), + ReplicaN: 1, } db.defaultRetentionPolicy = DefaultRetentionPolicyName s.Logger.Printf("retention policy '%s' auto-created for database '%s'", DefaultRetentionPolicyName, c.Name) @@ -1277,32 +1278,33 @@ func (s *Server) CreateRetentionPolicy(database string, rp *RetentionPolicy) err return ErrRetentionPolicyMinDuration } - const ( - day = time.Hour * 24 - month = day * 30 - ) - - var sgd time.Duration - switch { - case rp.Duration > 6*month || rp.Duration == 0: - sgd = 7 * day - case rp.Duration > 2*day: - sgd = 1 * day - default: - sgd = 1 * time.Hour - } - c := &createRetentionPolicyCommand{ Database: database, Name: rp.Name, Duration: rp.Duration, - ShardGroupDuration: sgd, + ShardGroupDuration: calculateShardGroupDuration(rp.Duration), ReplicaN: rp.ReplicaN, } _, err := s.broadcast(createRetentionPolicyMessageType, c) return err } +func calculateShardGroupDuration(d time.Duration) time.Duration { + const ( + day = time.Hour * 24 + month = day * 30 + ) + + switch { + case d > 6*month || d == 0: + return 7 * day + case d > 2*day: + return 1 * day + default: + return 1 * time.Hour + } +} + func (s *Server) applyCreateRetentionPolicy(m *messaging.Message) error { var c createRetentionPolicyCommand mustUnmarshalJSON(m.Data, &c) diff --git a/server_test.go b/server_test.go index cc37f875005..8de079b0ca3 100644 --- a/server_test.go +++ b/server_test.go @@ -585,6 +585,38 @@ func TestServer_CreateRetentionPolicyInfinite(t *testing.T) { } } +// Ensure the database can creates a default retention policy. +func TestServer_CreateRetentionPolicyDefault(t *testing.T) { + s := OpenServer(NewMessagingClient()) + defer s.Close() + + s.RetentionAutoCreate = true + + // Create a database. + if err := s.CreateDatabase("foo"); err != nil { + t.Fatal(err) + } + + s.Restart() + + rp := &influxdb.RetentionPolicy{ + Name: "default", + Duration: 0, + ShardGroupDuration: time.Hour * 24 * 7, + ReplicaN: 1, + } + + // Verify that the policy exists. + if o, err := s.RetentionPolicy("foo", "default"); err != nil { + t.Fatalf("unexpected error: %s", err) + } else if o == nil { + t.Fatalf("retention policy not found") + } else if !reflect.DeepEqual(rp, o) { + t.Logf("expected: %#v\n", rp) + t.Fatalf("retention policy mismatch: %#v", o) + } +} + // Ensure the server returns an error when creating a retention policy with an invalid db. func TestServer_CreateRetentionPolicy_ErrDatabaseNotFound(t *testing.T) { s := OpenServer(NewMessagingClient())