server.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package v2v3
  15. import (
  16. "context"
  17. "net/http"
  18. "time"
  19. "github.com/coreos/etcd/clientv3"
  20. "github.com/coreos/etcd/etcdserver"
  21. "github.com/coreos/etcd/etcdserver/api"
  22. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  23. "github.com/coreos/etcd/etcdserver/membership"
  24. "github.com/coreos/etcd/pkg/types"
  25. "github.com/coreos/go-semver/semver"
  26. )
  27. type fakeStats struct{}
  28. func (s *fakeStats) SelfStats() []byte { return nil }
  29. func (s *fakeStats) LeaderStats() []byte { return nil }
  30. func (s *fakeStats) StoreStats() []byte { return nil }
  31. type v2v3Server struct {
  32. c *clientv3.Client
  33. store *v2v3Store
  34. fakeStats
  35. }
  36. func NewServer(c *clientv3.Client, pfx string) etcdserver.ServerPeer {
  37. return &v2v3Server{c: c, store: newStore(c, pfx)}
  38. }
  39. func (s *v2v3Server) ClientCertAuthEnabled() bool { return false }
  40. func (s *v2v3Server) LeaseHandler() http.Handler { panic("STUB: lease handler") }
  41. func (s *v2v3Server) RaftHandler() http.Handler { panic("STUB: raft handler") }
  42. func (s *v2v3Server) Leader() types.ID {
  43. ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
  44. defer cancel()
  45. resp, err := s.c.Status(ctx, s.c.Endpoints()[0])
  46. if err != nil {
  47. return 0
  48. }
  49. return types.ID(resp.Leader)
  50. }
  51. func (s *v2v3Server) AddMember(ctx context.Context, memb membership.Member) ([]*membership.Member, error) {
  52. resp, err := s.c.MemberAdd(ctx, memb.PeerURLs)
  53. if err != nil {
  54. return nil, err
  55. }
  56. return v3MembersToMembership(resp.Members), nil
  57. }
  58. func (s *v2v3Server) RemoveMember(ctx context.Context, id uint64) ([]*membership.Member, error) {
  59. resp, err := s.c.MemberRemove(ctx, id)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return v3MembersToMembership(resp.Members), nil
  64. }
  65. func (s *v2v3Server) UpdateMember(ctx context.Context, m membership.Member) ([]*membership.Member, error) {
  66. resp, err := s.c.MemberUpdate(ctx, uint64(m.ID), m.PeerURLs)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return v3MembersToMembership(resp.Members), nil
  71. }
  72. func v3MembersToMembership(v3membs []*pb.Member) []*membership.Member {
  73. membs := make([]*membership.Member, len(v3membs))
  74. for i, m := range v3membs {
  75. membs[i] = &membership.Member{
  76. ID: types.ID(m.ID),
  77. RaftAttributes: membership.RaftAttributes{
  78. PeerURLs: m.PeerURLs,
  79. },
  80. Attributes: membership.Attributes{
  81. Name: m.Name,
  82. ClientURLs: m.ClientURLs,
  83. },
  84. }
  85. }
  86. return membs
  87. }
  88. func (s *v2v3Server) ClusterVersion() *semver.Version { return s.Version() }
  89. func (s *v2v3Server) Cluster() api.Cluster { return s }
  90. func (s *v2v3Server) Alarms() []*pb.AlarmMember { return nil }
  91. func (s *v2v3Server) Do(ctx context.Context, r pb.Request) (etcdserver.Response, error) {
  92. applier := etcdserver.NewApplierV2(s.store, nil)
  93. reqHandler := etcdserver.NewStoreRequestV2Handler(s.store, applier)
  94. req := (*etcdserver.RequestV2)(&r)
  95. resp, err := req.Handle(ctx, reqHandler)
  96. if resp.Err != nil {
  97. return resp, resp.Err
  98. }
  99. return resp, err
  100. }