github.com/mre-fog/trillianxx@v1.1.2-0.20180615153820-ae375a99d36a/util/election/election.go (about) 1 // Copyright 2017 Google Inc. All Rights Reserved. 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 15 // Package election provides implementation of master election and tracking, as 16 // well as interface for plugging in a custom underlying mechanism. 17 package election 18 19 import ( 20 "context" 21 "errors" 22 ) 23 24 // MasterElection provides operations for determining if a local instance is 25 // the current master for a particular election. 26 type MasterElection interface { 27 // Start kicks off this instance's participation in master election. 28 Start(context.Context) error 29 // WaitForMastership blocks until the current instance is the master. 30 WaitForMastership(context.Context) error 31 // IsMaster returns whether the current instance is the master. 32 IsMaster(context.Context) (bool, error) 33 // Resign releases mastership, and stops this instance from participating in 34 // the master election. 35 Resign(context.Context) error 36 // Close releases all the resources associated with this MasterElection. 37 Close(context.Context) error 38 // GetCurrentMaster returns the instance ID of the current elected master, if 39 // any. Implementations should allow election participants to specify their 40 // instance ID string, participants should ensure that it is unique to them. 41 // If there is currently no master, ErrNoMaster will be returned. 42 GetCurrentMaster(context.Context) (string, error) 43 } 44 45 // ErrNoMaster indicates that there is currently no master elected. 46 var ErrNoMaster = errors.New("no master") 47 48 // Factory encapsulates the creation of a MasterElection instance for a treeID. 49 type Factory interface { 50 NewElection(ctx context.Context, treeID int64) (MasterElection, error) 51 } 52 53 // NoopElection is a stub implementation that tells every instance that it is master. 54 type NoopElection struct { 55 treeID int64 56 instanceID string 57 } 58 59 // Start kicks off the process of mastership election. 60 func (ne *NoopElection) Start(ctx context.Context) error { 61 return nil 62 } 63 64 // WaitForMastership blocks until the current instance is master for this election. 65 func (ne *NoopElection) WaitForMastership(ctx context.Context) error { 66 return nil 67 } 68 69 // IsMaster returns whether the current instance is master. 70 func (ne *NoopElection) IsMaster(ctx context.Context) (bool, error) { 71 return true, nil 72 } 73 74 // Resign releases mastership. 75 func (ne *NoopElection) Resign(ctx context.Context) error { 76 return nil 77 } 78 79 // Close permanently stops the mastership election process. 80 func (ne *NoopElection) Close(ctx context.Context) error { 81 return nil 82 } 83 84 // GetCurrentMaster returns ne.instanceID 85 func (ne *NoopElection) GetCurrentMaster(ctx context.Context) (string, error) { 86 return ne.instanceID, nil 87 } 88 89 // NoopFactory creates NoopElection instances. 90 type NoopFactory struct { 91 InstanceID string 92 } 93 94 // NewElection creates a specific NoopElection instance. 95 func (nf NoopFactory) NewElection(ctx context.Context, treeID int64) (MasterElection, error) { 96 return &NoopElection{instanceID: nf.InstanceID, treeID: treeID}, nil 97 }