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

Default Retention Policy incorrectly auto created #1929

Merged
merged 2 commits into from
Mar 12, 2015
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -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.
Expand Down
42 changes: 22 additions & 20 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 32 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down