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