fixing default retention policy creation

pull/1929/head
Cory LaNou 2015-03-12 10:01:26 -06:00
parent b4e1795e0b
commit 8655ed3d91
2 changed files with 54 additions and 20 deletions

View File

@ -766,9 +766,10 @@ func (s *Server) applyCreateDatabase(m *messaging.Message) (err error) {
if s.RetentionAutoCreate { if s.RetentionAutoCreate {
// Create the default retention policy. // Create the default retention policy.
db.policies[c.Name] = &RetentionPolicy{ db.policies[DefaultRetentionPolicyName] = &RetentionPolicy{
Name: DefaultRetentionPolicyName, Name: DefaultRetentionPolicyName,
Duration: 0, Duration: 0,
ShardGroupDuration: calculateShardGroupDuration(0),
ReplicaN: 1, ReplicaN: 1,
} }
db.defaultRetentionPolicy = DefaultRetentionPolicyName db.defaultRetentionPolicy = DefaultRetentionPolicyName
@ -1277,30 +1278,31 @@ func (s *Server) CreateRetentionPolicy(database string, rp *RetentionPolicy) err
return ErrRetentionPolicyMinDuration 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 ( const (
day = time.Hour * 24 day = time.Hour * 24
month = day * 30 month = day * 30
) )
var sgd time.Duration
switch { switch {
case rp.Duration > 6*month || rp.Duration == 0: case d > 6*month || d == 0:
sgd = 7 * day return 7 * day
case rp.Duration > 2*day: case d > 2*day:
sgd = 1 * day return 1 * day
default: 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 { func (s *Server) applyCreateRetentionPolicy(m *messaging.Message) error {

View File

@ -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. // Ensure the server returns an error when creating a retention policy with an invalid db.
func TestServer_CreateRetentionPolicy_ErrDatabaseNotFound(t *testing.T) { func TestServer_CreateRetentionPolicy_ErrDatabaseNotFound(t *testing.T) {
s := OpenServer(NewMessagingClient()) s := OpenServer(NewMessagingClient())