state.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package state
  2. import (
  3. "sync"
  4. "sync/atomic"
  5. "time"
  6. "google.golang.org/protobuf/proto"
  7. "google.golang.org/protobuf/types/known/timestamppb"
  8. "hth/internal/clock"
  9. hthv1 "hth/internal/gen/hth/api/v1"
  10. "hth/internal/ids"
  11. )
  12. type Store struct {
  13. mu sync.RWMutex
  14. deviceID string
  15. daemonInstanceID string
  16. epoch uint64
  17. seq uint64
  18. controlRev uint64
  19. eventID atomic.Uint64
  20. lifecycle hthv1.DeviceLifecycleState
  21. snap *hthv1.Snapshot
  22. clk clock.Clock
  23. }
  24. func NewStore(deviceID, daemonID string, clk clock.Clock) *Store {
  25. if clk == nil {
  26. clk = clock.Real{}
  27. }
  28. s := &Store{
  29. deviceID: deviceID,
  30. daemonInstanceID: daemonID,
  31. epoch: 1,
  32. controlRev: 1,
  33. lifecycle: hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_STARTING,
  34. clk: clk,
  35. }
  36. s.snap = s.base(nil)
  37. return s
  38. }
  39. func (s *Store) DeviceID() string { return s.deviceID }
  40. func (s *Store) Epoch() uint64 {
  41. s.mu.RLock()
  42. defer s.mu.RUnlock()
  43. return s.epoch
  44. }
  45. func (s *Store) Seq() uint64 {
  46. s.mu.RLock()
  47. defer s.mu.RUnlock()
  48. return s.seq
  49. }
  50. func (s *Store) ControlRevision() uint64 {
  51. s.mu.RLock()
  52. defer s.mu.RUnlock()
  53. return s.controlRev
  54. }
  55. func (s *Store) Lifecycle() hthv1.DeviceLifecycleState {
  56. s.mu.RLock()
  57. defer s.mu.RUnlock()
  58. return s.lifecycle
  59. }
  60. func (s *Store) SetLifecycle(st hthv1.DeviceLifecycleState) {
  61. s.mu.Lock()
  62. s.lifecycle = st
  63. s.mu.Unlock()
  64. }
  65. func (s *Store) BumpEpoch() uint64 {
  66. s.mu.Lock()
  67. defer s.mu.Unlock()
  68. s.epoch++
  69. s.lifecycle = hthv1.DeviceLifecycleState_DEVICE_LIFECYCLE_STATE_UNAVAILABLE
  70. return s.epoch
  71. }
  72. func (s *Store) BumpControlRevision() uint64 {
  73. s.mu.Lock()
  74. defer s.mu.Unlock()
  75. s.controlRev++
  76. return s.controlRev
  77. }
  78. func (s *Store) NextEventID() uint64 {
  79. id := s.eventID.Add(1)
  80. if id == 0 {
  81. id = s.eventID.Add(1)
  82. }
  83. return id
  84. }
  85. func (s *Store) Get() *hthv1.Snapshot {
  86. s.mu.RLock()
  87. defer s.mu.RUnlock()
  88. if s.snap == nil {
  89. return nil
  90. }
  91. return proto.Clone(s.snap).(*hthv1.Snapshot)
  92. }
  93. func (s *Store) Commit(mutator func(*hthv1.Snapshot)) *hthv1.Snapshot {
  94. s.mu.Lock()
  95. defer s.mu.Unlock()
  96. s.seq++
  97. eid := s.eventID.Add(1)
  98. snap := s.base(s.snap)
  99. snap.SnapshotSeq = s.seq
  100. snap.EventId = eid
  101. snap.DeviceEpoch = s.epoch
  102. snap.ControlRevision = s.controlRev
  103. snap.Lifecycle = s.lifecycle
  104. if mutator != nil {
  105. mutator(snap)
  106. }
  107. s.snap = snap
  108. return proto.Clone(snap).(*hthv1.Snapshot)
  109. }
  110. func (s *Store) ReplaceFromReplay(snap *hthv1.Snapshot) *hthv1.Snapshot {
  111. s.mu.Lock()
  112. defer s.mu.Unlock()
  113. cp := proto.Clone(snap).(*hthv1.Snapshot)
  114. cp.EventId = s.eventID.Add(1)
  115. s.seq++
  116. cp.SnapshotSeq = s.seq
  117. s.snap = cp
  118. s.epoch = cp.DeviceEpoch
  119. s.controlRev = cp.ControlRevision
  120. s.lifecycle = cp.Lifecycle
  121. return proto.Clone(cp).(*hthv1.Snapshot)
  122. }
  123. func (s *Store) base(prev *hthv1.Snapshot) *hthv1.Snapshot {
  124. now := s.clk.Now()
  125. out := &hthv1.Snapshot{
  126. DeviceId: s.deviceID,
  127. DaemonInstanceId: s.daemonInstanceID,
  128. DeviceEpoch: s.epoch,
  129. ControlRevision: s.controlRev,
  130. WallTime: timestamppb.New(now),
  131. MonotonicNs: s.clk.MonotonicNS(),
  132. Lifecycle: s.lifecycle,
  133. }
  134. if prev != nil {
  135. out.Measurements = cloneMeas(prev.Measurements)
  136. out.ContinuousTargets = cloneCommanded(prev.ContinuousTargets)
  137. out.LastRequested = cloneRecord(prev.LastRequested)
  138. out.LastAccepted = cloneRecord(prev.LastAccepted)
  139. out.LastDispatched = cloneRecord(prev.LastDispatched)
  140. out.DeviceConfirmation = prev.DeviceConfirmation
  141. out.Lease = cloneLease(prev.Lease)
  142. out.DroppedTelemetryCount = prev.DroppedTelemetryCount
  143. out.ActiveFaults = cloneFaults(prev.ActiveFaults)
  144. out.CaptureWindow = cloneWindow(prev.CaptureWindow)
  145. out.TransportState = prev.TransportState
  146. out.DeviceReadiness = prev.DeviceReadiness
  147. out.SafetyControlMode = prev.SafetyControlMode
  148. }
  149. return out
  150. }
  151. func cloneMeas(in []*hthv1.Measurement) []*hthv1.Measurement {
  152. out := make([]*hthv1.Measurement, 0, len(in))
  153. for _, m := range in {
  154. out = append(out, proto.Clone(m).(*hthv1.Measurement))
  155. }
  156. return out
  157. }
  158. func cloneCommanded(in []*hthv1.CommandedActuator) []*hthv1.CommandedActuator {
  159. if in == nil {
  160. return nil
  161. }
  162. out := make([]*hthv1.CommandedActuator, 0, len(in))
  163. for _, a := range in {
  164. if a == nil {
  165. out = append(out, nil)
  166. continue
  167. }
  168. out = append(out, proto.Clone(a).(*hthv1.CommandedActuator))
  169. }
  170. return out
  171. }
  172. func cloneRecord(in *hthv1.ActionRecord) *hthv1.ActionRecord {
  173. if in == nil {
  174. return nil
  175. }
  176. return proto.Clone(in).(*hthv1.ActionRecord)
  177. }
  178. func cloneLease(in *hthv1.LeaseSummary) *hthv1.LeaseSummary {
  179. if in == nil {
  180. return nil
  181. }
  182. return proto.Clone(in).(*hthv1.LeaseSummary)
  183. }
  184. func cloneFaults(in []*hthv1.Fault) []*hthv1.Fault {
  185. if in == nil {
  186. return nil
  187. }
  188. out := make([]*hthv1.Fault, 0, len(in))
  189. for _, f := range in {
  190. if f == nil {
  191. out = append(out, nil)
  192. continue
  193. }
  194. out = append(out, proto.Clone(f).(*hthv1.Fault))
  195. }
  196. return out
  197. }
  198. func cloneWindow(in *hthv1.TimeWindow) *hthv1.TimeWindow {
  199. if in == nil {
  200. return nil
  201. }
  202. return proto.Clone(in).(*hthv1.TimeWindow)
  203. }
  204. func MissingTemps(n int, now time.Time, reason string) []*hthv1.Measurement {
  205. out := make([]*hthv1.Measurement, 0, n)
  206. for i := 0; i < n; i++ {
  207. out = append(out, &hthv1.Measurement{
  208. Id: ids.TempChannel(i),
  209. Unit: "degC",
  210. Quality: hthv1.DataQuality_DATA_QUALITY_MISSING,
  211. QualityReason: reason,
  212. HostTime: timestamppb.New(now),
  213. SourceTimestampAvailable: false,
  214. })
  215. }
  216. return out
  217. }