-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathservice.go
207 lines (167 loc) · 4.38 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package leader
import (
"errors"
"fmt"
"sync"
"github.com/m3db/m3/src/cluster/services"
"github.com/m3db/m3/src/cluster/services/leader/campaign"
"github.com/coreos/etcd/clientv3"
)
const (
leaderKeyPrefix = "_ld"
keyFormat = "%s/%s"
)
var (
// errClientClosed indicates the election service client has been closed and
// no more elections can be started.
errClientClosed = errors.New("election client is closed")
)
type multiClient struct {
sync.RWMutex
closed bool
clients map[string]*client
opts Options
etcdClient *clientv3.Client
}
// NewService creates a new leader service client based on an etcd client.
func NewService(cli *clientv3.Client, opts Options) (services.LeaderService, error) {
if err := opts.Validate(); err != nil {
return nil, err
}
return &multiClient{
clients: make(map[string]*client),
opts: opts,
etcdClient: cli,
}, nil
}
// Close closes all underlying election clients and returns all errors
// encountered, if any.
func (s *multiClient) Close() error {
s.Lock()
if s.closed {
s.Unlock()
return nil
}
s.closed = true
s.Unlock()
return s.closeClients()
}
func (s *multiClient) isClosed() bool {
s.RLock()
defer s.RUnlock()
return s.closed
}
func (s *multiClient) closeClients() error {
s.RLock()
errC := make(chan error, 1)
var wg sync.WaitGroup
for _, cl := range s.clients {
wg.Add(1)
go func(cl *client) {
if err := cl.close(); err != nil {
select {
case errC <- err:
default:
}
}
wg.Done()
}(cl)
}
s.RUnlock()
wg.Wait()
close(errC)
select {
case err := <-errC:
return err
default:
return nil
}
}
func (s *multiClient) getOrCreateClient(electionID string) (*client, error) {
s.RLock()
cl, ok := s.clients[electionID]
s.RUnlock()
if ok {
return cl, nil
}
clientNew, err := newClient(s.etcdClient, s.opts, electionID)
if err != nil {
return nil, err
}
s.Lock()
defer s.Unlock()
cl, ok = s.clients[electionID]
if ok {
// another client was created between RLock and now, close new one
go clientNew.close()
return cl, nil
}
s.clients[electionID] = clientNew
return clientNew, nil
}
func (s *multiClient) Campaign(electionID string, opts services.CampaignOptions) (<-chan campaign.Status, error) {
if opts == nil {
return nil, errors.New("cannot pass nil campaign options")
}
if s.isClosed() {
return nil, errClientClosed
}
client, err := s.getOrCreateClient(electionID)
if err != nil {
return nil, err
}
return client.campaign(opts)
}
func (s *multiClient) Resign(electionID string) error {
if s.isClosed() {
return errClientClosed
}
s.RLock()
cl, ok := s.clients[electionID]
s.RUnlock()
if !ok {
return fmt.Errorf("no election with ID '%s' to resign", electionID)
}
return cl.resign()
}
func (s *multiClient) Leader(electionID string) (string, error) {
if s.isClosed() {
return "", errClientClosed
}
// always create a client so we can check election statuses without
// campaigning
client, err := s.getOrCreateClient(electionID)
if err != nil {
return "", err
}
return client.leader()
}
func (s *multiClient) Observe(electionID string) (<-chan string, error) {
if s.isClosed() {
return nil, errClientClosed
}
cl, err := s.getOrCreateClient(electionID)
if err != nil {
return nil, err
}
return cl.observe()
}