-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathclientcommon.go
361 lines (301 loc) · 11.5 KB
/
clientcommon.go
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package internal
import (
"context"
"errors"
"fmt"
"sync"
"github.com/open-telemetry/opamp-go/client/types"
"github.com/open-telemetry/opamp-go/protobufs"
"google.golang.org/protobuf/proto"
)
var (
ErrAgentDescriptionMissing = errors.New("AgentDescription is nil")
ErrAgentDescriptionNoAttributes = errors.New("AgentDescription has no attributes defined")
ErrAgentHealthMissing = errors.New("AgentHealth is nil")
ErrReportsEffectiveConfigNotSet = errors.New("ReportsEffectiveConfig capability is not set")
ErrReportsRemoteConfigNotSet = errors.New("ReportsRemoteConfig capability is not set")
ErrPackagesStateProviderNotSet = errors.New("PackagesStateProvider must be set")
ErrAcceptsPackagesNotSet = errors.New("AcceptsPackages and ReportsPackageStatuses must be set")
errAlreadyStarted = errors.New("already started")
errCannotStopNotStarted = errors.New("cannot stop because not started")
errReportsPackageStatusesNotSet = errors.New("ReportsPackageStatuses capability is not set")
)
// ClientCommon contains the OpAMP logic that is common between WebSocket and
// plain HTTP transports.
type ClientCommon struct {
Logger types.Logger
Callbacks types.Callbacks
// Agent's capabilities defined at Start() time.
Capabilities protobufs.AgentCapabilities
// Client state storage. This is needed if the Server asks to report the state.
ClientSyncedState ClientSyncedState
// PackagesStateProvider provides access to the local state of packages.
PackagesStateProvider types.PackagesStateProvider
// The transport-specific sender.
sender Sender
// True if Start() is successful.
isStarted bool
// Cancellation func for background go routines.
runCancel context.CancelFunc
// True when stopping is in progress.
isStoppingFlag bool
isStoppingMutex sync.RWMutex
// Indicates that the Client is fully stopped.
stoppedSignal chan struct{}
}
// NewClientCommon creates a new ClientCommon.
func NewClientCommon(logger types.Logger, sender Sender) ClientCommon {
return ClientCommon{
Logger: logger, sender: sender, stoppedSignal: make(chan struct{}, 1),
}
}
// PrepareStart prepares the client state for the next Start() call.
// It returns an error if the client is already started, or if the settings are invalid.
func (c *ClientCommon) PrepareStart(
_ context.Context, settings types.StartSettings,
) error {
if c.isStarted {
return errAlreadyStarted
}
c.Capabilities = settings.Capabilities
// According to OpAMP spec this capability MUST be set, since all Agents MUST report status.
c.Capabilities |= protobufs.AgentCapabilities_AgentCapabilities_ReportsStatus
if c.ClientSyncedState.AgentDescription() == nil {
return ErrAgentDescriptionMissing
}
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsHealth != 0 && c.ClientSyncedState.Health() == nil {
return ErrAgentHealthMissing
}
// Prepare remote config status.
if settings.RemoteConfigStatus == nil {
// RemoteConfigStatus is not provided. Start with empty.
settings.RemoteConfigStatus = &protobufs.RemoteConfigStatus{
Status: protobufs.RemoteConfigStatuses_RemoteConfigStatuses_UNSET,
}
}
if err := c.ClientSyncedState.SetRemoteConfigStatus(settings.RemoteConfigStatus); err != nil {
return err
}
// Prepare package statuses.
c.PackagesStateProvider = settings.PackagesStateProvider
var packageStatuses *protobufs.PackageStatuses
if settings.PackagesStateProvider != nil {
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_AcceptsPackages == 0 ||
c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses == 0 {
return ErrAcceptsPackagesNotSet
}
// Set package status from the value previously saved in the PackagesStateProvider.
var err error
packageStatuses, err = settings.PackagesStateProvider.LastReportedStatuses()
if err != nil {
return err
}
} else {
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_AcceptsPackages != 0 ||
c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses != 0 {
return ErrPackagesStateProviderNotSet
}
}
if packageStatuses == nil {
// PackageStatuses is not provided. Start with empty.
packageStatuses = &protobufs.PackageStatuses{}
}
if err := c.ClientSyncedState.SetPackageStatuses(packageStatuses); err != nil {
return err
}
// Prepare callbacks.
c.Callbacks = settings.Callbacks
if c.Callbacks == nil {
// Make sure it is always safe to call Callbacks.
c.Callbacks = types.CallbacksStruct{}
}
if err := c.sender.SetInstanceUid(settings.InstanceUid); err != nil {
return err
}
return nil
}
// Stop stops the client. It returns an error if the client is not started.
func (c *ClientCommon) Stop(ctx context.Context) error {
if !c.isStarted {
return errCannotStopNotStarted
}
c.isStoppingMutex.Lock()
cancelFunc := c.runCancel
c.isStoppingFlag = true
c.isStoppingMutex.Unlock()
cancelFunc()
// Wait until stopping is finished.
select {
case <-ctx.Done():
return ctx.Err()
case <-c.stoppedSignal:
}
return nil
}
// IsStopping returns true if Stop() was called.
func (c *ClientCommon) IsStopping() bool {
c.isStoppingMutex.RLock()
defer c.isStoppingMutex.RUnlock()
return c.isStoppingFlag
}
// StartConnectAndRun initiates the connection with the Server and starts the
// background goroutine that handles the communication unitl client is stopped.
func (c *ClientCommon) StartConnectAndRun(runner func(ctx context.Context)) {
// Create a cancellable context.
runCtx, runCancel := context.WithCancel(context.Background())
c.isStoppingMutex.Lock()
defer c.isStoppingMutex.Unlock()
if c.isStoppingFlag {
// Stop() was called. Don't connect.
runCancel()
return
}
c.runCancel = runCancel
go func() {
defer func() {
// We only return from runner() when we are instructed to stop.
// When returning signal that we stopped.
c.stoppedSignal <- struct{}{}
}()
runner(runCtx)
}()
c.isStarted = true
}
// PrepareFirstMessage prepares the initial state of NextMessage struct that client
// sends when it first establishes a connection with the Server.
func (c *ClientCommon) PrepareFirstMessage(ctx context.Context) error {
cfg, err := c.Callbacks.GetEffectiveConfig(ctx)
if err != nil {
return err
}
c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.AgentDescription = c.ClientSyncedState.AgentDescription()
msg.EffectiveConfig = cfg
msg.RemoteConfigStatus = c.ClientSyncedState.RemoteConfigStatus()
msg.PackageStatuses = c.ClientSyncedState.PackageStatuses()
msg.Capabilities = uint64(c.Capabilities)
},
)
return nil
}
// AgentDescription returns the current state of the AgentDescription.
func (c *ClientCommon) AgentDescription() *protobufs.AgentDescription {
// Return a cloned copy to allow caller to do whatever they want with the result.
return proto.Clone(c.ClientSyncedState.AgentDescription()).(*protobufs.AgentDescription)
}
// SetAgentDescription sends a status update to the Server with the new AgentDescription
// and remembers the AgentDescription in the client state so that it can be sent
// to the Server when the Server asks for it.
func (c *ClientCommon) SetAgentDescription(descr *protobufs.AgentDescription) error {
// store the Agent description to send on reconnect
if err := c.ClientSyncedState.SetAgentDescription(descr); err != nil {
return err
}
c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.AgentDescription = c.ClientSyncedState.AgentDescription()
},
)
c.sender.ScheduleSend()
return nil
}
// SetHealth sends a status update to the Server with the new AgentHealth
// and remembers the AgentHealth in the client state so that it can be sent
// to the Server when the Server asks for it.
func (c *ClientCommon) SetHealth(health *protobufs.AgentHealth) error {
// store the AgentHealth to send on reconnect
if err := c.ClientSyncedState.SetHealth(health); err != nil {
return err
}
c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.Health = c.ClientSyncedState.Health()
},
)
c.sender.ScheduleSend()
return nil
}
// UpdateEffectiveConfig fetches the current local effective config using
// GetEffectiveConfig callback and sends it to the Server using provided Sender.
func (c *ClientCommon) UpdateEffectiveConfig(ctx context.Context) error {
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsEffectiveConfig == 0 {
return ErrReportsEffectiveConfigNotSet
}
// Fetch the locally stored config.
cfg, err := c.Callbacks.GetEffectiveConfig(ctx)
if err != nil {
return fmt.Errorf("GetEffectiveConfig failed: %w", err)
}
// Send it to the Server.
c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.EffectiveConfig = cfg
},
)
// TODO: if this call is coming from OnMessage callback don't schedule the send
// immediately, wait until the end of OnMessage to send one message only.
c.sender.ScheduleSend()
// Note that we do not store the EffectiveConfig anywhere else. It will be deleted
// from NextMessage when the message is sent. This avoids storing EffectiveConfig
// in memory for longer than it is needed.
return nil
}
// SetRemoteConfigStatus sends a status update to the Server if the new RemoteConfigStatus
// is different from the status we already have in the state.
// It also remembers the new RemoteConfigStatus in the client state so that it can be
// sent to the Server when the Server asks for it.
func (c *ClientCommon) SetRemoteConfigStatus(status *protobufs.RemoteConfigStatus) error {
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsRemoteConfig == 0 {
return ErrReportsRemoteConfigNotSet
}
if status.LastRemoteConfigHash == nil {
return errLastRemoteConfigHashNil
}
statusChanged := !proto.Equal(c.ClientSyncedState.RemoteConfigStatus(), status)
// Remember the new status.
if err := c.ClientSyncedState.SetRemoteConfigStatus(status); err != nil {
return err
}
if statusChanged {
// Let the Server know about the new status.
c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.RemoteConfigStatus = c.ClientSyncedState.RemoteConfigStatus()
},
)
// TODO: if this call is coming from OnMessage callback don't schedule the send
// immediately, wait until the end of OnMessage to send one message only.
c.sender.ScheduleSend()
}
return nil
}
// SetPackageStatuses sends a status update to the Server if the new PackageStatuses
// are different from the ones we already have in the state.
// It also remembers the new PackageStatuses in the client state so that it can be
// sent to the Server when the Server asks for it.
func (c *ClientCommon) SetPackageStatuses(statuses *protobufs.PackageStatuses) error {
if c.Capabilities&protobufs.AgentCapabilities_AgentCapabilities_ReportsPackageStatuses == 0 {
return errReportsPackageStatusesNotSet
}
if statuses.ServerProvidedAllPackagesHash == nil {
return errServerProvidedAllPackagesHashNil
}
statusChanged := !proto.Equal(c.ClientSyncedState.PackageStatuses(), statuses)
if err := c.ClientSyncedState.SetPackageStatuses(statuses); err != nil {
return err
}
// Check if the new status is different from the previous.
if statusChanged {
// Let the Server know about the new status.
c.sender.NextMessage().Update(
func(msg *protobufs.AgentToServer) {
msg.PackageStatuses = c.ClientSyncedState.PackageStatuses()
},
)
// TODO: if this call is coming from OnMessage callback don't schedule the send
// immediately, wait until the end of OnMessage to send one message only.
c.sender.ScheduleSend()
}
return nil
}