@yarkivaev/scada 2.7.1 → 2.9.0
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.
- package/README.md +4 -4
- package/package.json +8 -3
- package/stream-processing/dependencies/stream-worker.jar +0 -0
- package/stream-processing/dependencies/timeline-segmentation.jar +0 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Decision.scala +2 -12
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineConfig.scala +2 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineId.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineMode.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/ReadingWorker.scala +17 -22
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Segments.scala +2 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/Checkpoint.scala +3 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/ClickhouseReadingsCheckpoint.scala +4 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/SupervisorSinkCheckpoint.scala +4 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/AppConfig.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/ConfigLoad.scala +3 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/ConfigProfile.scala +2 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/mqtt/Mqtt.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/mqtt/Sessions.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/AtomicStateFile.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/CheckpointSegments.scala +6 -5
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/SegmentStatePolicy.scala +2 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/Segments.scala +4 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/ClassifyQueue.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/DecisionMessage.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/DurableStomp.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SegmentMessage.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SegmentWirePolicy.scala +2 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SplitFamily.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompConnection.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompDecisions.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompFrame.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompMachineDecisions.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompQueue.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompReadings.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/StompSegments.scala +5 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ClassifyHistory.scala +4 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ClassifyStreams.scala +43 -18
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Close.scala +4 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HealthServer.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HistoryBus.scala +5 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HistoryClient.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/HistoryRun.scala +18 -14
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/LocalHistory.scala +17 -65
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ObservationLead.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Orphans.scala +4 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/PendingIsland.scala +4 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor/runtime/EngineApp.scala → stream-processing/src/main/scala/scada/processing/runtime/ProcessingApp.scala} +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ProcessingApplication.scala +11 -22
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/RuntimeProfile.scala +8 -8
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Seeding.scala +5 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/TimelineWriter.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/stream/worker/postgres → stream-processing/src/main/scala/scada/processing/runtime}/WorkerDatabase.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/WriterEpoch.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/WriterGate.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/WriterLease.scala +4 -21
- package/{supervisor-engine → stream-processing}/src/main/scala/scada/stream/replay/Replay.scala +1 -1
- package/stream-processing/stream-worker.lock +2 -0
- package/stream-processing/timeline-segmentation.lock +2 -0
- package/supervisor-engine/src/main/scala/scada/stream/worker/OutputScope.scala +0 -13
- package/supervisor-engine/src/main/scala/scada/stream/worker/Processor.scala +0 -8
- package/supervisor-engine/src/main/scala/scada/stream/worker/StreamWorker.scala +0 -135
- package/supervisor-engine/src/main/scala/scada/stream/worker/postgres/PgCheckpointStore.scala +0 -83
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Condition.scala +0 -33
- package/supervisor-engine/src/main/scala/scada/supervisor/core/CyclePhases.scala +0 -65
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Emission.scala +0 -19
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Params.scala +0 -15
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Reading.scala +0 -11
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Segment.scala +0 -28
- package/supervisor-engine/src/main/scala/scada/supervisor/core/SegmentDef.scala +0 -14
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Segmentation.scala +0 -63
- package/supervisor-engine/src/main/scala/scada/supervisor/core/SplitFollow.scala +0 -50
- package/supervisor-engine/src/main/scala/scada/supervisor/core/SplitPieces.scala +0 -102
- package/supervisor-engine/src/main/scala/scada/supervisor/core/TagDef.scala +0 -12
- package/supervisor-engine/src/main/scala/scada/supervisor/core/TaggedSegment.scala +0 -19
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Tagging.scala +0 -42
- package/supervisor-engine/src/main/scala/scada/supervisor/core/Timestamp.scala +0 -26
- package/supervisor-engine/src/main/scala/scada/supervisor/core/segmentation/Timeline.scala +0 -43
- package/supervisor-engine/src/main/scala/scada/supervisor/runtime/ClassifierCursors.scala +0 -58
- package/supervisor-engine/vendor/gaps-and-islands/UPSTREAM +0 -4
- package/supervisor-engine/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/Island.scala +0 -40
- package/supervisor-engine/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandEvent.scala +0 -55
- package/supervisor-engine/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsFlow.scala +0 -127
- package/supervisor-engine/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsPolicy.scala +0 -105
- package/supervisor-engine/vendor/temporal-streams/LICENSE +0 -21
- /package/{supervisor-engine/vendor/gaps-and-islands → stream-processing/vendor/temporal-streams}/LICENSE +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/UPSTREAM +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/History.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/LocalStreams.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/Overlay.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/temporal-streams/src/main/scala/io/yarkivaev/temporal/Worker.scala +0 -0
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{HistorySegment, HistoryState}
|
|
3
4
|
import cats.effect.IO
|
|
4
5
|
import cats.effect.std.Semaphore
|
|
5
6
|
import fs2.Stream
|
|
6
|
-
import io.circe.Json
|
|
7
|
-
import io.
|
|
7
|
+
import io.circe.{Codec, Decoder, Encoder, Json}
|
|
8
|
+
import io.circe.generic.semiauto.deriveCodec
|
|
9
|
+
import io.yarkivaev.temporal.{Conflict, Cursor, History, LocalStreams, Observation, Work}
|
|
10
|
+
import scala.collection.immutable.TreeMap
|
|
8
11
|
import java.time.Instant
|
|
9
|
-
import
|
|
10
|
-
import
|
|
12
|
+
import io.yarkivaev.streamworker.postgres.PgCheckpointStore
|
|
13
|
+
import io.yarkivaev.streamworker.{Checkpoint, CheckpointStore, Delivery, ProcessingError, Processor, ProcessorVersion, StepResult, StreamWorker, TaskIdentity}
|
|
14
|
+
import scada.processing.core.MachineConfig
|
|
11
15
|
|
|
12
16
|
/**
|
|
13
17
|
* Machine AMQP classification with independent event-time cursors.
|
|
14
|
-
*
|
|
18
|
+
* Leased workers checkpoint their active tapes and pending writes in PostgreSQL.
|
|
15
19
|
*
|
|
16
20
|
* {{{
|
|
17
21
|
* ClassifyStreams.start(machine, seed, announce).flatMap(_.accept(point))
|
|
@@ -23,22 +27,23 @@ final class ClassifyStreams private (
|
|
|
23
27
|
task: TaskIdentity,
|
|
24
28
|
ports: ClassifyPorts,
|
|
25
29
|
watermark: Option[Long],
|
|
26
|
-
lock: Semaphore[IO]
|
|
30
|
+
lock: Semaphore[IO],
|
|
31
|
+
ownership: Long
|
|
27
32
|
):
|
|
28
33
|
import ClassifyStreams.{Command, version}
|
|
29
34
|
|
|
30
|
-
private val ownership = 1L
|
|
31
35
|
private val processor = new Processor[Unit, StreamState, (Observation, Long), Command]:
|
|
32
36
|
override def step(config: Unit, state: StreamState, input: (Observation, Long))
|
|
33
37
|
: Either[ProcessingError, StepResult[StreamState, Command]] =
|
|
34
38
|
val (point, now) = input
|
|
35
39
|
val (next, routed) = ClassifyStreams.route(state, point, now, machine.window.toLong * 2000)
|
|
36
|
-
Right(StepResult(next, Vector(Command(point,
|
|
40
|
+
Right(StepResult(next, Vector(Command(point, routed))))
|
|
37
41
|
|
|
38
42
|
private val worker = StreamWorker(processor, store, (_, output) =>
|
|
39
43
|
val command = output.value
|
|
40
|
-
|
|
41
|
-
|
|
44
|
+
store.load(task, ownership).flatMap: checkpoint =>
|
|
45
|
+
IO.whenA(command.routed.changed)(publish(checkpoint.state, command.routed.cursor)) *>
|
|
46
|
+
ports.persist(checkpoint.state.pool))
|
|
42
47
|
|
|
43
48
|
def consume(input: Stream[IO, Observation]): Stream[IO, Unit] = input.evalMap(accept)
|
|
44
49
|
def consumeDurable(input: Stream[IO, (Observation, IO[Unit])]): Stream[IO, Unit] =
|
|
@@ -73,20 +78,40 @@ final case class StreamState(pool: HistoryPool, tapes: Map[Long, History], store
|
|
|
73
78
|
final case class RoutedPoint(cursor: Cursor, changed: Boolean)
|
|
74
79
|
|
|
75
80
|
object ClassifyStreams:
|
|
76
|
-
private final case class Command(point: Observation,
|
|
81
|
+
private final case class Command(point: Observation, routed: RoutedPoint)
|
|
82
|
+
private given Codec.AsObject[Cursor] = deriveCodec
|
|
83
|
+
private given Codec.AsObject[Work] = deriveCodec
|
|
84
|
+
private given Codec.AsObject[LocalStreams] = deriveCodec
|
|
85
|
+
private given Codec.AsObject[Observation] = deriveCodec
|
|
86
|
+
private given Codec.AsObject[HistorySegment] = deriveCodec
|
|
87
|
+
private given Codec.AsObject[HistoryState] = deriveCodec
|
|
88
|
+
private given Codec.AsObject[HistoryPool] = deriveCodec
|
|
89
|
+
private given Encoder.AsObject[History] = Encoder.forProduct2("rows", "seen")(value => (value.rows.toMap, value.seen))
|
|
90
|
+
private given Decoder[History] = Decoder.forProduct2("rows", "seen")(
|
|
91
|
+
(rows: Map[Long, Map[String, Observation]], seen: Set[String]) => History(TreeMap.from(rows), seen))
|
|
92
|
+
private given Codec.AsObject[StreamState] = deriveCodec
|
|
93
|
+
private given Codec.AsObject[RoutedPoint] = deriveCodec
|
|
94
|
+
private given Codec.AsObject[Command] = deriveCodec
|
|
77
95
|
private val version = ProcessorVersion("amqp-classify/1", "session", "history-cursors/1")
|
|
78
96
|
|
|
79
97
|
def start(machine: MachineConfig, seed: Vector[Json], announce: (Vector[Json], Boolean) => IO[Unit]): IO[ClassifyStreams] =
|
|
80
98
|
open(machine, seed, ClassifyPorts(_ => IO.unit, announce))
|
|
81
|
-
def open(machine: MachineConfig, seed: Vector[Json], ports: ClassifyPorts,
|
|
82
|
-
|
|
99
|
+
def open(machine: MachineConfig, seed: Vector[Json], ports: ClassifyPorts,
|
|
100
|
+
watermark: Option[Long] = None, database: Option[WorkerDatabase] = None): IO[ClassifyStreams] =
|
|
101
|
+
val pool = HistoryPool(LocalStreams.empty(machine.window.toLong * 2000, 10000), Map.empty)
|
|
102
|
+
val owner = database.fold(1L)(_.generation)
|
|
103
|
+
val run = database.fold(IO.randomUUID.map(_.toString))(_ => IO.pure("live"))
|
|
83
104
|
for
|
|
84
|
-
id <-
|
|
85
|
-
task = TaskIdentity(machine.id.raw, "amqp-classify", id
|
|
105
|
+
id <- run
|
|
106
|
+
task = TaskIdentity(machine.id.raw, "amqp-classify", id)
|
|
86
107
|
snapshot = Checkpoint[StreamState, Command](task, version, 0L, None, StreamState(pool, Map.empty, seed), Vector.empty)
|
|
87
|
-
store <-
|
|
108
|
+
store <- database match
|
|
109
|
+
case Some(db) =>
|
|
110
|
+
val durable = PgCheckpointStore[StreamState, Command](db.transactor)
|
|
111
|
+
durable.claim(owner, snapshot).as(durable)
|
|
112
|
+
case None => CheckpointStore.session(snapshot, owner)
|
|
88
113
|
lock <- Semaphore[IO](1)
|
|
89
|
-
yield new ClassifyStreams(machine, store, task, ports, watermark, lock)
|
|
114
|
+
yield new ClassifyStreams(machine, store, task, ports, watermark, lock, owner)
|
|
90
115
|
private[runtime] def route(current: StreamState, point: Observation, now: Long, maxGap: Long): (StreamState, RoutedPoint) =
|
|
91
116
|
val gap = if current.pool.cursors.maxGap > 0 then current.pool.cursors.maxGap else maxGap
|
|
92
117
|
val idle = current.pool.cursors.idleTimeout
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
|
|
3
4
|
import cats.effect.IO
|
|
4
|
-
import scada.
|
|
5
|
-
import scada.
|
|
6
|
-
import scada.supervisor.infra.checkpoint.PendingSegmentData
|
|
5
|
+
import scada.processing.core.Segments as CoreSegments
|
|
6
|
+
import scada.processing.infra.checkpoint.PendingSegmentData
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* Completes one pending segment row in storage. Upsert carries empty
|
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{Segment, TaggedSegment, Timestamp}
|
|
3
4
|
import cats.effect.IO
|
|
4
5
|
import cats.syntax.all.*
|
|
5
6
|
import io.circe.Json
|
|
6
7
|
import io.circe.parser.parse
|
|
7
8
|
import java.time.Instant
|
|
8
|
-
import scada.
|
|
9
|
-
import scada.
|
|
10
|
-
import scada.
|
|
9
|
+
import scada.processing.core.*
|
|
10
|
+
import scada.processing.infra.config.AppConfig
|
|
11
|
+
import scada.processing.infra.stomp.{SegmentMessage, StompFrame, StompSender}
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* Publishes a committed local-history page to the segment exchange.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import io.circe.Json
|
|
@@ -6,7 +6,7 @@ import io.circe.parser.parse
|
|
|
6
6
|
import java.net.URI
|
|
7
7
|
import java.net.http.{HttpClient,HttpRequest,HttpResponse}
|
|
8
8
|
import java.time.Duration
|
|
9
|
-
import scada.
|
|
9
|
+
import scada.processing.infra.config.CheckpointApiConfig
|
|
10
10
|
import scala.concurrent.duration.*
|
|
11
11
|
|
|
12
12
|
final case class HistoryHttpError(path: String, status: Int, code: Option[String])
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Resource}
|
|
4
4
|
import cats.syntax.all.*
|
|
@@ -6,8 +6,8 @@ import fs2.Stream
|
|
|
6
6
|
import io.circe.Json
|
|
7
7
|
import org.typelevel.log4cats.Logger
|
|
8
8
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
9
|
-
import scada.
|
|
10
|
-
import scada.
|
|
9
|
+
import scada.processing.infra.config.{AliasMapping, AppConfig, CheckpointApiConfig}
|
|
10
|
+
import scada.processing.infra.stomp.{ClassifyQueue, StompSender}
|
|
11
11
|
import scala.concurrent.duration.*
|
|
12
12
|
|
|
13
13
|
/**
|
|
@@ -21,11 +21,11 @@ import scala.concurrent.duration.*
|
|
|
21
21
|
*/
|
|
22
22
|
final class HistoryRun(profile: RuntimeProfile, writer: Option[TimelineWriter],
|
|
23
23
|
observe: (String, Option[Long]) => Resource[IO, Unit],
|
|
24
|
-
decisions: (AppConfig, scada.
|
|
24
|
+
decisions: (AppConfig, scada.processing.core.MachineConfig) => Stream[IO, Unit]):
|
|
25
25
|
private given Logger[IO] = Slf4jLogger.getLogger[IO]
|
|
26
26
|
|
|
27
27
|
/** Retries until this process is the sole classifier. */
|
|
28
|
-
def gated(config: AppConfig, machine: scada.
|
|
28
|
+
def gated(config: AppConfig, machine: scada.processing.core.MachineConfig, aliases: List[AliasMapping], sender: Option[StompSender], api: CheckpointApiConfig, observationMs: Long = 0L, segmentEndMs: Option[Long] = None): Stream[IO, Unit] =
|
|
29
29
|
if !config.leaseHeld then Stream.never[IO]
|
|
30
30
|
else
|
|
31
31
|
val leader = ObservationLead.leading(config.writerRole, config.leaseUrl)
|
|
@@ -35,32 +35,36 @@ final class HistoryRun(profile: RuntimeProfile, writer: Option[TimelineWriter],
|
|
|
35
35
|
case None => follow(config, machine, aliases, sender, api, observationMs, segmentEndMs).concurrently(decisions(config, machine))
|
|
36
36
|
case Some(url) => once(url, config, machine, aliases, sender, api) ++ Stream.eval(IO.unit).flatMap(_ => gated(config, machine, aliases, sender, api, observationMs, segmentEndMs))
|
|
37
37
|
|
|
38
|
-
private def once(url: String, config: AppConfig, machine: scada.
|
|
38
|
+
private def once(url: String, config: AppConfig, machine: scada.processing.core.MachineConfig, aliases: List[AliasMapping], sender: Option[StompSender], api: CheckpointApiConfig): Stream[IO, Unit] =
|
|
39
39
|
Stream.resource(WriterLease.open(url, machine.id.raw)).flatMap: hold =>
|
|
40
40
|
if !hold.held then Stream.exec(Logger[IO].info(s"classifier lease denied for ${machine.id.raw}")) ++ Stream.sleep_[IO](5.seconds)
|
|
41
41
|
else Stream.resource(observe(machine.id.raw, hold.generation)) >> Stream.eval(hold.seed(machine.id.raw)).flatMap: (end, rows) =>
|
|
42
42
|
val grant = hold.generation.map(value => LeaseGrant(true, value))
|
|
43
43
|
if machine.mode.isDefined then follow(config, machine, aliases, sender, api, end.getOrElse(0L), end, grant).concurrently(decisions(config, machine))
|
|
44
|
-
else
|
|
45
|
-
|
|
44
|
+
else
|
|
45
|
+
val database = hold.generation.map(token =>
|
|
46
|
+
WorkerDatabase(doobie.Transactor.fromConnection[IO](hold.connection, None), token))
|
|
47
|
+
amqp(config, machine, aliases, rows, end, grant, database)
|
|
46
48
|
|
|
47
|
-
private def follow(config: AppConfig, machine: scada.
|
|
48
|
-
if machine.mode.isEmpty then amqp(config, machine, aliases,
|
|
49
|
+
private def follow(config: AppConfig, machine: scada.processing.core.MachineConfig, aliases: List[AliasMapping], sender: Option[StompSender], api: CheckpointApiConfig, observationMs: Long, segmentEndMs: Option[Long], grant: Option[LeaseGrant] = None): Stream[IO, Unit] =
|
|
50
|
+
if machine.mode.isEmpty then amqp(config, machine, aliases, Vector.empty, segmentEndMs, grant)
|
|
49
51
|
else if !ObservationLead.ready(observationMs, segmentEndMs, ObservationLead.leading(config.writerRole, config.leaseUrl)) then
|
|
50
52
|
Stream.exec(Logger[IO].info(s"classifier waiting for telemetry on ${machine.id.raw}")) ++ Stream.sleep_[IO](5.seconds)
|
|
51
53
|
else custom(config, machine, aliases, sender, api, observationMs, segmentEndMs, grant)
|
|
52
54
|
|
|
53
|
-
private def custom(config: AppConfig, machine: scada.
|
|
55
|
+
private def custom(config: AppConfig, machine: scada.processing.core.MachineConfig, aliases: List[AliasMapping], sender: Option[StompSender], api: CheckpointApiConfig, observationMs: Long, segmentEndMs: Option[Long], grant: Option[LeaseGrant] = None): Stream[IO, Unit] =
|
|
54
56
|
val announce = HistoryBus.lead(sender, config, machine.id, observationMs, segmentEndMs, grant)
|
|
55
57
|
profile.history(machine, aliases, api, announce, grant.filter(_.held).map(_.generation))
|
|
56
58
|
|
|
57
|
-
private def amqp(config: AppConfig, machine: scada.
|
|
59
|
+
private def amqp(config: AppConfig, machine: scada.processing.core.MachineConfig,
|
|
60
|
+
aliases: List[AliasMapping], seed: Vector[Json], segmentEndMs: Option[Long],
|
|
61
|
+
grant: Option[LeaseGrant], database: Option[WorkerDatabase] = None): Stream[IO, Unit] =
|
|
58
62
|
val leader = ObservationLead.leading(config.writerRole, config.leaseUrl)
|
|
59
63
|
val queue = s"scada.classify.${machine.id.raw}"
|
|
60
64
|
val input = ClassifyQueue(config.connectivity.rabbitmq, queue, aliases).subscribeDurable
|
|
61
65
|
val write = writer.getOrElse(throw IllegalStateException("Historical classification requires a timeline writer"))
|
|
62
66
|
val owner = grant.filter(_.held).map(_.generation)
|
|
63
|
-
val ports = ClassifyPorts(
|
|
67
|
+
val ports = ClassifyPorts(_ => IO.unit, (rows, historical) =>
|
|
64
68
|
ClassifyBus.publish(segmentEndMs, leader, historical)(rows): approved =>
|
|
65
69
|
approved.traverse_ { row =>
|
|
66
70
|
val message = if row.hcursor.get[String]("type").contains("split-family") then row else HistoryBus.message(machine.id, row)
|
|
@@ -68,7 +72,7 @@ final class HistoryRun(profile: RuntimeProfile, writer: Option[TimelineWriter],
|
|
|
68
72
|
config.connectivity.rabbitmq.destinations.segments)
|
|
69
73
|
}
|
|
70
74
|
)
|
|
71
|
-
Stream.eval(Logger[IO].info(s"consuming AMQP classify queue $queue")) >> Stream.eval(ClassifyStreams.open(machine, seed, ports, segmentEndMs,
|
|
75
|
+
Stream.eval(Logger[IO].info(s"consuming AMQP classify queue $queue")) >> Stream.eval(ClassifyStreams.open(machine, seed, ports, segmentEndMs, database)).flatMap(_.consumeDurable(input))
|
|
72
76
|
|
|
73
77
|
object ClassifyBus:
|
|
74
78
|
def publish(segmentEndMs: Option[Long], leader: Boolean, historical: Boolean = false)(rows: Vector[Json])(
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{HistoryRange, HistorySeed, HistorySegment, HistoryState, ObservedSegmentation, Reading, Segment, Tagging, Timestamp}
|
|
3
4
|
import cats.effect.{IO, Ref}
|
|
4
5
|
import cats.syntax.all.*
|
|
5
6
|
import fs2.Stream
|
|
@@ -13,17 +14,12 @@ import java.security.MessageDigest
|
|
|
13
14
|
import java.time.Instant
|
|
14
15
|
import java.util.HexFormat
|
|
15
16
|
import scada.stream.replay.{Replay, ReplayFailure, ReplayInput, ReplayManifest, ReplayRange, ReplaySeed}
|
|
16
|
-
import
|
|
17
|
-
import scada.
|
|
18
|
-
import
|
|
19
|
-
import scada.
|
|
20
|
-
import scada.supervisor.infra.config.{AliasMapping, CheckpointApiConfig}
|
|
17
|
+
import io.yarkivaev.streamworker.{ProcessingError, Processor, StepResult}
|
|
18
|
+
import scada.processing.core.*
|
|
19
|
+
import io.yarkivaev.segmentation.ObservedSegmentation.{Sample, State, segment}
|
|
20
|
+
import scada.processing.infra.config.{AliasMapping, CheckpointApiConfig}
|
|
21
21
|
import scala.concurrent.duration.*
|
|
22
22
|
|
|
23
|
-
final case class HistoryRange(from: Long, until: Long)
|
|
24
|
-
final case class HistorySeed(time: Long, values: Map[String,Double], segment: Option[HistorySegment] = None)
|
|
25
|
-
final case class HistorySegment(name: String, start: Long, end: Long)
|
|
26
|
-
final case class HistoryState(position: Long, parameters: Map[String,(Long,Double)], segment: Option[HistorySegment])
|
|
27
23
|
final case class HistoryPool(cursors: LocalStreams, states: Map[Long,HistoryState]):
|
|
28
24
|
def expire(now: Long): HistoryPool =
|
|
29
25
|
val alive = cursors.evict(now)
|
|
@@ -35,72 +31,28 @@ final case class HistoryPool(cursors: LocalStreams, states: Map[Long,HistoryStat
|
|
|
35
31
|
val merged = equal.foldLeft(advanced)((pool,id) => pool.merge(stream,id,true))
|
|
36
32
|
HistoryPool(merged,known.filter((id,_) => merged.streams.exists(_.id == id)))
|
|
37
33
|
object LocalHistory:
|
|
38
|
-
def
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
points.map(_.time).maxOption.map: end =>
|
|
42
|
-
val gap = machine.window.toLong*2000
|
|
43
|
-
val state = points.groupBy(_.time).toVector.sortBy(_._1).foldLeft((seed.map(_.time),seed.fold(Map.empty[String,(Long,Double)])(s => s.values.map((k,v) => k -> (s.time,v))))):
|
|
44
|
-
case ((previous,values),(time,group)) =>
|
|
45
|
-
val prior = if previous.exists(t => connected(t,time,gap,continuity)) then values else Map.empty[String,(Long,Double)]
|
|
46
|
-
(Some(time),prior ++ group.map(p => p.channel -> (p.time,p.value)))
|
|
47
|
-
HistoryState(end,state._2,result.lastOption)
|
|
34
|
+
def classify(machine: MachineConfig, points: Vector[Observation], seed: Option[HistorySeed] = None,
|
|
35
|
+
continuity: Vector[HistoryRange] = Vector.empty): Vector[HistorySegment] =
|
|
36
|
+
ObservedSegmentation(machine.segments, machine.window, continuity).run(grouped(points), seed)
|
|
48
37
|
|
|
49
|
-
/** Finite event-time classification: never inject wall-clock silence into past data. */
|
|
50
|
-
def classify(machine: MachineConfig, points: Vector[Observation], seed: Option[HistorySeed] = None, continuity: Vector[HistoryRange] = Vector.empty): Vector[HistorySegment] =
|
|
51
|
-
val classifier = Classification(machine, continuity)
|
|
52
|
-
val (terminal, closed) = grouped(points).foldLeft((classifier.initial(seed), Vector.empty[HistorySegment])):
|
|
53
|
-
case ((state, completed), sample) =>
|
|
54
|
-
val next = classifier.advance(state, sample)
|
|
55
|
-
(next.state, completed ++ next.outputs)
|
|
56
|
-
closed ++ terminal.segment.map(segment)
|
|
57
|
-
|
|
58
|
-
private final case class Sample(time: Long, values: Map[String, Double])
|
|
59
|
-
private final case class ClassificationState(
|
|
60
|
-
time: Option[Long], parameters: Map[String, (Long, Double)], segment: Option[Interval[Long]]
|
|
61
|
-
)
|
|
62
38
|
private[runtime] final case class Replayed(segments: Vector[HistorySegment], state: Option[HistoryState])
|
|
63
39
|
|
|
64
|
-
private def segment(row: Interval[Long]): HistorySegment = HistorySegment(row.name, row.start, row.end)
|
|
65
|
-
|
|
66
40
|
private def grouped(points: Vector[Observation]): Vector[Sample] =
|
|
67
41
|
points.groupBy(_.time).toVector.sortBy(_._1).map: (time, group) =>
|
|
68
42
|
Sample(time, group.map(p => p.channel -> p.value).toMap)
|
|
69
43
|
|
|
70
|
-
private
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
val carried = seed.flatMap: value =>
|
|
77
|
-
val stored = value.segment.filter(_.start <= value.time)
|
|
78
|
-
val evaluated = classify(Params(value.values)) match
|
|
79
|
-
case Some(name) => Some(stored.filter(_.name == name).getOrElse(HistorySegment(name, value.time, value.time)))
|
|
80
|
-
case None => stored
|
|
81
|
-
evaluated.map(row => Interval(row.name, row.start, row.end))
|
|
82
|
-
val parameters = seed.fold(Map.empty[String, (Long, Double)])(value => value.values.map((key, reading) => key -> (value.time, reading)))
|
|
83
|
-
ClassificationState(seed.map(_.time), parameters, carried)
|
|
84
|
-
|
|
85
|
-
def advance(state: ClassificationState, sample: Sample): StepResult[ClassificationState, HistorySegment] =
|
|
86
|
-
val continuous = state.time.exists(connected(_, sample.time, gap, continuity))
|
|
87
|
-
val parameters = (if continuous then state.parameters else Map.empty) ++
|
|
88
|
-
sample.values.map((key, value) => key -> (sample.time, value))
|
|
89
|
-
val name = classify(Params(parameters.map((key, value) => key -> value._2)))
|
|
90
|
-
.orElse(if continuous then state.segment.map(_.name) else None)
|
|
91
|
-
val next = ClassificationState(Some(sample.time), parameters, state.segment)
|
|
92
|
-
name.fold(StepResult(next, Vector.empty[HistorySegment])): classified =>
|
|
93
|
-
val result = Timeline.step(Boundary.Observed, state.segment, Event.Sample(sample.time, classified, continuous))
|
|
94
|
-
StepResult(next.copy(segment = result.open), result.outputs.collect { case Output.Closed(row) => segment(row) })
|
|
95
|
-
|
|
96
|
-
override def step(config: Unit, state: ClassificationState, sample: Sample)
|
|
97
|
-
: Either[ProcessingError, StepResult[ClassificationState, HistorySegment]] = Right(advance(state, sample))
|
|
44
|
+
private def processor(classifier: ObservedSegmentation): Processor[Unit, State, Sample, HistorySegment] =
|
|
45
|
+
new Processor[Unit, State, Sample, HistorySegment]:
|
|
46
|
+
def step(config: Unit, state: State, sample: Sample)
|
|
47
|
+
: Either[ProcessingError, StepResult[State, HistorySegment]] =
|
|
48
|
+
val result = classifier.step(state, sample)
|
|
49
|
+
Right(StepResult(result.state, result.outputs))
|
|
98
50
|
|
|
99
51
|
private[runtime] def replay(
|
|
100
52
|
machine: MachineConfig, points: Vector[Observation], baseRevision: String, runId: String,
|
|
101
53
|
seed: Option[HistorySeed] = None, continuity: Vector[HistoryRange] = Vector.empty
|
|
102
54
|
): IO[Replayed] = IO.defer:
|
|
103
|
-
val classifier =
|
|
55
|
+
val classifier = ObservedSegmentation(machine.segments, machine.window, continuity)
|
|
104
56
|
val initial = classifier.initial(seed)
|
|
105
57
|
val samples = grouped(points)
|
|
106
58
|
def values(readings: Map[String, Double]): Json =
|
|
@@ -118,7 +70,7 @@ object LocalHistory:
|
|
|
118
70
|
val manifest = ReplayManifest(timeline, ReplayRange(0, samples.size), rulesVersion, input.id, seedId, baseRevision, runId)
|
|
119
71
|
def failure(problem: ReplayFailure) = IllegalStateException(s"History page replay failed: $problem")
|
|
120
72
|
IO.fromEither(Replay.plan(manifest, input, start).leftMap(failure)).flatMap: plan =>
|
|
121
|
-
Replay.run(plan, (), classifier).flatMap(result => IO.fromEither(result.leftMap(failure))).map: result =>
|
|
73
|
+
Replay.run(plan, (), processor(classifier)).flatMap(result => IO.fromEither(result.leftMap(failure))).map: result =>
|
|
122
74
|
val terminal = result.terminalState
|
|
123
75
|
val tail = terminal.segment.map(segment)
|
|
124
76
|
val state = samples.lastOption.map(last => HistoryState(last.time, terminal.parameters, tail))
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
-
import scada.
|
|
4
|
-
import scada.
|
|
5
|
-
import scada.
|
|
3
|
+
import scada.processing.core.MachineId
|
|
4
|
+
import scada.processing.infra.config.AppConfig
|
|
5
|
+
import scada.processing.infra.checkpoint.PendingSegmentData
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Orphan pending segment rows left in the checkpoint by repeated
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.SegmentCursor
|
|
3
4
|
import cats.effect.IO
|
|
4
5
|
import org.typelevel.log4cats.Logger
|
|
5
6
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
6
|
-
import scada.
|
|
7
|
-
import scada.
|
|
7
|
+
import scada.processing.core.MachineConfig
|
|
8
|
+
import scada.processing.infra.checkpoint.Checkpoint
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Resolves the pending open segment cursor for one machine from
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.{ExitCode, IO, IOApp}
|
|
4
4
|
|
|
5
5
|
/** Standalone entry point for the built-in rules-based worker. */
|
|
6
|
-
object
|
|
6
|
+
object ProcessingApp extends IOApp:
|
|
7
7
|
private val application = ProcessingApplication(RuntimeProfile.default)
|
|
8
8
|
def run(args: List[String]): IO[ExitCode] = application.run(args)
|
|
@@ -1,30 +1,19 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{Reading, Segmentation, TagDef, TaggedSegment, Timestamp}
|
|
3
4
|
import cats.effect.{ExitCode, IO, Ref, Resource}
|
|
4
5
|
import cats.effect.std.Semaphore
|
|
5
6
|
import cats.syntax.all.*
|
|
6
7
|
import fs2.Stream
|
|
7
|
-
import
|
|
8
|
-
import scada.stream.worker.postgres.WorkerDatabase
|
|
8
|
+
import io.yarkivaev.streamworker.OutputScope
|
|
9
9
|
import org.typelevel.log4cats.Logger
|
|
10
10
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
11
|
-
import scada.
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
Segments as CoreSegments,
|
|
18
|
-
ReadingWorker,
|
|
19
|
-
TagDef,
|
|
20
|
-
TaggedSegment,
|
|
21
|
-
Timestamp
|
|
22
|
-
}
|
|
23
|
-
import scada.supervisor.infra.checkpoint.{Checkpoint, ClickhouseReadingsCheckpoint, SupervisorSinkCheckpoint}
|
|
24
|
-
import scada.supervisor.infra.config.{AliasMapping, AppConfig, ConfigLoad, RabbitMqConfig}
|
|
25
|
-
import scada.supervisor.infra.mqtt.Mqtt
|
|
26
|
-
import scada.supervisor.infra.state.{CheckpointSegments, Segments as StateSegments}
|
|
27
|
-
import scada.supervisor.infra.stomp.{DurableStomp, StompConnection, StompDecisions, StompSegments, StompSender}
|
|
11
|
+
import scada.processing.core.{Decision, MachineConfig, MachineId, Segments as CoreSegments, ReadingWorker}
|
|
12
|
+
import scada.processing.infra.checkpoint.{Checkpoint, ClickhouseReadingsCheckpoint, SupervisorSinkCheckpoint}
|
|
13
|
+
import scada.processing.infra.config.{AliasMapping, AppConfig, ConfigLoad, RabbitMqConfig}
|
|
14
|
+
import scada.processing.infra.mqtt.Mqtt
|
|
15
|
+
import scada.processing.infra.state.{CheckpointSegments, Segments as StateSegments}
|
|
16
|
+
import scada.processing.infra.stomp.{DurableStomp, StompConnection, StompDecisions, StompSegments, StompSender}
|
|
28
17
|
import scala.collection.immutable.SortedMap
|
|
29
18
|
import scala.concurrent.duration.*
|
|
30
19
|
|
|
@@ -67,7 +56,7 @@ final class ProcessingApplication(profile: RuntimeProfile):
|
|
|
67
56
|
private def runStreams(config: AppConfig, segments: CoreSegments[IO], checkpoint: Option[Checkpoint], base: StateSegments[IO], publish: Option[StompSender], writer: Option[TimelineWriter],
|
|
68
57
|
owners: Ref[IO, Map[String, Long]], outputs: OutputScope): IO[Unit] =
|
|
69
58
|
|
|
70
|
-
val worker = ReadingWorker(Segmentation
|
|
59
|
+
val worker = ReadingWorker(Segmentation(profile.classifyReading), profile.tagging, segments, config.cyclePhases, outputs = Some(outputs))
|
|
71
60
|
val decisionSegments = checkpoint.filter(_ => config.localHistory).fold(segments)(cp =>
|
|
72
61
|
CheckpointSegments(segments,cp,preferCheckpoint=true,refreshCache=Some(base.refresh)))
|
|
73
62
|
val stall = config.connectivity.checkpointApi.map(api => api.timeoutSeconds.seconds).getOrElse(60.seconds)
|
|
@@ -130,7 +119,7 @@ final class ProcessingApplication(profile: RuntimeProfile):
|
|
|
130
119
|
slot
|
|
131
120
|
)
|
|
132
121
|
Some(new StompSender:
|
|
133
|
-
def send(frame: scada.
|
|
122
|
+
def send(frame: scada.processing.infra.stomp.StompFrame): IO[Unit] = lock.permit.use(_ => durable.send(frame))
|
|
134
123
|
)
|
|
135
124
|
|
|
136
125
|
private def timelineWriter(config: AppConfig, owners: Ref[IO, Map[String, Long]], outputs: OutputScope,
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{Reading, ReadingOverride, Tagging}
|
|
3
4
|
import cats.effect.IO
|
|
4
5
|
import fs2.Stream
|
|
5
6
|
import io.circe.Json
|
|
6
|
-
import
|
|
7
|
-
import scada.
|
|
8
|
-
import scada.
|
|
9
|
-
import scada.
|
|
10
|
-
import scada.
|
|
11
|
-
import scada.
|
|
12
|
-
import scada.supervisor.infra.stomp.SegmentWirePolicy
|
|
7
|
+
import io.yarkivaev.streamworker.OutputScope
|
|
8
|
+
import scada.processing.core.{MachineConfig, Segments}
|
|
9
|
+
import scada.processing.infra.checkpoint.Checkpoint
|
|
10
|
+
import scada.processing.infra.config.{AliasMapping, AppConfig, CheckpointApiConfig, ConfigProfile}
|
|
11
|
+
import scada.processing.infra.state.SegmentStatePolicy
|
|
12
|
+
import scada.processing.infra.stomp.SegmentWirePolicy
|
|
13
13
|
|
|
14
14
|
/** Site overrides for configured machine modes. The default runs the shared classifier. */
|
|
15
15
|
trait RuntimeProfile:
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
|
+
import io.yarkivaev.segmentation.{Params, Reading, Timestamp}
|
|
3
4
|
import cats.effect.{IO, Outcome}
|
|
4
5
|
import cats.effect.std.Queue
|
|
5
6
|
import fs2.Stream
|
|
6
7
|
import org.typelevel.log4cats.Logger
|
|
7
8
|
import org.typelevel.log4cats.slf4j.Slf4jLogger
|
|
8
9
|
import scala.concurrent.duration.*
|
|
9
|
-
import scada.
|
|
10
|
-
import scada.
|
|
11
|
-
import scada.
|
|
10
|
+
import scada.processing.core.{MachineConfig, MachineId}
|
|
11
|
+
import scada.processing.infra.checkpoint.{Checkpoint, RawReadingData}
|
|
12
|
+
import scada.processing.infra.config.AliasMapping
|
|
12
13
|
|
|
13
14
|
/**
|
|
14
15
|
* Seamless replay-then-live readings source for one machine.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, IOLocal}
|
|
4
4
|
import io.circe.{Json, Printer}
|
|
@@ -8,8 +8,8 @@ import java.net.URLEncoder
|
|
|
8
8
|
import java.nio.charset.StandardCharsets.UTF_8
|
|
9
9
|
import java.security.MessageDigest
|
|
10
10
|
import java.util.HexFormat
|
|
11
|
-
import
|
|
12
|
-
import scada.
|
|
11
|
+
import io.yarkivaev.streamworker.{OutputId, OutputScope}
|
|
12
|
+
import scada.processing.infra.stomp.{StompFrame, StompSender}
|
|
13
13
|
|
|
14
14
|
/** Submits the segment wire format through the authoritative sink transaction. */
|
|
15
15
|
final class TimelineWriter private (
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.IO
|
|
4
4
|
import doobie.Transactor
|
|
5
5
|
|
|
6
|
-
/**
|
|
6
|
+
/** Checkpoint access supplied by the SCADA classifier lease. */
|
|
7
7
|
final case class WorkerDatabase(transactor: Transactor[IO], generation: Long)
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
package scada.
|
|
1
|
+
package scada.processing.runtime
|
|
2
2
|
|
|
3
3
|
import cats.effect.{IO, Resource}
|
|
4
4
|
import java.sql.{Connection, DriverManager}
|
|
@@ -71,7 +71,7 @@ object WriterLease:
|
|
|
71
71
|
|
|
72
72
|
/**
|
|
73
73
|
* Epoch to publish. None means this process must not publish.
|
|
74
|
-
* Some(None)
|
|
74
|
+
* Some(None) allows an unleased sender to omit the epoch.
|
|
75
75
|
*
|
|
76
76
|
* {{{
|
|
77
77
|
* WriterLease.publication(Some("postgres://127.0.0.1:1/scada"), Some(LeaseGrant(true, 7)), Some(1L))
|
|
@@ -129,23 +129,11 @@ object WriterLease:
|
|
|
129
129
|
|
|
130
130
|
/** Reads the stored counter on this session and returns the generation the holder may stamp. */
|
|
131
131
|
private def advance(connection: Connection, machine: String, held: Boolean): Option[Long] =
|
|
132
|
-
val grant = claim(held, if held then
|
|
132
|
+
val grant = claim(held, if held then currentGeneration(connection, machine) else 0L)
|
|
133
133
|
grant.foreach(item => remember(connection, machine, item.generation))
|
|
134
134
|
grant.flatMap(_.epoch)
|
|
135
135
|
|
|
136
136
|
/** Last generation issued for this machine, or zero when the lock has never been held. */
|
|
137
|
-
private def counter(connection: Connection, machine: String): Long =
|
|
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
137
|
private def currentGeneration(connection: Connection, machine: String): Long =
|
|
150
138
|
val query = connection.prepareStatement("SELECT generation FROM classifier_generation WHERE machine = ?")
|
|
151
139
|
try
|
|
@@ -161,12 +149,7 @@ object WriterLease:
|
|
|
161
149
|
val stamped =
|
|
162
150
|
"INSERT INTO classifier_generation (machine, generation, holder_pid) VALUES (?, ?, pg_backend_pid()) " +
|
|
163
151
|
"ON CONFLICT (machine) DO UPDATE SET generation = EXCLUDED.generation, holder_pid = EXCLUDED.holder_pid"
|
|
164
|
-
|
|
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)
|
|
152
|
+
writeGeneration(connection, machine, generation, stamped)
|
|
170
153
|
|
|
171
154
|
private def writeGeneration(connection: Connection, machine: String, generation: Long, sql: String): Unit =
|
|
172
155
|
val query = connection.prepareStatement(sql)
|