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

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lease
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  )
    11  
    12  const (
    13  	// ApplicationLeadershipNamespace is the namespace used to manage
    14  	// leadership leases.
    15  	ApplicationLeadershipNamespace = "application-leadership"
    16  
    17  	// SingularControllerNamespace is the namespace used to manage
    18  	// controller leases.
    19  	SingularControllerNamespace = "singular-controller"
    20  )
    21  
    22  // ErrClaimDenied indicates that a Claimer.Claim() has been denied.
    23  var ErrClaimDenied = errors.New("lease claim denied")
    24  
    25  // ErrNotHeld indicates that some holder does not hold some lease.
    26  var ErrNotHeld = errors.New("lease not held")
    27  
    28  // ErrWaitCancelled is returned by Claimer.WaitUntilExpired if the
    29  // cancel channel is closed.
    30  var ErrWaitCancelled = errors.New("waiting for lease cancelled by client")
    31  
    32  // Claimer exposes lease acquisition and expiry notification capabilities.
    33  type Claimer interface {
    34  
    35  	// Claim acquires or extends the named lease for the named holder. If it
    36  	// succeeds, the holder is guaranteed to keep the lease until at least
    37  	// duration after the *start* of the call. If it returns ErrClaimDenied,
    38  	// the holder is guaranteed not to have the lease. If it returns any other
    39  	// error, no reasonable inferences may be made.
    40  	Claim(leaseName, holderName string, duration time.Duration) error
    41  
    42  	// WaitUntilExpired returns nil when the named lease is no longer held. If it
    43  	// returns any error, no reasonable inferences may be made. If the supplied
    44  	// cancel channel is non-nil, it can be used to cancel the request; in this
    45  	// case, the method will return ErrWaitCancelled.
    46  	WaitUntilExpired(leaseName string, cancel <-chan struct{}) error
    47  }
    48  
    49  // Pinner describes methods used to manage suspension of lease expiry.
    50  type Pinner interface {
    51  
    52  	// Pin ensures that the current holder of input lease name will not lose
    53  	// the lease to expiry.
    54  	// If there is no current holder of the lease, the next claimant will be
    55  	// the recipient of the pin behaviour.
    56  	// The input entity denotes the party responsible for the
    57  	// pinning operation.
    58  	Pin(leaseName string, entity string) error
    59  
    60  	// Unpin reverses a Pin operation for the same application and entity.
    61  	// Normal expiry behaviour is restored when no entities remain with
    62  	// pins for the application.
    63  	Unpin(leaseName string, entity string) error
    64  
    65  	// Pinned returns all names for pinned leases, with the entities requiring
    66  	// their pinned behaviour.
    67  	Pinned() map[string][]string
    68  }
    69  
    70  // Checker exposes facts about lease ownership.
    71  type Checker interface {
    72  
    73  	// Token returns a Token that can be interrogated at any time to discover
    74  	// whether the supplied lease is currently held by the supplied holder.
    75  	Token(leaseName, holderName string) Token
    76  }
    77  
    78  // Token represents a fact -- but not necessarily a *true* fact -- about some
    79  // holder's ownership of some lease.
    80  type Token interface {
    81  
    82  	// Check returns ErrNotHeld if the lease it represents is not held
    83  	// by the holder it represents. If attempt is 0, trapdoorKey is
    84  	// nil, and Check returns nil, then the token continues to
    85  	// represent a true fact.
    86  	//
    87  	// Attempt is the number of times this check has been tried (not
    88  	// necessarily with this token instance). This enables an
    89  	// implementation to vary how it deals with trapdoorKey across
    90  	// attempts. For example, the raft implementation generates
    91  	// different ops for the first attempt than for subsequent
    92  	// attempts.
    93  	//
    94  	// If the token represents a true fact and trapdoorKey is *not*
    95  	// nil, it will be passed through layers for the attention of the
    96  	// underlying lease.Client implementation. If you need to do this,
    97  	// consult the documentation for the particular Client you're
    98  	// using to determine what key should be passed and what errors
    99  	// that might induce.
   100  	Check(attempt int, trapdoorKey interface{}) error
   101  }
   102  
   103  // Manager describes methods for acquiring objects that manipulate and query
   104  // leases for different models.
   105  type Manager interface {
   106  	Checker(namespace string, modelUUID string) (Checker, error)
   107  	Claimer(namespace string, modelUUID string) (Claimer, error)
   108  	Pinner(namespace string, modelUUID string) (Pinner, error)
   109  }