github.com/swiftstack/proxyfs@v0.0.0-20201223034610-5434d919416e/transitions/api.go (about)

     1  package transitions
     2  
     3  import (
     4  	"github.com/swiftstack/ProxyFS/conf"
     5  )
     6  
     7  // Callbacks is the interface implemented by each package desiring notification of
     8  // configuration changes. Each such package should implement a struct with pointer
     9  // receivers for each API listed below even when there is no interest in being
    10  // notified of a particular condition.
    11  //
    12  // By calling transitions.Register() in the package's init() func, the proper order
    13  // of registration will be ensured. In specific, the following callbacks will be
    14  // issued in the same order as package init() func calls have registered:
    15  //
    16  //   Up()
    17  //   VolumeGroupCreated()
    18  //   VolumeGroupMoved()
    19  //   VolumeCreated()
    20  //   VolumeMoved()
    21  //   ServeVolume()
    22  //   SignaledFinish()
    23  //
    24  // By contrast, the following callbacks will be issued in the reverse order as package
    25  // init() func calls have registered:
    26  //
    27  //   SignaledStart()
    28  //   UnserveVolume()
    29  //   VolumeDestroyed()
    30  //   VolumeGroupDestroyed()
    31  //   Down()
    32  //
    33  type Callbacks interface {
    34  	Up(confMap conf.ConfMap) (err error)
    35  	VolumeGroupCreated(confMap conf.ConfMap, volumeGroupName string, activePeer string, virtualIPAddr string) (err error)
    36  	VolumeGroupMoved(confMap conf.ConfMap, volumeGroupName string, activePeer string, virtualIPAddr string) (err error)
    37  	VolumeGroupDestroyed(confMap conf.ConfMap, volumeGroupName string) (err error)
    38  	VolumeCreated(confMap conf.ConfMap, volumeName string, volumeGroupName string) (err error)
    39  	VolumeMoved(confMap conf.ConfMap, volumeName string, volumeGroupName string) (err error)
    40  	VolumeDestroyed(confMap conf.ConfMap, volumeName string) (err error)
    41  	ServeVolume(confMap conf.ConfMap, volumeName string) (err error)
    42  	UnserveVolume(confMap conf.ConfMap, volumeName string) (err error)
    43  	VolumeToBeUnserved(confMap conf.ConfMap, volumeName string) (err error)
    44  	SignaledStart(confMap conf.ConfMap) (err error)
    45  	SignaledFinish(confMap conf.ConfMap) (err error)
    46  	Down(confMap conf.ConfMap) (err error)
    47  }
    48  
    49  // Register should be called from a package's init() func should the package be interested
    50  // in one or more of the callbacks that they will receive. Each callback func should receive
    51  // a struct implementing the Callbacks interface by reference.
    52  //
    53  // As an example, consider the following:
    54  //
    55  //   package foo
    56  //
    57  //   import "github.com/swiftstack/ProxyFS/conf"
    58  //   import "github.com/swiftstack/ProxyFS/transitions"
    59  //
    60  //   type transitionsCallbackInterfaceStruct struct {
    61  //   }
    62  //
    63  //   var transitionsCallbackInterface transitionsCallbackInterfaceStruct
    64  //
    65  //   func init() {
    66  //       transitions.Register("foo", &transitionsCallbackInterface)
    67  //   }
    68  //
    69  //   func (transitionsCallbackInterface *transitionsCallbackInterfaceStruct) Up(confMap conf.ConfMap) (err error) {
    70  //       // Perform start-up initialization derived from confMap
    71  //       // ...set err at some point
    72  //       return
    73  //   }
    74  //
    75  //   ...
    76  //
    77  // Package foo would also have to provide callbacks for each of the other APIs in
    78  // the transitions.Callbacks interface (returning nil if simply not interested).
    79  //
    80  // A special exception to the need for registration is the package logger. Package
    81  // transitions makes an explicit reference to logging functions in package logger and,
    82  // as such, will perform the registration for package logger itself.
    83  //
    84  func Register(packageName string, callbacks Callbacks) {
    85  	register(packageName, callbacks)
    86  }
    87  
    88  // Up should be called at startup by the main() (or setup func) of each program including
    89  // any of the packages needing callback notifications. This will trigger Up() callbacks
    90  // to each of the packages that have registered with package transitions starting with
    91  // package logger (that was registered automatically by package transitions).
    92  //
    93  // Following the Up() callbacks, the following subset of the callbacks triggered
    94  // by a call to Signaled() will be made as if the prior confMap were empty:
    95  //
    96  //   VolumeGroupCreated() - registration order (for each such volume group)
    97  //   VolumeCreated()      - registration order (for each such volume)
    98  //   ServeVolume()        - registration order (for each such volume)
    99  //   SignaledFinish()     - registration order
   100  //
   101  func Up(confMap conf.ConfMap) (err error) {
   102  	return up(confMap)
   103  }
   104  
   105  // Signaled should be called during execution of a signal handler for e.g. SIGHUP by the
   106  // main() (or monitoring func) of each program including any of the packages needing
   107  // callback notifications. This will potentially trigger multiple of the following
   108  // callbacks to each of the packages that have registered with package transitions.
   109  //
   110  // As part of this call, a determination will be made as to which volumes have migrated
   111  // as well as which volumes have either been created or destroyed. Upon determining
   112  // these volume sets, the following callbacks will be issued to each of the packages
   113  // that have registered with package transitions:
   114  //
   115  //   VolumeToBeUnserved()   - reverse registration order (for each such volume)
   116  //   SignaledStart()        - reverse registration order
   117  //   VolumeGroupCreated()   -         registration order (for each such volume group)
   118  //   VolumeCreated()        -         registration order (for each such volume)
   119  //   UnserveVolume()        - reverse registration order (for each such volume)
   120  //   VolumeGroupMoved()     -         registration order (for each such volume group)
   121  //   VolumeMoved()          -         registration order (for each such volume)
   122  //   ServeVolume()          -         registration order (for each such volume)
   123  //   VolumeDestroyed()      - reverse registration order (for each such volume)
   124  //   VolumeGroupDestroyed() - reverse registration order (for each such volume group)
   125  //   SignaledFinish()       -         registration order
   126  //
   127  func Signaled(confMap conf.ConfMap) (err error) {
   128  	return signaled(confMap)
   129  }
   130  
   131  // Down should be called just before shutdown by the main() (or teardown func) of each
   132  // program including any of the packages needing callback notifications. This will trigger
   133  // Down() callbacks to each of the packages that have registered with package transitions
   134  // ending with package logger (that was registered automatically by package transitions).
   135  //
   136  // Prior to the Down() callbacks, the following subset of the callbacks triggered
   137  // by a call to Signaled() will be made as if the prior confMap were empty:
   138  //
   139  //   VolumeToBeUnserved()   - reverse registration order (for each such volume)
   140  //   SignaledStart()        - reverse registration order
   141  //   UnserveVolume()        - reverse registration order (for each such volume)
   142  //   VolumeDestroyed()      - reverse registration order (for each such volume)
   143  //   VolumeGroupDestroyed() - reverse registration order (for each such volume group)
   144  //
   145  func Down(confMap conf.ConfMap) (err error) {
   146  	return down(confMap)
   147  }
   148  
   149  // UpgradeConfMapIfNeeded should be removed once backwards compatibility is no longer required...
   150  //
   151  func UpgradeConfMapIfNeeded(confMap conf.ConfMap) (err error) {
   152  	return upgradeConfMapIfNeeded(confMap)
   153  }