@yarkivaev/scada 2.7.0 → 2.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (37) hide show
  1. package/db/migrations/C0008__segment_write_receipts_grants.sql +1 -0
  2. package/db/migrations/E0019__segment_write_fence.sql +36 -0
  3. package/db/migrations/E0020__segment_write_fence_grants.sql +4 -0
  4. package/db/migrations/V0015__segment_write_receipts.sql +7 -0
  5. package/index.js +2 -0
  6. package/package.json +1 -1
  7. package/src/application/supervisorSink.js +15 -5
  8. package/src/domain/segment/segmentCoalesce.js +2 -1
  9. package/src/infrastructure/http/edge/edgeApi.js +1 -1
  10. package/src/infrastructure/http/edge/routes/history/historyRoutes.js +8 -3
  11. package/src/infrastructure/ingest/codecs/segmentCodec.js +26 -4
  12. package/src/infrastructure/ingest/db/migrationProfile.js +4 -1
  13. package/src/infrastructure/ingest/pipelines/segmentPipeline.js +11 -5
  14. package/src/infrastructure/ingest/sinks/fencedSegments.js +117 -0
  15. package/src/infrastructure/persistence/pg/history.js +4 -3
  16. package/src/infrastructure/persistence/pg/segmentWriteFence.js +112 -0
  17. package/supervisor-engine/src/main/scala/scada/stream/worker/OutputScope.scala +13 -0
  18. package/supervisor-engine/src/main/scala/scada/stream/worker/StreamWorker.scala +17 -1
  19. package/supervisor-engine/src/main/scala/scada/stream/worker/postgres/WorkerDatabase.scala +7 -0
  20. package/supervisor-engine/src/main/scala/scada/supervisor/core/ReadingWorker.scala +77 -0
  21. package/supervisor-engine/src/main/scala/scada/supervisor/core/Segmentation.scala +24 -13
  22. package/supervisor-engine/src/main/scala/scada/supervisor/core/Segments.scala +2 -0
  23. package/supervisor-engine/src/main/scala/scada/supervisor/infra/state/CheckpointSegments.scala +2 -0
  24. package/supervisor-engine/src/main/scala/scada/supervisor/infra/stomp/ClassifyQueue.scala +2 -2
  25. package/supervisor-engine/src/main/scala/scada/supervisor/infra/stomp/StompReadings.scala +0 -1
  26. package/supervisor-engine/src/main/scala/scada/supervisor/infra/stomp/StompSegments.scala +17 -10
  27. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/ClassifyStreams.scala +57 -20
  28. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/EngineApp.scala +6 -2
  29. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/HistoryBus.scala +37 -5
  30. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/HistoryClient.scala +9 -2
  31. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/HistoryRun.scala +28 -30
  32. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/LocalHistory.scala +117 -31
  33. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/{SupervisorRuntime.scala → ProcessingApplication.scala} +101 -98
  34. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/RuntimeProfile.scala +5 -6
  35. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/TimelineWriter.scala +81 -0
  36. package/supervisor-engine/src/main/scala/scada/supervisor/runtime/WriterLease.scala +24 -5
  37. package/supervisor-engine/src/main/scala/scada/supervisor/core/Supervisor.scala +0 -66
@@ -135,10 +135,18 @@ object WriterLease:
135
135
 
136
136
  /** Last generation issued for this machine, or zero when the lock has never been held. */
137
137
  private def counter(connection: Connection, machine: String): Long =
138
- val created = connection.prepareStatement("CREATE TABLE IF NOT EXISTS classifier_generation (machine text PRIMARY KEY, generation bigint NOT NULL)")
139
- try
140
- val _ = created.execute()
141
- finally created.close()
138
+ try currentGeneration(connection, machine)
139
+ catch
140
+ case error: java.sql.SQLException if error.getSQLState == "42P01" =>
141
+ val created = connection.prepareStatement(
142
+ "CREATE TABLE IF NOT EXISTS classifier_generation (machine text PRIMARY KEY, generation bigint NOT NULL, holder_pid integer)"
143
+ )
144
+ try
145
+ val _ = created.execute()
146
+ finally created.close()
147
+ currentGeneration(connection, machine)
148
+
149
+ private def currentGeneration(connection: Connection, machine: String): Long =
142
150
  val query = connection.prepareStatement("SELECT generation FROM classifier_generation WHERE machine = ?")
143
151
  try
144
152
  query.setString(1, machine)
@@ -150,7 +158,18 @@ object WriterLease:
150
158
 
151
159
  /** Stores the generation this session just issued so the next hold is greater. */
152
160
  private def remember(connection: Connection, machine: String, generation: Long): Unit =
