From 8655ed3d91383bf5b3dbfecf195ee6e4be1a3244 Mon Sep 17 00:00:00 2001 From: Cory LaNou Date: Thu, 12 Mar 2015 10:01:26 -0600 Subject: [PATCH] fixing default retention policy creation --- server.go | 42 ++++++++++++++++++++++-------------------- server_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 20 deletions(-) diff --git a/server.go b/server.go index bb48fd5f44..9c2649a2f8 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,30 +1278,31 @@ func (s *Server) CreateRetentionPolicy(database string, rp *RetentionPolicy) err return ErrRetentionPolicyMinDuration } + c := &createRetentionPolicyCommand{ + Database: database, + Name: rp.Name, + Duration: rp.Duration, + 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 ) - var sgd time.Duration switch { - case rp.Duration > 6*month || rp.Duration == 0: - sgd = 7 * day - case rp.Duration > 2*day: - sgd = 1 * day + case d > 6*month || d == 0: + return 7 * day + case d > 2*day: + return 1 * day default: - sgd = 1 * time.Hour + return 1 * time.Hour } - - c := &createRetentionPolicyCommand{ - Database: database, - Name: rp.Name, - Duration: rp.Duration, - ShardGroupDuration: sgd, - ReplicaN: rp.ReplicaN, - } - _, err := s.broadcast(createRetentionPolicyMessageType, c) - return err } func (s *Server) applyCreateRetentionPolicy(m *messaging.Message) error { diff --git a/server_test.go b/server_test.go index cc37f87500..8de079b0ca 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())