Debian Codegraph @

service/internal/status/status.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package status // import "go.opentelemetry.io/collector/service/internal/status"
5
6 import (
7 "errors"
8 "fmt"
9 "sync"
10
11 "go.opentelemetry.io/collector/component/componentstatus"
12 )
13
14 // onTransitionFunc receives a componentstatus.Event on a successful state transition
15 type onTransitionFunc func(*componentstatus.Event)
16
17 // errInvalidStateTransition is returned for invalid state transitions
18 var errInvalidStateTransition = errors.New("invalid state transition")
19
20 // fsm is a finite state machine that models transitions for component status
21 type fsm struct {
22 current *componentstatus.Event
23 transitions map[componentstatus.Status]map[componentstatus.Status]struct{}
24 onTransition onTransitionFunc
25 }
26
27 // transition will attempt to execute a state transition. If it's successful, it calls the
28 // onTransitionFunc with a Event representing the new state. Returns an error if the arguments
29 // result in an invalid status, or if the state transition is not valid.
30 func (m *fsm) transition(ev *componentstatus.Event) error {
31 if _, ok := m.transitions[m.current.Status()][ev.Status()]; !ok {
32 return fmt.Errorf(
33 "cannot transition from %s to %s: %w",
34 m.current.Status(),
35 ev.Status(),
36 errInvalidStateTransition,
37 )
38 }
39 m.current = ev
40 m.onTransition(ev)
41 return nil
42 }
43
44 // newFSM creates a state machine with all valid transitions for componentstatus.Status.
45 // The initial state is set to componentstatus.StatusNone.
46 func newFSM(onTransition onTransitionFunc) *fsm {
47 return &fsm{
48 current: componentstatus.NewEvent(componentstatus.StatusNone),
49 onTransition: onTransition,
50 transitions: map[componentstatus.Status]map[componentstatus.Status]struct{}{
51 componentstatus.StatusNone: {
52 componentstatus.StatusStarting: {},
53 },
54 componentstatus.StatusStarting: {
55 componentstatus.StatusOK: {},
56 componentstatus.StatusRecoverableError: {},
57 componentstatus.StatusPermanentError: {},
58 componentstatus.StatusFatalError: {},
59 componentstatus.StatusStopping: {},
60 },
61 componentstatus.StatusOK: {
62 componentstatus.StatusRecoverableError: {},
63 componentstatus.StatusPermanentError: {},
64 componentstatus.StatusFatalError: {},
65 componentstatus.StatusStopping: {},
66 },
67 componentstatus.StatusRecoverableError: {
68 componentstatus.StatusOK: {},
69 componentstatus.StatusPermanentError: {},
70 componentstatus.StatusFatalError: {},
71 componentstatus.StatusStopping: {},
72 },
73 componentstatus.StatusPermanentError: {
74 componentstatus.StatusStopping: {},
75 },
76 componentstatus.StatusFatalError: {},
77 componentstatus.StatusStopping: {
78 componentstatus.StatusRecoverableError: {},
79 componentstatus.StatusPermanentError: {},
80 componentstatus.StatusFatalError: {},
81 componentstatus.StatusStopped: {},
82 },
83 componentstatus.StatusStopped: {},
84 },
85 }
86 }
87
88 // NotifyStatusFunc is the receiver of status events after successful state transitions
89 type NotifyStatusFunc func(*componentstatus.InstanceID, *componentstatus.Event)
90
91 // InvalidTransitionFunc is the receiver of invalid transition errors
92 type InvalidTransitionFunc func(error)
93
94 // ServiceStatusFunc is the expected type of ReportStatus
95 type ServiceStatusFunc func(*componentstatus.InstanceID, *componentstatus.Event)
96
97 // ErrStatusNotReady is returned when trying to report status before service start
98 var ErrStatusNotReady = errors.New("report component status is not ready until service start")
99
100 // Reporter handles component status reporting
101 type Reporter interface {
102 ReportStatus(id *componentstatus.InstanceID, ev *componentstatus.Event)
103 ReportOKIfStarting(id *componentstatus.InstanceID)
104 }
105
106 type reporter struct {
107 mu sync.Mutex
108 fsmMap map[*componentstatus.InstanceID]*fsm
109 onStatusChange NotifyStatusFunc
110 onInvalidTransition InvalidTransitionFunc
111 }
112
113 // NewReporter returns a reporter that will invoke the NotifyStatusFunc when a component's status
114 // has changed.
115 func NewReporter(onStatusChange NotifyStatusFunc, onInvalidTransition InvalidTransitionFunc) Reporter {
116 return &reporter{
117 fsmMap: make(map[*componentstatus.InstanceID]*fsm),
118 onStatusChange: onStatusChange,
119 onInvalidTransition: onInvalidTransition,
120 }
121 }
122
123 // ReportStatus reports status for the given InstanceID
124 func (r *reporter) ReportStatus(
125 id *componentstatus.InstanceID,
126 ev *componentstatus.Event,
127 ) {
128 r.mu.Lock()
129 defer r.mu.Unlock()
130
131 if err := r.componentFSM(id).transition(ev); err != nil {
132 r.onInvalidTransition(err)
133 }
134 }
135
136 func (r *reporter) ReportOKIfStarting(id *componentstatus.InstanceID) {
137 r.mu.Lock()
138 defer r.mu.Unlock()
139 fsm := r.componentFSM(id)
140 if fsm.current.Status() == componentstatus.StatusStarting {
141 if err := fsm.transition(componentstatus.NewEvent(componentstatus.StatusOK)); err != nil {
142 r.onInvalidTransition(err)
143 }
144 }
145 }
146
147 // Note: a lock must be acquired before calling this method.
148 func (r *reporter) componentFSM(id *componentstatus.InstanceID) *fsm {
149 fsm, ok := r.fsmMap[id]
150 if !ok {
151 fsm = newFSM(func(ev *componentstatus.Event) { r.onStatusChange(id, ev) })
152 r.fsmMap[id] = fsm
153 }
154 return fsm
155 }
156
157 // NewReportStatusFunc returns a function to be used as ReportStatus for componentstatus.TelemetrySettings
158 func NewReportStatusFunc(
159 id *componentstatus.InstanceID,
160 srvStatus ServiceStatusFunc,
161 ) func(*componentstatus.Event) {
162 return func(ev *componentstatus.Event) {
163 srvStatus(id, ev)
164 }
165 }