func TestMemberRemove(t *testing.T) { defer testutil.AfterTest(t) clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3}) defer clus.Terminate(t) capi := clientv3.NewCluster(clus.Client(1)) resp, err := capi.MemberList(context.Background()) if err != nil { t.Fatalf("failed to list member %v", err) } _, err = capi.MemberRemove(context.Background(), resp.Members[0].ID) if err != nil { t.Fatalf("failed to remove member %v", err) } resp, err = capi.MemberList(context.Background()) if err != nil { t.Fatalf("failed to list member %v", err) } if len(resp.Members) != 2 { t.Errorf("number of members = %d, want %d", len(resp.Members), 2) } }
func TestMemberUpdate(t *testing.T) { defer testutil.AfterTest(t) clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3}) defer clus.Terminate(t) capi := clientv3.NewCluster(clus.RandClient()) resp, err := capi.MemberList(context.Background()) if err != nil { t.Fatalf("failed to list member %v", err) } urls := []string{"http://127.0.0.1:1234"} _, err = capi.MemberUpdate(context.Background(), resp.Members[0].ID, urls) if err != nil { t.Fatalf("failed to update member %v", err) } resp, err = capi.MemberList(context.Background()) if err != nil { t.Fatalf("failed to list member %v", err) } if !reflect.DeepEqual(resp.Members[0].PeerURLs, urls) { t.Errorf("urls = %v, want %v", urls, resp.Members[0].PeerURLs) } }
func ExampleCluster_memberLeader() { cli, err := clientv3.New(clientv3.Config{ Endpoints: endpoints, DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() capi := clientv3.NewCluster(cli) resp, err := capi.MemberLeader(context.Background()) if err != nil { log.Fatal(err) } fmt.Println("leader:", resp.Name) // leader: infra1 }
func ExampleCluster_memberList() { cli, err := clientv3.New(clientv3.Config{ Endpoints: endpoints, DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() capi := clientv3.NewCluster(cli) resp, err := capi.MemberList(context.Background()) if err != nil { log.Fatal(err) } fmt.Println("members:", len(resp.Members)) // members: 3 }
func ExampleCluster_memberAdd() { cli, err := clientv3.New(clientv3.Config{ Endpoints: endpoints[:2], DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() capi := clientv3.NewCluster(cli) peerURLs := endpoints[2:] mresp, err := capi.MemberAdd(context.Background(), peerURLs) if err != nil { log.Fatal(err) } fmt.Println("added member.PeerURLs:", mresp.Member.PeerURLs) // added member.PeerURLs: [http://localhost:32380] }
func ExampleCluster_memberRemove() { cli, err := clientv3.New(clientv3.Config{ Endpoints: endpoints[1:], DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() capi := clientv3.NewCluster(cli) resp, err := capi.MemberList(context.Background()) if err != nil { log.Fatal(err) } _, err = capi.MemberRemove(context.Background(), resp.Members[0].ID) if err != nil { log.Fatal(err) } }
func ExampleCluster_memberUpdate() { cli, err := clientv3.New(clientv3.Config{ Endpoints: endpoints, DialTimeout: dialTimeout, }) if err != nil { log.Fatal(err) } defer cli.Close() capi := clientv3.NewCluster(cli) resp, err := capi.MemberList(context.Background()) if err != nil { log.Fatal(err) } peerURLs := []string{"http://localhost:12378"} _, err = capi.MemberUpdate(context.Background(), resp.Members[0].ID, peerURLs) if err != nil { log.Fatal(err) } }
func TestMemberRemove(t *testing.T) { defer testutil.AfterTest(t) clus := integration.NewClusterV3(t, &integration.ClusterConfig{Size: 3}) defer clus.Terminate(t) capi := clientv3.NewCluster(clus.Client(1)) resp, err := capi.MemberList(context.Background()) if err != nil { t.Fatalf("failed to list member %v", err) } rmvID := resp.Members[0].ID // indexes in capi member list don't necessarily match cluster member list; // find member that is not the client to remove for _, m := range resp.Members { mURLs, _ := types.NewURLs(m.PeerURLs) if !reflect.DeepEqual(mURLs, clus.Members[1].ServerConfig.PeerURLs) { rmvID = m.ID break } } _, err = capi.MemberRemove(context.Background(), rmvID) if err != nil { t.Fatalf("failed to remove member %v", err) } resp, err = capi.MemberList(context.Background()) if err != nil { t.Fatalf("failed to list member %v", err) } if len(resp.Members) != 2 { t.Errorf("number of members = %d, want %d", len(resp.Members), 2) } }
// GetLeader returns the index of leader and error if any. func (c *cluster) GetLeader() (int, error) { if c.v2Only { return 0, nil } cli, err := clientv3.New(clientv3.Config{ Endpoints: c.GRPCURLs, DialTimeout: 5 * time.Second, }) if err != nil { return 0, err } defer cli.Close() clus := clientv3.NewCluster(cli) mem, err := clus.MemberLeader(context.Background()) if err != nil { return 0, err } for i, name := range c.Names { if name == mem.Name { return i, nil } } return 0, fmt.Errorf("no leader found") }