153
- val query = connection.prepareStatement("INSERT INTO classifier_generation (machine, generation) VALUES (?, ?) ON CONFLICT (machine) DO UPDATE SET generation = EXCLUDED.generation")
161
+ val stamped =
162
+ "INSERT INTO classifier_generation (machine, generation, holder_pid) VALUES (?, ?, pg_backend_pid()) " +
163
+ "ON CONFLICT (machine) DO UPDATE SET generation = EXCLUDED.generation, holder_pid = EXCLUDED.holder_pid"
164
+ try writeGeneration(connection, machine, generation, stamped)
165
+ catch
166
+ case error: java.sql.SQLException if error.getSQLState == "42703" =>
167
+ val legacy = "INSERT INTO classifier_generation (machine, generation) VALUES (?, ?) " +
168
+ "ON CONFLICT (machine) DO UPDATE SET generation = EXCLUDED.generation"
169
+ writeGeneration(connection, machine, generation, legacy)
170
+
171
+ private def writeGeneration(connection: Connection, machine: String, generation: Long, sql: String): Unit =
172
+ val query = connection.prepareStatement(sql)
154
173
  try
155
174
  query.setString(1, machine)
156
175
  query.setLong(2, generation)
@@ -1,66 +0,0 @@
1
- package scada.supervisor.core
2
-
3
- import cats.effect.Async
4
- import cats.syntax.all.*
5
- import fs2.Stream
6
- import scala.concurrent.duration.*
7
-
8
- /**
9
- * Per-machine processing loop. Takes readings in, emits
10
- * [[TaggedSegment]]s through a [[Segments]] collection as each
11
- * segment opens and closes.
12
- *
13
- * The outbound stream is supplied by the caller so the supervisor
14
- * stays transport-agnostic (live MQTT, replay, or test feed all plug
15
- * in through the same `readings` parameter).
16
- *
17
- * A restored pending island is written before live readings. While that
18
- * island stays open, a one-second pulse advances `end` so a silent
19
- * machine still heartbeats the tape instead of leaving a hole until now.
20
- *
21
- * {{{
22
- * Supervisor[F](segmentation, tagging, segments).run(machine, readings, None)
23
- * }}}
24
- */
25
- final class Supervisor[F[_]: Async](
26
- segmentation: Segmentation[F],
27
- tagging: Tagging,
28
- segments: Segments[F],
29
- phases: CyclePhases = CyclePhases.empty,
30
- customRun: Option[(MachineConfig, Stream[F, Reading], Segments[F]) => F[Unit]] = None
31
- ):
32
- def run(machine: MachineConfig, readings: Stream[F, Reading], initial: Option[SegmentCursor] = None): F[Unit] =
33
- if machine.mode.isDefined then
34
- customRun.getOrElse(throw IllegalStateException("Machine mode has no configured runner"))(machine,readings,segments)
35
- else
36
- val classified = readings
37
- .through(segmentation.flow(machine, initial))
38
- .evalMap(emission => write(machine, tagging.resolve(machine.tags, emission.value)))
39
- val beats = Stream.awakeEvery[F](1.second).evalMap(_ => pulse(machine)).drain
40
- open(machine, initial) *> classified.mergeHaltL(beats).compile.drain
41
-
42
- private def open(machine: MachineConfig, initial: Option[SegmentCursor]): F[Unit] =
43
- initial.fold(Async[F].unit): cursor =>
44
- write(machine, TaggedSegment(Segment(cursor.name, cursor.start, cursor.end, 0.0), Nil, Nil, Map.empty))
45
-
46
- private def pulse(machine: MachineConfig): F[Unit] =
47
- segments.list(machine.id).flatMap: rows =>
48
- Async[F].realTime.map(_.toMillis).flatMap: millis =>
49
- val now = Timestamp(millis.toDouble / 1000.0)
50
- val open = rows.filter(_.segment.duration == 0).sortBy(_.segment.start.epoch).lastOption
51
- open.fold(cover(machine, rows, now))(tagged => extend(machine, tagged, now))
52
-
53
- private def extend(machine: MachineConfig, tagged: TaggedSegment, now: Timestamp): F[Unit] =
54
- if now.isAfter(tagged.segment.end) then write(machine, tagged.copy(segment = tagged.segment.copy(end = now)))
55
- else Async[F].unit
56
-
57
- private def cover(machine: MachineConfig, rows: List[TaggedSegment], now: Timestamp): F[Unit] =
58
- rows.sortBy(_.segment.end.epoch).lastOption.fold(Async[F].unit): last =>
59
- if !now.isAfter(last.segment.end) then Async[F].unit
60
- else write(machine, TaggedSegment(Segment(last.segment.name, last.segment.end, now, 0.0), Nil, Nil, Map.empty))
61
-
62
- private def write(machine: MachineConfig, tagged: TaggedSegment): F[Unit] =
63
- if phases.isEmpty then segments.upsert(machine.id, tagged)
64
- else
65
- segments.list(machine.id).flatMap: all =>
66
- segments.upsert(machine.id, phases.narrow(tagged, all))