Documentation
¶
Index ¶
- Constants
- Variables
- func SendToLeader(ctx context.Context, op *Op, m []byte, args ...*SendToLeaderArgs) ([]byte, error)
- type BroadcastArgs
- type BroadcastOutput
- type FnMsgHandler
- type KeyValue
- type LogItem
- type Op
- func (op *Op) Broadcast(ctx context.Context, msg []byte, args ...BroadcastArgs) []BroadcastOutput
- func (op *Op) HostPort() string
- func (op *Op) IsRunning() bool
- func (op *Op) Members() []string
- func (op *Op) Name() string
- func (op *Op) NewSoS(name string, opts ...*SoSOptions) *SoS
- func (op *Op) Run(ctx context.Context, done ...chan error) error
- func (op *Op) Send(ctx context.Context, msg []byte) ([]byte, error)
- func (op *Op) StreamBroadcast(ctx context.Context, args ...StreamBroadcastArgs) (*StreamBroadcastOutput, error)
- func (op *Op) StreamToLeader(ctx context.Context) (*StreamToLeaderOutput, error)
- func (op *Op) String() string
- type Option
- func WithBroadcastHandler(d any, h FnMsgHandler) Option
- func WithBroadcastStreamChannels(in chan *StreamMessage, out chan *StreamMessage) Option
- func WithDuration(v int64) Option
- func WithGroupSyncInterval(v time.Duration) Option
- func WithGrpcHostPort(v string) Option
- func WithLeaderCallback(d any, f spindle.FnLeaderCallback) Option
- func WithLeaderHandler(d any, h FnMsgHandler) Option
- func WithLeaderStreamChannels(in chan *StreamMessage, out chan *StreamMessage) Option
- func WithLogger(v *log.Logger) Option
- type Reader
- type SendToLeaderArgs
- type SoS
- type SoSOptions
- type StreamBroadcastArgs
- type StreamBroadcastOutput
- type StreamMessage
- type StreamToLeaderOutput
- type Writer
Constants ¶
const ( CmdLeader = "LDR" // for leader confirmation, reply="ACK" CmdWrite = "PUT" // write key/value, fmt="PUT <base64(payload)> [noappend]" CmdSend = "SND" // member to leader, fmt="SND <base64(payload)>" CmdPing = "HEY" // heartbeat to indicate availability, fmt="HEY [id]" CmdMembers = "MEM" // members info from leader to all, fmt="MEM base64(JSON(members))" CmdBroadcast = "ALL" // broadcast to all, fmt="ALL base64(payload)" CmdAck = "ACK" // generic reply, fmt="ACK"|"ACK base64(err)"|"ACK base64(JSON(members))" CmdSemaphore = "SEM" // create semaphore, fmt="SEM {name} {limit} {caller}, reply="ACK" CmdSemAcquire = "SEA" // acquire semaphore, fmt="SEA {name} {caller}", reply="ACK[ base64([0:|1:]err)]" (0=final,1=retry) CmdSemRelease = "SER" // release semaphore, fmt="SER {name} {caller}" FlagNoAppend = "noappend" )
Variables ¶
Functions ¶
func SendToLeader ¶
SendToLeader is a wrapper to hedge.Send() with builtin retry mechanisms.
Types ¶
type BroadcastArgs ¶
type BroadcastArgs struct {
SkipSelf bool // if true, skip broadcasting to self
Out chan BroadcastOutput
}
type BroadcastOutput ¶
type KeyValue ¶
type KeyValue struct {
Key string `json:"key"`
Value string `json:"value"`
Timestamp time.Time `json:"timestamp"` // read-only, populated when Get()
}
KeyValue is for Put()/Get() callers.
type Op ¶
type Op struct {
*spindle.Lock // handles our distributed lock
// contains filtered or unexported fields
}
Op is our main instance for hedge operations.
func New ¶
New creates an instance of Op. hostPort can be in "ip:port" format, or ":port" format, in which case the IP part will be resolved internally, or empty, in which case port 8080 will be used. The internal spindle object's lock table name will be lockTable, and lockName is the lock name. logTable will serve as our append-only, distributed key/value storage table. If logTable is empty, Put, Get, and Semaphore features will be disabled.
func (*Op) Broadcast ¶
func (op *Op) Broadcast(ctx context.Context, msg []byte, args ...BroadcastArgs) []BroadcastOutput
Broadcast sends msg to all nodes (send to all). Any node can broadcast messages, including the leader itself. Note that this is best-effort basis only; by the time you call this API, the handler might not have all the active members in record yet, as is the usual situation with k8s deployments, where pods come and go, and our internal heartbeat protocol hasn't been completed yet. This call will also block until it receives all the reply from all nodes' broadcast handlers.
If args[].Out is set, the output will be streamed to that channel instead. Useful if you prefer a streamed output (as reply comes) instead of waiting for all replies before returning. If set, the return value (output slice) will be set to empty []. Also, close() will be called on the Out channel to indicate streaming end.
func (*Op) NewSoS ¶
func (op *Op) NewSoS(name string, opts ...*SoSOptions) *SoS
NewSoS returns an object for writing data to spill-over storage across the cluster. The order of writing is local memory, local disk, other pod's memory, other pod's disk, and so on.
func (*Op) Run ¶
Run starts the main handler. It blocks until ctx is cancelled, optionally sending an error message to done when finished.
func (*Op) Send ¶
Send sends msg to the current leader. Any node can send messages, including the leader itself (send to self). It also blocks until it receives the reply from the leader's message handler.
func (*Op) StreamBroadcast ¶
func (op *Op) StreamBroadcast(ctx context.Context, args ...StreamBroadcastArgs) (*StreamBroadcastOutput, error)
StreamBroadcast returns input and output channels for doing streaming broadcasts. Any node can broadcast messages, including the leader itself. Note that this is best-effort basis only; by the time you call this API, the handler might not have all the active members in record yet, as is the usual situation with k8s deployments, where pods come and go, and our internal heartbeat protocol hasn't been completed yet. This call will also block until it receives all the reply from all nodes' broadcast handlers.
To use the channels, send your request message(s) to the input channel, close it (i.e. close(input)), then read the replies from the output channels. This function will close all output channels when done.
StreamBroadcast is sequential in the sense that you need to send all your input messages first before getting any response from all the nodes.
func (*Op) StreamToLeader ¶
func (op *Op) StreamToLeader(ctx context.Context) (*StreamToLeaderOutput, error)
StreamToLeader returns an input and output channels for streaming to leader. To use the channels, send your request message(s) to the input channel, close it (i.e. close(input)), then read the replies from the output channel. This function will close the output channel when done.
StreamToLeader is sequential in the sense that you need to send all your input messages first before getting any response from the leader.
type Option ¶
type Option interface {
Apply(*Op)
}
func WithBroadcastHandler ¶
func WithBroadcastHandler(d any, h FnMsgHandler) Option
WithBroadcastHandler sets the node's callback function for broadcast messages from anyone in the group using the Broadcast(...) API. Any arbitrary data represented by d will be passed to the callback h every time it is called. If d is nil, the default callback data will be the *Op object itself. The handler's returning []byte will serve as reply.
A nil broadcast handler disables the internal heartbeat function.
func WithBroadcastStreamChannels ¶
func WithBroadcastStreamChannels(in chan *StreamMessage, out chan *StreamMessage) Option
WithBroadcastStreamChannels sets the streaming input and output channels for broadcasting messages to all nodes. All incoming stream messages will be sent to the `in` channel. A nil message indicates the end of the streaming data. After sending all messages to `in`, the handler will then listen to the `out` channel for reply messages. A nil message indicates the end of the reply stream.
func WithDuration ¶
WithDuration sets Op's internal spindle object's lease duration in milliseconds. Defaults to 30000ms (30s) when not set. Minimum value is 2000ms (2s).
func WithGroupSyncInterval ¶
WithGroupSyncInterval sets the internal interval timeout to sync membership within the group in seconds. If not set, defaults to 30s. Minimum value is 2s.
func WithGrpcHostPort ¶
WithGrpcHostPort sets Op's internal grpc host/port address. Defaults to the internal TCP host:port+1.
func WithLeaderCallback ¶
func WithLeaderCallback(d any, f spindle.FnLeaderCallback) Option
WithLeaderCallback sets the node's callback function that will be called when a leader node selected (or deselected). The msg arg for f will be set to either 0 or 1.
func WithLeaderHandler ¶
func WithLeaderHandler(d any, h FnMsgHandler) Option
WithLeaderHandler sets the node's callback function when it is the current leader and when members send messages to it using the Send(...) API. Any arbitrary data represented by d will be passed to the callback h every time it is called. If d is nil, the default callback data will be the *Op object itself. The handler's returning []byte will serve as reply.
Typical flow would be:
- Any node (including the leader) calls the Send(...) API.
- The current leader handles the call by reading the input.
- Leader will then call FnLeaderHandler, passing the arbitrary data along with the message.
- FnLeaderHandler will process the data as leader, then returns the reply to the calling member.
func WithLeaderStreamChannels ¶
func WithLeaderStreamChannels(in chan *StreamMessage, out chan *StreamMessage) Option
WithLeaderStreamChannels sets the streaming input and output channels for sending streaming messages to the leader. All incoming stream messages to the leader will be sent to the `in` channel. A nil message indicates the end of the streaming data. After sending all messages to `in`, the handler will then listen to the `out` channel for reply messages. A nil message indicates the end of the reply stream.
func WithLogger ¶
WithLogger sets Op's logger object. Can be silenced by setting v to:
log.New(io.Discard, "", 0)
type Reader ¶
type SendToLeaderArgs ¶
type SendToLeaderArgs struct {
// Number of retry attempts to contact the leader.
// Defaults to 10. If set to a negative number, it
// will retry forever.
Retries int
}
type SoS ¶
type SoS struct {
sync.Mutex
Name string // the name of this instance
// contains filtered or unexported fields
}
SoS (Spillover-Store) represents an object for spill-over (or stitched) storage. Useful for load-process-discard types of data processing. The order of storage priority is local memory, local disk, other pod's memory, other pod's disk, and so on.
Limitation: At the moment, it's not allowed to reuse a name for SOS once it's used and closed within hedge's lifetime.
type SoSOptions ¶
type SoSOptions struct {
// MemLimit sets the memory limit in bytes to be used per node.
MemLimit uint64
// DiskLimit sets the disk limit in bytes to be used per node.
DiskLimit uint64
// Expiration sets the TTL (time-to-live) of the backing storage.
// If not set, the default is 30s.
Expiration int64
}
type StreamBroadcastArgs ¶
type StreamBroadcastArgs struct {
SkipSelf bool // if true, skip broadcasting to self
}
type StreamBroadcastOutput ¶
type StreamBroadcastOutput struct {
In chan *StreamMessage
Outs map[string]chan *StreamMessage
}
type StreamMessage ¶
type StreamToLeaderOutput ¶
type StreamToLeaderOutput struct {
In chan *StreamMessage `json:"in"`
Out chan *StreamMessage `json:"out"`
}