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,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 {

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.
func TestServer_CreateRetentionPolicy_ErrDatabaseNotFound(t *testing.T) {
s := OpenServer(NewMessagingClient())