Merge pull request #5071 from gyuho/member_api_change

*: Member api change
release-3.0
Gyu-Ho Lee 2016-04-13 16:45:10 -07:00
commit e9aa8ff235
17 changed files with 101 additions and 118 deletions

View File

@ -19,9 +19,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -34,9 +34,6 @@ type Cluster interface {
// MemberList lists the current cluster membership.
MemberList(ctx context.Context) (*MemberListResponse, error)
// MemberLeader returns the current leader member.
MemberLeader(ctx context.Context) (*Member, error)
// MemberAdd adds a new member into the cluster.
MemberAdd(ctx context.Context, peerAddrs []string) (*MemberAddResponse, error)
@ -135,19 +132,6 @@ func (c *cluster) MemberList(ctx context.Context) (*MemberListResponse, error) {
}
}
func (c *cluster) MemberLeader(ctx context.Context) (*Member, error) {
resp, err := c.MemberList(ctx)
if err != nil {
return nil, err
}
for _, m := range resp.Members {
if m.IsLeader {
return (*Member)(m), nil
}
}
return nil, nil
}
func (c *cluster) getRemote() pb.ClusterClient {
c.mu.Lock()
defer c.mu.Unlock()

View File

@ -40,24 +40,6 @@ func ExampleCluster_memberList() {
// members: 3
}
func ExampleCluster_memberLeader() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints,
DialTimeout: dialTimeout,
})
if err != nil {
log.Fatal(err)
}
defer cli.Close()
resp, err := cli.MemberLeader(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("leader:", resp.Name)
// leader: infra1
}
func ExampleCluster_memberAdd() {
cli, err := clientv3.New(clientv3.Config{
Endpoints: endpoints[:2],

View File

@ -0,0 +1,51 @@
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package clientv3_test
import (
"fmt"
"log"
"golang.org/x/net/context"
"github.com/coreos/etcd/clientv3"
)
func ExampleMaintenance_Status() {
for _, ep := range endpoints {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
DialTimeout: dialTimeout,
})
if err != nil {
log.Fatal(err)
}
defer cli.Close()
// resp, err := cli.Status(context.Background(), ep)
//
// or
//
mapi := clientv3.NewMaintenance(cli)
resp, err := mapi.Status(context.Background(), ep)
if err != nil {
log.Fatal(err)
}
fmt.Printf("endpoint: %s / IsLeader: %v\n", ep, resp.Header.MemberId == resp.Leader)
}
// endpoint: localhost:2379 / IsLeader: false
// endpoint: localhost:22379 / IsLeader: false
// endpoint: localhost:32379 / IsLeader: true
}

View File

@ -25,7 +25,7 @@ import (
var (
dialTimeout = 5 * time.Second
requestTimeout = 1 * time.Second
endpoints = []string{"localhost:2379", "localhost:22379", "http://localhost:32379"}
endpoints = []string{"localhost:2379", "localhost:22379", "localhost:32379"}
)
func Example() {

View File

@ -108,7 +108,7 @@ func (s *simplePrinter) Alarm(resp v3.AlarmResponse) {
func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs", "Is Leader"})
table.SetHeader([]string{"ID", "Status", "Name", "Peer Addrs", "Client Addrs"})
for _, m := range resp.Members {
status := "started"
@ -122,7 +122,6 @@ func (s *simplePrinter) MemberList(resp v3.MemberListResponse) {
m.Name,
strings.Join(m.PeerURLs, ","),
strings.Join(m.ClientURLs, ","),
fmt.Sprint(m.IsLeader),
})
}

View File

@ -62,7 +62,7 @@ func (cs *ClusterServer) MemberAdd(ctx context.Context, r *pb.MemberAddRequest)
return &pb.MemberAddResponse{
Header: cs.header(),
Member: &pb.Member{ID: uint64(m.ID), IsLeader: m.ID == cs.server.Leader(), PeerURLs: m.PeerURLs},
Member: &pb.Member{ID: uint64(m.ID), PeerURLs: m.PeerURLs},
}, nil
}
@ -106,7 +106,6 @@ func (cs *ClusterServer) MemberList(ctx context.Context, r *pb.MemberListRequest
protoMembs[i] = &pb.Member{
Name: membs[i].Name,
ID: uint64(membs[i].ID),
IsLeader: membs[i].ID == cs.server.Leader(),
PeerURLs: membs[i].PeerURLs,
ClientURLs: membs[i].ClientURLs,
}

View File

@ -94,9 +94,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -8,9 +8,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -8,20 +8,21 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
math "math"
authpb "github.com/coreos/etcd/auth/authpb"
io "io"
)
import math "math"
import storagepb "github.com/coreos/etcd/storage/storagepb"
import authpb "github.com/coreos/etcd/auth/authpb"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
import io "io"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
@ -1094,11 +1095,10 @@ type Member struct {
ID uint64 `protobuf:"varint,1,opt,name=ID,proto3" json:"ID,omitempty"`
// If the member is not started, name will be an empty string.
Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"`
IsLeader bool `protobuf:"varint,3,opt,name=IsLeader,proto3" json:"IsLeader,omitempty"`
PeerURLs []string `protobuf:"bytes,4,rep,name=peerURLs" json:"peerURLs,omitempty"`
PeerURLs []string `protobuf:"bytes,3,rep,name=peerURLs" json:"peerURLs,omitempty"`
// If the member is not started, client_URLs will be an zero length
// string array.
ClientURLs []string `protobuf:"bytes,5,rep,name=clientURLs" json:"clientURLs,omitempty"`
ClientURLs []string `protobuf:"bytes,4,rep,name=clientURLs" json:"clientURLs,omitempty"`
}
func (m *Member) Reset() { *m = Member{} }
@ -4138,19 +4138,9 @@ func (m *Member) MarshalTo(data []byte) (int, error) {
i = encodeVarintRpc(data, i, uint64(len(m.Name)))
i += copy(data[i:], m.Name)
}
if m.IsLeader {
data[i] = 0x18
i++
if m.IsLeader {
data[i] = 1
} else {
data[i] = 0
}
i++
}
if len(m.PeerURLs) > 0 {
for _, s := range m.PeerURLs {
data[i] = 0x22
data[i] = 0x1a
i++
l = len(s)
for l >= 1<<7 {
@ -4165,7 +4155,7 @@ func (m *Member) MarshalTo(data []byte) (int, error) {
}
if len(m.ClientURLs) > 0 {
for _, s := range m.ClientURLs {
data[i] = 0x2a
data[i] = 0x22
i++
l = len(s)
for l >= 1<<7 {
@ -5916,9 +5906,6 @@ func (m *Member) Size() (n int) {
if l > 0 {
n += 1 + l + sovRpc(uint64(l))
}
if m.IsLeader {
n += 2
}
if len(m.PeerURLs) > 0 {
for _, s := range m.PeerURLs {
l = len(s)
@ -9759,26 +9746,6 @@ func (m *Member) Unmarshal(data []byte) error {
m.Name = string(data[iNdEx:postIndex])
iNdEx = postIndex
case 3:
if wireType != 0 {
return fmt.Errorf("proto: wrong wireType = %d for field IsLeader", wireType)
}
var v int
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowRpc
}
if iNdEx >= l {
return io.ErrUnexpectedEOF
}
b := data[iNdEx]
iNdEx++
v |= (int(b) & 0x7F) << shift
if b < 0x80 {
break
}
}
m.IsLeader = bool(v != 0)
case 4:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field PeerURLs", wireType)
}
@ -9807,7 +9774,7 @@ func (m *Member) Unmarshal(data []byte) error {
}
m.PeerURLs = append(m.PeerURLs, string(data[iNdEx:postIndex]))
iNdEx = postIndex
case 5:
case 4:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field ClientURLs", wireType)
}

View File

@ -417,11 +417,10 @@ message Member {
uint64 ID = 1;
// If the member is not started, name will be an empty string.
string name = 2;
bool IsLeader = 3;
repeated string peerURLs = 4;
repeated string peerURLs = 3;
// If the member is not started, client_URLs will be an zero length
// string array.
repeated string clientURLs = 5;
repeated string clientURLs = 4;
}
message MemberAddRequest {
@ -459,7 +458,6 @@ message MemberListResponse {
}
message DefragmentRequest {
}
message DefragmentResponse {

View File

@ -17,9 +17,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -23,9 +23,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -17,9 +17,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -18,9 +18,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"

View File

@ -185,24 +185,27 @@ 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 {
for i, ep := range c.GRPCURLs {
cli, err := clientv3.New(clientv3.Config{
Endpoints: []string{ep},
DialTimeout: 5 * time.Second,
})
if err != nil {
return 0, err
}
defer cli.Close()
mapi := clientv3.NewMaintenance(cli)
resp, err := mapi.Status(context.Background(), ep)
if err != nil {
return 0, err
}
if resp.Header.MemberId == resp.Leader {
return i, nil
}
}
return 0, fmt.Errorf("no leader found")
}

View File

@ -18,9 +18,9 @@ import (
"fmt"
proto "github.com/gogo/protobuf/proto"
)
import math "math"
math "math"
)
import io "io"