github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/leadership/interface.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  /*
     5  Package leadership holds code pertaining to application leadership in juju. It's
     6  expected to grow as we're able to extract (e.g.) the Ticket and Tracker
     7  interfaces from worker/leadership; and quite possible the implementations
     8  themselves; but that'll have to wait until it can all be expressed without
     9  reference to non-core code.
    10  */
    11  package leadership
    12  
    13  import (
    14  	"time"
    15  
    16  	"github.com/juju/errors"
    17  )
    18  
    19  // TODO (manadart 2010-10-05) Add interfaces to the end of this line,
    20  // separated by commas, as they become required for mocking in tests.
    21  //go:generate mockgen -package mocks -destination mocks/leadership_mock.go github.com/juju/juju/core/leadership Pinner
    22  
    23  // ErrClaimDenied is the error which will be returned when a
    24  // leadership claim has been denied.
    25  var ErrClaimDenied = errors.New("leadership claim denied")
    26  
    27  // ErrBlockCancelled is returned from BlockUntilLeadershipReleased
    28  // if the client cancels the request by closing the cancel channel.
    29  var ErrBlockCancelled = errors.New("waiting for leadership cancelled by client")
    30  
    31  // Claimer exposes leadership acquisition capabilities.
    32  type Claimer interface {
    33  
    34  	// ClaimLeadership claims leadership of the named application on behalf of the
    35  	// named unit. If no error is returned, leadership will be guaranteed for
    36  	// at least the supplied duration from the point when the call was made.
    37  	ClaimLeadership(applicationId, unitId string, duration time.Duration) error
    38  
    39  	// BlockUntilLeadershipReleased blocks until the named application is known
    40  	// to have no leader, in which case it returns no error; or until the
    41  	// manager is stopped, in which case it will fail.
    42  	BlockUntilLeadershipReleased(applicationId string, cancel <-chan struct{}) (err error)
    43  }
    44  
    45  // Pinner describes methods used to manage suspension of application leadership
    46  // expiry. All methods should be idempotent.
    47  type Pinner interface {
    48  
    49  	// PinLeadership ensures that the leadership of the input application will
    50  	// not expire. The input entity records the party responsible for the
    51  	// pinning operation.
    52  	PinLeadership(applicationId string, entity string) error
    53  
    54  	// UnpinLeadership reverses a PinLeadership operation for the same
    55  	// application and entity. Normal expiry behaviour is restored when no
    56  	// entities remain with pins for the application.
    57  	UnpinLeadership(applicationId string, entity string) error
    58  
    59  	// PinnedLeadership returns a map keyed on pinned application names,
    60  	// with entities that require the application's pinned behaviour.
    61  	PinnedLeadership() map[string][]string
    62  }
    63  
    64  // Token represents a unit's leadership of its application.
    65  type Token interface {
    66  
    67  	// Check returns an error if the condition it embodies no longer
    68  	// holds.  If you pass a non-nil trapdoorKey value into Check, it
    69  	// must be a pointer to data of the correct type, into which the
    70  	// token's content will be copied.
    71  	//
    72  	// The "correct type" is implementation-specific, and no implementation
    73  	// is obliged to accept any non-nil parameter; but methods that return
    74  	// Tokens should explain whether, and how, they will expose their content.
    75  	//
    76  	// In practice, most Token implementations will likely expect *[]txn.Op,
    77  	// so that they can be used to gate mgo/txn-based state changes.
    78  	//
    79  	// If a non-nil trapdoorKey value is passed, attempt should be how
    80  	// many times this check has been tried previously (for example,
    81  	// in a buildTxn function it'll be the transaction attempt). This
    82  	// enables the token to generate different ops for the second
    83  	// attempt (the raft lease implementation uses this).
    84  	Check(attempt int, trapdoorKey interface{}) error
    85  }
    86  
    87  // Checker exposes leadership testing capabilities.
    88  type Checker interface {
    89  
    90  	// LeadershipCheck returns a Token representing the supplied unit's
    91  	// application leadership. The existence of the token does not imply
    92  	// its accuracy; you need to Check() it.
    93  	//
    94  	// This method returns a token that accepts a *[]txn.Op, into which
    95  	// it will (on success) copy mgo/txn operations that can be used to
    96  	// verify the unit's continued leadership as part of another txn.
    97  	LeadershipCheck(applicationName, unitName string) Token
    98  }
    99  
   100  // Ticket is used to communicate leadership status to Tracker clients.
   101  type Ticket interface {
   102  
   103  	// Wait returns true if its Tracker is prepared to guarantee leadership
   104  	// for some period from the ticket request. The guaranteed duration depends
   105  	// upon the Tracker.
   106  	Wait() bool
   107  
   108  	// Ready returns a channel that will be closed when a result is available
   109  	// to Wait(), and is helpful for clients that want to select rather than
   110  	// block on long-waiting tickets.
   111  	Ready() <-chan struct{}
   112  }
   113  
   114  // Tracker allows clients to discover current leadership status by attempting to
   115  // claim it for themselves.
   116  type Tracker interface {
   117  
   118  	// ApplicationName returns the name of the application for which leadership claims
   119  	// are made.
   120  	ApplicationName() string
   121  
   122  	// ClaimDuration returns the duration for which a Ticket's true Wait result
   123  	// is guaranteed valid.
   124  	ClaimDuration() time.Duration
   125  
   126  	// ClaimLeader will return a Ticket which, when Wait()ed for, will return
   127  	// true if leadership is guaranteed for at least the tracker's duration from
   128  	// the time the ticket was issued. Leadership claims should be resolved
   129  	// relatively quickly.
   130  	ClaimLeader() Ticket
   131  
   132  	// WaitLeader will return a Ticket which, when Wait()ed for, will block
   133  	// until the tracker attains leadership.
   134  	WaitLeader() Ticket
   135  
   136  	// WaitMinion will return a Ticket which, when Wait()ed for, will block
   137  	// until the tracker's future leadership can no longer be guaranteed.
   138  	WaitMinion() Ticket
   139  }