// Ensure that a retention policy can be updated. func TestData_UpdateRetentionPolicy(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err != nil { t.Fatal(err) } // Update the policy. var rpu meta.RetentionPolicyUpdate rpu.SetName("rp1") rpu.SetDuration(10 * time.Hour) rpu.SetReplicaN(3) if err := data.UpdateRetentionPolicy("db0", "rp0", &rpu); err != nil { t.Fatal(err) } // Verify the policy was changed. if rpi, _ := data.RetentionPolicy("db0", "rp1"); !reflect.DeepEqual(rpi, &meta.RetentionPolicyInfo{ Name: "rp1", Duration: 10 * time.Hour, ShardGroupDuration: 3600000000000, ReplicaN: 3, }) { t.Fatalf("unexpected policy: %#v", rpi) } }
// Ensure that creating a retention policy on a non-existent database returns an error. func TestData_CreateRetentionPolicy_ErrDatabaseNotFound(t *testing.T) { data := meta.Data{Nodes: []meta.NodeInfo{{ID: 1}}} expErr := influxdb.ErrDatabaseNotFound("db0") if err := data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err.Error() != expErr.Error() { t.Fatalf("unexpected error: %s", err) } }
// Ensure a retention policy can be created. func TestData_CreateRetentionPolicy(t *testing.T) { data := meta.Data{Nodes: []meta.NodeInfo{{ID: 1}, {ID: 2}}} if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } // Create policy. if err := data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{ Name: "rp0", ReplicaN: 2, Duration: 4 * time.Hour, }); err != nil { t.Fatal(err) } // Verify policy exists. if !reflect.DeepEqual(data.Databases[0].RetentionPolicies, []meta.RetentionPolicyInfo{ { Name: "rp0", ReplicaN: 2, Duration: 4 * time.Hour, ShardGroupDuration: 1 * time.Hour, }, }) { t.Fatalf("unexpected policies: %#v", data.Databases[0].RetentionPolicies) } }
// Ensure that a shard group can be created on a database for a given timestamp. func TestData_CreateShardGroup(t *testing.T) { var data meta.Data if err := data.CreateNode("node0"); err != nil { t.Fatal(err) } else if err = data.CreateNode("node1"); err != nil { t.Fatal(err) } else if err = data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 2, Duration: 1 * time.Hour}); err != nil { t.Fatal(err) } // Create shard group. if err := data.CreateShardGroup("db0", "rp0", time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)); err != nil { t.Fatal(err) } // Verify the shard group was created. if sgi, _ := data.ShardGroupByTimestamp("db0", "rp0", time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)); !reflect.DeepEqual(sgi, &meta.ShardGroupInfo{ ID: 1, StartTime: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), EndTime: time.Date(2000, time.January, 1, 1, 0, 0, 0, time.UTC), Shards: []meta.ShardInfo{ {ID: 1, OwnerIDs: []uint64{1, 2}}, }, }) { t.Fatalf("unexpected shard group: %#v", sgi) } else if !sgi.Shards[0].OwnedBy(1) || !sgi.Shards[0].OwnedBy(2) || sgi.Shards[0].OwnedBy(3) { // Verify shard is correctly owned-by the node. t.Fatalf("new shard is not owned by correct node") } }
// Ensure that creating a policy with a replication factor that doesn't match // the number of nodes in the cluster will return an error. This is a temporary // restriction until v0.9.1 is released. func TestData_CreateRetentionPolicy_ErrReplicationFactorMismatch(t *testing.T) { data := meta.Data{ Nodes: []meta.NodeInfo{{ID: 1}, {ID: 2}, {ID: 3}}, } if err := data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 2}); err != meta.ErrReplicationFactorMismatch { t.Fatalf("unexpected error: %s", err) } }
// Ensure that creating an already existing policy returns an error. func TestData_CreateRetentionPolicy_ErrRetentionPolicyExists(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err != nil { t.Fatal(err) } if err := data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err != meta.ErrRetentionPolicyExists { t.Fatalf("unexpected error: %s", err) } }
// Ensure a retention policy can be removed. func TestData_DropRetentionPolicy(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err != nil { t.Fatal(err) } if err := data.DropRetentionPolicy("db0", "rp0"); err != nil { t.Fatal(err) } else if len(data.Databases[0].RetentionPolicies) != 0 { t.Fatalf("unexpected policies: %#v", data.Databases[0].RetentionPolicies) } }
// Ensure a subscription can be created. func TestData_CreateSubscription(t *testing.T) { var data meta.Data rpi := &meta.RetentionPolicyInfo{ Name: "rp0", ReplicaN: 3, } if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err := data.CreateRetentionPolicy("db0", rpi); err != nil { t.Fatal(err) } else if err := data.CreateSubscription("db0", "rp0", "s0", "ANY", []string{"udp://h0:1234", "udp://h1:1234"}); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Databases[0].RetentionPolicies[0].Subscriptions, []meta.SubscriptionInfo{ {Name: "s0", Mode: "ANY", Destinations: []string{"udp://h0:1234", "udp://h1:1234"}}, }) { t.Fatalf("unexpected subscriptions: %#v", data.Databases[0].RetentionPolicies[0].Subscriptions) } }
// Ensure a shard group can be removed by ID. func TestData_DeleteShardGroup(t *testing.T) { var data meta.Data if err := data.CreateNode("node0"); err != nil { t.Fatal(err) } else if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err != nil { t.Fatal(err) } else if err := data.CreateShardGroup("db0", "rp0", time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)); err != nil { t.Fatal(err) } if err := data.DeleteShardGroup("db0", "rp0", 1); err != nil { t.Fatal(err) } if sg := data.Databases[0].RetentionPolicies[0].ShardGroups[0]; !sg.Deleted() { t.Fatalf("shard group not correctly flagged as deleted") } }
// Ensure that a shard group is correctly detected as expired. func TestData_ShardGroupExpiredDeleted(t *testing.T) { var data meta.Data if err := data.CreateNode("node0"); err != nil { t.Fatal(err) } else if err = data.CreateNode("node1"); err != nil { t.Fatal(err) } else if err = data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 2, Duration: 1 * time.Hour}); err != nil { t.Fatal(err) } // Create shard groups. if err := data.CreateShardGroup("db0", "rp0", time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)); err != nil { t.Fatal(err) } if err := data.CreateShardGroup("db0", "rp0", time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)); err != nil { t.Fatal(err) } // Check expiration. rp, _ := data.RetentionPolicy("db0", "rp0") groups := rp.ExpiredShardGroups(time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC)) if len(groups) != 1 { t.Fatalf("wrong number of expired shard groups returned, got %d, exp 1", len(groups)) } if groups[0].StartTime != time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) { t.Fatal("wrong shard group marked as expired") } // Check deletion. if err := data.DeleteShardGroup("db0", "rp0", groups[0].ID); err != nil { t.Fatal(err) } groups = rp.DeletedShardGroups() if len(groups) != 1 { t.Fatalf("wrong number of deleted shard groups returned, got %d, exp 1", len(groups)) } if groups[0].StartTime != time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) { t.Fatal("wrong shard group marked as expired") } }
// Ensure that a retention policy can be retrieved. func TestData_RetentionPolicy(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0"}); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp1"}); err != nil { t.Fatal(err) } if rpi, err := data.RetentionPolicy("db0", "rp0"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(rpi, &meta.RetentionPolicyInfo{ Name: "rp0", ShardGroupDuration: 604800000000000, }) { t.Fatalf("unexpected value: %#v", rpi) } }
// Ensure a node can be removed with shard info in play func TestData_DeleteNode_Shards(t *testing.T) { var data meta.Data if err := data.CreateNode("host0"); err != nil { t.Fatal(err) } else if err = data.CreateNode("host1"); err != nil { t.Fatal(err) } else if err := data.CreateNode("host2"); err != nil { t.Fatal(err) } else if err := data.CreateNode("host3"); err != nil { t.Fatal(err) } if err := data.CreateDatabase("mydb"); err != nil { t.Fatal(err) } rpi := &meta.RetentionPolicyInfo{ Name: "myrp", ReplicaN: 3, } if err := data.CreateRetentionPolicy("mydb", rpi); err != nil { t.Fatal(err) } if err := data.CreateShardGroup("mydb", "myrp", time.Now()); err != nil { t.Fatal(err) } if len(data.Databases[0].RetentionPolicies[0].ShardGroups[0].Shards[0].Owners) != 3 { t.Fatal("wrong number of shard owners") } if err := data.DeleteNode(2, false); err != nil { t.Fatal(err) } if got, exp := len(data.Databases[0].RetentionPolicies[0].ShardGroups[0].Shards[0].Owners), 2; exp != got { t.Fatalf("wrong number of shard owners, got %d, exp %d", got, exp) } for _, s := range data.Databases[0].RetentionPolicies[0].ShardGroups[0].Shards { if s.OwnedBy(2) { t.Fatal("shard still owned by delted node") } } }
// Ensure that a default retention policy can be set. func TestData_SetDefaultRetentionPolicy(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err = data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: "rp0", ReplicaN: 1}); err != nil { t.Fatal(err) } // Verify there is no default policy on the database initially. if name := data.Database("db0").DefaultRetentionPolicy; name != "" { t.Fatalf("unexpected initial default retention policy: %s", name) } // Set the default policy. if err := data.SetDefaultRetentionPolicy("db0", "rp0"); err != nil { t.Fatal(err) } // Verify the default policy is now set. if name := data.Database("db0").DefaultRetentionPolicy; name != "rp0" { t.Fatalf("unexpected default retention policy: %s", name) } }
// Ensure that creating a policy without a name returns an error. func TestData_CreateRetentionPolicy_ErrNameRequired(t *testing.T) { data := meta.Data{Nodes: []meta.NodeInfo{{ID: 1}}} if err := data.CreateRetentionPolicy("db0", &meta.RetentionPolicyInfo{Name: ""}); err != meta.ErrRetentionPolicyNameRequired { t.Fatalf("unexpected error: %s", err) } }