// backupMetastore will backup the metastore on the host to the passed in path. Database and retention policy backups // will force a backup of the metastore as well as requesting a specific shard backup from the command line func (cmd *Command) backupMetastore() error { metastoreArchivePath, err := cmd.nextPath(filepath.Join(cmd.path, Metafile)) if err != nil { return err } cmd.Logger.Printf("backing up metastore to %s", metastoreArchivePath) req := &snapshotter.Request{ Type: snapshotter.RequestMetastoreBackup, } return cmd.downloadAndVerify(req, metastoreArchivePath, func(file string) error { var data meta.Data binData, err := ioutil.ReadFile(file) if err != nil { return err } if data.UnmarshalBinary(binData) != nil { cmd.Logger.Println("Invalid metadata blob, ensure the metadata service is running (default port 8088)") return errors.New("invalid metadata received") } return nil }) }
// 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 that retrieving a policy from a non-existent database returns an error. func TestData_RetentionPolicy_ErrDatabaseNotFound(t *testing.T) { var data meta.Data expErr := influxdb.ErrDatabaseNotFound("db0") if _, err := data.RetentionPolicy("db0", "rp0"); err.Error() != expErr.Error() { t.Fatal(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 a node can be created. func TestData_CreateNode(t *testing.T) { var data meta.Data if err := data.CreateNode("host0"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Nodes, []meta.NodeInfo{{ID: 1, Host: "host0"}}) { t.Fatalf("unexpected node: %#v", data.Nodes[0]) } }
// 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 a database can be created. func TestData_CreateDatabase(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Databases, []meta.DatabaseInfo{{Name: "db0"}}) { t.Fatalf("unexpected databases: %#v", data.Databases) } }
// unpackMeta reads the metadata from the snapshot and initializes a raft // cluster and replaces the root metadata. func (cmd *Command) unpackMeta(mr *snapshot.MultiReader, sf snapshot.File, config *Config) error { // Read meta into buffer. var buf bytes.Buffer if _, err := io.CopyN(&buf, mr, sf.Size); err != nil { return fmt.Errorf("copy: %s", err) } // Unpack into metadata. var data meta.Data if err := data.UnmarshalBinary(buf.Bytes()); err != nil { return fmt.Errorf("unmarshal: %s", err) } // Copy meta config and remove peers so it starts in single mode. c := config.Meta c.Peers = nil // Initialize meta store. store := meta.NewStore(config.Meta) store.RaftListener = newNopListener() store.ExecListener = newNopListener() store.RPCListener = newNopListener() // Determine advertised address. _, port, err := net.SplitHostPort(config.Meta.BindAddress) if err != nil { return fmt.Errorf("split bind address: %s", err) } hostport := net.JoinHostPort(config.Meta.Hostname, port) // Resolve address. addr, err := net.ResolveTCPAddr("tcp", hostport) if err != nil { return fmt.Errorf("resolve tcp: addr=%s, err=%s", hostport, err) } store.Addr = addr store.RemoteAddr = addr // Open the meta store. if err := store.Open(); err != nil { return fmt.Errorf("open store: %s", err) } defer store.Close() // Wait for the store to be ready or error. select { case <-store.Ready(): case err := <-store.Err(): return err } // Force set the full metadata. if err := store.SetData(&data); err != nil { return fmt.Errorf("set data: %s", err) } return nil }
// Ensure that creating the same user twice returns an error. func TestData_CreateUser_ErrUserExists(t *testing.T) { var data meta.Data if err := data.CreateUser("susy", "", false); err != nil { t.Fatal(err) } if err := data.CreateUser("susy", "", false); err != meta.ErrUserExists { t.Fatal(err) } }
// Ensure that creating an already existing database returns an error. func TestData_CreateDatabase_ErrDatabaseExists(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } if err := data.CreateDatabase("db0"); err != meta.ErrDatabaseExists { t.Fatalf("unexpected error: %s", err) } }
// Ensure an error is returned when deleting a non-existent policy. func TestData_DropRetentionPolicy_ErrRetentionPolicyNotFound(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } if err := data.DropRetentionPolicy("db0", "rp0"); err != meta.ErrRetentionPolicyNotFound { t.Fatal(err) } }
// Ensure a user can be created. func TestData_CreateUser(t *testing.T) { var data meta.Data if err := data.CreateUser("susy", "ABC123", true); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Users, []meta.UserInfo{ {Name: "susy", Hash: "ABC123", Admin: true}, }) { t.Fatalf("unexpected users: %#v", data.Users) } }
// Ensure an error is returned when deleting a non-existent policy. func TestData_DropRetentionPolicy_ErrRetentionPolicyNotFound(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } expErr := influxdb.ErrRetentionPolicyNotFound("rp0") if err := data.DropRetentionPolicy("db0", "rp0"); err.Error() != expErr.Error() { t.Fatal(err) } }
// 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 a continuous query can be created. func TestData_CreateContinuousQuery(t *testing.T) { var data meta.Data if err := data.CreateDatabase("db0"); err != nil { t.Fatal(err) } else if err := data.CreateContinuousQuery("db0", "cq0", "SELECT count() FROM foo"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Databases[0].ContinuousQueries, []meta.ContinuousQueryInfo{ {Name: "cq0", Query: "SELECT count() FROM foo"}, }) { t.Fatalf("unexpected queries: %#v", data.Databases[0].ContinuousQueries) } }
// Ensure a database can be removed. func TestData_DropDatabase(t *testing.T) { var data meta.Data for i := 0; i < 3; i++ { if err := data.CreateDatabase(fmt.Sprintf("db%d", i)); err != nil { t.Fatal(err) } } if err := data.DropDatabase("db1"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Databases, []meta.DatabaseInfo{{Name: "db0"}, {Name: "db2"}}) { t.Fatalf("unexpected databases: %#v", data.Databases) } }
// Ensure that user privileges are updated correctly when database is renamed. func TestData_RenameDatabaseUpdatesPrivileges(t *testing.T) { var data meta.Data for i := 0; i < 2; i++ { if err := data.CreateDatabase(fmt.Sprintf("db%d", i)); err != nil { t.Fatal(err) } } data.Users = []meta.UserInfo{{ Name: "susy", Hash: "ABC123", Admin: true, Privileges: map[string]influxql.Privilege{ "db1": influxql.AllPrivileges, "db0": influxql.ReadPrivilege}}} if err := data.RenameDatabase("db1", "db2"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Users, []meta.UserInfo{{ Name: "susy", Hash: "ABC123", Admin: true, Privileges: map[string]influxql.Privilege{ "db2": influxql.AllPrivileges, "db0": influxql.ReadPrivilege}}}) { t.Fatalf("unexpected user privileges: %#v", data.Users) } }
// 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 that renaming a database without both old and new names returns an error. func TestData_RenameDatabase_ErrNameRequired(t *testing.T) { var data meta.Data if err := data.RenameDatabase("", ""); err != meta.ErrDatabaseNameRequired { t.Fatalf("unexpected error: %s", err) } if err := data.RenameDatabase("from_foo", ""); err != meta.ErrDatabaseNameRequired { t.Fatalf("unexpected error: %s", err) } if err := data.RenameDatabase("", "to_foo"); err != meta.ErrDatabaseNameRequired { t.Fatalf("unexpected error: %s", err) } }
// 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. func TestData_DeleteNode_Basic(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) } if err := data.DeleteNode(1, false); err != nil { t.Fatal(err) } else if len(data.Nodes) != 2 { t.Fatalf("unexpected node count: %d", len(data.Nodes)) } else if data.Nodes[0] != (meta.NodeInfo{ID: 2, Host: "host1"}) { t.Fatalf("unexpected node: %#v", data.Nodes[0]) } else if data.Nodes[1] != (meta.NodeInfo{ID: 3, Host: "host2"}) { t.Fatalf("unexpected node: %#v", data.Nodes[1]) } }
// 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 user can be removed. func TestData_DropUser(t *testing.T) { var data meta.Data if err := data.CreateUser("susy", "", false); err != nil { t.Fatal(err) } else if err := data.CreateUser("bob", "", false); err != nil { t.Fatal(err) } if err := data.DropUser("bob"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.Users, []meta.UserInfo{ {Name: "susy"}, }) { t.Fatalf("unexpected users: %#v", data.Users) } }
// 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 the data can be marshaled and unmarshaled. func TestData_MarshalBinary(t *testing.T) { data := meta.Data{ Term: 10, Index: 20, Nodes: []meta.NodeInfo{ {ID: 1, Host: "host0"}, {ID: 2, Host: "host1"}, }, Databases: []meta.DatabaseInfo{ { Name: "db0", DefaultRetentionPolicy: "default", RetentionPolicies: []meta.RetentionPolicyInfo{ { Name: "rp0", ReplicaN: 3, Duration: 10 * time.Second, ShardGroupDuration: 3 * time.Millisecond, ShardGroups: []meta.ShardGroupInfo{ { ID: 100, StartTime: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), EndTime: time.Date(2000, time.February, 1, 0, 0, 0, 0, time.UTC), Shards: []meta.ShardInfo{ { ID: 200, Owners: []meta.ShardOwner{ {NodeID: 1}, {NodeID: 3}, {NodeID: 4}, }, }, }, }, }, }, }, ContinuousQueries: []meta.ContinuousQueryInfo{ {Query: "SELECT count() FROM foo"}, }, }, }, Users: []meta.UserInfo{ { Name: "susy", Hash: "ABC123", Admin: true, Privileges: map[string]influxql.Privilege{"db0": influxql.AllPrivileges}, }, }, } // Marshal the data struture. buf, err := data.MarshalBinary() if err != nil { t.Fatal(err) } // Unmarshal into new data. var other meta.Data if err := other.UnmarshalBinary(buf); err != nil { t.Fatal(err) } if !reflect.DeepEqual(data.Nodes, other.Nodes) { t.Fatalf("unexpected nodes: %#v", other.Nodes) } else if !reflect.DeepEqual(data.Databases, other.Databases) { spew.Dump(data.Databases) spew.Dump(other.Databases) t.Fatalf("unexpected databases: %#v", other.Databases) } else if !reflect.DeepEqual(data.Users, other.Users) { t.Fatalf("unexpected users: %#v", other.Users) } }
// Ensure the data can be deeply copied. func TestData_Clone(t *testing.T) { data := meta.Data{ Term: 10, Index: 20, Nodes: []meta.NodeInfo{ {ID: 1, Host: "host0"}, {ID: 2, Host: "host1"}, }, Databases: []meta.DatabaseInfo{ { Name: "db0", DefaultRetentionPolicy: "default", RetentionPolicies: []meta.RetentionPolicyInfo{ { Name: "rp0", ReplicaN: 3, Duration: 10 * time.Second, ShardGroupDuration: 3 * time.Millisecond, ShardGroups: []meta.ShardGroupInfo{ { ID: 100, StartTime: time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC), EndTime: time.Date(2000, time.February, 1, 0, 0, 0, 0, time.UTC), Shards: []meta.ShardInfo{ { ID: 200, Owners: []meta.ShardOwner{ {NodeID: 1}, {NodeID: 3}, {NodeID: 4}, }, }, }, }, }, }, }, ContinuousQueries: []meta.ContinuousQueryInfo{ {Query: "SELECT count() FROM foo"}, }, }, }, Users: []meta.UserInfo{ { Name: "susy", Hash: "ABC123", Admin: true, Privileges: map[string]influxql.Privilege{"db0": influxql.AllPrivileges}, }, }, } // Copy the root structure. other := data.Clone() if !reflect.DeepEqual(data.Nodes, other.Nodes) { t.Fatalf("unexpected cloned nodes: %#v", other.Nodes) } else if !reflect.DeepEqual(data.Databases, other.Databases) { t.Fatalf("unexpected cloned databases: %#v", other.Databases) } else if !reflect.DeepEqual(data.Users, other.Users) { t.Fatalf("unexpected cloned users: %#v", other.Users) } // Ensure that changing data in the clone does not affect the original. other.Databases[0].RetentionPolicies[0].ShardGroups[0].Shards[0].Owners[1].NodeID = 9 if v := data.Databases[0].RetentionPolicies[0].ShardGroups[0].Shards[0].Owners[1].NodeID; v != 3 { t.Fatalf("editing clone changed original: %v", v) } }
// Ensure that updating a non-existent user returns an error. func TestData_UpdateUser_ErrUserNotFound(t *testing.T) { var data meta.Data if err := data.UpdateUser("bob", "ZZZ"); err != meta.ErrUserNotFound { t.Fatal(err) } }
// Ensure a user can be updated. func TestData_UpdateUser(t *testing.T) { var data meta.Data if err := data.CreateUser("susy", "", false); err != nil { t.Fatal(err) } else if err := data.CreateUser("bob", "", false); err != nil { t.Fatal(err) } // Update password hash. if err := data.UpdateUser("bob", "XXX"); err != nil { t.Fatal(err) } else if !reflect.DeepEqual(data.User("bob"), &meta.UserInfo{Name: "bob", Hash: "XXX"}) { t.Fatalf("unexpected user: %#v", data.User("bob")) } }
// Ensure that removing a non-existent user returns an error. func TestData_DropUser_ErrUserNotFound(t *testing.T) { var data meta.Data if err := data.DropUser("bob"); err != meta.ErrUserNotFound { t.Fatal(err) } }
// Ensure that creating a user with no username returns an error. func TestData_CreateUser_ErrUsernameRequired(t *testing.T) { var data meta.Data if err := data.CreateUser("", "", false); err != meta.ErrUsernameRequired { t.Fatal(err) } }