@yarkivaev/scada 2.7.1 → 2.8.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 +6 -3
- package/stream-processing/dependencies/stream-worker.jar +0 -0
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Condition.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/CyclePhases.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Decision.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Emission.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/MachineConfig.scala +1 -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/Params.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Reading.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/ReadingWorker.scala +12 -7
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Segment.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/SegmentDef.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Segmentation.scala +6 -22
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Segments.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/SplitFollow.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/SplitPieces.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/TagDef.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/TaggedSegment.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Tagging.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/Timestamp.scala +1 -1
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/core/segmentation/Timeline.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/Checkpoint.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/ClickhouseReadingsCheckpoint.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/checkpoint/SupervisorSinkCheckpoint.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/AppConfig.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/ConfigLoad.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/config/ConfigProfile.scala +2 -2
- 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 +5 -5
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/SegmentStatePolicy.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/state/Segments.scala +3 -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 +2 -2
- 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 +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SegmentWirePolicy.scala +2 -2
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/infra/stomp/SplitFamily.scala +2 -2
- 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 +4 -4
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ClassifyHistory.scala +3 -3
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/ClassifyStreams.scala +42 -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 +4 -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 +6 -6
- 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 +3 -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 +10 -11
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/RuntimeProfile.scala +7 -8
- package/{supervisor-engine/src/main/scala/scada/supervisor → stream-processing/src/main/scala/scada/processing}/runtime/Seeding.scala +4 -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/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/runtime/ClassifierCursors.scala +0 -58
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/LICENSE +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/UPSTREAM +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/Island.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandEvent.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsFlow.scala +0 -0
- /package/{supervisor-engine → stream-processing}/vendor/gaps-and-islands/src/main/scala/yarkivaev/gapsandislands/api/IslandsPolicy.scala +0 -0
- /package/{supervisor-engine → 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,135 +0,0 @@
|
|
|
1
|
-
package scada.stream.worker
|
|
2
|
-
|
|
3
|
-
import cats.effect.{IO, Ref}
|
|
4
|
-
import fs2.Stream
|
|
5
|
-
|
|
6
|
-
final case class TaskIdentity(taskId: String, inputId: String, runId: String)
|
|
7
|
-
final case class ProcessorVersion(implementation: String, rules: String, stateSchema: String)
|
|
8
|
-
|
|
9
|
-
final case class OutputId(task: TaskIdentity, position: Long, index: Int)
|
|
10
|
-
final case class Output[O](id: OutputId, value: O)
|
|
11
|
-
|
|
12
|
-
/** Positions identify immutable records in one ordered input. An adapter must not skip unseen records. */
|
|
13
|
-
final case class Delivery[I](position: Long, value: I, acknowledge: IO[Unit])
|
|
14
|
-
|
|
15
|
-
final case class Checkpoint[S, O](
|
|
16
|
-
task: TaskIdentity,
|
|
17
|
-
version: ProcessorVersion,
|
|
18
|
-
revision: Long,
|
|
19
|
-
position: Option[Long],
|
|
20
|
-
state: S,
|
|
21
|
-
pendingOutputs: Vector[Output[O]]
|
|
22
|
-
)
|
|
23
|
-
|
|
24
|
-
final case class StaleOwnership(task: TaskIdentity, ownership: Long)
|
|
25
|
-
extends RuntimeException(s"Ownership $ownership is stale for $task")
|
|
26
|
-
|
|
27
|
-
final case class IncompatibleCheckpoint(
|
|
28
|
-
expectedTask: TaskIdentity,
|
|
29
|
-
expectedVersion: ProcessorVersion,
|
|
30
|
-
actualTask: TaskIdentity,
|
|
31
|
-
actualVersion: ProcessorVersion
|
|
32
|
-
) extends RuntimeException(
|
|
33
|
-
s"Expected checkpoint for $expectedTask at $expectedVersion, found $actualTask at $actualVersion"
|
|
34
|
-
)
|
|
35
|
-
|
|
36
|
-
final case class StepFailed(error: ProcessingError) extends RuntimeException(error.message)
|
|
37
|
-
final case class UnconfirmedOutputs(task: TaskIdentity)
|
|
38
|
-
extends RuntimeException(s"Checkpoint for $task still has unconfirmed outputs")
|
|
39
|
-
|
|
40
|
-
private[worker] object CheckpointCompatibility:
|
|
41
|
-
def check(task: TaskIdentity, version: ProcessorVersion, actualTask: TaskIdentity, actualVersion: ProcessorVersion)
|
|
42
|
-
: IO[Unit] =
|
|
43
|
-
IO.raiseUnless(actualTask == task && actualVersion == version)(
|
|
44
|
-
IncompatibleCheckpoint(task, version, actualTask, actualVersion)
|
|
45
|
-
)
|
|
46
|
-
|
|
47
|
-
trait CheckpointStore[S, O]:
|
|
48
|
-
/** Load the checkpoint while validating current ownership. */
|
|
49
|
-
def load(task: TaskIdentity, ownership: Long): IO[Checkpoint[S, O]]
|
|
50
|
-
|
|
51
|
-
/** Validate the checkpoint version before consumption. Serialized stores can check the header first. */
|
|
52
|
-
def loadCompatible(task: TaskIdentity, version: ProcessorVersion, ownership: Long): IO[Checkpoint[S, O]] =
|
|
53
|
-
load(task, ownership).flatMap: checkpoint =>
|
|
54
|
-
CheckpointCompatibility.check(task, version, checkpoint.task, checkpoint.version).as(checkpoint)
|
|
55
|
-
|
|
56
|
-
/** Atomically validate ownership and revision, then replace the checkpoint. */
|
|
57
|
-
def commit(ownership: Long, expectedRevision: Long, next: Checkpoint[S, O]): IO[Unit]
|
|
58
|
-
|
|
59
|
-
object CheckpointStore:
|
|
60
|
-
/** A session store for sources that recover by reseeding and cannot resume by delivery position. */
|
|
61
|
-
def session[S, O](initial: Checkpoint[S, O], token: Long): IO[CheckpointStore[S, O]] =
|
|
62
|
-
Ref.of[IO, Checkpoint[S, O]](initial).map: ref =>
|
|
63
|
-
new CheckpointStore[S, O]:
|
|
64
|
-
def load(task: TaskIdentity, ownership: Long): IO[Checkpoint[S, O]] =
|
|
65
|
-
IO.raiseUnless(task == initial.task && ownership == token)(StaleOwnership(task, ownership)) *> ref.get
|
|
66
|
-
|
|
67
|
-
def commit(ownership: Long, expectedRevision: Long, next: Checkpoint[S, O]): IO[Unit] =
|
|
68
|
-
IO.raiseUnless(next.task == initial.task && ownership == token)(StaleOwnership(next.task, ownership)) *>
|
|
69
|
-
ref.modify: current =>
|
|
70
|
-
if current.revision == expectedRevision && next.revision == expectedRevision + 1 then
|
|
71
|
-
(next, IO.unit)
|
|
72
|
-
else (current, IO.raiseError(IllegalStateException("Session checkpoint revision changed")))
|
|
73
|
-
.flatten
|
|
74
|
-
|
|
75
|
-
/** Runtime for a deterministic processor. The sink checks ownership and deduplicates by OutputId. */
|
|
76
|
-
final class StreamWorker[C, S, I, O](
|
|
77
|
-
processor: Processor[C, S, I, O],
|
|
78
|
-
store: CheckpointStore[S, O],
|
|
79
|
-
publish: (Long, Output[O]) => IO[Unit]
|
|
80
|
-
):
|
|
81
|
-
def run(task: TaskIdentity, version: ProcessorVersion, config: C, ownership: Long)(
|
|
82
|
-
input: Option[Long] => Stream[IO, Delivery[I]]
|
|
83
|
-
): IO[Checkpoint[S, O]] =
|
|
84
|
-
for
|
|
85
|
-
checkpoint <- store.loadCompatible(task, version, ownership)
|
|
86
|
-
start <- deliver(ownership, checkpoint)
|
|
87
|
-
end <- input(start.position)
|
|
88
|
-
.evalMapAccumulate(start): (current, event) =>
|
|
89
|
-
process(config, ownership, current, event).map(next => (next, next))
|
|
90
|
-
.map(_._2)
|
|
91
|
-
.compile
|
|
92
|
-
.last
|
|
93
|
-
yield end.getOrElse(start)
|
|
94
|
-
|
|
95
|
-
/** Change rules while input consumption is stopped, after confirming the previous version's outputs.
|
|
96
|
-
* The current codec must be able to read the previous state.
|
|
97
|
-
*/
|
|
98
|
-
def reconfigure(task: TaskIdentity, previous: ProcessorVersion, next: ProcessorVersion, ownership: Long)(
|
|
99
|
-
migrate: S => Either[ProcessingError, S]
|
|
100
|
-
): IO[Checkpoint[S, O]] =
|
|
101
|
-
for
|
|
102
|
-
current <- store.loadCompatible(task, previous, ownership)
|
|
103
|
-
_ <- IO.raiseWhen(current.pendingOutputs.nonEmpty)(UnconfirmedOutputs(task))
|
|
104
|
-
migrated <- IO.defer(IO.fromEither(migrate(current.state).left.map(StepFailed.apply)))
|
|
105
|
-
updated = current.copy(version = next, revision = current.revision + 1, state = migrated)
|
|
106
|
-
_ <- store.commit(ownership, current.revision, updated)
|
|
107
|
-
yield updated
|
|
108
|
-
|
|
109
|
-
private def process(config: C, ownership: Long, current: Checkpoint[S, O], event: Delivery[I])
|
|
110
|
-
: IO[Checkpoint[S, O]] =
|
|
111
|
-
if current.position.exists(_ >= event.position) then event.acknowledge.as(current)
|
|
112
|
-
else
|
|
113
|
-
for
|
|
114
|
-
result <- IO.defer(IO.fromEither(
|
|
115
|
-
processor.step(config, current.state, event.value).left.map(StepFailed.apply)
|
|
116
|
-
))
|
|
117
|
-
outputs = result.outputs.zipWithIndex.map: (value, index) =>
|
|
118
|
-
Output(OutputId(current.task, event.position, index), value)
|
|
119
|
-
updated = current.copy(
|
|
120
|
-
revision = current.revision + 1,
|
|
121
|
-
position = Some(event.position),
|
|
122
|
-
state = result.state,
|
|
123
|
-
pendingOutputs = outputs
|
|
124
|
-
)
|
|
125
|
-
_ <- store.commit(ownership, current.revision, updated)
|
|
126
|
-
_ <- event.acknowledge
|
|
127
|
-
delivered <- deliver(ownership, updated)
|
|
128
|
-
yield delivered
|
|
129
|
-
|
|
130
|
-
private def deliver(ownership: Long, current: Checkpoint[S, O]): IO[Checkpoint[S, O]] =
|
|
131
|
-
if current.pendingOutputs.isEmpty then IO.pure(current)
|
|
132
|
-
else
|
|
133
|
-
val updated = current.copy(revision = current.revision + 1, pendingOutputs = Vector.empty[Output[O]])
|
|
134
|
-
Stream.emits(current.pendingOutputs).evalMap(row => publish(ownership, row)).compile.drain *>
|
|
135
|
-
store.commit(ownership, current.revision, updated).as(updated)
|
package/supervisor-engine/src/main/scala/scada/stream/worker/postgres/PgCheckpointStore.scala
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
package scada.stream.worker.postgres
|
|
2
|
-
|
|
3
|
-
import cats.effect.IO
|
|
4
|
-
import cats.syntax.all.*
|
|
5
|
-
import doobie.*
|
|
6
|
-
import doobie.implicits.*
|
|
7
|
-
import io.circe.{Decoder, Encoder, Json}
|
|
8
|
-
import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
|
|
9
|
-
import io.circe.parser.parse
|
|
10
|
-
import io.circe.syntax.*
|
|
11
|
-
import scada.stream.worker.*
|
|
12
|
-
|
|
13
|
-
final case class CheckpointConflict(task: TaskIdentity, expectedRevision: Long)
|
|
14
|
-
extends RuntimeException(s"Checkpoint for $task is no longer at revision $expectedRevision")
|
|
15
|
-
|
|
16
|
-
/** Shared storage for workers. The coordinator supplies strictly increasing ownership tokens. */
|
|
17
|
-
final class PgCheckpointStore[S: Encoder: Decoder, O: Encoder: Decoder](xa: Transactor[IO])
|
|
18
|
-
extends CheckpointStore[S, O]:
|
|
19
|
-
private given CodecForTask: Encoder[TaskIdentity] = deriveEncoder
|
|
20
|
-
private given DecodeTask: Decoder[TaskIdentity] = deriveDecoder
|
|
21
|
-
private given EncodeVersion: Encoder[ProcessorVersion] = deriveEncoder
|
|
22
|
-
private given DecodeVersion: Decoder[ProcessorVersion] = deriveDecoder
|
|
23
|
-
private given EncodeId: Encoder[OutputId] = deriveEncoder
|
|
24
|
-
private given DecodeId: Decoder[OutputId] = deriveDecoder
|
|
25
|
-
private given EncodeOutput: Encoder[Output[O]] = deriveEncoder
|
|
26
|
-
private given DecodeOutput: Decoder[Output[O]] = deriveDecoder
|
|
27
|
-
private given EncodeCheckpoint: Encoder[Checkpoint[S, O]] = deriveEncoder
|
|
28
|
-
private given DecodeCheckpoint: Decoder[Checkpoint[S, O]] = deriveDecoder
|
|
29
|
-
|
|
30
|
-
/** Activate the coordinator's grant. A takeover keeps the previous checkpoint and pending output. */
|
|
31
|
-
def claim(ownership: Long, initial: Checkpoint[S, O]): IO[Unit] =
|
|
32
|
-
val task = initial.task
|
|
33
|
-
val document = initial.asJson.noSpaces
|
|
34
|
-
sql"""
|
|
35
|
-
INSERT INTO stream_worker_checkpoint (task_id, input_id, run_id, ownership, revision, document)
|
|
36
|
-
VALUES (${task.taskId}, ${task.inputId}, ${task.runId}, $ownership, ${initial.revision}, $document::jsonb)
|
|
37
|
-
ON CONFLICT (task_id, input_id, run_id) DO UPDATE SET ownership = EXCLUDED.ownership
|
|
38
|
-
WHERE stream_worker_checkpoint.ownership <= EXCLUDED.ownership
|
|
39
|
-
""".update.run.transact(xa).flatMap(rows => IO.raiseWhen(rows != 1)(StaleOwnership(task, ownership)))
|
|
40
|
-
|
|
41
|
-
def load(task: TaskIdentity, ownership: Long): IO[Checkpoint[S, O]] =
|
|
42
|
-
document(task, ownership).flatMap(value => IO.fromEither(value.as[Checkpoint[S, O]]))
|
|
43
|
-
|
|
44
|
-
override def loadCompatible(task: TaskIdentity, version: ProcessorVersion, ownership: Long)
|
|
45
|
-
: IO[Checkpoint[S, O]] =
|
|
46
|
-
for
|
|
47
|
-
value <- document(task, ownership)
|
|
48
|
-
actualTask <- IO.fromEither(value.hcursor.get[TaskIdentity]("task"))
|
|
49
|
-
actualVersion <- IO.fromEither(value.hcursor.get[ProcessorVersion]("version"))
|
|
50
|
-
_ <- CheckpointCompatibility.check(task, version, actualTask, actualVersion)
|
|
51
|
-
checkpoint <- IO.fromEither(value.as[Checkpoint[S, O]])
|
|
52
|
-
yield checkpoint
|
|
53
|
-
|
|
54
|
-
private def document(task: TaskIdentity, ownership: Long): IO[Json] =
|
|
55
|
-
sql"""
|
|
56
|
-
SELECT document::text FROM stream_worker_checkpoint
|
|
57
|
-
WHERE task_id = ${task.taskId} AND input_id = ${task.inputId} AND run_id = ${task.runId}
|
|
58
|
-
AND ownership = $ownership
|
|
59
|
-
""".query[String].option.transact(xa).flatMap:
|
|
60
|
-
case Some(document) => IO.fromEither(parse(document))
|
|
61
|
-
case None => IO.raiseError(StaleOwnership(task, ownership))
|
|
62
|
-
|
|
63
|
-
def commit(ownership: Long, expectedRevision: Long, next: Checkpoint[S, O]): IO[Unit] =
|
|
64
|
-
val task = next.task
|
|
65
|
-
val document = next.asJson.noSpaces
|
|
66
|
-
val update = sql"""
|
|
67
|
-
UPDATE stream_worker_checkpoint SET revision = ${next.revision}, document = $document::jsonb
|
|
68
|
-
WHERE task_id = ${task.taskId} AND input_id = ${task.inputId} AND run_id = ${task.runId}
|
|
69
|
-
AND ownership = $ownership AND revision = $expectedRevision
|
|
70
|
-
""".update.run
|
|
71
|
-
if next.revision != expectedRevision + 1 then IO.raiseError(CheckpointConflict(task, expectedRevision))
|
|
72
|
-
else
|
|
73
|
-
val checked = update.flatMap:
|
|
74
|
-
case 1 => ().pure[ConnectionIO]
|
|
75
|
-
case _ =>
|
|
76
|
-
sql"""
|
|
77
|
-
SELECT ownership FROM stream_worker_checkpoint
|
|
78
|
-
WHERE task_id = ${task.taskId} AND input_id = ${task.inputId} AND run_id = ${task.runId}
|
|
79
|
-
""".query[Long].option.flatMap:
|
|
80
|
-
case Some(active) if active == ownership =>
|
|
81
|
-
CheckpointConflict(task, expectedRevision).raiseError[ConnectionIO, Unit]
|
|
82
|
-
case _ => StaleOwnership(task, ownership).raiseError[ConnectionIO, Unit]
|
|
83
|
-
checked.transact(xa)
|
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
package scada.supervisor.runtime
|
|
2
|
-
|
|
3
|
-
import io.circe.Json
|
|
4
|
-
import io.circe.parser.parse
|
|
5
|
-
import io.yarkivaev.temporal.{Cursor, LocalStreams}
|
|
6
|
-
import java.sql.Connection
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Persists LocalStreams cursor metadata for one machine. Telemetry
|
|
10
|
-
* points stay out of Postgres; only id, position and touchedAt remain.
|
|
11
|
-
*
|
|
12
|
-
* {{{
|
|
13
|
-
* ClassifierCursors.save(connection, "machine-2", pool)
|
|
14
|
-
* ClassifierCursors.load(connection, "machine-2", 30000, 10000)
|
|
15
|
-
* }}}
|
|
16
|
-
*/
|
|
17
|
-
object ClassifierCursors:
|
|
18
|
-
val ddl: String = "CREATE TABLE IF NOT EXISTS classifier_cursors (machine text PRIMARY KEY, snapshot text NOT NULL)"
|
|
19
|
-
val writeSql: String = "INSERT INTO classifier_cursors (machine, snapshot) VALUES (?, ?) ON CONFLICT (machine) DO UPDATE SET snapshot = EXCLUDED.snapshot"
|
|
20
|
-
val readSql: String = "SELECT snapshot FROM classifier_cursors WHERE machine = ?"
|
|
21
|
-
def save(connection: Connection, machine: String, pool: HistoryPool): Unit =
|
|
22
|
-
val created = connection.prepareStatement(ddl)
|
|
23
|
-
try created.execute()
|
|
24
|
-
finally created.close()
|
|
25
|
-
val statement = connection.prepareStatement(writeSql)
|
|
26
|
-
try
|
|
27
|
-
statement.setString(1, machine)
|
|
28
|
-
statement.setString(2, snapshot(pool).noSpaces)
|
|
29
|
-
val _ = statement.executeUpdate()
|
|
30
|
-
finally statement.close()
|
|
31
|
-
def load(connection: Connection, machine: String, maxGap: Long, idleTimeout: Long): HistoryPool =
|
|
32
|
-
val created = connection.prepareStatement(ddl)
|
|
33
|
-
try created.execute()
|
|
34
|
-
finally created.close()
|
|
35
|
-
val statement = connection.prepareStatement(readSql)
|
|
36
|
-
try
|
|
37
|
-
statement.setString(1, machine)
|
|
38
|
-
val rows = statement.executeQuery()
|
|
39
|
-
val pool = if rows.next() then restore(rows.getString(1), maxGap, idleTimeout) else HistoryPool(LocalStreams.empty(maxGap, idleTimeout), Map.empty)
|
|
40
|
-
rows.close()
|
|
41
|
-
pool
|
|
42
|
-
finally statement.close()
|
|
43
|
-
private def snapshot(pool: HistoryPool): Json =
|
|
44
|
-
Json.obj(
|
|
45
|
-
"nextId" -> Json.fromLong(pool.cursors.nextId),
|
|
46
|
-
"streams" -> Json.arr(pool.cursors.streams.map: cursor =>
|
|
47
|
-
Json.obj("id" -> Json.fromLong(cursor.id), "position" -> Json.fromLong(cursor.position), "touchedAt" -> Json.fromLong(cursor.touchedAt))
|
|
48
|
-
*)
|
|
49
|
-
)
|
|
50
|
-
private def restore(body: String, maxGap: Long, idleTimeout: Long): HistoryPool =
|
|
51
|
-
val json = parse(body).getOrElse(Json.obj())
|
|
52
|
-
val nextId = json.hcursor.get[Long]("nextId").getOrElse(1L)
|
|
53
|
-
val streams = json.hcursor.get[Vector[Json]]("streams").getOrElse(Vector.empty).flatMap: row =>
|
|
54
|
-
for
|
|
55
|
-
id <- row.hcursor.get[Long]("id").toOption
|
|
56
|
-
position <- row.hcursor.get[Long]("position").toOption
|
|
57
|
-
yield Cursor(id, position, System.currentTimeMillis())
|
|
58
|
-
HistoryPool(LocalStreams(maxGap, idleTimeout, nextId, streams, Vector.empty), Map.empty)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|