github.com/m3db/m3@v1.5.0/src/cmd/services/m3coordinator/downsample/leader_local.go (about) 1 // Copyright (c) 2018 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 downsample 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 32 // localLeaderService provides a mocked out local leader service so that 33 // we do not need to rely on using an etcd cluster just to elect a leader 34 // for aggregation in-process (which doesn't need leader election at all, 35 // however it is simpler to keep the current aggregator code structured the 36 // way it is which is most natural for accommodating the distributed and 37 // non-in-process aggregation use case). 38 type localLeaderService struct { 39 sync.Mutex 40 id services.ServiceID 41 elections map[string]chan campaign.Status 42 leaders map[string]string 43 } 44 45 func newLocalLeaderService(id services.ServiceID) services.LeaderService { 46 l := &localLeaderService{id: id} 47 l.reset() 48 return l 49 } 50 51 func (l *localLeaderService) reset() { 52 l.Lock() 53 defer l.Unlock() 54 l.elections = make(map[string]chan campaign.Status) 55 l.leaders = make(map[string]string) 56 } 57 58 func (l *localLeaderService) Campaign( 59 electionID string, 60 opts services.CampaignOptions, 61 ) (<-chan campaign.Status, error) { 62 l.Lock() 63 defer l.Unlock() 64 65 campaignCh, ok := l.elections[electionID] 66 if !ok { 67 campaignCh = make(chan campaign.Status, 1) 68 campaignCh <- campaign.Status{State: campaign.Leader} 69 l.elections[electionID] = campaignCh 70 l.leaders[electionID] = l.id.String() 71 } 72 return campaignCh, nil 73 } 74 75 func (l *localLeaderService) push(id string, status campaign.Status) error { 76 l.Lock() 77 defer l.Unlock() 78 79 campaignCh, ok := l.elections[id] 80 if !ok { 81 return fmt.Errorf("no such campaign: %s", id) 82 } 83 84 campaignCh <- status 85 return nil 86 } 87 88 func (l *localLeaderService) Resign(electionID string) error { 89 return l.push(electionID, campaign.Status{State: campaign.Follower}) 90 } 91 92 func (l *localLeaderService) Leader(electionID string) (string, error) { 93 l.Lock() 94 defer l.Unlock() 95 96 leader, ok := l.leaders[electionID] 97 if !ok { 98 return "", fmt.Errorf("no such campaign: %s", electionID) 99 } 100 return leader, nil 101 } 102 103 func (l *localLeaderService) Observe(electionID string) (<-chan string, error) { 104 return nil, errors.New("unimplemented") 105 } 106 107 func (l *localLeaderService) Close() error { 108 l.reset() 109 return nil 110 }