Repository navigation
Expand file tree
/
Copy pathnode.go
More file actions
182 lines (149 loc) · 5.04 KB
/
Copy pathnode.go
File metadata and controls
182 lines (149 loc) · 5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Package node provides distributed worker pools with supervisors.
package node
import (
"encoding/gob"
"errors"
"fmt"
am "github.com/pancsta/asyncmachine-go/pkg/machine"
"github.com/pancsta/asyncmachine-go/pkg/rpc"
)
const (
// EnvAmNodeLogSupervisor enables extra logging for node supervisor.
EnvAmNodeLogSupervisor = "AM_NODE_LOG_SUPERVISOR"
// EnvAmNodeLogClient enables extra logging for node client.
EnvAmNodeLogClient = "AM_NODE_LOG_CLIENT"
)
// states of a worker
type WorkerState string
const (
StateIniting WorkerState = "initing"
StateRpc WorkerState = "rpc"
StateIdle WorkerState = "idle"
StateBusy WorkerState = "busy"
StateReady WorkerState = "ready"
)
var Pass = am.Pass
// ///// ///// /////
// ///// ERRORS
// ///// ///// /////
// sentinel errors
var (
ErrWorker = errors.New("worker error")
ErrWorkerMissing = errors.New("worker missing")
ErrWorkerHealth = errors.New("worker failed healthcheck")
ErrWorkerConn = errors.New("error starting connection")
ErrWorkerKill = errors.New("error killing worker")
ErrPool = errors.New("pool error")
ErrHeartbeat = errors.New("heartbeat failed")
ErrRpc = errors.New("rpc error")
)
// error mutations
// AddErrWorker wraps an error in the ErrWorker sentinel and adds to a machine.
func AddErrWorker(event *am.Event, mach *am.Machine,
err error, args am.A,
) am.Result {
err = fmt.Errorf("%w: %w", ErrWorker, err)
return mach.EvAddErrState(event, ssS.ErrWorker, err, args)
}
// AddErrWorkerStr wraps a msg in the ErrWorker sentinel and adds to a machine.
// TODO add event param
func AddErrWorkerStr(event *am.Event, mach *am.Machine,
msg string, args am.A,
) am.Result {
err := fmt.Errorf("%w: %s", ErrWorker, msg)
return mach.EvAddErrState(event, ssS.ErrWorker, err, args)
}
// AddErrPool wraps an error in the ErrPool sentinel and adds to a machine.
// TODO add event param
func AddErrPool(event *am.Event, mach *am.Machine,
err error, args am.A,
) am.Result {
wrappedErr := fmt.Errorf("%w: %w", ErrPool, err)
return mach.EvAddErrState(event, ssS.ErrPool, wrappedErr, args)
}
// AddErrPoolStr wraps a msg in the ErrPool sentinel and adds to a machine.
// TODO add event param
func AddErrPoolStr(event *am.Event, mach *am.Machine,
msg string, args am.A,
) am.Result {
err := fmt.Errorf("%w: %s", ErrPool, msg)
return mach.EvAddErrState(event, ssS.ErrPool, err, args)
}
// AddErrRpc wraps an error in the ErrRpc sentinel and adds to a machine.
// TODO add event param
func AddErrRpc(event *am.Event, mach *am.Machine,
err error, args am.A,
) am.Result {
wrappedErr := fmt.Errorf("%w: %w", ErrRpc, err)
return mach.EvAddErrState(event, ssS.ErrNetwork, wrappedErr, args)
}
// ///// ///// /////
// ///// ARGS
// ///// ///// /////
const APrefix = "am_node"
type Args struct {
am.ArgsBase `json:"-"`
}
func (Args) ArgsPrefix() string {
return APrefix
}
// ----- TODO per-state args
// A is a struct for node arguments. It's a typesafe alternative to [am.A].
type A struct {
Args `json:"-"`
// Id is a machine ID.
Id string `log:"id"`
// PublicAddr is the public address of a Supervisor or WorkerRpc.
PublicAddr string `log:"public_addr"`
// LocalAddr is the public address of a Supervisor or WorkerRpc.
LocalAddr string `log:"local_addr"`
// BootAddr is the local address of the Bootstrap machine.
BootAddr string `log:"boot_addr"`
// NodesList is a list of available nodes (supervisors' public RPC addresses).
NodesList []string
// WorkerRpcId is a machine ID of the worker RPC client.
WorkerRpcId string `log:"id"`
// SuperRpcId is a machine ID of the super RPC client.
SuperRpcId string `log:"id"`
// non-rpc fields
// WorkerRpc is the RPC client connected to a WorkerRpc.
WorkerRpc *rpc.Client
// Bootstrap is the RPC machine used to connect WorkerRpc to the Supervisor.
Bootstrap *bootstrap
// Dispose the worker.
Dispose bool
// WorkerAddr is an index for WorkerInfo.
WorkerAddr string
// WorkerInfo describes a worker.
WorkerInfo *workerInfo
// WorkersCh returns a list of workers. This channel has to be buffered.
WorkersCh chan<- []*workerInfo
// WorkerState is a requested state of workers, eg for listings.
WorkerState WorkerState
}
// ARpc is a subset of A, that can be passed over RPC.
type ARpc struct {
Args `json:"-"`
// Id is a machine ID.
Id string `log:"id"`
// PublicAddr is the public address of a Supervisor or Worker.
PublicAddr string `log:"public_addr"`
// LocalAddr is the public address of a Supervisor, Worker, or [bootstrap].
LocalAddr string `log:"local_addr"`
// BootAddr is the local address of the Bootstrap machine.
BootAddr string `log:"boot_addr"`
// NodesList is a list of available nodes (supervisors' public RPC addresses).
NodesList []string
// WorkerRpcId is a machine ID of the worker RPC client.
WorkerRpcId string `log:"worker_rpc_id"`
// SuperRpcId is a machine ID of the super RPC client.
SuperRpcId string `log:"super_rpc_id"`
}
// ----- RPC boilerplate
// ArgsRpc will be available in the REPL.
var ArgsRpc = []am.ArgsApi{ARpc{}}
func init() {
for _, arg := range ArgsRpc {
gob.Register(arg)
}
}