From d399f721b00bd58e5980c6548d0e04b469407e67 Mon Sep 17 00:00:00 2001 From: Xinyuan Lin Date: Wed, 18 Nov 2026 21:06:02 -0800 Subject: [PATCH] test(amber): share engine and service test fixtures Consolidate repeated scheduling, coordinator handler, worker checkpoint, storage, replay, reconfiguration, and execution-service test scaffolding across 28 Amber test files (25 modified, 3 added; +1,416 / -2,209 lines) without altering production code or test assertions. --- .../execution/WorkflowExecutionSpec.scala | 41 +- .../EmbeddedControlMessageHandlerSpec.scala | 68 +-- .../JumpToOperatorRegionHandlerSpec.scala | 34 +- .../RetrieveWorkflowStateHandlerSpec.scala | 86 +--- .../logreplay/ReplayLogGeneratorSpec.scala | 240 +++------- .../scheduling/DefaultCostEstimatorSpec.scala | 101 ++-- .../scheduling/RegionPlanSpec.scala | 33 +- .../architecture/scheduling/RegionSpec.scala | 25 +- .../scheduling/ScheduleSpec.scala | 22 +- .../scheduling/SchedulingUtilsSpec.scala | 21 +- .../architecture/worker/DPThreadSpec.scala | 450 ++++++++---------- .../CheckpointHandlerSpecSupport.scala | 120 +++++ .../FinalizeCheckpointHandlerSpec.scala | 170 ++----- .../PrepareCheckpointHandlerSpec.scala | 112 +---- .../common/BarePhysicalPlanFixture.scala | 63 +++ .../FriesReconfigurationAlgorithmSpec.scala | 294 +++++------- .../storage/HDFSRecordStorageSpec.scala | 148 ++---- .../storage/SequentialRecordStorageSpec.scala | 174 ++++--- .../common/storage/VFSRecordStorageSpec.scala | 133 ++---- .../web/ExecutionDatabaseTestSupport.scala | 220 +++++++++ .../AdminExecutionResourceSpec.scala | 156 ++---- .../service/ExecutionConsoleServiceSpec.scala | 286 +++-------- .../ExecutionReconfigurationServiceSpec.scala | 271 +++++------ .../service/ExecutionStatsServiceSpec.scala | 61 +-- ...ExecutionsMetadataPersistServiceSpec.scala | 108 +---- .../service/RecordingExecutionClient.scala | 17 + .../web/service/ResultExportServiceSpec.scala | 92 +--- .../WorkflowExecutionServiceSpec.scala | 79 +-- 28 files changed, 1416 insertions(+), 2209 deletions(-) create mode 100644 amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/CheckpointHandlerSpecSupport.scala create mode 100644 amber/src/test/scala/org/apache/texera/amber/engine/common/BarePhysicalPlanFixture.scala create mode 100644 amber/src/test/scala/org/apache/texera/web/ExecutionDatabaseTestSupport.scala diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/execution/WorkflowExecutionSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/execution/WorkflowExecutionSpec.scala index 6a6cd675e05..312c0a4f47c 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/execution/WorkflowExecutionSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/execution/WorkflowExecutionSpec.scala @@ -19,36 +19,14 @@ package org.apache.texera.amber.engine.architecture.coordinator.execution -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ActorVirtualIdentity, - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} -import org.apache.texera.amber.core.workflow.{GlobalPortIdentity, PhysicalOp, PortIdentity} +import org.apache.texera.amber.core.virtualidentity.ActorVirtualIdentity import org.apache.texera.amber.engine.architecture.rpc.controlreturns.WorkflowAggregatedState import org.apache.texera.amber.engine.architecture.scheduling.{Region, RegionIdentity} import org.apache.texera.amber.engine.architecture.worker.statistics.WorkerState +import org.apache.texera.amber.engine.common.BarePhysicalPlanFixture import org.scalatest.flatspec.AnyFlatSpec -class WorkflowExecutionSpec extends AnyFlatSpec { - - private def physicalOpId(opId: String): PhysicalOpIdentity = - PhysicalOpIdentity(OperatorIdentity(opId), "main") - - private def op(opId: String): PhysicalOp = - PhysicalOp( - physicalOpId(opId), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) - - /** A region with no ports — its `RegionExecution.getState` defaults to COMPLETED. */ - private def region(regionId: Long, opId: String): Region = - Region(RegionIdentity(regionId), Set(op(opId)), Set.empty) +class WorkflowExecutionSpec extends AnyFlatSpec with BarePhysicalPlanFixture { /** * A region carrying a single (input) port on `opId`. Because a freshly created @@ -57,18 +35,7 @@ class WorkflowExecutionSpec extends AnyFlatSpec { * so its operators' states are actually inspected by `WorkflowExecution.getState`. */ private def regionWithPort(regionId: Long, opId: String): Region = - Region( - RegionIdentity(regionId), - Set(op(opId)), - Set.empty, - ports = Set( - GlobalPortIdentity( - opId = physicalOpId(opId), - portId = PortIdentity(id = 0, internal = false), - input = true - ) - ) - ) + region(regionId, opId, Set(globalPort(opId))) "WorkflowExecution.initRegionExecution" should "create a new RegionExecution for the given region" in { val we = WorkflowExecution() diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/EmbeddedControlMessageHandlerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/EmbeddedControlMessageHandlerSpec.scala index 94037bcad9e..656a623c180 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/EmbeddedControlMessageHandlerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/EmbeddedControlMessageHandlerSpec.scala @@ -19,31 +19,13 @@ package org.apache.texera.amber.engine.architecture.coordinator.promisehandlers -import com.twitter.util.{Await, Duration} -import org.apache.texera.amber.core.executor.OpExecInitInfo +import com.twitter.util.Await import org.apache.texera.amber.core.virtualidentity.{ - ActorVirtualIdentity, ChannelIdentity, - EmbeddedControlMessageIdentity, - OperatorIdentity, - PhysicalOpIdentity -} -import org.apache.texera.amber.core.workflow.WorkflowContext.{ - DEFAULT_EXECUTION_ID, - DEFAULT_WORKFLOW_ID -} -import org.apache.texera.amber.core.workflow.{ - GlobalPortIdentity, - PhysicalLink, - PhysicalOp, - PortIdentity, - WorkflowContext -} -import org.apache.texera.amber.engine.architecture.coordinator.{ - CoordinatorAsyncRPCHandlerInitializer, - CoordinatorConfig, - CoordinatorProcessor + EmbeddedControlMessageIdentity } +import org.apache.texera.amber.core.workflow.{GlobalPortIdentity, PhysicalLink, PortIdentity} +import org.apache.texera.amber.engine.architecture.coordinator.CoordinatorAsyncRPCHandlerInitializer import org.apache.texera.amber.engine.architecture.logreplay.{ReplayLogManager, ReplayLogRecord} import org.apache.texera.amber.engine.architecture.rpc.controlcommands.EmbeddedControlMessageType.{ NO_ALIGNMENT, @@ -62,11 +44,9 @@ import org.apache.texera.amber.engine.architecture.rpc.controlreturns.{ } import org.apache.texera.amber.engine.architecture.rpc.workerservice.WorkerServiceGrpc.METHOD_RETRIEVE_STATE import org.apache.texera.amber.engine.architecture.scheduling.{Region, RegionIdentity} -import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.MainThreadDelegateMessage import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage import org.apache.texera.amber.engine.common.storage.SequentialRecordStorage.SequentialRecordWriter import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR -import org.apache.texera.amber.util.VirtualIdentityUtils import org.scalatest.flatspec.AnyFlatSpec import scala.collection.mutable.ArrayBuffer @@ -89,15 +69,14 @@ import scala.collection.mutable.ArrayBuffer * propagation is still in flight. * - dispatching to workers of a region execution that has already completed. */ -class EmbeddedControlMessageHandlerSpec extends AnyFlatSpec { +class EmbeddedControlMessageHandlerSpec extends AnyFlatSpec with CoordinatorMessageFixture { - private val awaitTimeout = Duration.fromSeconds(1) private val ecmId = EmbeddedControlMessageIdentity("ecm-under-test") private val retrieveState = METHOD_RETRIEVE_STATE.getBareMethodName - private val sourceOp = mkPhysicalOp("source", "main") - private val middleOp = mkPhysicalOp("middle", "main") - private val sinkOp = mkPhysicalOp("sink", "main") + private val sourceOp = mkPhysicalOp("source") + private val middleOp = mkPhysicalOp("middle") + private val sinkOp = mkPhysicalOp("sink") private val sourceWorker = mkWorkerId(sourceOp, 0) private val middleWorker = mkWorkerId(middleOp, 0) private val sinkWorker = mkWorkerId(sinkOp, 0) @@ -105,17 +84,6 @@ class EmbeddedControlMessageHandlerSpec extends AnyFlatSpec { PhysicalLink(sourceOp.id, PortIdentity(), middleOp.id, PortIdentity()) private val middleToSink = PhysicalLink(middleOp.id, PortIdentity(), sinkOp.id, PortIdentity()) - private def mkPhysicalOp(logicalOpId: String, layerName: String): PhysicalOp = - PhysicalOp( - PhysicalOpIdentity(OperatorIdentity(logicalOpId), layerName), - DEFAULT_WORKFLOW_ID, - DEFAULT_EXECUTION_ID, - OpExecInitInfo.Empty - ) - - private def mkWorkerId(physicalOp: PhysicalOp, index: Int): ActorVirtualIdentity = - VirtualIdentityUtils.createWorkerIdentity(DEFAULT_WORKFLOW_ID, physicalOp.id, index) - /** Records the ECM ids the handler marks, so completion ordering can be asserted. */ private class RecordingLogManager extends ReplayLogManager { val marked: ArrayBuffer[EmbeddedControlMessageIdentity] = ArrayBuffer() @@ -123,7 +91,10 @@ class EmbeddedControlMessageHandlerSpec extends AnyFlatSpec { override def setupWriter(logWriter: SequentialRecordWriter[ReplayLogRecord]): Unit = () override def sendCommitted( - msg: Either[MainThreadDelegateMessage, WorkflowFIFOMessage] + msg: Either[ + org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.MainThreadDelegateMessage, + WorkflowFIFOMessage + ] ): Unit = () override def terminate(): Unit = () @@ -138,19 +109,10 @@ class EmbeddedControlMessageHandlerSpec extends AnyFlatSpec { ) private def newFixture(): Fixture = { - val sent = ArrayBuffer[WorkflowFIFOMessage]() - val cp = new CoordinatorProcessor( - new WorkflowContext(), - CoordinatorConfig(None, None, None, None), - COORDINATOR, - { - case Right(m) => sent += m - case _ => () - } - ) + val (init, sent) = createCoordinatorFixture() val logManager = new RecordingLogManager() - cp.setupLogManager(logManager) - Fixture(new CoordinatorAsyncRPCHandlerInitializer(cp), sent, logManager) + init.cp.setupLogManager(logManager) + Fixture(init, sent, logManager) } /** diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/JumpToOperatorRegionHandlerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/JumpToOperatorRegionHandlerSpec.scala index 720b3aa17f0..f589b6f2124 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/JumpToOperatorRegionHandlerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/JumpToOperatorRegionHandlerSpec.scala @@ -19,14 +19,8 @@ package org.apache.texera.amber.engine.architecture.coordinator.promisehandlers -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} -import org.apache.texera.amber.core.workflow.{PhysicalOp, WorkflowContext} +import org.apache.texera.amber.core.virtualidentity.OperatorIdentity +import org.apache.texera.amber.core.workflow.WorkflowContext import org.apache.texera.amber.engine.architecture.coordinator.{ CoordinatorAsyncRPCHandlerInitializer, CoordinatorConfig, @@ -36,24 +30,12 @@ import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{ AsyncRPCContext, JumpToOperatorRegionRequest } -import org.apache.texera.amber.engine.architecture.scheduling.{Region, RegionIdentity, Schedule} +import org.apache.texera.amber.engine.architecture.scheduling.{Region, Schedule} +import org.apache.texera.amber.engine.common.BarePhysicalPlanFixture import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR import org.scalatest.flatspec.AnyFlatSpec -class JumpToOperatorRegionHandlerSpec extends AnyFlatSpec { - - // A single-operator Region, matching the construction pattern used by - // WorkflowExecutionManagerSpec. The logical operator id (opId) is what the handler - // matches against JumpToOperatorRegionRequest.targetOperatorId. - private def singleOpRegion(regionId: Long, opId: String): Region = { - val physicalOp = PhysicalOp( - PhysicalOpIdentity(OperatorIdentity(opId), "main"), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) - Region(RegionIdentity(regionId), Set(physicalOp), Set.empty) - } +class JumpToOperatorRegionHandlerSpec extends AnyFlatSpec with BarePhysicalPlanFixture { // Build a real CoordinatorProcessor + our own initializer (the processor's own // initializer is private/inaccessible). Seed a 3-level, single-op-per-region schedule @@ -66,9 +48,9 @@ class JumpToOperatorRegionHandlerSpec extends AnyFlatSpec { COORDINATOR, _ => () ) - val first = singleOpRegion(1, "first") - val second = singleOpRegion(2, "second") - val third = singleOpRegion(3, "third") + val first = region(1, "first") + val second = region(2, "second") + val third = region(3, "third") cp.workflowExecutionManager.schedule = Schedule( Map( 0 -> Set(first), diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/RetrieveWorkflowStateHandlerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/RetrieveWorkflowStateHandlerSpec.scala index e64373c5ab8..7ffc08d9df0 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/RetrieveWorkflowStateHandlerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/coordinator/promisehandlers/RetrieveWorkflowStateHandlerSpec.scala @@ -20,25 +20,12 @@ package org.apache.texera.amber.engine.architecture.coordinator.promisehandlers import com.twitter.util.{Await, Duration} -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} import org.apache.texera.amber.core.workflow.{ GlobalPortIdentity, - PhysicalOp, PhysicalPlan, PortIdentity, WorkflowContext } -import org.apache.texera.amber.engine.architecture.coordinator.{ - CoordinatorAsyncRPCHandlerInitializer, - CoordinatorConfig, - CoordinatorProcessor -} import org.apache.texera.amber.engine.architecture.rpc.controlcommands.EmbeddedControlMessageType.NO_ALIGNMENT import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{ AsyncRPCContext, @@ -55,12 +42,10 @@ import org.apache.texera.amber.engine.architecture.rpc.controlreturns.{ } import org.apache.texera.amber.engine.architecture.rpc.workerservice.WorkerServiceGrpc.METHOD_RETRIEVE_STATE import org.apache.texera.amber.engine.architecture.scheduling.{Region, RegionIdentity} -import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.MainThreadDelegateMessage import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR import org.apache.texera.amber.engine.e2e.TestUtils.buildWorkflow import org.apache.texera.amber.operator.TestOperators -import org.apache.texera.amber.util.VirtualIdentityUtils import org.scalatest.flatspec.AnyFlatSpec import scala.collection.mutable.ArrayBuffer @@ -78,31 +63,10 @@ import scala.collection.mutable.ArrayBuffer * The harness mirrors the worker-side `EndHandlerSpec`: build a real `CoordinatorProcessor` plus a * `CoordinatorAsyncRPCHandlerInitializer`, then invoke the handler method directly (no ActorSystem). */ -class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec { +class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec with CoordinatorMessageFixture { private val rpcContext = AsyncRPCContext(COORDINATOR, COORDINATOR) - /** - * Build a coordinator handler initializer whose dispatched output messages are appended to the returned - * buffer. `workflowScheduler.physicalPlan` defaults to null, so callers must assign it before invoking the - * handler (which dereferences it). - */ - private def newFixture() - : (CoordinatorAsyncRPCHandlerInitializer, ArrayBuffer[WorkflowFIFOMessage]) = { - val sent = ArrayBuffer[WorkflowFIFOMessage]() - val outputHandler: Either[MainThreadDelegateMessage, WorkflowFIFOMessage] => Unit = { - case Right(m) => sent += m - case _ => () - } - val cp = new CoordinatorProcessor( - new WorkflowContext(), - CoordinatorConfig(None, None, None, None), - COORDINATOR, - outputHandler - ) - (new CoordinatorAsyncRPCHandlerInitializer(cp), sent) - } - /** * Assert that exactly one control message was dispatched and it carries a propagate-ECM invocation, then * return the unwrapped request for further inspection. @@ -111,22 +75,18 @@ class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec { sent: ArrayBuffer[WorkflowFIFOMessage] ): PropagateEmbeddedControlMessageRequest = { assert(sent.size == 1) - sent.head.payload match { - case ci: ControlInvocation => - assert(ci.methodName == "propagateEmbeddedControlMessage") - ci.command match { - case req: PropagateEmbeddedControlMessageRequest => req - case other => fail(s"unexpected control command: $other") - } - case other => fail(s"unexpected dispatched payload: $other") + val invocations = controlInvocationsNamed(sent, "propagateEmbeddedControlMessage") + assert(invocations.size == 1, s"expected propagateEmbeddedControlMessage, got: $sent") + invocations.head.command match { + case req: PropagateEmbeddedControlMessageRequest => req + case other => fail(s"unexpected control command: $other") } } behavior of "RetrieveWorkflowStateHandler" it should "dispatch a single propagate-ECM control message with no targets for an empty plan" in { - val (init, sent) = newFixture() - init.cp.workflowScheduler.physicalPlan = PhysicalPlan(Set.empty, Set.empty) + val (init, sent) = createCoordinatorFixture(Some(PhysicalPlan(Set.empty, Set.empty))) init.retrieveWorkflowState(EmptyRequest(), rpcContext) @@ -147,8 +107,7 @@ class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec { List(), new WorkflowContext() ).physicalPlan - val (init, sent) = newFixture() - init.cp.workflowScheduler.physicalPlan = physicalPlan + val (init, sent) = createCoordinatorFixture(Some(physicalPlan)) init.retrieveWorkflowState(EmptyRequest(), rpcContext) @@ -161,8 +120,7 @@ class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec { } it should "map StringResponse replies to their strings and fallback other returns to empty strings" in { - val (init, sent) = newFixture() - init.cp.workflowScheduler.physicalPlan = PhysicalPlan(Set.empty, Set.empty) + val (init, sent) = createCoordinatorFixture(Some(PhysicalPlan(Set.empty, Set.empty))) val response = init.retrieveWorkflowState(EmptyRequest(), rpcContext) assert(sent.size == 1) @@ -184,16 +142,8 @@ class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec { } it should "include only running region operators in sourceOpToStartProp" in { - def mkOp(id: String): PhysicalOp = - PhysicalOp( - PhysicalOpIdentity(OperatorIdentity(id), "main"), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) - - val runningOp = mkOp("running") - val completedOp = mkOp("completed") + val runningOp = mkPhysicalOp("running") + val completedOp = mkPhysicalOp("completed") val runningRegion = Region( RegionIdentity(1), physicalOps = Set(runningOp), @@ -206,22 +156,16 @@ class RetrieveWorkflowStateHandlerSpec extends AnyFlatSpec { physicalLinks = Set.empty, ports = Set(GlobalPortIdentity(completedOp.id, PortIdentity(), input = false)) ) - val (init, sent) = newFixture() + val (init, sent) = + createCoordinatorFixture(Some(PhysicalPlan(Set(runningOp, completedOp), Set.empty))) val runningExec = init.cp.workflowExecution.initRegionExecution(runningRegion) - runningExec - .initOperatorExecution(runningOp.id) - .initWorkerExecution( - VirtualIdentityUtils.createWorkerIdentity(WorkflowIdentity(0), runningOp.id, 0) - ) + runningExec.initOperatorExecution(runningOp.id).initWorkerExecution(mkWorkerId(runningOp, 0)) val completedExec = init.cp.workflowExecution.initRegionExecution(completedRegion) completedExec .initOperatorExecution(completedOp.id) - .initWorkerExecution( - VirtualIdentityUtils.createWorkerIdentity(WorkflowIdentity(0), completedOp.id, 0) - ) + .initWorkerExecution(mkWorkerId(completedOp, 0)) .getOutputPortExecution(PortIdentity()) .setCompleted() - init.cp.workflowScheduler.physicalPlan = PhysicalPlan(Set(runningOp, completedOp), Set.empty) init.retrieveWorkflowState(EmptyRequest(), rpcContext) diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/logreplay/ReplayLogGeneratorSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/logreplay/ReplayLogGeneratorSpec.scala index d2f67371fea..fc1b828e6ee 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/logreplay/ReplayLogGeneratorSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/logreplay/ReplayLogGeneratorSpec.scala @@ -30,12 +30,10 @@ import org.apache.texera.amber.engine.common.ambermessage.{DataFrame, WorkflowFI import org.apache.texera.amber.engine.common.storage.{SequentialRecordStorage, VFSRecordStorage} import org.scalatest.flatspec.AnyFlatSpec -import java.nio.file.{Files, Path} +import java.nio.file.Files class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { - private def cleanup(sub: Path): Unit = FileUtils.deleteQuietly(sub.getParent.toFile) - // --------------------------------------------------------------------------- // Fixtures // --------------------------------------------------------------------------- @@ -45,31 +43,40 @@ class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { private val destA: EmbeddedControlMessageIdentity = EmbeddedControlMessageIdentity("dest-A") private val destB: EmbeddedControlMessageIdentity = EmbeddedControlMessageIdentity("dest-B") - private def newStorage(): (Path, SequentialRecordStorage[ReplayLogRecord]) = { + private def step(seq: Long): ProcessingStep = ProcessingStep(cid, seq) + + private def msg(seq: Long): WorkflowFIFOMessage = + WorkflowFIFOMessage(cid, seq, DataFrame(Array.empty)) + + private def generateFrom(records: ReplayLogRecord*) = { val root = Files.createTempDirectory("replay-log-generator-spec-") val sub = root.resolve("logs") val storage = new VFSRecordStorage[ReplayLogRecord](sub.toUri) - (sub, storage) - } - - private def writeLog( - storage: SequentialRecordStorage[ReplayLogRecord], - records: Seq[ReplayLogRecord] - ): Unit = { val writer = storage.getWriter("log") // Close in a finally so a serialization failure mid-write does not leak // the underlying output stream (which would otherwise block temp-dir // cleanup, especially on Windows). try { - records.foreach(writer.writeRecord) - writer.flush() + try { + records.foreach(writer.writeRecord) + writer.flush() + } finally { + writer.close() + } + ReplayLogGenerator.generate(storage, "log", destA) } finally { - writer.close() + FileUtils.deleteQuietly(sub.getParent.toFile) } } - private def msg(seq: Long): WorkflowFIFOMessage = - WorkflowFIFOMessage(cid, seq, DataFrame(Array.empty)) + private def assertGenerated( + expectedSteps: Seq[ProcessingStep], + expectedMessages: Seq[WorkflowFIFOMessage] + )(records: ReplayLogRecord*): Unit = { + val (steps, messages) = generateFrom(records: _*) + assert(steps.toList == expectedSteps) + assert(messages.toList == expectedMessages) + } // --------------------------------------------------------------------------- // Empty storage @@ -84,15 +91,7 @@ class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { } it should "return empty queues when the storage file exists but holds no records" in { - val (sub, storage) = newStorage() - try { - writeLog(storage, Seq.empty) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.isEmpty) - assert(messages.isEmpty) - } finally { - cleanup(sub) - } + assertGenerated(Nil, Nil)() } // --------------------------------------------------------------------------- @@ -100,57 +99,27 @@ class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { // --------------------------------------------------------------------------- it should "enqueue all ProcessingStep records into the steps queue (preserving order)" in { - val (sub, storage) = newStorage() - try { - val recs = Seq( - ProcessingStep(cid, 1L), - ProcessingStep(cid, 2L), - ProcessingStep(cid, 3L) - ) - writeLog(storage, recs) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.toList == recs) - assert(messages.isEmpty) - } finally { - cleanup(sub) - } + val recs = Seq(step(1L), step(2L), step(3L)) + assertGenerated(recs, Nil)(recs: _*) } it should "enqueue all MessageContent records into the messages queue (preserving order)" in { - val (sub, storage) = newStorage() - try { - val m1 = msg(1L) - val m2 = msg(2L) - writeLog(storage, Seq(MessageContent(m1), MessageContent(m2))) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.isEmpty) - // The pair is round-tripped through Kryo; the message reference is - // not preserved (a fresh deserialized copy comes back) — so compare - // by case-class equality, not `eq`. - assert(messages.toList == List(m1, m2)) - } finally { - cleanup(sub) - } + val (m1, m2) = (msg(1L), msg(2L)) + // The pair is round-tripped through Kryo; the message reference is + // not preserved (a fresh deserialized copy comes back) — so compare + // by case-class equality, not `eq`. + assertGenerated(Nil, Seq(m1, m2))(MessageContent(m1), MessageContent(m2)) } it should "partition steps and messages independently when records are interleaved" in { - val (sub, storage) = newStorage() - try { - val s1 = ProcessingStep(cid, 1L) - val s2 = ProcessingStep(cid, 2L) - val m1 = msg(1L) - val m2 = msg(2L) - writeLog( - storage, - Seq(s1, MessageContent(m1), s2, MessageContent(m2)) - ) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.toList == List(s1, s2)) - assert(messages.toList == List(m1, m2)) - } finally { - cleanup(sub) - } + val (s1, s2, m1, m2) = (step(1L), step(2L), msg(1L), msg(2L)) + assertGenerated(Seq(s1, s2), Seq(m1, m2))( + s1, + MessageContent(m1), + s2, + MessageContent(m2) + ) } // --------------------------------------------------------------------------- @@ -159,72 +128,35 @@ class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { it should "short-circuit at the matching ReplayDestination, ignoring records that follow it" in { - val (sub, storage) = newStorage() - try { - val s1 = ProcessingStep(cid, 1L) - val m1 = msg(1L) - val m2Past = msg(2L) // must NOT appear in the result - val s2Past = ProcessingStep(cid, 99L) // must NOT appear either - writeLog( - storage, - Seq( - s1, - MessageContent(m1), - ReplayDestination(destA), // <-- replayTo target; iteration stops here - MessageContent(m2Past), - s2Past - ) - ) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.toList == List(s1)) - assert(messages.toList == List(m1)) - } finally { - cleanup(sub) - } + val (s1, m1) = (step(1L), msg(1L)) + assertGenerated(Seq(s1), Seq(m1))( + s1, + MessageContent(m1), + ReplayDestination(destA), // <-- replayTo target; iteration stops here + MessageContent(msg(2L)), // must NOT appear in the result + step(99L) // must NOT appear either + ) } it should "silently skip a ReplayDestination whose id does not match replayTo (iteration continues)" in { - val (sub, storage) = newStorage() - try { - val s1 = ProcessingStep(cid, 1L) - val m1 = msg(1L) - writeLog( - storage, - Seq( - s1, - ReplayDestination(destB), // <-- different id; skipped - MessageContent(m1) - ) - ) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.toList == List(s1)) - assert(messages.toList == List(m1)) - } finally { - cleanup(sub) - } + val (s1, m1) = (step(1L), msg(1L)) + assertGenerated(Seq(s1), Seq(m1))( + s1, + ReplayDestination(destB), // <-- different id; skipped + MessageContent(m1) + ) } it should "stop at the FIRST matching ReplayDestination when multiple matching records exist" in { - val (sub, storage) = newStorage() - try { - val s1 = ProcessingStep(cid, 1L) - val s2Past = ProcessingStep(cid, 2L) - writeLog( - storage, - Seq( - s1, - ReplayDestination(destA), // <-- first match - s2Past, // <-- after the cut - ReplayDestination(destA) - ) - ) - val (steps, _) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.toList == List(s1)) - } finally { - cleanup(sub) - } + val s1 = step(1L) + assertGenerated(Seq(s1), Nil)( + s1, + ReplayDestination(destA), // <-- first match + step(2L), // <-- after the cut + ReplayDestination(destA) + ) } // --------------------------------------------------------------------------- @@ -232,19 +164,13 @@ class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { // --------------------------------------------------------------------------- it should "throw RuntimeException on an unhandled record type (e.g. TerminateSignal)" in { - val (sub, storage) = newStorage() - try { - writeLog(storage, Seq(TerminateSignal)) - val ex = intercept[RuntimeException] { - ReplayLogGenerator.generate(storage, "log", destA) - } - assert( - ex.getMessage.toLowerCase.contains("cannot handle"), - s"expected diagnostic message about unhandled record, got: ${ex.getMessage}" - ) - } finally { - cleanup(sub) + val ex = intercept[RuntimeException] { + generateFrom(TerminateSignal) } + assert( + ex.getMessage.toLowerCase.contains("cannot handle"), + s"expected diagnostic message about unhandled record, got: ${ex.getMessage}" + ) } // --------------------------------------------------------------------------- @@ -252,30 +178,16 @@ class ReplayLogGeneratorSpec extends AnyFlatSpec with AmberSerdeTestSupport { // --------------------------------------------------------------------------- it should "handle a realistic mix of steps + messages + non-matching destinations" in { - val (sub, storage) = newStorage() - try { - val s1 = ProcessingStep(cid, 10L) - val s2 = ProcessingStep(cid, 20L) - val s3 = ProcessingStep(cid, 30L) - val m1 = msg(1L) - val m2 = msg(2L) - writeLog( - storage, - Seq( - s1, - MessageContent(m1), - ReplayDestination(destB), // skipped (id mismatch) - s2, - MessageContent(m2), - ReplayDestination(destB), // also skipped - s3 - ) - ) - val (steps, messages) = ReplayLogGenerator.generate(storage, "log", destA) - assert(steps.toList == List(s1, s2, s3)) - assert(messages.toList == List(m1, m2)) - } finally { - cleanup(sub) - } + val (s1, s2, s3) = (step(10L), step(20L), step(30L)) + val (m1, m2) = (msg(1L), msg(2L)) + assertGenerated(Seq(s1, s2, s3), Seq(m1, m2))( + s1, + MessageContent(m1), + ReplayDestination(destB), // skipped (id mismatch) + s2, + MessageContent(m2), + ReplayDestination(destB), // also skipped + s3 + ) } } diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/DefaultCostEstimatorSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/DefaultCostEstimatorSpec.scala index 607330fd155..c3438d5d387 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/DefaultCostEstimatorSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/DefaultCostEstimatorSpec.scala @@ -35,11 +35,10 @@ import org.apache.texera.amber.operator.TestOperators import org.apache.texera.amber.operator.aggregate.{AggregateOpDesc, AggregationFunction} import org.apache.texera.amber.operator.keywordSearch.KeywordSearchOpDesc import org.apache.texera.amber.operator.source.scan.csv.CSVScanSourceOpDesc +import org.apache.texera.common.compiler.model.LogicalLink import org.apache.texera.dao.MockTexeraDB import org.apache.texera.dao.jooq.generated.enums.UserRoleEnum -import org.apache.texera.dao.jooq.generated.tables.daos._ -import org.apache.texera.dao.jooq.generated.tables.pojos._ -import org.apache.texera.common.compiler.model.LogicalLink +import org.apache.texera.web.ExecutionDatabaseTestSupport import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} @@ -51,7 +50,8 @@ class DefaultCostEstimatorSpec extends AnyFlatSpec with BeforeAndAfterAll with BeforeAndAfterEach - with MockTexeraDB { + with MockTexeraDB + with ExecutionDatabaseTestSupport { private val headerlessCsvOpDesc: CSVScanSourceOpDesc = TestOperators.headerlessSmallCsvScanOpDesc() @@ -60,55 +60,48 @@ class DefaultCostEstimatorSpec private val groupByOpDesc: AggregateOpDesc = TestOperators.aggregateAndGroupByDesc("column-1", AggregationFunction.COUNT, List[String]()) - private val testUser: User = { - val user = new User - user.setUid(Integer.valueOf(1)) - user.setName("test_user") - user.setRole(UserRoleEnum.ADMIN) - user.setEmail("test_user@test.com") - user - } - - private val testWorkflowEntry: Workflow = { - val workflow = new Workflow - workflow.setName("test workflow") - workflow.setWid(Integer.valueOf(1)) - workflow.setContent("test workflow content") - workflow.setDescription("test description") - workflow - } - - private val testWorkflowVersionEntry: WorkflowVersion = { - val workflowVersion = new WorkflowVersion - workflowVersion.setWid(Integer.valueOf(1)) - workflowVersion.setVid(Integer.valueOf(1)) - workflowVersion.setContent("test version content") - workflowVersion - } - - private val testWorkflowExecutionEntry: WorkflowExecutions = { - val workflowExecution = new WorkflowExecutions - workflowExecution.setEid(Integer.valueOf(1)) - workflowExecution.setVid(Integer.valueOf(1)) - workflowExecution.setUid(Integer.valueOf(1)) - workflowExecution.setStatus(3.toByte) - workflowExecution.setEnvironmentVersion("test engine") - workflowExecution - } - private var uri: URI = _ private var writer: BufferedItemWriter[Tuple] = _ private var document: VirtualDocument[_] = _ + private def seedExecutionRecord(): Unit = { + insertUser(1, "test_user", "test_user@test.com", role = Some(UserRoleEnum.ADMIN)) + insertWorkflow( + 1, + "test workflow", + content = "test workflow content", + description = "test description", + withTimestamps = false + ) + insertWorkflowVersion( + 1, + vidOpt = Some(1), + content = "test version content", + withTimestamp = false + ) + insertWorkflowExecution( + vid = 1, + uid = 1, + eidOpt = Some(1), + status = 3.toByte, + name = "", + envVersion = "test engine", + result = None, + runtimeStatsUri = Some(uri.toString), + bookmarked = None, + startOffsetMillis = None + ) + } + override protected def beforeEach(): Unit = { initializeDBAndReplaceDSLContext() uri = VFSURIFactory.createRuntimeStatisticsURI( - WorkflowIdentity(testWorkflowEntry.getWid.longValue()), - ExecutionIdentity(testWorkflowExecutionEntry.getEid.longValue()) + WorkflowIdentity(1L), + ExecutionIdentity(1L) ) document = DocumentFactory.createDocument(uri, ResultSchema.runtimeStatisticsSchema) writer = document - .writer(s"runtime_statistics_${testWorkflowExecutionEntry.getEid.longValue()}") + .writer("runtime_statistics_1") .asInstanceOf[BufferedItemWriter[Tuple]] writer.open() } @@ -177,16 +170,7 @@ class DefaultCostEstimatorSpec new WorkflowContext() ) - val userDao = new UserDao(getDSLContext.configuration()) - val workflowDao = new WorkflowDao(getDSLContext.configuration()) - val workflowExecutionsDao = new WorkflowExecutionsDao(getDSLContext.configuration()) - val workflowVersionDao = new WorkflowVersionDao(getDSLContext.configuration()) - - userDao.insert(testUser) - workflowDao.insert(testWorkflowEntry) - workflowVersionDao.insert(testWorkflowVersionEntry) - testWorkflowExecutionEntry.setRuntimeStatsUri(uri.toString) - workflowExecutionsDao.insert(testWorkflowExecutionEntry) + seedExecutionRecord() val headerlessCsvOpRuntimeStatistics = new Tuple( ResultSchema.runtimeStatisticsSchema, @@ -278,16 +262,7 @@ class DefaultCostEstimatorSpec new WorkflowContext() ) - val userDao = new UserDao(getDSLContext.configuration()) - val workflowDao = new WorkflowDao(getDSLContext.configuration()) - val workflowExecutionsDao = new WorkflowExecutionsDao(getDSLContext.configuration()) - val workflowVersionDao = new WorkflowVersionDao(getDSLContext.configuration()) - - userDao.insert(testUser) - workflowDao.insert(testWorkflowEntry) - workflowVersionDao.insert(testWorkflowVersionEntry) - testWorkflowExecutionEntry.setRuntimeStatsUri(uri.toString) - workflowExecutionsDao.insert(testWorkflowExecutionEntry) + seedExecutionRecord() val headerlessCsvOpRuntimeStatistics = new Tuple( ResultSchema.runtimeStatisticsSchema, diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionPlanSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionPlanSpec.scala index 5ac3fd985b2..f8d71ea91de 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionPlanSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionPlanSpec.scala @@ -19,39 +19,10 @@ package org.apache.texera.amber.engine.architecture.scheduling -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} -import org.apache.texera.amber.core.workflow.{ - GlobalPortIdentity, - PhysicalLink, - PhysicalOp, - PortIdentity -} +import org.apache.texera.amber.engine.common.BarePhysicalPlanFixture import org.scalatest.flatspec.AnyFlatSpec -class RegionPlanSpec extends AnyFlatSpec { - - private def physicalOpId(opId: String): PhysicalOpIdentity = - PhysicalOpIdentity(OperatorIdentity(opId), "main") - - private def op(opId: String): PhysicalOp = - PhysicalOp( - physicalOpId(opId), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) - - private def link(fromOp: String, toOp: String): PhysicalLink = - PhysicalLink(physicalOpId(fromOp), PortIdentity(0), physicalOpId(toOp), PortIdentity(0)) - - private def globalPort(opId: String): GlobalPortIdentity = - GlobalPortIdentity(physicalOpId(opId), PortIdentity(0), input = true) +class RegionPlanSpec extends AnyFlatSpec with BarePhysicalPlanFixture { "RegionPlan.getRegion" should "return the region with the given id" in { val r0 = Region(RegionIdentity(0), Set(op("a")), Set.empty) diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionSpec.scala index 42236132bea..8974a33cf88 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/RegionSpec.scala @@ -19,18 +19,10 @@ package org.apache.texera.amber.engine.architecture.scheduling -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} import org.apache.texera.amber.core.workflow.{ GlobalPortIdentity, InputPort, OutputPort, - PhysicalLink, PhysicalOp, PortIdentity } @@ -39,31 +31,18 @@ import org.apache.texera.amber.engine.architecture.scheduling.config.{ OutputPortConfig, ResourceConfig } +import org.apache.texera.amber.engine.common.BarePhysicalPlanFixture import org.scalatest.flatspec.AnyFlatSpec import java.net.URI -class RegionSpec extends AnyFlatSpec { - - private def physicalOpId(opId: String): PhysicalOpIdentity = - PhysicalOpIdentity(OperatorIdentity(opId), "main") - - private def op(opId: String): PhysicalOp = - PhysicalOp( - physicalOpId(opId), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) +class RegionSpec extends AnyFlatSpec with BarePhysicalPlanFixture { private def wiredOp(opId: String): PhysicalOp = op(opId) .withInputPorts(List(InputPort(PortIdentity(0)))) .withOutputPorts(List(OutputPort(PortIdentity(0)))) - private def link(fromOp: String, toOp: String): PhysicalLink = - PhysicalLink(physicalOpId(fromOp), PortIdentity(0), physicalOpId(toOp), PortIdentity(0)) - "Region" should "expose the physical operators provided at construction" in { val a = op("a") val b = op("b") diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/ScheduleSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/ScheduleSpec.scala index 95dda9b6754..b4709fb2d1b 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/ScheduleSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/ScheduleSpec.scala @@ -19,28 +19,12 @@ package org.apache.texera.amber.engine.architecture.scheduling -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} -import org.apache.texera.amber.core.workflow.{PhysicalOp, PhysicalPlan, WorkflowContext} +import org.apache.texera.amber.core.workflow.{PhysicalPlan, WorkflowContext} +import org.apache.texera.amber.engine.common.BarePhysicalPlanFixture import org.apache.texera.common.config.ApplicationConfig import org.scalatest.flatspec.AnyFlatSpec -class ScheduleSpec extends AnyFlatSpec { - - private def region(regionId: Long, opId: String): Region = { - val physicalOp = PhysicalOp( - PhysicalOpIdentity(OperatorIdentity(opId), "main"), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) - Region(RegionIdentity(regionId), Set(physicalOp), Set.empty) - } +class ScheduleSpec extends AnyFlatSpec with BarePhysicalPlanFixture { "Schedule.getRegions" should "return all regions across all levels" in { val r0 = region(0, "a") diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/SchedulingUtilsSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/SchedulingUtilsSpec.scala index 18ee5e88f12..b42032304ce 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/SchedulingUtilsSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/scheduling/SchedulingUtilsSpec.scala @@ -19,30 +19,13 @@ package org.apache.texera.amber.engine.architecture.scheduling -import org.apache.texera.amber.core.executor.OpExecInitInfo -import org.apache.texera.amber.core.virtualidentity.{ - ExecutionIdentity, - OperatorIdentity, - PhysicalOpIdentity, - WorkflowIdentity -} -import org.apache.texera.amber.core.workflow.PhysicalOp +import org.apache.texera.amber.engine.common.BarePhysicalPlanFixture import org.jgrapht.graph.DirectedAcyclicGraph import org.scalatest.flatspec.AnyFlatSpec import scala.jdk.CollectionConverters.CollectionHasAsScala -class SchedulingUtilsSpec extends AnyFlatSpec { - - private def region(regionId: Long, opId: String): Region = { - val physicalOp = PhysicalOp( - PhysicalOpIdentity(OperatorIdentity(opId), "main"), - WorkflowIdentity(0), - ExecutionIdentity(0), - OpExecInitInfo.Empty - ) - Region(RegionIdentity(regionId), Set(physicalOp), Set.empty) - } +class SchedulingUtilsSpec extends AnyFlatSpec with BarePhysicalPlanFixture { private def newGraph(): DirectedAcyclicGraph[Region, RegionLink] = new DirectedAcyclicGraph[Region, RegionLink](classOf[RegionLink]) diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DPThreadSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DPThreadSpec.scala index e441619886a..e99c15a6621 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DPThreadSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/DPThreadSpec.scala @@ -94,31 +94,66 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { cond } - /** A DataProcessor wired to one input port on `dataChannelId`, driven by `queue`. - * `outputHandler` defaults to swallowing everything; pass one in to observe what the - * DP thread hands back to the main thread. - */ + /** A DataProcessor wired to `dataChannelId` (plus `extraSender` if given), driven by `queue`. */ private def newDataProcessor( queue: LinkedBlockingQueue[DPInputQueueElement], - outputHandler: Either[MainThreadDelegateMessage, WorkflowFIFOMessage] => Unit = _ => {} + outputHandler: Either[MainThreadDelegateMessage, WorkflowFIFOMessage] => Unit = _ => {}, + executor: OperatorExecutor = null, + extraSender: Option[ActorVirtualIdentity] = None, + dp: DataProcessor = null ): DataProcessor = { - val dp = new DataProcessor(workerId, outputHandler, inputMessageQueue = queue) - dp.inputManager.addPort(mockInputPortId, schema, List.empty, List.empty) - dp.inputGateway.getChannel(dataChannelId).setPortId(mockInputPortId) - dp.adaptiveBatchingMonitor = mock[WorkerTimerService] - (dp.adaptiveBatchingMonitor.startAdaptiveBatching _).expects().anyNumberOfTimes() - dp + val proc = + if (dp != null) dp else new DataProcessor(workerId, outputHandler, inputMessageQueue = queue) + if (executor != null) proc.executor = executor + proc.inputManager.addPort(mockInputPortId, schema, List.empty, List.empty) + proc.inputGateway.getChannel(dataChannelId).setPortId(mockInputPortId) + extraSender.foreach(id => + proc.inputGateway + .getChannel(ChannelIdentity(id, workerId, isControl = false)) + .setPortId(mockInputPortId) + ) + proc.adaptiveBatchingMonitor = mock[WorkerTimerService] + (proc.adaptiveBatchingMonitor.startAdaptiveBatching _).expects().anyNumberOfTimes() + proc } + private def withStartedDPThread( + onTuple: (Tuple, Int) => Iterator[TupleLike] = null, + dpFactory: LinkedBlockingQueue[DPInputQueueElement] => DataProcessor = null, + logMgr: ReplayLogManager = logManager, + baseUriOpt: Option[String] = None + )( + body: (LinkedBlockingQueue[DPInputQueueElement], DataProcessor, DPThread) => Unit + ): Unit = { + val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() + val dp = if (dpFactory != null) dpFactory(inputQueue) else newDataProcessor(inputQueue) + if (onTuple != null) { + dp.executor = new OperatorExecutor { + override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = + onTuple(tuple, port) + } + } + val dpThread = baseUriOpt match { + case Some(uri) => new DPThread(workerId, dp, logMgr, inputQueue, uri) + case None => new DPThread(workerId, dp, logMgr, inputQueue) + } + try { + dpThread.start() + body(inputQueue, dp, dpThread) + } finally { + dpThread.stop() + } + } + + private def awaitUntil(cond: => Boolean, msg: => String, budgetMs: Int = 5000): Unit = + assert(awaitCond(cond, budgetMs), msg) + private def dataFrameOf(count: Int, seq: Long = 0): FIFOMessageElement = - FIFOMessageElement( - WorkflowFIFOMessage(dataChannelId, seq, DataFrame(tuples.slice(0, count))) - ) + FIFOMessageElement(WorkflowFIFOMessage(dataChannelId, seq, DataFrame(tuples.slice(0, count)))) private def expectTuplesProcessed(): Unit = { tuples.foreach { x => - ((tuple: Tuple, input: Int) => executor.processTupleMultiPort(tuple, input)) - .expects(x, 0) + ((tuple: Tuple, input: Int) => executor.processTupleMultiPort(tuple, input)).expects(x, 0) } } @@ -126,61 +161,46 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { pauseElement: DPInputQueueElement, resumeElement: DPInputQueueElement ): Unit = { - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = new DataProcessor(workerId, _ => {}, inputMessageQueue = inputQueue) - dp.executor = executor - dp.inputManager.addPort(mockInputPortId, schema, List.empty, List.empty) - dp.inputGateway.getChannel(dataChannelId).setPortId(mockInputPortId) - dp.adaptiveBatchingMonitor = mock[WorkerTimerService] - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - dpThread.start() expectTuplesProcessed() - val message = WorkflowFIFOMessage(dataChannelId, 0, DataFrame(tuples)) - inputQueue.put(FIFOMessageElement(message)) - inputQueue.put(pauseElement) - Thread.sleep(1000) - assert(dp.pauseManager.isPaused) - inputQueue.put(resumeElement) - Thread.sleep(1000) - while (dp.inputManager.hasUnfinishedInput) { - Thread.sleep(100) + withStartedDPThread(dpFactory = q => newDataProcessor(q, executor = executor)) { + (inputQueue, dp, _) => + inputQueue.put(FIFOMessageElement(WorkflowFIFOMessage(dataChannelId, 0, DataFrame(tuples)))) + inputQueue.put(pauseElement) + Thread.sleep(1000) + assert(dp.pauseManager.isPaused) + inputQueue.put(resumeElement) + Thread.sleep(1000) + while (dp.inputManager.hasUnfinishedInput) { + Thread.sleep(100) + } } } - private def newMultiSourceDataProcessor( - inputQueue: LinkedBlockingQueue[DPInputQueueElement], - anotherSenderWorkerId: ActorVirtualIdentity - ): DataProcessor = { - val dp = new DataProcessor(workerId, _ => {}, inputMessageQueue = inputQueue) - dp.executor = executor - dp.inputManager.addPort(mockInputPortId, schema, List.empty, List.empty) - dp.inputGateway.getChannel(dataChannelId).setPortId(mockInputPortId) - dp.inputGateway - .getChannel(ChannelIdentity(anotherSenderWorkerId, workerId, isControl = false)) - .setPortId(mockInputPortId) - dp.adaptiveBatchingMonitor = mock[WorkerTimerService] - dp - } - - private def feedMultiSourceBatches( - inputQueue: LinkedBlockingQueue[DPInputQueueElement], - dataChannelId2: ChannelIdentity, + private def feedMultiSourceAndWait( + logMgr: ReplayLogManager, pauseBetweenBatches: Boolean - ): Unit = { - val message1 = WorkflowFIFOMessage(dataChannelId, 0, DataFrame(tuples.slice(0, 100))) - val message2 = WorkflowFIFOMessage(dataChannelId, 1, DataFrame(tuples.slice(100, 200))) - val message3 = WorkflowFIFOMessage(dataChannelId2, 0, DataFrame(tuples.slice(300, 1000))) - val message4 = WorkflowFIFOMessage(dataChannelId, 2, DataFrame(tuples.slice(200, 300))) - val message5 = WorkflowFIFOMessage(dataChannelId2, 1, DataFrame(tuples.slice(1000, 5000))) - inputQueue.put(FIFOMessageElement(message1)) - inputQueue.put(FIFOMessageElement(message2)) - inputQueue.put(FIFOMessageElement(message3)) - if (pauseBetweenBatches) { + )(waitFor: (DataProcessor, ChannelIdentity) => Unit): Unit = { + val anotherSenderWorkerId = ActorVirtualIdentity("another") + val dataChannelId2 = ChannelIdentity(anotherSenderWorkerId, workerId, isControl = false) + expectTuplesProcessed() + withStartedDPThread( + dpFactory = + q => newDataProcessor(q, executor = executor, extraSender = Some(anotherSenderWorkerId)), + logMgr = logMgr + ) { (inputQueue, dp, _) => + Seq( + WorkflowFIFOMessage(dataChannelId, 0, DataFrame(tuples.slice(0, 100))), + WorkflowFIFOMessage(dataChannelId, 1, DataFrame(tuples.slice(100, 200))), + WorkflowFIFOMessage(dataChannelId2, 0, DataFrame(tuples.slice(300, 1000))) + ).foreach(m => inputQueue.put(FIFOMessageElement(m))) + if (pauseBetweenBatches) Thread.sleep(1000) + Seq( + WorkflowFIFOMessage(dataChannelId, 2, DataFrame(tuples.slice(200, 300))), + WorkflowFIFOMessage(dataChannelId2, 1, DataFrame(tuples.slice(1000, 5000))) + ).foreach(m => inputQueue.put(FIFOMessageElement(m))) Thread.sleep(1000) + waitFor(dp, dataChannelId2) } - inputQueue.put(FIFOMessageElement(message4)) - inputQueue.put(FIFOMessageElement(message5)) - Thread.sleep(1000) } private def assertMidBatchStall( @@ -193,23 +213,16 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { liftStall: LinkedBlockingQueue[DPInputQueueElement] => Unit, stallLabel: String ): Unit = { - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = newDataProcessor(inputQueue) val processed = new AtomicInteger(0) val firstTuple = new CountDownLatch(1) val gate = new CountDownLatch(1) - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { - if (processed.getAndIncrement() == 0) { - firstTuple.countDown() - gate.await() - } - Iterator.empty + withStartedDPThread(onTuple = (_, _) => { + if (processed.getAndIncrement() == 0) { + firstTuple.countDown() + gate.await() } - } - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - try { - dpThread.start() + Iterator.empty + }) { (inputQueue, dp, dpThread) => inputQueue.put(dataFrameOf(2000)) assert(firstTuple.await(5, TimeUnit.SECONDS), "batch must start") triggerStall(dp, inputQueue) @@ -229,9 +242,7 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { ) liftStall(inputQueue) - assert(awaitCond(processed.get() == 2000), s"drained ${processed.get()} of 2000") - } finally { - dpThread.stop() + awaitUntil(processed.get() == 2000, s"drained ${processed.get()} of 2000") } } @@ -266,42 +277,29 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { } "DP Thread" should "handle multiple batches from multiple sources" in { - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val anotherSenderWorkerId = ActorVirtualIdentity("another") - val dp = newMultiSourceDataProcessor(inputQueue, anotherSenderWorkerId) - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - dpThread.start() - expectTuplesProcessed() - feedMultiSourceBatches( - inputQueue, - ChannelIdentity(anotherSenderWorkerId, workerId, isControl = false), - pauseBetweenBatches = false - ) - while (dp.inputManager.hasUnfinishedInput) { - Thread.sleep(100) + feedMultiSourceAndWait(logManager, pauseBetweenBatches = false) { (dp, _) => + while (dp.inputManager.hasUnfinishedInput) { + Thread.sleep(100) + } } } "DP Thread" should "write determinant logs to local storage while processing" in { - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val anotherSenderWorkerId = ActorVirtualIdentity("another") - val dp = newMultiSourceDataProcessor(inputQueue, anotherSenderWorkerId) val logStorage = SequentialRecordStorage.getStorage[ReplayLogRecord]( Some(new URI("ram:///recovery-logs/tmp")) ) logStorage.deleteStorage() - val logManager: ReplayLogManager = + val localLogManager: ReplayLogManager = ReplayLogManager.createLogManager(logStorage, "tmpLog", _ => {}) - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - dpThread.start() - expectTuplesProcessed() - val dataChannelId2 = ChannelIdentity(anotherSenderWorkerId, workerId, isControl = false) - feedMultiSourceBatches(inputQueue, dataChannelId2, pauseBetweenBatches = true) - while (logManager.getStep < 4999) { - Thread.sleep(100) + var dataChannelId2: ChannelIdentity = null + feedMultiSourceAndWait(localLogManager, pauseBetweenBatches = true) { (_, ch2) => + dataChannelId2 = ch2 + while (localLogManager.getStep < 4999) { + Thread.sleep(100) + } + localLogManager.sendCommitted(null) // drain in-mem records to flush + localLogManager.terminate() } - logManager.sendCommitted(null) // drain in-mem records to flush - logManager.terminate() val logs = logStorage.getReader("tmpLog").mkRecordIterator().toArray logStorage.deleteStorage() assert(logs.length > 1) @@ -320,27 +318,22 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { val baseUri = LargeBinaryManager.baseUriForExecution(eid) // create() runs on the DP thread; capture what it produces there. val capturedUri = new CompletableFuture[String]() - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = new DataProcessor(workerId, x => {}, inputMessageQueue = inputQueue) - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { + withStartedDPThread( + onTuple = (_, _) => { capturedUri.complete(LargeBinaryManager.create()) Iterator.empty - } - } - dp.inputManager.addPort(mockInputPortId, schema, List.empty, List.empty) - dp.inputGateway.getChannel(dataChannelId).setPortId(mockInputPortId) - dp.adaptiveBatchingMonitor = mock[WorkerTimerService] - val dpThread = new DPThread(workerId, dp, logManager, inputQueue, baseUri) - dpThread.start() - inputQueue.put( - FIFOMessageElement(WorkflowFIFOMessage(dataChannelId, 0, DataFrame(Array(tuples(0))))) - ) + }, + baseUriOpt = Some(baseUri) + ) { (inputQueue, _, _) => + inputQueue.put( + FIFOMessageElement(WorkflowFIFOMessage(dataChannelId, 0, DataFrame(Array(tuples(0))))) + ) - val uri = capturedUri.get(5, TimeUnit.SECONDS) - assert(uri.startsWith(s"s3://${LargeBinaryManager.DEFAULT_BUCKET}/objects/$eid/")) - // a unique suffix is appended to the execution-scoped base URI - assert(uri.length > baseUri.length) + val uri = capturedUri.get(5, TimeUnit.SECONDS) + assert(uri.startsWith(s"s3://${LargeBinaryManager.DEFAULT_BUCKET}/objects/$eid/")) + // a unique suffix is appended to the execution-scoped base URI + assert(uri.length > baseUri.length) + } } "DP Thread" should "toggle backpressureStatus on Backpressure and ignore other ActorCommands" in { @@ -363,12 +356,11 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { } "DP Thread" should "treat a second start() as a no-op" in { - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = new DataProcessor(workerId, x => {}, inputMessageQueue = inputQueue) - dp.adaptiveBatchingMonitor = mock[WorkerTimerService] - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - try { - dpThread.start() + withStartedDPThread(dpFactory = q => { + val dp = new DataProcessor(workerId, x => {}, inputMessageQueue = q) + dp.adaptiveBatchingMonitor = mock[WorkerTimerService] + dp + }) { (_, _, dpThread) => val executorAfterFirstStart = dpThread.dpThreadExecutor val futureAfterFirstStart = dpThread.dpThread // The second start() should log "already running" and change nothing: @@ -376,8 +368,6 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { dpThread.start() assert(dpThread.dpThreadExecutor eq executorAfterFirstStart) assert(dpThread.dpThread eq futureAfterFirstStart) - } finally { - dpThread.stop() } } @@ -386,36 +376,32 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // back to the main thread as a Left(MainThreadDelegateMessage). We throw from // DataProcessor.processDataPayload (not from a mock executor: DataProcessor swallows // executor exceptions in handleExecutorException, so they never reach DPThread). - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() val captured = new CompletableFuture[Either[MainThreadDelegateMessage, WorkflowFIFOMessage]]() val outputHandler: Either[MainThreadDelegateMessage, WorkflowFIFOMessage] => Unit = e => captured.complete(e) - val dp = new DataProcessor(workerId, outputHandler, inputMessageQueue = inputQueue) { - override def processDataPayload( - channelId: ChannelIdentity, - dataPayload: DataPayload - ): Unit = throw new RuntimeException("boom") - } - dp.inputManager.addPort(mockInputPortId, schema, List.empty, List.empty) - dp.inputGateway.getChannel(dataChannelId).setPortId(mockInputPortId) - dp.adaptiveBatchingMonitor = mock[WorkerTimerService] - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - try { - dpThread.start() + withStartedDPThread(dpFactory = + q => + newDataProcessor( + q, + dp = new DataProcessor(workerId, outputHandler, inputMessageQueue = q) { + override def processDataPayload( + channelId: ChannelIdentity, + dataPayload: DataPayload + ): Unit = throw new RuntimeException("boom") + } + ) + ) { (inputQueue, _, _) => inputQueue.put( FIFOMessageElement(WorkflowFIFOMessage(dataChannelId, 0, DataFrame(Array(tuples(0))))) ) - val result = captured.get(5, TimeUnit.SECONDS) - result match { + captured.get(5, TimeUnit.SECONDS) match { case Left(MainThreadDelegateMessage(closure)) => // Running the delegate on the main thread must re-throw the original processing error. val thrown = intercept[RuntimeException](closure(null)) assert(thrown.getMessage == "boom") case other => fail(s"expected Left(MainThreadDelegateMessage), got $other") } - } finally { - dpThread.stop() } } @@ -443,44 +429,35 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // suite constrains it. We park the DP thread in a NON-interruptible spin (an // interruptible wait would be swallowed by DataProcessor.handleExecutorException // and let stop() legitimately return early) and check that stop() blocks. - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = newDataProcessor(inputQueue) val release = new AtomicBoolean(false) val inSpin = new CountDownLatch(1) - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { + try { + withStartedDPThread(onTuple = (_, _) => { inSpin.countDown() while (!release.get()) { Thread.onSpinWait() } Iterator.empty + }) { (inputQueue, _, dpThread) => + inputQueue.put(dataFrameOf(4)) + assert(inSpin.await(5, TimeUnit.SECONDS), "the DP thread must be inside the executor") + val stopReturned = new CountDownLatch(1) + val stopper = new Thread(() => { + try dpThread.stop() + finally stopReturned.countDown() + }) + stopper.setDaemon(true) + stopper.start() + assert( + !stopReturned.await(500, TimeUnit.MILLISECONDS), + "stop() must not return while the DP thread is still inside run()" + ) + release.set(true) + assert( + stopReturned.await(10, TimeUnit.SECONDS), + "stop() must return once the DP thread exits" + ) } - } - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - val stopReturned = new CountDownLatch(1) - try { - dpThread.start() - inputQueue.put(dataFrameOf(4)) - assert(inSpin.await(5, TimeUnit.SECONDS), "the DP thread must be inside the executor") - val stopper = new Thread(() => { - try { - dpThread.stop() - } finally { - stopReturned.countDown() - } - }) - stopper.setDaemon(true) - stopper.start() - assert( - !stopReturned.await(500, TimeUnit.MILLISECONDS), - "stop() must not return while the DP thread is still inside run()" - ) - release.set(true) - assert( - stopReturned.await(10, TimeUnit.SECONDS), - "stop() must return once the DP thread exits" - ) } finally { release.set(true) - dpThread.stop() } } @@ -494,32 +471,29 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // nothing more. The other catch arm delegates the error back to the worker actor, and // with the usual swallowing output handler the two are indistinguishable, so record // what the handler receives instead of dropping it. - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() val delegates = new ConcurrentLinkedQueue[MainThreadDelegateMessage]() - val dp = newDataProcessor( - inputQueue, - { - case Left(m) => delegates.add(m) - case _ => () - } - ) // processTuple runs ON the DP thread, so this is how we get hold of that exact // thread. Every DPThread names its thread "DP-thread" and earlier tests in this // suite leave theirs alive, so matching by name would be ambiguous. val dpWorkerThread = new AtomicReference[Thread]() - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { + val idleState = () => Option(dpWorkerThread.get()).map(_.getState) + withStartedDPThread( + onTuple = (_, _) => { dpWorkerThread.set(Thread.currentThread()) Iterator.empty - } - } - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - val idleState = () => Option(dpWorkerThread.get()).map(_.getState) - try { - dpThread.start() + }, + dpFactory = q => + newDataProcessor( + q, + { + case Left(m) => delegates.add(m) + case _ => () + } + ) + ) { (inputQueue, _, dpThread) => inputQueue.put(dataFrameOf(1)) - assert( - awaitCond(idleState().contains(Thread.State.WAITING)), + awaitUntil( + idleState().contains(Thread.State.WAITING), s"an idle DP thread must park on take, was ${idleState()}" ) // stop() joins the DP thread, so run() has returned by the time this returns. @@ -528,8 +502,6 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { delegates.isEmpty, s"an interrupted teardown must not delegate an error, got ${delegates.peek()}" ) - } finally { - dpThread.stop() } } @@ -538,18 +510,11 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // which is how the worker actor actually sends it. While it is on, the DP // thread must stop taking data, so a data frame that arrives afterwards stays // queued in its channel and no tuple is processed until backpressure is lifted. - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = newDataProcessor(inputQueue) val processed = new AtomicInteger(0) - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { - processed.incrementAndGet() - Iterator.empty - } - } - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - try { - dpThread.start() + withStartedDPThread(onTuple = (_, _) => { + processed.incrementAndGet() + Iterator.empty + }) { (inputQueue, _, dpThread) => inputQueue.put(ActorCommandElement(Backpressure(enableBackpressure = true))) // `backpressureStatus` is a plain (non-volatile) var written by the DP thread, so // reading it from here needs a happens-before edge. CreditUpdate is inert -- it @@ -557,7 +522,7 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // queue drained is a read of LinkedBlockingQueue's AtomicInteger count, which the // DP thread decremented after it had already handled the Backpressure element. inputQueue.put(ActorCommandElement(CreditUpdate())) - assert(awaitCond(inputQueue.isEmpty), "the queued commands must be consumed") + awaitUntil(inputQueue.isEmpty, "the queued commands must be consumed") assert(dpThread.backpressureStatus, "backpressure must be picked up") inputQueue.put(dataFrameOf(200)) @@ -573,9 +538,7 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // freeze that behaviour in place, so this test only pins the data hold-back. inputQueue.put(ActorCommandElement(Backpressure(enableBackpressure = false))) - assert(awaitCond(processed.get() == 200), s"drained ${processed.get()} of 200") - } finally { - dpThread.stop() + awaitUntil(processed.get() == 200, s"drained ${processed.get()} of 200") } } @@ -593,7 +556,7 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { inputQueue.put(ActorCommandElement(CreditUpdate())) }, verifyStallPickedUp = (_, dpThread, inputQueue) => { - assert(awaitCond(inputQueue.isEmpty), "the queued commands must be consumed") + awaitUntil(inputQueue.isEmpty, "the queued commands must be consumed") assert(dpThread.backpressureStatus, "backpressure must be picked up") }, liftStall = _.put(ActorCommandElement(Backpressure(enableBackpressure = false))), @@ -618,7 +581,7 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { ) ), verifyStallPickedUp = - (dp, _, _) => assert(awaitCond(dp.pauseManager.isPaused), "pause must be picked up"), + (dp, _, _) => awaitUntil(dp.pauseManager.isPaused, "pause must be picked up"), liftStall = _.put( TimerBasedControlElement( ControlInvocation(METHOD_RESUME_WORKER, EmptyRequest(), AsyncRPCContext(SELF, SELF), 1) @@ -633,24 +596,17 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // busy in the executor rather than blocked on the queue, so it never sees an // InterruptedException and has to fall out of the main loop on the flag alone, // abandoning the rest of the batch. - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = newDataProcessor(inputQueue) val processed = new AtomicInteger(0) val firstTuple = new CountDownLatch(1) - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = { - processed.incrementAndGet() - firstTuple.countDown() - // A non-interruptible spin: ~0.5ms of work per tuple, so 2000 tuples take - // about a second while stop() lands after ~100ms. - val until = System.nanoTime() + 500000L - while (System.nanoTime() < until) {} - Iterator.empty - } - } - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - try { - dpThread.start() + withStartedDPThread(onTuple = (_, _) => { + processed.incrementAndGet() + firstTuple.countDown() + // A non-interruptible spin: ~0.5ms of work per tuple, so 2000 tuples take + // about a second while stop() lands after ~100ms. + val until = System.nanoTime() + 500000L + while (System.nanoTime() < until) {} + Iterator.empty + }) { (inputQueue, dp, dpThread) => inputQueue.put(dataFrameOf(2000)) assert(firstTuple.await(5, TimeUnit.SECONDS), "batch must start") Thread.sleep(100) @@ -658,8 +614,6 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { val done = processed.get() assert(done < 2000, s"stop() must abandon the rest of the batch, processed $done of 2000") assert(dp.inputManager.hasUnfinishedInput, "the abandoned batch is left unfinished") - } finally { - dpThread.stop() } } @@ -668,36 +622,32 @@ class DPThreadSpec extends AnyFlatSpec with Matchers with MockFactory { // consumed there is no unfinished input and no pause, so only the pending // output backlog can keep the loop calling continueDataProcessing(). val fanOut = 50 - val inputQueue = new LinkedBlockingQueue[DPInputQueueElement]() - val dp = newDataProcessor(inputQueue) - dp.outputManager.addPort(PortIdentity(1), schema, None) - dp.executor = new OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = - (0 until fanOut).iterator.map(i => TupleLike(i)) - } - val dpThread = new DPThread(workerId, dp, logManager, inputQueue) - try { - dpThread.start() + withStartedDPThread( + onTuple = (_, _) => (0 until fanOut).iterator.map(i => TupleLike(i)), + dpFactory = q => { + val dp = newDataProcessor(q) + dp.outputManager.addPort(PortIdentity(1), schema, None) + dp + } + ) { (inputQueue, dp, _) => inputQueue.put(dataFrameOf(1)) // Fixture guard, sampled DURING the drain: processDataPayload consumes the whole // one-tuple frame in the same step that sets up the output iterator, so input is // already exhausted when the first output tuple appears. Checked here rather than // at the end because at the end it holds for any frame size -- it would not // notice the frame growing and the output clause silently going untested. - assert( - awaitCond(dp.statisticsManager.getOutputTupleCount >= 1L), + awaitUntil( + dp.statisticsManager.getOutputTupleCount >= 1L, "the first output tuple must be emitted" ) assert( !dp.inputManager.hasUnfinishedInput, "input must already be exhausted while output tuples are still pending" ) - assert( - awaitCond(dp.statisticsManager.getOutputTupleCount == fanOut.toLong), + awaitUntil( + dp.statisticsManager.getOutputTupleCount == fanOut.toLong, s"emitted ${dp.statisticsManager.getOutputTupleCount} of $fanOut output tuples" ) - } finally { - dpThread.stop() } } diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/CheckpointHandlerSpecSupport.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/CheckpointHandlerSpecSupport.scala new file mode 100644 index 00000000000..de64536cf99 --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/CheckpointHandlerSpecSupport.scala @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.amber.engine.architecture.worker.promisehandlers + +import com.twitter.util.{Await, Duration, Future} +import org.apache.pekko.actor.{ActorSystem, Props} +import org.apache.pekko.testkit.{TestActorRef, TestKit} +import org.apache.texera.amber.clustering.SingleNodeListener +import org.apache.texera.amber.core.executor.OperatorExecutor +import org.apache.texera.amber.core.tuple.{Tuple, TupleLike} +import org.apache.texera.amber.core.virtualidentity.{ + ActorVirtualIdentity, + ChannelIdentity, + EmbeddedControlMessageIdentity +} +import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{ + AsyncRPCContext, + EmptyRequest +} +import org.apache.texera.amber.engine.architecture.rpc.workerservice.WorkerServiceGrpc.METHOD_QUERY_STATISTICS +import org.apache.texera.amber.engine.architecture.scheduling.config.WorkerConfig +import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.{ + DPInputQueueElement, + MainThreadDelegateMessage, + WorkerReplayInitialization +} +import org.apache.texera.amber.engine.architecture.worker.{ + DataProcessor, + DataProcessorRPCHandlerInitializer, + WorkflowWorker +} +import org.apache.texera.amber.engine.common.AmberRuntime +import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage +import org.apache.texera.amber.engine.common.rpc.AsyncRPCClient.ControlInvocation +import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR +import org.scalatest.BeforeAndAfterAll +import org.scalatest.flatspec.AnyFlatSpecLike + +import java.util.concurrent.LinkedBlockingQueue + +abstract class CheckpointHandlerSpecSupport( + actorSystemName: String, + workerName: String, + checkpointName: String, + timeoutSeconds: Int +) extends TestKit(ActorSystem(actorSystemName, AmberRuntime.pekkoConfig)) + with AnyFlatSpecLike + with BeforeAndAfterAll { + + protected val workerId: ActorVirtualIdentity = ActorVirtualIdentity(workerName) + protected val rpcContext: AsyncRPCContext = AsyncRPCContext(COORDINATOR, workerId) + protected val checkpointId: EmbeddedControlMessageIdentity = + EmbeddedControlMessageIdentity(checkpointName) + private val awaitTimeout = Duration.fromSeconds(timeoutSeconds) + + override def beforeAll(): Unit = { + // WorkflowActor's actor service resolves node addresses through /user/cluster-info. + system.actorOf(Props[SingleNodeListener](), "cluster-info") + } + + override def afterAll(): Unit = TestKit.shutdownActorSystem(system) + + protected def await[T](future: Future[T]): T = Await.result(future, awaitTimeout) + + protected def newDpHandler( + executor: OperatorExecutor, + outputHandler: Either[MainThreadDelegateMessage, WorkflowFIFOMessage] => Unit = _ => (), + inputQueue: LinkedBlockingQueue[DPInputQueueElement] = + new LinkedBlockingQueue[DPInputQueueElement]() + ): DataProcessorRPCHandlerInitializer = { + val dp = new DataProcessor(workerId, outputHandler, inputQueue) + dp.executor = executor + new DataProcessorRPCHandlerInitializer(dp) + } + + /** + * A live worker, so the main-thread hand-off is delivered as in production + * (`logManager.sendCommitted` -> `self !` -> `handleTriggerClosure`). + */ + protected def liveWorker(): TestActorRef[WorkflowWorker] = { + val worker = TestActorRef( + new WorkflowWorker(WorkerConfig(workerId), WorkerReplayInitialization()) + ) + // The DP thread would otherwise race the assertions on the worker's queue and recorded inputs. + worker.underlyingActor.dpThread.stop() + worker + } + + protected def queryStatisticsMessage(seq: Long): WorkflowFIFOMessage = + WorkflowFIFOMessage( + ChannelIdentity(COORDINATOR, workerId, isControl = true), + seq, + ControlInvocation(METHOD_QUERY_STATISTICS, EmptyRequest(), rpcContext, seq) + ) +} + +object CheckpointHandlerSpecSupport { + + /** Not `CheckpointSupport`, so estimate and serializeState take their skip branches. */ + class PlainExecutor extends OperatorExecutor { + override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = Iterator.empty + } +} diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/FinalizeCheckpointHandlerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/FinalizeCheckpointHandlerSpec.scala index 81cd6919d96..a3cf13b0b87 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/FinalizeCheckpointHandlerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/FinalizeCheckpointHandlerSpec.scala @@ -19,49 +19,18 @@ package org.apache.texera.amber.engine.architecture.worker.promisehandlers -import com.twitter.util.{Await, Duration, Future} -import org.apache.pekko.actor.{ActorSystem, Props} -import org.apache.pekko.testkit.{TestActorRef, TestKit} -import org.apache.texera.amber.clustering.SingleNodeListener import org.apache.texera.amber.core.executor.OperatorExecutor import org.apache.texera.amber.core.tuple.{Tuple, TupleLike} -import org.apache.texera.amber.core.virtualidentity.{ - ActorVirtualIdentity, - ChannelIdentity, - EmbeddedControlMessageIdentity -} +import org.apache.texera.amber.core.virtualidentity.EmbeddedControlMessageIdentity import org.apache.texera.amber.core.workflow.PortIdentity -import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{ - AsyncRPCContext, - EmptyRequest, - FinalizeCheckpointRequest -} -import org.apache.texera.amber.engine.architecture.rpc.workerservice.WorkerServiceGrpc.METHOD_QUERY_STATISTICS -import org.apache.texera.amber.engine.architecture.scheduling.config.WorkerConfig -import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.{ - DPInputQueueElement, - WorkerReplayInitialization -} -import org.apache.texera.amber.engine.architecture.worker.{ - DataProcessor, - DataProcessorRPCHandlerInitializer, - WorkflowWorker -} +import org.apache.texera.amber.engine.architecture.rpc.controlcommands.FinalizeCheckpointRequest +import org.apache.texera.amber.engine.architecture.worker.DataProcessorRPCHandlerInitializer +import org.apache.texera.amber.engine.architecture.worker.promisehandlers.CheckpointHandlerSpecSupport.PlainExecutor import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage -import org.apache.texera.amber.engine.common.rpc.AsyncRPCClient.ControlInvocation import org.apache.texera.amber.engine.common.storage.SequentialRecordStorage -import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR -import org.apache.texera.amber.engine.common.{ - AmberRuntime, - CheckpointState, - CheckpointSupport, - SerializedState -} -import org.scalatest.BeforeAndAfterAll -import org.scalatest.flatspec.AnyFlatSpecLike +import org.apache.texera.amber.engine.common.{CheckpointState, CheckpointSupport, SerializedState} import java.net.URI -import java.util.concurrent.LinkedBlockingQueue import scala.collection.mutable import scala.collection.mutable.ArrayBuffer @@ -87,59 +56,21 @@ import scala.collection.mutable.ArrayBuffer * main thread and uses a bare `DataProcessor`. */ class FinalizeCheckpointHandlerSpec - extends TestKit(ActorSystem("FinalizeCheckpointHandlerSpec", AmberRuntime.pekkoConfig)) - with AnyFlatSpecLike - with BeforeAndAfterAll { + extends CheckpointHandlerSpecSupport( + "FinalizeCheckpointHandlerSpec", + "Worker:WF1-finalize-main-0", + "finalize-checkpoint-1", + 10 + ) { import FinalizeCheckpointHandlerSpec._ - private val workerId = ActorVirtualIdentity("Worker:WF1-finalize-main-0") - private val rpcContext = AsyncRPCContext(COORDINATOR, workerId) - private val awaitTimeout = Duration.fromSeconds(10) - private val checkpointId = EmbeddedControlMessageIdentity("finalize-checkpoint-1") private val unrelatedCheckpointId = EmbeddedControlMessageIdentity("some-other-checkpoint") - override def beforeAll(): Unit = { - // WorkflowActor's actor service resolves node addresses through /user/cluster-info. - system.actorOf(Props[SingleNodeListener](), "cluster-info") - } - - override def afterAll(): Unit = { - TestKit.shutdownActorSystem(system) - } - - private def await[T](future: Future[T]): T = Await.result(future, awaitTimeout) - - /** A handler over a bare DataProcessor: enough for the estimate branch. */ - private def estimateHandler(executor: OperatorExecutor): DataProcessorRPCHandlerInitializer = { - val dp = new DataProcessor( - workerId, - _ => (), - new LinkedBlockingQueue[DPInputQueueElement]() - ) - dp.executor = executor - new DataProcessorRPCHandlerInitializer(dp) - } - - /** - * A live worker, so the main-thread hand-off in the write branch is delivered exactly as in - * production (`logManager.sendCommitted` -> `self !` -> `handleTriggerClosure`). - */ - private def liveWorker(): TestActorRef[WorkflowWorker] = { - val worker = TestActorRef( - new WorkflowWorker(WorkerConfig(workerId), WorkerReplayInitialization()) - ) - // The DP thread would otherwise race the assertions on the worker's recorded inputs. - worker.underlyingActor.dpThread.stop() - worker - } - - private def recordedMessage(seq: Long): WorkflowFIFOMessage = - WorkflowFIFOMessage( - ChannelIdentity(COORDINATOR, workerId, isControl = true), - seq, - ControlInvocation(METHOD_QUERY_STATISTICS, EmptyRequest(), rpcContext, seq) - ) + private def finalizeSize(handler: DataProcessorRPCHandlerInitializer, writeTo: String): Long = + await( + handler.finalizeCheckpoint(FinalizeCheckpointRequest(checkpointId, writeTo), rpcContext) + ).size private def storageAt(uri: String): SequentialRecordStorage[CheckpointState] = SequentialRecordStorage.getStorage[CheckpointState](Some(new URI(uri))) @@ -147,40 +78,23 @@ class FinalizeCheckpointHandlerSpec behavior of "FinalizeCheckpointHandler" it should "report zero for an operator that cannot checkpoint" in { - val handler = estimateHandler(new PlainExecutor) - - val response = await( - handler.finalizeCheckpoint( - FinalizeCheckpointRequest(checkpointId, "ram:///finalize-plain-estimate/"), - rpcContext - ) - ) - - assert(response.size == 0L) + assert(finalizeSize(newDpHandler(new PlainExecutor), "ram:///finalize-plain-estimate/") == 0L) } it should "report the operator's own estimated cost when it can checkpoint" in { - val handler = estimateHandler(new EstimatingExecutor(estimatedCost = 4242L)) - - val response = await( - handler.finalizeCheckpoint( - FinalizeCheckpointRequest(checkpointId, "ram:///finalize-supported-estimate/"), - rpcContext - ) + assert( + finalizeSize( + newDpHandler(new EstimatingExecutor(estimatedCost = 4242L)), + "ram:///finalize-supported-estimate/" + ) == 4242L ) - - assert(response.size == 4242L) } it should "not create the destination folder for an estimate-only request" in { - val handler = estimateHandler(new EstimatingExecutor(estimatedCost = 1L)) val parent = "ram:///finalize-estimate-no-write/" - - await( - handler.finalizeCheckpoint( - FinalizeCheckpointRequest(checkpointId, parent + "checkpoint-folder/"), - rpcContext - ) + finalizeSize( + newDpHandler(new EstimatingExecutor(estimatedCost = 1L)), + parent + "checkpoint-folder/" ) // Opening a writer creates the folder, so its absence is what shows nothing was written. @@ -194,19 +108,13 @@ class FinalizeCheckpointHandlerSpec val checkpoint = new CheckpointState() checkpoint.save("unicode-payload", "checkpoint-δ") dp.ecmManager.checkpoints(checkpointId) = checkpoint - val handler = new DataProcessorRPCHandlerInitializer(dp) - val response = await( - handler.finalizeCheckpoint( - FinalizeCheckpointRequest(checkpointId, destination), - rpcContext - ) - ) + val size = finalizeSize(new DataProcessorRPCHandlerInitializer(dp), destination) val restored = SequentialRecordStorage .fetchAllRecords(storageAt(destination), workerId.name.replace("Worker:", "")) .toList - assert(response.size > 0L) + assert(size > 0L) assert(restored.size == 1) assert(restored.head.load[String]("unicode-payload") == "checkpoint-δ") } @@ -219,17 +127,12 @@ class FinalizeCheckpointHandlerSpec dp.executor = new EstimatingExecutor(estimatedCost = 4242L) val checkpoint = new CheckpointState() dp.ecmManager.checkpoints(checkpointId) = checkpoint - val recorded = ArrayBuffer(recordedMessage(1), recordedMessage(2)) + val recorded = ArrayBuffer(queryStatisticsMessage(1), queryStatisticsMessage(2)) worker.underlyingActor.recordedInputs(checkpointId) = recorded - worker.underlyingActor.recordedInputs(unrelatedCheckpointId) = ArrayBuffer(recordedMessage(99)) - val handler = new DataProcessorRPCHandlerInitializer(dp) + worker.underlyingActor.recordedInputs(unrelatedCheckpointId) = + ArrayBuffer(queryStatisticsMessage(99)) - await( - handler.finalizeCheckpoint( - FinalizeCheckpointRequest(checkpointId, "ram:///finalize-fold/"), - rpcContext - ) - ) + finalizeSize(new DataProcessorRPCHandlerInitializer(dp), "ram:///finalize-fold/") assert( checkpoint.load[mutable.ArrayBuffer[WorkflowFIFOMessage]]( @@ -246,14 +149,8 @@ class FinalizeCheckpointHandlerSpec val dp = worker.underlyingActor.dp val checkpoint = new CheckpointState() dp.ecmManager.checkpoints(checkpointId) = checkpoint - val handler = new DataProcessorRPCHandlerInitializer(dp) - await( - handler.finalizeCheckpoint( - FinalizeCheckpointRequest(checkpointId, "ram:///finalize-fold-empty/"), - rpcContext - ) - ) + finalizeSize(new DataProcessorRPCHandlerInitializer(dp), "ram:///finalize-fold-empty/") // `WorkflowWorker.loadFromCheckpoint` reads this key unconditionally, so it has to be present // even when no input arrived between prepare and finalize. @@ -267,11 +164,6 @@ class FinalizeCheckpointHandlerSpec object FinalizeCheckpointHandlerSpec { - /** Not `CheckpointSupport`: the estimate branch must fall through to 0. */ - class PlainExecutor extends OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = Iterator.empty - } - class EstimatingExecutor(estimatedCost: Long) extends OperatorExecutor with CheckpointSupport { override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = Iterator.empty diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/PrepareCheckpointHandlerSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/PrepareCheckpointHandlerSpec.scala index b375d97c3f0..77fffc0cbfa 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/PrepareCheckpointHandlerSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/PrepareCheckpointHandlerSpec.scala @@ -19,47 +19,30 @@ package org.apache.texera.amber.engine.architecture.worker.promisehandlers -import com.twitter.util.{Await, Duration, Future} -import org.apache.pekko.actor.{ActorSystem, Props} -import org.apache.pekko.testkit.{TestActorRef, TestKit} -import org.apache.texera.amber.clustering.SingleNodeListener +import org.apache.pekko.testkit.TestActorRef import org.apache.texera.amber.core.executor.OperatorExecutor import org.apache.texera.amber.core.tuple.{Schema, Tuple, TupleLike} -import org.apache.texera.amber.core.virtualidentity.{ - ActorVirtualIdentity, - ChannelIdentity, - EmbeddedControlMessageIdentity -} import org.apache.texera.amber.core.workflow.PortIdentity import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{ - AsyncRPCContext, EmptyRequest, PrepareCheckpointRequest } import org.apache.texera.amber.engine.architecture.rpc.controlreturns.EmptyReturn import org.apache.texera.amber.engine.architecture.rpc.workerservice.WorkerServiceGrpc.METHOD_QUERY_STATISTICS -import org.apache.texera.amber.engine.architecture.scheduling.config.WorkerConfig import org.apache.texera.amber.engine.architecture.worker.WorkflowWorker.{ - DPInputQueueElement, FIFOMessageElement, MainThreadDelegateMessage, - TimerBasedControlElement, - WorkerReplayInitialization + TimerBasedControlElement } +import org.apache.texera.amber.engine.architecture.worker.promisehandlers.CheckpointHandlerSpecSupport.PlainExecutor import org.apache.texera.amber.engine.architecture.worker.{ - DataProcessor, DataProcessorRPCHandlerInitializer, WorkflowWorker } -import org.apache.texera.amber.engine.common.AmberRuntime import org.apache.texera.amber.engine.common.ambermessage.WorkflowFIFOMessage import org.apache.texera.amber.engine.common.rpc.AsyncRPCClient.ControlInvocation -import org.apache.texera.amber.engine.common.virtualidentity.util.COORDINATOR import org.apache.texera.amber.engine.common.{CheckpointState, CheckpointSupport, SerializedState} -import org.scalatest.BeforeAndAfterAll -import org.scalatest.flatspec.AnyFlatSpecLike -import java.util.concurrent.LinkedBlockingQueue import scala.collection.mutable.ArrayBuffer /** @@ -91,26 +74,15 @@ import scala.collection.mutable.ArrayBuffer * prepare-to-finalize handshake is verified end to end. */ class PrepareCheckpointHandlerSpec - extends TestKit(ActorSystem("PrepareCheckpointHandlerSpec", AmberRuntime.pekkoConfig)) - with AnyFlatSpecLike - with BeforeAndAfterAll { + extends CheckpointHandlerSpecSupport( + "PrepareCheckpointHandlerSpec", + "Worker:WF1-prepare-main-0", + "prepare-checkpoint-1", + 5 + ) { import PrepareCheckpointHandlerSpec._ - private val workerId = ActorVirtualIdentity("Worker:WF1-prepare-main-0") - private val rpcContext = AsyncRPCContext(COORDINATOR, workerId) - private val awaitTimeout = Duration.fromSeconds(5) - private val checkpointId = EmbeddedControlMessageIdentity("prepare-checkpoint-1") - - override def beforeAll(): Unit = { - // WorkflowActor's actor service resolves node addresses through /user/cluster-info. - system.actorOf(Props[SingleNodeListener](), "cluster-info") - } - - override def afterAll(): Unit = { - TestKit.shutdownActorSystem(system) - } - /** Records main-thread hand-offs, then aborts so the blocking wait is never reached. */ private class OutputRecorder { val delegates: ArrayBuffer[MainThreadDelegateMessage] = ArrayBuffer() @@ -128,29 +100,7 @@ class PrepareCheckpointHandlerSpec executor: OperatorExecutor ): (DataProcessorRPCHandlerInitializer, OutputRecorder) = { val recorder = new OutputRecorder - val dp = new DataProcessor( - workerId, - recorder.handle, - new LinkedBlockingQueue[DPInputQueueElement]() - ) - dp.executor = executor - (new DataProcessorRPCHandlerInitializer(dp), recorder) - } - - private def await[T](future: Future[T]): T = Await.result(future, awaitTimeout) - - /** - * A live worker, so the main-thread hand-off is delivered the way production delivers it - * (`self !` -> `handleTriggerClosure`). `TestActorRef` dispatches on the calling thread, so the - * closure runs inside `dp.outputHandler` exactly as the blocking wait after it assumes. - */ - private def liveWorker(): TestActorRef[WorkflowWorker] = { - val worker = TestActorRef( - new WorkflowWorker(WorkerConfig(workerId), WorkerReplayInitialization()) - ) - // The DP thread would otherwise race the assertions on the worker's queue and recorded inputs. - worker.underlyingActor.dpThread.stop() - worker + (newDpHandler(executor, recorder.handle), recorder) } /** @@ -161,24 +111,21 @@ class PrepareCheckpointHandlerSpec private def newHandlerOn( worker: TestActorRef[WorkflowWorker], executor: OperatorExecutor - ): DataProcessorRPCHandlerInitializer = { - val dp = new DataProcessor( - workerId, + ): DataProcessorRPCHandlerInitializer = + newDpHandler( + executor, { case Left(delegate) => worker ! delegate case Right(_) => () }, worker.underlyingActor.inputQueue ) - dp.executor = executor - new DataProcessorRPCHandlerInitializer(dp) - } - private def queryStatisticsMessage(sequenceNumber: Long): WorkflowFIFOMessage = - WorkflowFIFOMessage( - ChannelIdentity(COORDINATOR, workerId, isControl = true), - sequenceNumber, - ControlInvocation(METHOD_QUERY_STATISTICS, EmptyRequest(), rpcContext, sequenceNumber) + private def prepare(handler: DataProcessorRPCHandlerInitializer, estimateOnly: Boolean): Unit = + assert( + await( + handler.prepareCheckpoint(PrepareCheckpointRequest(checkpointId, estimateOnly), rpcContext) + ) == EmptyReturn() ) behavior of "PrepareCheckpointHandler" @@ -186,11 +133,7 @@ class PrepareCheckpointHandlerSpec it should "register no serialization for an estimate-only request" in { val (handler, recorder) = newHandler(new PlainExecutor) - assert( - await( - handler.prepareCheckpoint(PrepareCheckpointRequest(checkpointId, true), rpcContext) - ) == EmptyReturn() - ) + prepare(handler, estimateOnly = true) // Nothing was registered, so the DP's serialization point is a no-op and no checkpoint state // exists for this id — the coordinator will get a size estimate from `finalizeCheckpoint` @@ -203,11 +146,7 @@ class PrepareCheckpointHandlerSpec it should "reply before any state is serialized for a real checkpoint" in { val (handler, recorder) = newHandler(new PlainExecutor) - assert( - await( - handler.prepareCheckpoint(PrepareCheckpointRequest(checkpointId, false), rpcContext) - ) == EmptyReturn() - ) + prepare(handler, estimateOnly = false) // The reply is what releases the embedded control message; the serialization itself must still // be pending, waiting for the DP loop to reach a safe point. @@ -225,7 +164,7 @@ class PrepareCheckpointHandlerSpec // this executor has no serializeState at all, so it could never appear.) val pendingOutput: (TupleLike, Option[PortIdentity]) = (emptyTuple, Some(PortIdentity(3))) handler.dp.outputManager.outputIterator.setTupleOutput(Iterator(pendingOutput)) - await(handler.prepareCheckpoint(PrepareCheckpointRequest(checkpointId, false), rpcContext)) + prepare(handler, estimateOnly = false) intercept[MainThreadHandOffReached] { handler.dp.serializationManager.applySerialization() @@ -242,7 +181,7 @@ class PrepareCheckpointHandlerSpec val (handler, _) = newHandler(executor) val pendingOutput: (TupleLike, Option[PortIdentity]) = (emptyTuple, Some(PortIdentity(3))) handler.dp.outputManager.outputIterator.setTupleOutput(Iterator(pendingOutput)) - await(handler.prepareCheckpoint(PrepareCheckpointRequest(checkpointId, false), rpcContext)) + prepare(handler, estimateOnly = false) intercept[MainThreadHandOffReached] { handler.dp.serializationManager.applySerialization() @@ -274,7 +213,7 @@ class PrepareCheckpointHandlerSpec ) ) - await(handler.prepareCheckpoint(PrepareCheckpointRequest(checkpointId, false), rpcContext)) + prepare(handler, estimateOnly = false) handler.dp.serializationManager.applySerialization() val checkpoint = handler.dp.ecmManager.checkpoints(checkpointId) @@ -307,11 +246,6 @@ object PrepareCheckpointHandlerSpec { class MainThreadHandOffReached extends RuntimeException("output handler reached the main-thread hand-off") - /** Not `CheckpointSupport`, so `serializeWorkerState` must take its skip branch. */ - class PlainExecutor extends OperatorExecutor { - override def processTuple(tuple: Tuple, port: Int): Iterator[TupleLike] = Iterator.empty - } - class CheckpointAwareExecutor extends OperatorExecutor with CheckpointSupport { var receivedOutputState: List[(TupleLike, Option[PortIdentity])] = Nil diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/common/BarePhysicalPlanFixture.scala b/amber/src/test/scala/org/apache/texera/amber/engine/common/BarePhysicalPlanFixture.scala new file mode 100644 index 00000000000..e88e256e2ae --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/amber/engine/common/BarePhysicalPlanFixture.scala @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.amber.engine.common + +import org.apache.texera.amber.core.executor.OpExecInitInfo +import org.apache.texera.amber.core.virtualidentity.{ + ExecutionIdentity, + OperatorIdentity, + PhysicalOpIdentity, + WorkflowIdentity +} +import org.apache.texera.amber.core.workflow.{ + GlobalPortIdentity, + PhysicalLink, + PhysicalOp, + PortIdentity +} +import org.apache.texera.amber.engine.architecture.scheduling.{Region, RegionIdentity} + +/** Shared bare physical-op, link, global-port, and single-operator region constructors for engine unit tests. */ +trait BarePhysicalPlanFixture { + + protected def physicalOpId(opId: String, layerName: String = "main"): PhysicalOpIdentity = + PhysicalOpIdentity(OperatorIdentity(opId), layerName) + + protected def op( + opId: String, + layerName: String = "main", + workflowId: WorkflowIdentity = WorkflowIdentity(0), + executionId: ExecutionIdentity = ExecutionIdentity(0) + ): PhysicalOp = + PhysicalOp(physicalOpId(opId, layerName), workflowId, executionId, OpExecInitInfo.Empty) + + protected def region( + regionId: Long, + opId: String, + ports: Set[GlobalPortIdentity] = Set.empty + ): Region = + Region(RegionIdentity(regionId), Set(op(opId)), Set.empty, ports = ports) + + protected def link(fromOp: String, toOp: String): PhysicalLink = + PhysicalLink(physicalOpId(fromOp), PortIdentity(0), physicalOpId(toOp), PortIdentity(0)) + + protected def globalPort(opId: String, input: Boolean = true): GlobalPortIdentity = + GlobalPortIdentity(physicalOpId(opId), PortIdentity(0), input = input) +} diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/common/FriesReconfigurationAlgorithmSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/common/FriesReconfigurationAlgorithmSpec.scala index a99b8dc005c..fbee2c30409 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/common/FriesReconfigurationAlgorithmSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/common/FriesReconfigurationAlgorithmSpec.scala @@ -107,110 +107,91 @@ class FriesReconfigurationAlgorithmSpec extends AnyFlatSpec { components.head } + private def verifySingleReconfiguration( + ops: Set[PhysicalOp], + links: Set[PhysicalLink], + targets: Seq[String], + expectedScope: Set[String], + expectedSources: Set[String], + excludedFromScope: Set[String] = Set.empty, + extraRegions: Seq[Region] = Seq.empty + ): Unit = { + val region = buildRegion(1, ops, links) + val request = reconfigureRequest(targets: _*) + val component = singleComponent( + FriesReconfigurationAlgorithm.getReconfigurations( + managerWithRegions((region +: extraRegions): _*), + request + ) + ) + assert(component.scope == expectedScope.map(physicalOpId)) + assert(component.sources == expectedSources.map(physicalOpId)) + assert(component.reconfigurations == request.reconfiguration.toSet) + excludedFromScope.foreach(id => assert(!component.scope.contains(physicalOpId(id)))) + } + "getReconfigurations" should "limit the scope to the reconfigured operator when no one-to-many operator is upstream" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a"), op("b"), op("c")), - Set(link("a", "b"), link("b", "c")) + Set(link("a", "b"), link("b", "c")), + Seq("b"), + expectedScope = Set("b"), + expectedSources = Set("b"), + excludedFromScope = Set("a", "c") ) - val request = reconfigureRequest("b") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("b"))) - assert(component.sources == Set(physicalOpId("b"))) - assert(component.reconfigurations == request.reconfiguration.toSet) - assert(!component.scope.contains(physicalOpId("a"))) - assert(!component.scope.contains(physicalOpId("c"))) } it should "extend the scope up to a one-to-many operator that has a reconfigured descendant" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a", oneToMany = true), op("b"), op("c")), - Set(link("a", "b"), link("b", "c")) + Set(link("a", "b"), link("b", "c")), + Seq("c"), + expectedScope = Set("a", "b", "c"), + expectedSources = Set("a") ) - val request = reconfigureRequest("c") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("a"), physicalOpId("b"), physicalOpId("c"))) - assert(component.sources == Set(physicalOpId("a"))) - assert(component.reconfigurations == request.reconfiguration.toSet) } it should "ignore one-to-many operators that have no reconfigured descendant" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a"), op("b"), op("x", oneToMany = true), op("y")), - Set(link("a", "b"), link("x", "y")) + Set(link("a", "b"), link("x", "y")), + Seq("b"), + expectedScope = Set("b"), + expectedSources = Set("b"), + excludedFromScope = Set("x", "y") ) - val request = reconfigureRequest("b") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("b"))) - assert(!component.scope.contains(physicalOpId("x"))) - assert(!component.scope.contains(physicalOpId("y"))) } it should "not extend the scope to a one-to-many operator downstream of the reconfigured operator" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a"), op("b"), op("m", oneToMany = true), op("c")), - Set(link("a", "b"), link("b", "m"), link("m", "c")) + Set(link("a", "b"), link("b", "m"), link("m", "c")), + Seq("b"), + expectedScope = Set("b"), + expectedSources = Set("b"), + excludedFromScope = Set("m", "c") ) - val request = reconfigureRequest("b") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("b"))) - assert(!component.scope.contains(physicalOpId("m"))) - assert(!component.scope.contains(physicalOpId("c"))) } it should "exclude side branches that do not lead back to a reconfigured operator" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("s", oneToMany = true), op("b"), op("c"), op("x")), - Set(link("s", "b"), link("b", "c"), link("s", "x")) + Set(link("s", "b"), link("b", "c"), link("s", "x")), + Seq("c"), + expectedScope = Set("s", "b", "c"), + expectedSources = Set("s"), + excludedFromScope = Set("x") ) - val request = reconfigureRequest("c") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("s"), physicalOpId("b"), physicalOpId("c"))) - assert(component.sources == Set(physicalOpId("s"))) - assert(!component.scope.contains(physicalOpId("x"))) } it should "include all parallel branches between a one-to-many operator and the reconfigured operator" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("s", oneToMany = true), op("a"), op("b"), op("t")), - Set(link("s", "a"), link("s", "b"), link("a", "t"), link("b", "t")) + Set(link("s", "a"), link("s", "b"), link("a", "t"), link("b", "t")), + Seq("t"), + expectedScope = Set("s", "a", "b", "t"), + expectedSources = Set("s") ) - val request = reconfigureRequest("t") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert( - component.scope == - Set(physicalOpId("s"), physicalOpId("a"), physicalOpId("b"), physicalOpId("t")) - ) - assert(component.sources == Set(physicalOpId("s"))) } it should "split disconnected closures into separate components with their own reconfigurations" in { @@ -235,64 +216,48 @@ class FriesReconfigurationAlgorithmSpec extends AnyFlatSpec { } it should "merge reconfigured operators connected by a link into a single component" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a"), op("b"), op("c"), op("d")), - Set(link("a", "b"), link("b", "c"), link("c", "d")) + Set(link("a", "b"), link("b", "c"), link("c", "d")), + Seq("b", "c"), + expectedScope = Set("b", "c"), + expectedSources = Set("b"), + excludedFromScope = Set("a", "d") ) - val request = reconfigureRequest("b", "c") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("b"), physicalOpId("c"))) - assert(component.sources == Set(physicalOpId("b"))) - assert(component.reconfigurations == request.reconfiguration.toSet) - assert(!component.scope.contains(physicalOpId("a"))) - assert(!component.scope.contains(physicalOpId("d"))) } it should "handle a single-operator region" in { - val region = buildRegion(1, Set(op("only")), Set.empty) - val request = reconfigureRequest("only") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("only"))) - assert(component.sources == Set(physicalOpId("only"))) - assert(component.reconfigurations == request.reconfiguration.toSet) + verifySingleReconfiguration( + Set(op("only")), + Set.empty, + Seq("only"), + expectedScope = Set("only"), + expectedSources = Set("only") + ) } it should "select the reconfigured operator itself as the marker source when it has no upstream" in { - val region = buildRegion(1, Set(op("a"), op("b")), Set(link("a", "b"))) - val request = reconfigureRequest("a") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("a"))) - assert(component.sources == Set(physicalOpId("a"))) - assert(!component.scope.contains(physicalOpId("b"))) + verifySingleReconfiguration( + Set(op("a"), op("b")), + Set(link("a", "b")), + Seq("a"), + expectedScope = Set("a"), + expectedSources = Set("a"), + excludedFromScope = Set("b") + ) } it should "produce components only for regions that contain reconfigured operators" in { - val regionWithReconfiguration = - buildRegion(1, Set(op("a1"), op("b1")), Set(link("a1", "b1"))) val regionWithoutReconfiguration = buildRegion(2, Set(op("a2"), op("b2")), Set(link("a2", "b2"))) - val request = reconfigureRequest("b1") - - val components = FriesReconfigurationAlgorithm.getReconfigurations( - managerWithRegions(regionWithReconfiguration, regionWithoutReconfiguration), - request + verifySingleReconfiguration( + Set(op("a1"), op("b1")), + Set(link("a1", "b1")), + Seq("b1"), + expectedScope = Set("b1"), + expectedSources = Set("b1"), + extraRegions = Seq(regionWithoutReconfiguration) ) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("b1"))) } it should "schedule reconfigurations independently for each executing region" in { @@ -309,6 +274,8 @@ class FriesReconfigurationAlgorithmSpec extends AnyFlatSpec { assert(components.map(_.scope) == Set(Set(physicalOpId("b1")), Set(physicalOpId("b2")))) val componentOfB1 = components.find(_.scope.contains(physicalOpId("b1"))).get val componentOfB2 = components.find(_.scope.contains(physicalOpId("b2"))).get + assert(componentOfB1.sources == Set(physicalOpId("b1"))) + assert(componentOfB2.sources == Set(physicalOpId("b2"))) assert(componentOfB1.reconfigurations.map(_.targetOpId) == Set(physicalOpId("b1"))) assert(componentOfB2.reconfigurations.map(_.targetOpId) == Set(physicalOpId("b2"))) } @@ -327,39 +294,24 @@ class FriesReconfigurationAlgorithmSpec extends AnyFlatSpec { // The descendant-based pull-in only inspects OTHER one-to-many operators upstream; // a reconfigured one-to-many operator does not widen its own scope, so // ReconfigurationHandler's scope.size == 1 fast path applies. - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a"), op("m", oneToMany = true), op("c")), - Set(link("a", "m"), link("m", "c")) + Set(link("a", "m"), link("m", "c")), + Seq("m"), + expectedScope = Set("m"), + expectedSources = Set("m"), + excludedFromScope = Set("a", "c") ) - val request = reconfigureRequest("m") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("m"))) - assert(component.sources == Set(physicalOpId("m"))) - assert(component.reconfigurations == request.reconfiguration.toSet) - assert(!component.scope.contains(physicalOpId("a"))) - assert(!component.scope.contains(physicalOpId("c"))) } it should "select multiple marker sources when several one-to-many operators converge on the reconfigured operator" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("s1", oneToMany = true), op("s2", oneToMany = true), op("t")), - Set(link("s1", "t"), link("s2", "t")) + Set(link("s1", "t"), link("s2", "t")), + Seq("t"), + expectedScope = Set("s1", "s2", "t"), + expectedSources = Set("s1", "s2") ) - val request = reconfigureRequest("t") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("s1"), physicalOpId("s2"), physicalOpId("t"))) - assert(component.sources == Set(physicalOpId("s1"), physicalOpId("s2"))) - assert(component.reconfigurations == request.reconfiguration.toSet) } it should "traverse links on every input port when branches arrive on different ports" in { @@ -370,39 +322,18 @@ class FriesReconfigurationAlgorithmSpec extends AnyFlatSpec { OpExecInitInfo.Empty ).withInputPorts(List(InputPort(PortIdentity()), InputPort(PortIdentity(1)))) .withOutputPorts(List(OutputPort(PortIdentity()))) - val region = buildRegion( - 1, - Set( - op("m0", oneToMany = true), - op("b0"), - op("m1", oneToMany = true), - op("b1"), - targetOp - ), + verifySingleReconfiguration( + Set(op("m0", oneToMany = true), op("b0"), op("m1", oneToMany = true), op("b1"), targetOp), Set( link("m0", "b0"), link("b0", "t", toPort = 0), link("m1", "b1"), link("b1", "t", toPort = 1) - ) - ) - val request = reconfigureRequest("t") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert( - component.scope == Set( - physicalOpId("m0"), - physicalOpId("b0"), - physicalOpId("m1"), - physicalOpId("b1"), - physicalOpId("t") - ) + ), + Seq("t"), + expectedScope = Set("m0", "b0", "m1", "b1", "t"), + expectedSources = Set("m0", "m1") ) - assert(component.sources == Set(physicalOpId("m0"), physicalOpId("m1"))) - assert(component.reconfigurations == request.reconfiguration.toSet) } it should "return no components for an empty reconfiguration request" in { @@ -416,20 +347,13 @@ class FriesReconfigurationAlgorithmSpec extends AnyFlatSpec { } it should "keep multiple reconfigured operators in the same connected component" in { - val region = buildRegion( - 1, + verifySingleReconfiguration( Set(op("a"), op("b"), op("c")), - Set(link("a", "b"), link("b", "c")) + Set(link("a", "b"), link("b", "c")), + Seq("a", "c"), + expectedScope = Set("a", "b", "c"), + expectedSources = Set("a") ) - val request = reconfigureRequest("a", "c") - - val components = - FriesReconfigurationAlgorithm.getReconfigurations(managerWithRegions(region), request) - - val component = singleComponent(components) - assert(component.scope == Set(physicalOpId("a"), physicalOpId("b"), physicalOpId("c"))) - assert(component.sources == Set(physicalOpId("a"))) - assert(component.reconfigurations == request.reconfiguration.toSet) } it should "ignore unknown target operators when splitting multiple disconnected components" in { diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/HDFSRecordStorageSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/HDFSRecordStorageSpec.scala index 3ae758a05be..6ba6324ebcd 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/HDFSRecordStorageSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/HDFSRecordStorageSpec.scala @@ -19,7 +19,6 @@ package org.apache.texera.amber.engine.common.storage -import org.apache.commons.io.FileUtils import org.apache.hadoop.conf.Configuration import org.apache.hadoop.fs.FileSystem import org.apache.texera.amber.engine.common.AmberSerdeTestSupport @@ -52,7 +51,10 @@ import java.util.UUID * No HDFS cluster and no native Hadoop install is needed — see * `withSeededHdfs` below for how the `hdfs` scheme is served locally. */ -class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { +class HDFSRecordStorageSpec + extends AnyFlatSpec + with AmberSerdeTestSupport + with RecordStorageTestSupport { // --------------------------------------------------------------------------- // Serving the `hdfs` scheme locally, with no cluster and no winutils @@ -81,31 +83,30 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // this spec runs unchanged on Linux/macOS CI. private def withSeededHdfs(prefix: String)(body: (URI, Path) => Unit): Unit = { - val root = Files.createTempDirectory(s"hdfs-record-storage-spec-$prefix-") - // A unique authority per test means a unique cache key per test: tests can - // neither see each other's FileSystem instance nor its working directory. - val authority = s"hdfs-record-storage-spec-${UUID.randomUUID()}:9000" - val conf = new Configuration() - conf.set("fs.hdfs.impl", classOf[WinutilsFreeLocalFileSystem].getName) - val fs = FileSystem.get(URI.create(s"hdfs://$authority/"), conf) - try { - // HDFSRecordStorage resolves its folder as - // `Path.mergePaths(fs.getWorkingDirectory, new Path(uri.getPath))`, so - // pointing the working directory at our temp root nests the URI's - // `/logs` path underneath it. - fs.setWorkingDirectory(new org.apache.hadoop.fs.Path(root.toUri.toString)) - body(URI.create(s"hdfs://$authority/logs"), root.resolve("logs")) - } finally { - // `FileSystem.close()` removes only this instance's own key, so the - // JVM-global cache is left exactly as we found it. Close before walking - // the temp tree: an open handle makes deletion flake on Windows. - fs.close() - cleanup(root) + withTempFolder(s"hdfs-record-storage-spec-$prefix-") { folder => + val root = folder.getParent + // A unique authority per test means a unique cache key per test: tests can + // neither see each other's FileSystem instance nor its working directory. + val authority = s"hdfs-record-storage-spec-${UUID.randomUUID()}:9000" + val conf = new Configuration() + conf.set("fs.hdfs.impl", classOf[WinutilsFreeLocalFileSystem].getName) + val fs = FileSystem.get(URI.create(s"hdfs://$authority/"), conf) + try { + // HDFSRecordStorage resolves its folder as + // `Path.mergePaths(fs.getWorkingDirectory, new Path(uri.getPath))`, so + // pointing the working directory at our temp root nests the URI's + // `/logs` path underneath it. + fs.setWorkingDirectory(new org.apache.hadoop.fs.Path(root.toUri.toString)) + body(URI.create(s"hdfs://$authority/logs"), folder) + } finally { + // `FileSystem.close()` removes only this instance's own key, so the + // JVM-global cache is left exactly as we found it. Close before walking + // the temp tree: an open handle makes deletion flake on Windows. + fs.close() + } } } - private def cleanup(root: Path): Unit = FileUtils.deleteQuietly(root.toFile) - // --------------------------------------------------------------------------- // Constructor — scheme guard // --------------------------------------------------------------------------- @@ -114,15 +115,11 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // A file:// location belongs to VFSRecordStorage. The guard must also fire // *before* any file-system work, so nothing is created on disk — that is // what the second assertion pins. - val root = Files.createTempDirectory("hdfs-record-storage-spec-bad-scheme-") - val folder = root.resolve("logs") - try { + withTempFolder("hdfs-record-storage-spec-bad-scheme-") { folder => intercept[AssertionError] { new HDFSRecordStorage[String](folder.toUri) } assert(!Files.exists(folder), "a rejected URI must not create anything on disk") - } finally { - cleanup(root) } } @@ -131,25 +128,17 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // --------------------------------------------------------------------------- it should "create the log folder when it does not yet exist" in { - withSeededHdfs("auto-create") { (uri, folder) => - assert(!Files.exists(folder), "precondition: folder must not exist before construction") - val _ = new HDFSRecordStorage[String](uri) - assert(Files.exists(folder), "constructor should auto-create the folder") - assert(Files.isDirectory(folder)) - } + withSeededHdfs("auto-create")((uri, folder) => + assertCreatesFolder(folder, new HDFSRecordStorage[String](uri)) + ) } it should "leave an existing folder intact" in { // If the folder already exists the constructor's existence check // short-circuits; pre-existing logs must survive re-opening the storage. - withSeededHdfs("existing") { (uri, folder) => - Files.createDirectories(folder) - val sentinel = folder.resolve("sentinel.txt") - Files.writeString(sentinel, "keep-me") - val _ = new HDFSRecordStorage[String](uri) - assert(Files.exists(sentinel), "constructor must not delete pre-existing files") - assert(Files.readString(sentinel) == "keep-me") - } + withSeededHdfs("existing")((uri, folder) => + assertPreservesExistingFolder(folder, new HDFSRecordStorage[String](uri)) + ) } // --------------------------------------------------------------------------- @@ -159,24 +148,7 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { "HDFSRecordStorage.getWriter + getReader" should "round-trip a sequence of records in write order" in { withSeededHdfs("round-trip") { (uri, folder) => - val storage = new HDFSRecordStorage[String](uri) - val writer = storage.getWriter("records.bin") - writer.writeRecord("one") - writer.writeRecord("two") - writer.writeRecord("three") - writer.flush() - writer.close() - - // The file must materialize *inside* the resolved log folder, i.e. under - // `mergePaths(fs.getWorkingDirectory, uri.getPath)`, which the fixture - // pins to root/logs. Note this is deliberately NOT a claim that the file - // lands at the URI's authority-rooted path: production appends the URI - // path to the working directory, so against a real NameNode - // `hdfs://nn:9000/logs` resolves under the user's home, not at `/logs`. - assert(Files.exists(folder.resolve("records.bin"))) - - val records = storage.getReader("records.bin").mkRecordIterator().toList - assert(records == List("one", "two", "three")) + assertRoundTripsThree(folder, new HDFSRecordStorage[String](uri)) } } @@ -184,17 +156,7 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // Two writers from one HDFSRecordStorage must not cross-pollinate; the // payloads differ so a swapped-path bug cannot pass. withSeededHdfs("two-files") { (uri, folder) => - val storage = new HDFSRecordStorage[String](uri) - val w1 = storage.getWriter("a.bin") - w1.writeRecord("from-a") - w1.flush(); w1.close() - val w2 = storage.getWriter("b.bin") - w2.writeRecord("from-b") - w2.flush(); w2.close() - assert(Files.exists(folder.resolve("a.bin"))) - assert(Files.exists(folder.resolve("b.bin"))) - assert(storage.getReader("a.bin").mkRecordIterator().toList == List("from-a")) - assert(storage.getReader("b.bin").mkRecordIterator().toList == List("from-b")) + assertIndependentFiles(new HDFSRecordStorage[String](uri), Some(folder)) } } @@ -204,16 +166,9 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // contents — appending would resurrect stale records during recovery. withSeededHdfs("overwrite") { (uri, _) => val storage = new HDFSRecordStorage[String](uri) - val first = storage.getWriter("log.bin") - first.writeRecord("stale-1") - first.writeRecord("stale-2") - first.flush(); first.close() - - val second = storage.getWriter("log.bin") - second.writeRecord("fresh") - second.flush(); second.close() - - assert(storage.getReader("log.bin").mkRecordIterator().toList == List("fresh")) + writeRecords(storage, "log.bin", "stale-1", "stale-2") + writeRecords(storage, "log.bin", "fresh") + assert(readRecords(storage, "log.bin") == List("fresh")) } } @@ -228,17 +183,15 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // Populate a *different* file first: the fallback must key off the // requested file's existence, not the folder being empty, and must not // hand back the sibling's records. - val writer = storage.getWriter("present.bin") - writer.writeRecord("sibling-payload") - writer.flush(); writer.close() + writeRecords(storage, "present.bin", "sibling-payload") assert(!Files.exists(folder.resolve("missing.bin")), "precondition: file must be absent") val iter = storage.getReader("missing.bin").mkRecordIterator() assert(!iter.hasNext, "absent file must yield a zero-element iterator") - assert(storage.getReader("missing.bin").mkRecordIterator().toList.isEmpty) + assert(readRecords(storage, "missing.bin").isEmpty) // Sanity: the present file is still readable, so the fallback did not // break the normal path. - assert(storage.getReader("present.bin").mkRecordIterator().toList == List("sibling-payload")) + assert(readRecords(storage, "present.bin") == List("sibling-payload")) } } @@ -249,14 +202,7 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { "HDFSRecordStorage.deleteStorage" should "remove the log folder along with its contents" in { withSeededHdfs("delete") { (uri, folder) => - val storage = new HDFSRecordStorage[String](uri) - val writer = storage.getWriter("data.bin") - writer.writeRecord("payload") - writer.flush(); writer.close() - assert(Files.exists(folder.resolve("data.bin"))) - - storage.deleteStorage() - assert(!Files.exists(folder), "deleteStorage should remove the storage folder recursively") + assertDeleteRemovesFolder(folder, new HDFSRecordStorage[String](uri)) } } @@ -319,11 +265,9 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { assert(storage.isInstanceOf[HDFSRecordStorage[_]]) // Round-trip through the dispatched instance so the assertion proves a // usable storage, not just a type tag. - val writer = storage.getWriter("data.bin") - writer.writeRecord("via-factory") - writer.flush(); writer.close() + writeRecords(storage, "data.bin", "via-factory") assert(Files.exists(folder.resolve("data.bin"))) - assert(storage.getReader("data.bin").mkRecordIterator().toList == List("via-factory")) + assert(readRecords(storage, "data.bin") == List("via-factory")) } } @@ -337,11 +281,9 @@ class HDFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { val upperCase = URI.create(s"HDFS://${uri.getAuthority}${uri.getPath}") val storage = SequentialRecordStorage.getStorage[String](Some(upperCase)) assert(storage.isInstanceOf[HDFSRecordStorage[_]]) - val writer = storage.getWriter("upper.bin") - writer.writeRecord("upper-case-scheme") - writer.flush(); writer.close() + writeRecords(storage, "upper.bin", "upper-case-scheme") assert(Files.exists(folder.resolve("upper.bin"))) - assert(storage.getReader("upper.bin").mkRecordIterator().toList == List("upper-case-scheme")) + assert(readRecords(storage, "upper.bin") == List("upper-case-scheme")) } } } diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/SequentialRecordStorageSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/SequentialRecordStorageSpec.scala index 1cdb70ccc44..8bc459505b6 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/SequentialRecordStorageSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/SequentialRecordStorageSpec.scala @@ -28,9 +28,104 @@ import org.apache.texera.amber.engine.common.storage.SequentialRecordStorage.{ import org.scalatest.flatspec.AnyFlatSpec import java.io.{ByteArrayInputStream, ByteArrayOutputStream, DataInputStream, DataOutputStream} -import java.nio.file.Files +import java.nio.file.{Files, Path} -class SequentialRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { +private[storage] trait RecordStorageTestSupport { + this: AnyFlatSpec => + + protected def withTempFolder(prefix: String)(body: Path => Unit): Unit = { + val root = Files.createTempDirectory(prefix) + try body(root.resolve("logs")) + finally FileUtils.deleteQuietly(root.toFile) + } + + protected def writeRecords[T >: Null <: AnyRef]( + storage: SequentialRecordStorage[T], + fileName: String, + records: T* + ): Unit = { + val writer = storage.getWriter(fileName) + records.foreach(writer.writeRecord) + writer.flush() + writer.close() + } + + protected def readRecords[T >: Null <: AnyRef]( + storage: SequentialRecordStorage[T], + fileName: String + ): List[T] = + storage.getReader(fileName).mkRecordIterator().toList + + protected def assertCreatesFolder[T >: Null <: AnyRef]( + folder: Path, + open: => SequentialRecordStorage[T] + ): Unit = { + assert(!Files.exists(folder), "precondition: folder must not exist before construction") + val _ = open + assert(Files.exists(folder), "constructor should auto-create the folder") + assert(Files.isDirectory(folder)) + } + + protected def assertPreservesExistingFolder[T >: Null <: AnyRef]( + folder: Path, + open: => SequentialRecordStorage[T] + ): Unit = { + Files.createDirectories(folder) + val sentinel = folder.resolve("sentinel.txt") + Files.writeString(sentinel, "keep-me") + val _ = open + assert(Files.exists(sentinel), "constructor must not delete pre-existing files") + assert(Files.readString(sentinel) == "keep-me") + } + + protected def assertRoundTripsThree( + folder: Path, + storage: SequentialRecordStorage[String] + ): Unit = { + writeRecords(storage, "records.bin", "one", "two", "three") + assert(Files.exists(folder.resolve("records.bin"))) + assert(readRecords(storage, "records.bin") == List("one", "two", "three")) + } + + protected def assertIndependentFiles( + storage: SequentialRecordStorage[String], + checkFolder: Option[Path] = None + ): Unit = { + writeRecords(storage, "a.bin", "from-a") + writeRecords(storage, "b.bin", "from-b") + checkFolder.foreach { folder => + assert(Files.exists(folder.resolve("a.bin"))) + assert(Files.exists(folder.resolve("b.bin"))) + } + assert(readRecords(storage, "a.bin") == List("from-a")) + assert(readRecords(storage, "b.bin") == List("from-b")) + } + + protected def assertDeleteRemovesFolder( + folder: Path, + storage: SequentialRecordStorage[String] + ): Unit = { + writeRecords(storage, "data.bin", "payload") + assert(Files.exists(folder.resolve("data.bin"))) + storage.deleteStorage() + assert(!Files.exists(folder), "deleteStorage should remove the storage folder") + } +} + +class SequentialRecordStorageSpec + extends AnyFlatSpec + with AmberSerdeTestSupport + with RecordStorageTestSupport { + + private def readerForWrites(records: String*): SequentialRecordReader[String] = { + val baos = new ByteArrayOutputStream() + val writer = new SequentialRecordWriter[String](new DataOutputStream(baos)) + records.foreach(writer.writeRecord) + writer.flush() + writer.close() + val payload = baos.toByteArray + new SequentialRecordReader[String](() => new DataInputStream(new ByteArrayInputStream(payload))) + } // --------------------------------------------------------------------------- // SequentialRecordWriter + SequentialRecordReader — in-memory round-trip @@ -43,28 +138,12 @@ class SequentialRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport "SequentialRecordWriter + SequentialRecordReader" should "round-trip a sequence of records through a size-prefixed binary frame" in { - val baos = new ByteArrayOutputStream() - val writer = new SequentialRecordWriter[String](new DataOutputStream(baos)) - writer.writeRecord("alpha") - writer.writeRecord("beta") - writer.writeRecord("gamma") - writer.flush() - writer.close() - - val reader = new SequentialRecordReader[String](() => - new DataInputStream(new ByteArrayInputStream(baos.toByteArray)) - ) + val reader = readerForWrites("alpha", "beta", "gamma") assert(reader.mkRecordIterator().toList == List("alpha", "beta", "gamma")) } it should "round-trip an empty stream as a zero-element iterator" in { - val baos = new ByteArrayOutputStream() - val writer = new SequentialRecordWriter[String](new DataOutputStream(baos)) - writer.flush() - writer.close() - val reader = new SequentialRecordReader[String](() => - new DataInputStream(new ByteArrayInputStream(baos.toByteArray)) - ) + val reader = readerForWrites() assert(reader.mkRecordIterator().toList.isEmpty) } @@ -72,14 +151,7 @@ class SequentialRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport // The size-prefixed format has the same shape for 1 element as for // many, but pinning the 1-record case independently catches an // off-by-one in the iterator's prefetch logic. - val baos = new ByteArrayOutputStream() - val writer = new SequentialRecordWriter[String](new DataOutputStream(baos)) - writer.writeRecord("only") - writer.flush() - writer.close() - val reader = new SequentialRecordReader[String](() => - new DataInputStream(new ByteArrayInputStream(baos.toByteArray)) - ) + val reader = readerForWrites("only") val iter = reader.mkRecordIterator() assert(iter.hasNext) assert(iter.next() == "only") @@ -92,16 +164,7 @@ class SequentialRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport // own DataInputStream (constructed by the thunk) and produce the // same sequence — proving the thunk is invoked per iterator and that // the reader holds no shared mutable input state. - val baos = new ByteArrayOutputStream() - val writer = new SequentialRecordWriter[String](new DataOutputStream(baos)) - writer.writeRecord("a") - writer.writeRecord("b") - writer.flush() - writer.close() - val payload = baos.toByteArray - val reader = new SequentialRecordReader[String](() => - new DataInputStream(new ByteArrayInputStream(payload)) - ) + val reader = readerForWrites("a", "b") assert(reader.mkRecordIterator().toList == List("a", "b")) assert(reader.mkRecordIterator().toList == List("a", "b")) } @@ -112,33 +175,19 @@ class SequentialRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport "SequentialRecordStorage.fetchAllRecords" should "return every record written to the underlying storage in order" in { - val tmp = Files.createTempDirectory("seq-storage-fetch-all-") - val sub = tmp.resolve("logs") - try { + withTempFolder("seq-storage-fetch-all-") { sub => val storage = new VFSRecordStorage[String](sub.toUri) - val writer = storage.getWriter("file-1") - writer.writeRecord("r1") - writer.writeRecord("r2") - writer.writeRecord("r3") - writer.flush(); writer.close() - + writeRecords(storage, "file-1", "r1", "r2", "r3") val all = SequentialRecordStorage.fetchAllRecords(storage, "file-1").toList assert(all == List("r1", "r2", "r3")) - } finally { - FileUtils.deleteQuietly(tmp.toFile) } } it should "return an empty Iterable when the underlying reader has no records" in { - val tmp = Files.createTempDirectory("seq-storage-fetch-empty-") - val sub = tmp.resolve("logs") - try { + withTempFolder("seq-storage-fetch-empty-") { sub => val storage = new VFSRecordStorage[String](sub.toUri) - val writer = storage.getWriter("empty") - writer.flush(); writer.close() + writeRecords(storage, "empty") assert(SequentialRecordStorage.fetchAllRecords(storage, "empty").toList.isEmpty) - } finally { - FileUtils.deleteQuietly(tmp.toFile) } } @@ -160,21 +209,14 @@ class SequentialRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport } it should "return a VFSRecordStorage for a file:// URI" in { - val tmp = Files.createTempDirectory("seq-storage-factory-vfs-") - val sub = tmp.resolve("logs") - try { + withTempFolder("seq-storage-factory-vfs-") { sub => val storage = SequentialRecordStorage.getStorage[String](Some(sub.toUri)) assert(storage.isInstanceOf[VFSRecordStorage[_]]) // Round-trip a record through the dispatched VFSRecordStorage to // prove the factory actually returned a usable instance (not a // half-initialized one). - val w = storage.getWriter("data") - w.writeRecord("payload") - w.flush(); w.close() - val r = storage.getReader("data").mkRecordIterator().toList - assert(r == List("payload")) - } finally { - FileUtils.deleteQuietly(tmp.toFile) + writeRecords(storage, "data", "payload") + assert(readRecords(storage, "data") == List("payload")) } } diff --git a/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/VFSRecordStorageSpec.scala b/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/VFSRecordStorageSpec.scala index c203f98fbfe..c2e27f24303 100644 --- a/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/VFSRecordStorageSpec.scala +++ b/amber/src/test/scala/org/apache/texera/amber/engine/common/storage/VFSRecordStorageSpec.scala @@ -19,32 +19,25 @@ package org.apache.texera.amber.engine.common.storage -import org.apache.commons.io.FileUtils import org.apache.texera.amber.engine.common.AmberSerdeTestSupport import org.scalatest.flatspec.AnyFlatSpec -import java.net.URI import java.nio.file.{Files, Path} -class VFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { +class VFSRecordStorageSpec + extends AnyFlatSpec + with AmberSerdeTestSupport + with RecordStorageTestSupport { // --------------------------------------------------------------------------- // Temp-directory helpers — every test owns its own scratch folder so the // cases are independent and parallel-safe. // --------------------------------------------------------------------------- - // Returns (sub, uri) where `sub` is the storage folder under a unique - // temp root and is NOT yet created on disk (so constructor tests can - // pin the auto-create-folder branch). The parent of `sub` IS the unique - // temp root, which is what cleanup() removes — keeping the disk clean - // even when a test fails before the storage folder gets created. - private def mkTempUri(prefix: String): (Path, URI) = { - val root = Files.createTempDirectory(s"vfs-record-storage-spec-$prefix-") - val sub = root.resolve("logs") - (sub, sub.toUri) - } - - private def cleanup(sub: Path): Unit = FileUtils.deleteQuietly(sub.getParent.toFile) + // `sub` is the storage folder under a unique temp root and is NOT yet + // created on disk (so constructor tests can pin the auto-create-folder branch). + private def withVfsFolder(prefix: String)(body: Path => Unit): Unit = + withTempFolder(s"vfs-record-storage-spec-$prefix-")(body) // --------------------------------------------------------------------------- // Constructor — auto-create folder @@ -52,31 +45,17 @@ class VFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { "VFSRecordStorage constructor" should "create the target folder when it does not yet exist" in { - val (path, uri) = mkTempUri("auto-create") - assert(!Files.exists(path), "precondition: folder must not exist before construction") - try { - val _ = new VFSRecordStorage[String](uri) - assert(Files.exists(path), "constructor should auto-create the folder") - assert(Files.isDirectory(path)) - } finally { - cleanup(path) - } + withVfsFolder("auto-create")(path => + assertCreatesFolder(path, new VFSRecordStorage[String](path.toUri)) + ) } it should "leave an existing folder intact" in { // If the folder already exists, the constructor's existence check // short-circuits and the folder must not be recreated / wiped. - val (path, uri) = mkTempUri("existing") - Files.createDirectories(path) - val sentinel = path.resolve("sentinel.txt") - Files.writeString(sentinel, "keep-me") - try { - val _ = new VFSRecordStorage[String](uri) - assert(Files.exists(sentinel), "constructor must not delete pre-existing files in the folder") - assert(Files.readString(sentinel) == "keep-me") - } finally { - cleanup(path) - } + withVfsFolder("existing")(path => + assertPreservesExistingFolder(path, new VFSRecordStorage[String](path.toUri)) + ) } // --------------------------------------------------------------------------- @@ -85,60 +64,27 @@ class VFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { "VFSRecordStorage.getWriter + getReader" should "round-trip a sequence of records through a local file:// URI" in { - val (path, uri) = mkTempUri("round-trip") - try { - val storage = new VFSRecordStorage[String](uri) - val writer = storage.getWriter("records.bin") - writer.writeRecord("one") - writer.writeRecord("two") - writer.writeRecord("three") - writer.flush() - writer.close() - - // The file produced by getWriter must be visible on disk under the - // configured folder URI (proves we wrote to the right place). - assert(Files.exists(path.resolve("records.bin"))) - - val records = storage.getReader("records.bin").mkRecordIterator().toList - assert(records == List("one", "two", "three")) - } finally { - cleanup(path) + withVfsFolder("round-trip") { path => + assertRoundTripsThree(path, new VFSRecordStorage[String](path.toUri)) } } it should "produce an empty iterator when reading a file containing no records" in { // An empty file (writer opened and closed without writing) must read // back as zero records, not throw. - val (path, uri) = mkTempUri("empty-file") - try { - val storage = new VFSRecordStorage[String](uri) - val writer = storage.getWriter("empty.bin") - writer.flush() - writer.close() + withVfsFolder("empty-file") { path => + val storage = new VFSRecordStorage[String](path.toUri) + writeRecords(storage, "empty.bin") assert(Files.exists(path.resolve("empty.bin"))) - val records = storage.getReader("empty.bin").mkRecordIterator().toList - assert(records.isEmpty) - } finally { - cleanup(path) + assert(readRecords(storage, "empty.bin").isEmpty) } } it should "support multiple distinct files under the same storage folder" in { // Two writers under the same VFSRecordStorage instance must produce // independent files — no cross-pollination. - val (path, uri) = mkTempUri("two-files") - try { - val storage = new VFSRecordStorage[String](uri) - val w1 = storage.getWriter("a.bin") - w1.writeRecord("from-a") - w1.flush(); w1.close() - val w2 = storage.getWriter("b.bin") - w2.writeRecord("from-b") - w2.flush(); w2.close() - assert(storage.getReader("a.bin").mkRecordIterator().toList == List("from-a")) - assert(storage.getReader("b.bin").mkRecordIterator().toList == List("from-b")) - } finally { - cleanup(path) + withVfsFolder("two-files") { path => + assertIndependentFiles(new VFSRecordStorage[String](path.toUri)) } } @@ -148,18 +94,8 @@ class VFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { "VFSRecordStorage.deleteStorage" should "remove the folder created by the constructor along with its contents" in { - val (path, uri) = mkTempUri("delete") - try { - val storage = new VFSRecordStorage[String](uri) - val writer = storage.getWriter("data.bin") - writer.writeRecord("payload") - writer.flush(); writer.close() - assert(Files.exists(path.resolve("data.bin"))) - - storage.deleteStorage() - assert(!Files.exists(path), "deleteStorage should remove the storage folder") - } finally { - cleanup(path) + withVfsFolder("delete") { path => + assertDeleteRemovesFolder(path, new VFSRecordStorage[String](path.toUri)) } } @@ -168,39 +104,30 @@ class VFSRecordStorageSpec extends AnyFlatSpec with AmberSerdeTestSupport { // --------------------------------------------------------------------------- "VFSRecordStorage.containsFolder" should "return true for an existing sub-folder" in { - val (path, uri) = mkTempUri("contains-folder") - try { - val storage = new VFSRecordStorage[String](uri) + withVfsFolder("contains-folder") { path => + val storage = new VFSRecordStorage[String](path.toUri) // Create the sub-folder AFTER the storage is constructed so the // test pins the live-lookup behavior of containsFolder (not a // value cached at construction time). Files.createDirectory(path.resolve("nested")) assert(storage.containsFolder("nested")) - } finally { - cleanup(path) } } it should "return false for a missing child entry" in { - val (path, uri) = mkTempUri("contains-missing") - try { - val storage = new VFSRecordStorage[String](uri) + withVfsFolder("contains-missing") { path => + val storage = new VFSRecordStorage[String](path.toUri) assert(!storage.containsFolder("does-not-exist")) - } finally { - cleanup(path) } } it should "return false when the child entry exists but is a file (not a folder)" in { // The `containsFolder` contract is "exists AND isFolder". A plain // file with the requested name must NOT register as a folder. - val (path, uri) = mkTempUri("contains-file") - try { - val storage = new VFSRecordStorage[String](uri) + withVfsFolder("contains-file") { path => + val storage = new VFSRecordStorage[String](path.toUri) Files.writeString(path.resolve("looks-like-folder"), "i am a file") assert(!storage.containsFolder("looks-like-folder")) - } finally { - cleanup(path) } } } diff --git a/amber/src/test/scala/org/apache/texera/web/ExecutionDatabaseTestSupport.scala b/amber/src/test/scala/org/apache/texera/web/ExecutionDatabaseTestSupport.scala new file mode 100644 index 00000000000..1b633e41bbe --- /dev/null +++ b/amber/src/test/scala/org/apache/texera/web/ExecutionDatabaseTestSupport.scala @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.texera.web + +import org.apache.texera.dao.MockTexeraDB +import org.apache.texera.dao.jooq.generated.Tables._ +import org.apache.texera.dao.jooq.generated.enums.{UserRoleEnum, WorkflowComputingUnitTypeEnum} +import org.apache.texera.dao.jooq.generated.tables.daos.{ + UserDao, + WorkflowComputingUnitDao, + WorkflowDao, + WorkflowExecutionsDao, + WorkflowVersionDao +} +import org.apache.texera.dao.jooq.generated.tables.pojos.{ + User, + Workflow, + WorkflowComputingUnit, + WorkflowExecutions, + WorkflowVersion +} + +import java.sql.Timestamp + +/** + * Shared `MockTexeraDB` row builders and reverse-FK cleanup helpers for execution-backed suites. + */ +trait ExecutionDatabaseTestSupport { this: MockTexeraDB => + + protected def workflowExecutionsDao: WorkflowExecutionsDao = + new WorkflowExecutionsDao(getDSLContext.configuration()) + + protected def insertUser( + uid: Int, + name: String, + email: String, + role: Option[UserRoleEnum] = None + ): User = { + val user = new User + user.setUid(uid) + user.setName(name) + user.setEmail(email) + role.foreach(user.setRole) + new UserDao(getDSLContext.configuration()).insert(user) + user + } + + protected def insertWorkflow( + wid: Int, + name: String, + content: String = "{}", + description: String = "", + withTimestamps: Boolean = true + ): Workflow = { + val workflow = new Workflow + workflow.setWid(wid) + workflow.setName(name) + workflow.setContent(content) + workflow.setDescription(description) + if (withTimestamps) { + val now = new Timestamp(System.currentTimeMillis()) + workflow.setCreationTime(now) + workflow.setLastModifiedTime(now) + } + new WorkflowDao(getDSLContext.configuration()).insert(workflow) + workflow + } + + protected def insertWorkflowVersion( + wid: Int, + vidOpt: Option[Int] = None, + content: String = "{}", + withTimestamp: Boolean = true + ): WorkflowVersion = { + val version = new WorkflowVersion + version.setWid(wid) + vidOpt.foreach(v => version.setVid(v)) + version.setContent(content) + if (withTimestamp) version.setCreationTime(new Timestamp(System.currentTimeMillis())) + new WorkflowVersionDao(getDSLContext.configuration()).insert(version) + version + } + + protected def insertComputingUnit( + uid: Int, + name: String, + cuidOpt: Option[Int] = None, + unitType: Option[WorkflowComputingUnitTypeEnum] = Some(WorkflowComputingUnitTypeEnum.local), + uri: Option[String] = Some("local://test"), + resource: Option[String] = Some("{}") + ): WorkflowComputingUnit = { + val cu = new WorkflowComputingUnit + cuidOpt.foreach(id => cu.setCuid(id)) + cu.setUid(uid) + cu.setName(name) + if (unitType.isDefined) cu.setCreationTime(new Timestamp(System.currentTimeMillis())) + unitType.foreach(cu.setType) + uri.foreach(cu.setUri) + resource.foreach(cu.setResource) + new WorkflowComputingUnitDao(getDSLContext.configuration()).insert(cu) + cu + } + + protected def insertWorkflowExecution( + vid: Int, + uid: Int, + eidOpt: Option[Int] = None, + cuidOpt: Option[Int] = None, + status: Byte = 0.toByte, + name: String = "execution", + envVersion: String = "test-env", + result: Option[String] = Some(""), + logLocation: Option[String] = None, + runtimeStatsUri: Option[String] = None, + bookmarked: Option[Boolean] = Some(false), + startOffsetMillis: Option[Long] = Some(0L), + durationMillis: Option[Long] = None + ): WorkflowExecutions = { + val execution = new WorkflowExecutions + eidOpt.foreach(id => execution.setEid(id)) + execution.setVid(vid) + execution.setUid(uid) + cuidOpt.foreach(id => execution.setCuid(id)) + execution.setStatus(status) + result.foreach(execution.setResult) + logLocation.foreach(execution.setLogLocation) + runtimeStatsUri.foreach(execution.setRuntimeStatsUri) + val now = System.currentTimeMillis() + startOffsetMillis.foreach(offset => execution.setStartingTime(new Timestamp(now - offset))) + for (offset <- startOffsetMillis; dur <- durationMillis) { + execution.setLastUpdateTime(new Timestamp(now - offset + dur)) + } + bookmarked.foreach(b => execution.setBookmarked(b)) + if (name.nonEmpty) execution.setName(name) + execution.setEnvironmentVersion(envVersion) + workflowExecutionsDao.insert(execution) + execution + } + + protected def cleanupExecutionsForVersion( + vid: Int, + includePortExecutions: Boolean = false + ): Unit = { + val vids = org.jooq.impl.DSL + .select(WORKFLOW_VERSION.VID) + .from(WORKFLOW_VERSION) + .where(WORKFLOW_VERSION.VID.eq(vid)) + deleteExecutionsForVersions(vids, includePortExecutions) + } + + private def deleteExecutionsForVersions( + vids: org.jooq.SelectConditionStep[org.jooq.Record1[Integer]], + includePortExecutions: Boolean + ): Unit = { + if (includePortExecutions) { + val eids = getDSLContext + .select(WORKFLOW_EXECUTIONS.EID) + .from(WORKFLOW_EXECUTIONS) + .where(WORKFLOW_EXECUTIONS.VID.in(vids)) + getDSLContext + .deleteFrom(OPERATOR_PORT_EXECUTIONS) + .where(OPERATOR_PORT_EXECUTIONS.WORKFLOW_EXECUTION_ID.in(eids)) + .execute() + } + getDSLContext.deleteFrom(WORKFLOW_EXECUTIONS).where(WORKFLOW_EXECUTIONS.VID.in(vids)).execute() + } + + protected def cleanupWorkflowRecords( + wids: Seq[Int], + uids: Seq[Int] = Seq.empty, + includePortExecutions: Boolean = false, + includeUserAccess: Boolean = false, + includeComputingUnits: Boolean = false + ): Unit = { + val boxedWids = wids.map(Integer.valueOf) + val vids = getDSLContext + .select(WORKFLOW_VERSION.VID) + .from(WORKFLOW_VERSION) + .where(WORKFLOW_VERSION.WID.in(boxedWids: _*)) + deleteExecutionsForVersions(vids, includePortExecutions) + if (includeUserAccess) { + getDSLContext + .deleteFrom(WORKFLOW_USER_ACCESS) + .where(WORKFLOW_USER_ACCESS.WID.in(boxedWids: _*)) + .execute() + } + getDSLContext + .deleteFrom(WORKFLOW_VERSION) + .where(WORKFLOW_VERSION.WID.in(boxedWids: _*)) + .execute() + getDSLContext.deleteFrom(WORKFLOW).where(WORKFLOW.WID.in(boxedWids: _*)).execute() + if (uids.nonEmpty) { + val boxedUids = uids.map(Integer.valueOf) + if (includeComputingUnits) { + getDSLContext + .deleteFrom(WORKFLOW_COMPUTING_UNIT) + .where(WORKFLOW_COMPUTING_UNIT.UID.in(boxedUids: _*)) + .execute() + } + getDSLContext.deleteFrom(USER).where(USER.UID.in(boxedUids: _*)).execute() + } + } +} diff --git a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/admin/execution/AdminExecutionResourceSpec.scala b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/admin/execution/AdminExecutionResourceSpec.scala index 260160f00d3..43491e7e38e 100644 --- a/amber/src/test/scala/org/apache/texera/web/resource/dashboard/admin/execution/AdminExecutionResourceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/resource/dashboard/admin/execution/AdminExecutionResourceSpec.scala @@ -21,28 +21,20 @@ package org.apache.texera.web.resource.dashboard.admin.execution import org.apache.texera.auth.SessionUser import org.apache.texera.dao.MockTexeraDB -import org.apache.texera.dao.jooq.generated.Tables._ +import org.apache.texera.dao.jooq.generated.Tables.WORKFLOW_OF_USER import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum -import org.apache.texera.dao.jooq.generated.tables.daos.{ - UserDao, - WorkflowDao, - WorkflowExecutionsDao, - WorkflowUserAccessDao, - WorkflowVersionDao -} +import org.apache.texera.dao.jooq.generated.tables.daos.WorkflowUserAccessDao import org.apache.texera.dao.jooq.generated.tables.pojos.{ User, - Workflow, WorkflowExecutions, WorkflowUserAccess, WorkflowVersion } -import org.scalatest.BeforeAndAfterAll -import org.scalatest.BeforeAndAfterEach +import org.apache.texera.web.ExecutionDatabaseTestSupport +import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers -import java.sql.Timestamp import java.util.UUID import java.util.concurrent.TimeUnit @@ -51,7 +43,8 @@ class AdminExecutionResourceSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach - with MockTexeraDB { + with MockTexeraDB + with ExecutionDatabaseTestSupport { // MockTexeraDB's embedded Postgres is a JVM singleton shared by all suites, which // run sequentially. Random ids keep this suite's rows from colliding with data other @@ -61,13 +54,7 @@ class AdminExecutionResourceSpec private val secondWid = testWid + 1 private var testUser: User = _ - private var testWorkflow: Workflow = _ private var testVersion: WorkflowVersion = _ - - private var userDao: UserDao = _ - private var workflowDao: WorkflowDao = _ - private var workflowVersionDao: WorkflowVersionDao = _ - private var workflowExecutionsDao: WorkflowExecutionsDao = _ private var workflowUserAccessDao: WorkflowUserAccessDao = _ private val resource = new AdminExecutionResource() @@ -81,62 +68,28 @@ class AdminExecutionResourceSpec } override protected def beforeEach(): Unit = { - userDao = new UserDao(getDSLContext.configuration()) - workflowDao = new WorkflowDao(getDSLContext.configuration()) - workflowVersionDao = new WorkflowVersionDao(getDSLContext.configuration()) - workflowExecutionsDao = new WorkflowExecutionsDao(getDSLContext.configuration()) workflowUserAccessDao = new WorkflowUserAccessDao(getDSLContext.configuration()) - cleanupTestData() - testUser = new User - testUser.setUid(testUid) - testUser.setName("test_user") - testUser.setEmail("admin_exec_test@example.com") - - testWorkflow = new Workflow - testWorkflow.setWid(testWid) - testWorkflow.setName("test_workflow_" + UUID.randomUUID().toString.substring(0, 8)) - testWorkflow.setContent("{}") - testWorkflow.setDescription("test description") - testWorkflow.setCreationTime(new Timestamp(System.currentTimeMillis())) - testWorkflow.setLastModifiedTime(new Timestamp(System.currentTimeMillis())) - - testVersion = new WorkflowVersion - testVersion.setWid(testWid) - testVersion.setContent("{}") - testVersion.setCreationTime(new Timestamp(System.currentTimeMillis())) - - userDao.insert(testUser) - workflowDao.insert(testWorkflow) - workflowVersionDao.insert(testVersion) + testUser = insertUser(testUid, "test_user", "admin_exec_test@example.com") + insertWorkflow( + testWid, + "test_workflow_" + UUID.randomUUID().toString.substring(0, 8), + description = "test description" + ) + testVersion = insertWorkflowVersion(testWid) } override protected def afterEach(): Unit = { cleanupTestData() } - private def cleanupTestData(): Unit = { - val vidSubquery = getDSLContext - .select(WORKFLOW_VERSION.VID) - .from(WORKFLOW_VERSION) - .where(WORKFLOW_VERSION.WID.in(testWid, secondWid)) - - getDSLContext - .deleteFrom(WORKFLOW_EXECUTIONS) - .where(WORKFLOW_EXECUTIONS.VID.in(vidSubquery)) - .execute() - getDSLContext - .deleteFrom(WORKFLOW_USER_ACCESS) - .where(WORKFLOW_USER_ACCESS.WID.in(testWid, secondWid)) - .execute() - getDSLContext - .deleteFrom(WORKFLOW_VERSION) - .where(WORKFLOW_VERSION.WID.in(testWid, secondWid)) - .execute() - getDSLContext.deleteFrom(WORKFLOW).where(WORKFLOW.WID.in(testWid, secondWid)).execute() - getDSLContext.deleteFrom(USER).where(USER.UID.eq(testUid)).execute() - } + private def cleanupTestData(): Unit = + cleanupWorkflowRecords( + Seq(testWid, secondWid), + uids = Seq(testUid), + includeUserAccess = true + ) // ─── helpers ──────────────────────────────────────────────────────────────── @@ -145,22 +98,17 @@ class AdminExecutionResourceSpec name: String = "run", startOffsetMillis: Long = 0L, durationMillis: Long = 1000L - ): WorkflowExecutions = { - val execution = new WorkflowExecutions - execution.setVid(testVersion.getVid) - execution.setUid(testUser.getUid) - execution.setStatus(status) - execution.setResult("") - execution.setLogLocation("") - val now = System.currentTimeMillis() - execution.setStartingTime(new Timestamp(now - startOffsetMillis)) - execution.setLastUpdateTime(new Timestamp(now - startOffsetMillis + durationMillis)) - execution.setBookmarked(false) - execution.setName(name) - execution.setEnvironmentVersion("test-env-1.0") - workflowExecutionsDao.insert(execution) - execution - } + ): WorkflowExecutions = + insertWorkflowExecution( + vid = testVersion.getVid, + uid = testUser.getUid, + status = status, + logLocation = Some(""), + envVersion = "test-env-1.0", + name = name, + startOffsetMillis = Some(startOffsetMillis), + durationMillis = Some(durationMillis) + ) private def grantAccess(uid: Int, privilege: PrivilegeEnum): Unit = { val access = new WorkflowUserAccess @@ -174,34 +122,22 @@ class AdminExecutionResourceSpec // testUser, whose latest execution ends `endOffsetMillis` before "now". Used to // give the ordering test two workflows with distinct end times. private def seedSecondWorkflow(endOffsetMillis: Long): Unit = { - val workflow = new Workflow - workflow.setWid(secondWid) - workflow.setName("second_workflow_" + UUID.randomUUID().toString.substring(0, 8)) - workflow.setContent("{}") - workflow.setDescription("second") - workflow.setCreationTime(new Timestamp(System.currentTimeMillis())) - workflow.setLastModifiedTime(new Timestamp(System.currentTimeMillis())) - workflowDao.insert(workflow) - - val version = new WorkflowVersion - version.setWid(secondWid) - version.setContent("{}") - version.setCreationTime(new Timestamp(System.currentTimeMillis())) - workflowVersionDao.insert(version) - - val execution = new WorkflowExecutions - execution.setVid(version.getVid) - execution.setUid(testUser.getUid) - execution.setStatus(3.toByte) - execution.setResult("") - execution.setLogLocation("") - val now = System.currentTimeMillis() - execution.setStartingTime(new Timestamp(now - endOffsetMillis - 1000)) - execution.setLastUpdateTime(new Timestamp(now - endOffsetMillis)) - execution.setBookmarked(false) - execution.setName("second-run") - execution.setEnvironmentVersion("test-env-1.0") - workflowExecutionsDao.insert(execution) + insertWorkflow( + secondWid, + "second_workflow_" + UUID.randomUUID().toString.substring(0, 8), + description = "second" + ) + val version = insertWorkflowVersion(secondWid) + insertWorkflowExecution( + vid = version.getVid, + uid = testUser.getUid, + status = 3.toByte, + logLocation = Some(""), + envVersion = "test-env-1.0", + name = "second-run", + startOffsetMillis = Some(endOffsetMillis + 1000), + durationMillis = Some(1000L) + ) } private def emptyFilter: java.util.List[String] = new java.util.ArrayList[String]() diff --git a/amber/src/test/scala/org/apache/texera/web/service/ExecutionConsoleServiceSpec.scala b/amber/src/test/scala/org/apache/texera/web/service/ExecutionConsoleServiceSpec.scala index c9cb96c1e09..7cedeb592e0 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/ExecutionConsoleServiceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/ExecutionConsoleServiceSpec.scala @@ -21,11 +21,9 @@ package org.apache.texera.web.service import com.google.protobuf.timestamp.Timestamp import com.twitter.util.{Future => TwitterFuture} -import io.reactivex.rxjava3.disposables.Disposable import org.apache.pekko.actor.ActorSystem import org.apache.pekko.testkit.TestKit -import org.apache.texera.amber.core.workflow.{PhysicalPlan, WorkflowContext} -import org.apache.texera.amber.engine.architecture.coordinator.CoordinatorConfig +import org.apache.texera.amber.core.workflow.WorkflowContext import org.apache.texera.amber.engine.architecture.rpc.controlcommands.{ ConsoleMessage, ConsoleMessageType, @@ -37,17 +35,16 @@ import org.apache.texera.amber.engine.architecture.rpc.controlreturns.{ TypedValue } import org.apache.texera.amber.engine.architecture.rpc.coordinatorservice.CoordinatorServiceFs2Grpc -import org.apache.texera.amber.engine.common.client.AmberClient import org.apache.texera.amber.engine.common.executionruntimestate.{ EvaluatedValueList, ExecutionConsoleStore, OperatorConsole } import org.apache.texera.web.WebsocketInput -import org.apache.texera.web.model.websocket.event.TexeraWebSocketEvent import org.apache.texera.web.model.websocket.event.python.ConsoleUpdateEvent import org.apache.texera.web.model.websocket.request.python.DebugCommandRequest import org.apache.texera.web.model.websocket.response.python.PythonExpressionEvaluateResponse +import org.apache.texera.web.service.RecordingExecutionClient.recordBatches import org.apache.texera.web.storage.ExecutionStateStore import org.scalamock.scalatest.MockFactory import org.scalatest.BeforeAndAfterAll @@ -60,7 +57,6 @@ import org.scalatest.time.{Millis, Span} import java.time.Instant import java.util.concurrent.ExecutorService import scala.collection.mutable.ListBuffer -import scala.reflect.ClassTag /** * The `ConsoleMessageProcessor` object is covered by the first half of this suite. The service @@ -90,226 +86,101 @@ class ExecutionConsoleServiceSpec val smallBufferSize: Int = 2 val messageDisplayLength: Int = 100 + private def message( + workerId: String = "Worker:WF1-udf1-main-0", + title: String = "hello", + msgType: ConsoleMessageType = ConsoleMessageType.PRINT, + source: String = "src", + content: String = "content" + ): ConsoleMessage = + new ConsoleMessage(workerId, Timestamp(Instant.now), msgType, source, title, content) + + private def addMessages(bufferSize: Int, titles: String*): ExecutionConsoleStore = + titles.zipWithIndex.foldLeft(new ExecutionConsoleStore()) { + case (store, (title, idx)) => + ConsoleMessageProcessor.addMessageToOperatorConsole( + store, + "op1", + message( + workerId = "worker1", + source = "test", + title = title, + content = s"content ${idx + 1}" + ), + bufferSize + ) + } + "processConsoleMessage" should "truncate message title when it exceeds display length" in { - // Create a long message title that exceeds display length val longTitle = "a" * (messageDisplayLength + 10) val expectedTruncatedTitle = "a" * (messageDisplayLength - 3) + "..." - - // Create a console message with a long title - val consoleMessage = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - longTitle, - "message content" + val processedMessage = ConsoleMessageProcessor.processConsoleMessage( + message( + workerId = "worker1", + source = "test", + title = longTitle, + content = "message content" + ), + messageDisplayLength ) - - // Call the method under test - val processedMessage = - ConsoleMessageProcessor.processConsoleMessage(consoleMessage, messageDisplayLength) - - // Verify the title was truncated processedMessage.title shouldBe expectedTruncatedTitle } it should "not truncate message title when it does not exceed display length" in { - // Create a short message title that doesn't exceed display length val shortTitle = "Short Title" - - // Create a console message with a short title - val consoleMessage = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - shortTitle, - "message content" + val processedMessage = ConsoleMessageProcessor.processConsoleMessage( + message( + workerId = "worker1", + source = "test", + title = shortTitle, + content = "message content" + ), + messageDisplayLength ) - - // Call the method under test - val processedMessage = - ConsoleMessageProcessor.processConsoleMessage(consoleMessage, messageDisplayLength) - - // Verify the title was not truncated processedMessage.title shouldBe shortTitle } "addMessageToOperatorConsole" should "add message to buffer when buffer is not full" in { - // Create a test console store - val consoleStore = new ExecutionConsoleStore() - val opId = "op1" - - // Create console messages - val message1 = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - "Message 1", - "content 1" - ) - - val message2 = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - "Message 2", - "content 2" - ) - - // Add first message - val storeWithMessage1 = - ConsoleMessageProcessor.addMessageToOperatorConsole( - consoleStore, - opId, - message1, - standardBufferSize - ) - - // Add second message - val storeWithMessage2 = ConsoleMessageProcessor.addMessageToOperatorConsole( - storeWithMessage1, - opId, - message2, - standardBufferSize - ) - - // Verify both messages are in the buffer - val opInfo = storeWithMessage2.operatorConsole(opId) + val opInfo = addMessages(standardBufferSize, "Message 1", "Message 2").operatorConsole("op1") opInfo.consoleMessages.size shouldBe 2 opInfo.consoleMessages.head.title shouldBe "Message 1" opInfo.consoleMessages(1).title shouldBe "Message 2" } it should "remove oldest message when buffer is full" in { - // Create a test console store - val consoleStore = new ExecutionConsoleStore() - val opId = "op1" - - // Create console messages - val message1 = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - "Message 1", - "content 1" - ) - - val message2 = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - "Message 2", - "content 2" - ) - - val message3 = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - "Message 3", - "content 3" - ) - - // Fill the buffer - val storeWithMessage1 = - ConsoleMessageProcessor.addMessageToOperatorConsole( - consoleStore, - opId, - message1, - smallBufferSize - ) - val storeWithMessage2 = - ConsoleMessageProcessor.addMessageToOperatorConsole( - storeWithMessage1, - opId, - message2, - smallBufferSize - ) - - // Add one more message which should remove the oldest - val storeWithMessage3 = - ConsoleMessageProcessor.addMessageToOperatorConsole( - storeWithMessage2, - opId, - message3, - smallBufferSize - ) - - // Verify the first message was removed and only the second and third remain - val opInfo = storeWithMessage3.operatorConsole(opId) + val opInfo = + addMessages(smallBufferSize, "Message 1", "Message 2", "Message 3").operatorConsole("op1") opInfo.consoleMessages.size shouldBe 2 opInfo.consoleMessages.head.title shouldBe "Message 2" opInfo.consoleMessages(1).title shouldBe "Message 3" } "the complete message processing flow" should "handle messages correctly" in { - // Create a test console store - val consoleStore = new ExecutionConsoleStore() val opId = "op1" - - // Create a message with a title that needs truncation val longTitle = "a" * (messageDisplayLength + 10) - val consoleMessage = new ConsoleMessage( - "worker1", - Timestamp(Instant.now), - ConsoleMessageType.PRINT, - "test", - longTitle, - "message content" + val processedMessage = ConsoleMessageProcessor.processConsoleMessage( + message( + workerId = "worker1", + source = "test", + title = longTitle, + content = "message content" + ), + messageDisplayLength ) - - // Process the message first - val processedMessage = - ConsoleMessageProcessor.processConsoleMessage(consoleMessage, messageDisplayLength) - - // Then update the store val updatedStore = ConsoleMessageProcessor.addMessageToOperatorConsole( - consoleStore, + new ExecutionConsoleStore(), opId, processedMessage, standardBufferSize ) - - // Verify correct processing val opInfo = updatedStore.operatorConsole(opId) opInfo.consoleMessages.size shouldBe 1 - - // Check that title was truncated - val expectedTruncatedTitle = "a" * (messageDisplayLength - 3) + "..." - opInfo.consoleMessages.head.title shouldBe expectedTruncatedTitle + opInfo.consoleMessages.head.title shouldBe ("a" * (messageDisplayLength - 3) + "...") } // ---------------------------------------------------------------- instance - /** Empty-plan client that captures the ConsoleMessage callback the service registers. */ - private final class TestAmberClient( - override val coordinatorInterface: CoordinatorServiceFs2Grpc[TwitterFuture, Unit] - ) extends AmberClient( - system, - new WorkflowContext(), - PhysicalPlan(Set.empty, Set.empty), - CoordinatorConfig(None, None, None, None), - _ => () - ) { - var consoleCallback: ConsoleMessage => Unit = _ - - override def registerCallback[T](callback: T => Unit)(implicit ct: ClassTag[T]): Disposable = { - if (ct.runtimeClass == classOf[ConsoleMessage]) { - consoleCallback = callback.asInstanceOf[ConsoleMessage => Unit] - } - Disposable.empty() - } - - def dispose(): Unit = super.shutdown() - } - private final class Fixture( - val client: TestAmberClient, + val client: RecordingExecutionClient, val coordinator: CoordinatorServiceFs2Grpc[TwitterFuture, Unit], val stateStore: ExecutionStateStore, val wsInput: WebsocketInput, @@ -321,24 +192,13 @@ class ExecutionConsoleServiceSpec } } - private def fixture(): Fixture = { + private def withFixture(body: Fixture => Unit): Unit = { val coordinator = mock[CoordinatorServiceFs2Grpc[TwitterFuture, Unit]] - val client = new TestAmberClient(coordinator) + val client = new RecordingExecutionClient(system, Some(coordinator)) val stateStore = new ExecutionStateStore val wsInput = new WebsocketInput(ListBuffer.empty[Throwable] += _) val service = new ExecutionConsoleService(client, stateStore, wsInput, new WorkflowContext()) - new Fixture(client, coordinator, stateStore, wsInput, service) - } - - private def message( - workerId: String = "Worker:WF1-udf1-main-0", - title: String = "hello", - msgType: ConsoleMessageType = ConsoleMessageType.PRINT - ): ConsoleMessage = - new ConsoleMessage(workerId, Timestamp(Instant.now), msgType, "src", title, "content") - - private def withFixture(body: Fixture => Unit): Unit = { - val f = fixture() + val f = new Fixture(client, coordinator, stateStore, wsInput, service) try body(f) finally f.close() } @@ -374,14 +234,10 @@ class ExecutionConsoleServiceSpec withFixture { f => // One batch is published per state update. Subscribing up front and reading the batch for the // SECOND message is what shows the delta: the first message must not appear in it again. - val batches = ListBuffer.empty[Iterable[TexeraWebSocketEvent]] - val sub = f.stateStore.consoleStore.getWebsocketEventObservable - .subscribe((batch: Iterable[TexeraWebSocketEvent]) => batches += batch) - - try { - f.client.consoleCallback(message(title = "first")) - f.client.consoleCallback(message(title = "second")) - } finally sub.dispose() + val batches = recordBatches(f.stateStore.consoleStore) { + f.client.fire(message(title = "first")) + f.client.fire(message(title = "second")) + } val titles = batches.last.collect { case e: ConsoleUpdateEvent => e.messages.map(_.title) }.flatten @@ -398,11 +254,7 @@ class ExecutionConsoleServiceSpec val val3 = EvaluatedValue(Some(TypedValue("z", "r3", "3", "int", expandable = false)), Seq.empty) - val batches = ListBuffer.empty[Iterable[TexeraWebSocketEvent]] - val sub = f.stateStore.consoleStore.getWebsocketEventObservable - .subscribe((batch: Iterable[TexeraWebSocketEvent]) => batches += batch) - - try { + val batches = recordBatches(f.stateStore.consoleStore) { f.stateStore.consoleStore.updateState { store => store.addOperatorConsole( "udf1" -> OperatorConsole(evaluateExprResults = @@ -416,7 +268,7 @@ class ExecutionConsoleServiceSpec "udf1" -> current.addEvaluateExprResults("z" -> EvaluatedValueList(Seq(val3))) ) } - } finally sub.dispose() + } val firstEval = batches.head.collect { case e: PythonExpressionEvaluateResponse => e.expression -> e.values @@ -434,7 +286,7 @@ class ExecutionConsoleServiceSpec // The worker id carries the physical layer and worker index; the frontend console is keyed by // the logical operator, so anything else silently strands the output. withFixture { f => - f.client.consoleCallback(message(workerId = "Worker:WF1-udf1-main-0")) + f.client.fire(message(workerId = "Worker:WF1-udf1-main-0")) f.stateStore.consoleStore.getState.operatorConsole.keys should contain("udf1") } @@ -444,7 +296,7 @@ class ExecutionConsoleServiceSpec withFixture { f => val long = "b" * (f.service.consoleMessageDisplayLength + 50) - f.client.consoleCallback(message(title = long)) + f.client.fire(message(title = long)) val stored = f.stateStore.consoleStore.getState.operatorConsole("udf1").consoleMessages stored.map(_.title.length) shouldBe Seq(f.service.consoleMessageDisplayLength) @@ -498,7 +350,7 @@ class ExecutionConsoleServiceSpec "unsubscribeAll" should "shutdown consoleWriterThread" in { withFixture { f => - f.client.consoleCallback(message(title = "test")) + f.client.fire(message(title = "test")) val threadField = classOf[ExecutionConsoleService].getDeclaredField("consoleWriterThread") threadField.setAccessible(true) diff --git a/amber/src/test/scala/org/apache/texera/web/service/ExecutionReconfigurationServiceSpec.scala b/amber/src/test/scala/org/apache/texera/web/service/ExecutionReconfigurationServiceSpec.scala index 8d2ec200bf7..fa650baf395 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/ExecutionReconfigurationServiceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/ExecutionReconfigurationServiceSpec.scala @@ -150,52 +150,37 @@ class ExecutionReconfigurationServiceSpec private val thrownMessage = "this operator blew up while being reconfigured" /** - * Returns `Failure` from `runtimeReconfiguration`. No production descriptor does: the six - * that override it all return `Success`, and the `LogicalOp` base throws instead. It is - * invisible to `OperatorMetadataGenerator`, which resolves subtypes from the `@JsonSubTypes` - * annotation rather than by scanning the classpath. + * Minimal test-only [[LogicalOp]] supporting runtime reconfiguration, with ports pre-wired so + * subclasses only define `runtimeReconfiguration`. */ - private class RejectingReconfigurationOpDesc extends LogicalOp { + private abstract class StubReconfigurationOpDesc(name: String, desc: String) extends LogicalOp { override def operatorInfo: OperatorInfo = OperatorInfo( - "Rejecting Reconfiguration", - "test-only operator whose runtime reconfiguration always fails", + name, + desc, OperatorGroupConstants.CLEANING_GROUP, inputPorts = List(InputPort()), outputPorts = List(OutputPort()), supportReconfiguration = true ) - - override def runtimeReconfiguration( - workflowId: WorkflowIdentity, - executionId: ExecutionIdentity, - oldOpDesc: LogicalOp, - newOpDesc: LogicalOp - ): Try[(PhysicalOp, Option[StateTransferFunc])] = - Failure(new RuntimeException(rejectionMessage)) } - /** Throws `failure()` from `runtimeReconfiguration`, the way an override that fails while - * rebuilding its executor does. + /** + * Test-only reconfiguration descriptor evaluating `outcome` on `runtimeReconfiguration` (to + * return `Failure` or throw). Invisible to `OperatorMetadataGenerator`, which scans `@JsonSubTypes`. */ - private class ThrowingReconfigurationOpDesc(failure: () => Throwable) extends LogicalOp { - override def operatorInfo: OperatorInfo = - OperatorInfo( - "Throwing Reconfiguration", - "test-only operator whose runtime reconfiguration always throws", - OperatorGroupConstants.CLEANING_GROUP, - inputPorts = List(InputPort()), - outputPorts = List(OutputPort()), - supportReconfiguration = true - ) - + private class StaticReconfigurationOpDesc( + outcome: => Try[(PhysicalOp, Option[StateTransferFunc])] + ) extends StubReconfigurationOpDesc( + "Stub Reconfiguration", + "test-only operator returning a configured reconfiguration outcome" + ) { override def runtimeReconfiguration( workflowId: WorkflowIdentity, executionId: ExecutionIdentity, oldOpDesc: LogicalOp, newOpDesc: LogicalOp - ): Try[(PhysicalOp, Option[StateTransferFunc])] = - throw failure() + ): Try[(PhysicalOp, Option[StateTransferFunc])] = outcome } /** @@ -204,22 +189,16 @@ class ExecutionReconfigurationServiceSpec * JavaUDFOpDesc, PythonUDFOpDescV2, RUDFOpDesc), so the service's choice of which descriptor * is the deployed one is unobservable without a descriptor that writes it down. */ - private class RecordingReconfigurationOpDesc extends LogicalOp { + private class RecordingReconfigurationOpDesc + extends StubReconfigurationOpDesc( + "Recording Reconfiguration", + "test-only operator that records the reconfiguration arguments it was given" + ) { var seenWorkflowId: WorkflowIdentity = _ var seenExecutionId: ExecutionIdentity = _ var seenOld: LogicalOp = _ var seenNew: LogicalOp = _ - override def operatorInfo: OperatorInfo = - OperatorInfo( - "Recording Reconfiguration", - "test-only operator that records the reconfiguration arguments it was given", - OperatorGroupConstants.CLEANING_GROUP, - inputPorts = List(InputPort()), - outputPorts = List(OutputPort()), - supportReconfiguration = true - ) - override def runtimeReconfiguration( workflowId: WorkflowIdentity, executionId: ExecutionIdentity, @@ -291,6 +270,23 @@ class ExecutionReconfigurationServiceSpec finally service.unsubscribeAll() } + private def withLiveOpPair(logicalOps: List[LogicalOp] = Nil)(body: LiveFixture => Unit): Unit = + withLiveService(logicalOps, physicalOps = Set(mkPhysicalOp("opA"), mkPhysicalOp("opB")))(body) + + private def enqueueReconfigs( + stateStore: ExecutionStateStore, + ops: (PhysicalOp, Option[StateTransferFunc])* + ): Unit = + stateStore.reconfigurationStore.updateState(old => + old.copy(unscheduledReconfigurations = ops.toList) + ) + + private def queuedReconfigs(stateStore: ExecutionStateStore) = + stateStore.reconfigurationStore.getState.unscheduledReconfigurations + + private def completedWorkers(stateStore: ExecutionStateStore): Set[ActorVirtualIdentity] = + stateStore.reconfigurationStore.getState.completedReconfigurations + /** * The store's diff subject is cold: handlers only run once something subscribes to the * websocket observable, and an update whose result equals the previous state is dropped @@ -340,7 +336,7 @@ class ExecutionReconfigurationServiceSpec // A user can edit several operators during one pause, so the queue has to accumulate: // overwriting would silently drop every edit but the last, and prepending would reverse the // order the engine reconfigures them in on resume. - val queued = stateStore.reconfigurationStore.getState.unscheduledReconfigurations + val queued = queuedReconfigs(stateStore) queued should have size 2 queued.map(_._1.id) shouldBe List( PhysicalOpIdentity(OperatorIdentity("limit-a"), "main"), @@ -389,87 +385,63 @@ class ExecutionReconfigurationServiceSpec val stateStore = new ExecutionStateStore val service = new WorkflowOnlyService(stateStore, mkWorkflow(List(deployedOp))) // An edit the user already made earlier in the same pause. - val alreadyQueued = mkPhysicalOp("already-queued") - stateStore.reconfigurationStore.updateState(_ => - ExecutionReconfigurationStore(unscheduledReconfigurations = List((alreadyQueued, None))) - ) + enqueueReconfigs(stateStore, (mkPhysicalOp("already-queued"), None)) val response = service.modifyOperatorLogic(ModifyLogicRequest(request)) - val queued = stateStore.reconfigurationStore.getState.unscheduledReconfigurations - (response, queued.map(_._1.id)) + (response, queuedReconfigs(stateStore).map(_._1.id)) } - it should "report the deployed operator's rejection and leave the pending queue untouched" in { - // The request carries a LimitOpDesc, which would reconfigure happily. Only the operator - // that is actually deployed refuses, so an invalid response proves the service asked the - // plan's operator rather than the one the frontend sent. - val opId = "rejecting-op" - val deployedOp = new RejectingReconfigurationOpDesc + private def assertRejectedEdit( + opId: String, + deployedOp: LogicalOp + )(checkMessage: String => Unit): Unit = { deployedOp.setOperatorId(opId) - val (response, queued) = modifyWithOnePending(deployedOp, mkLimitOp(opId, 7)) - response shouldBe a[ModifyLogicResponse] val rejected = response.asInstanceOf[ModifyLogicResponse] rejected.opId shouldBe opId rejected.isValid shouldBe false - // The frontend shows this string in an error notification, so it has to be the reason the - // operator gave -- not an empty string, which is also what the valid arm reports. - rejected.errorMessage shouldBe rejectionMessage - + checkMessage(rejected.errorMessage) // A rejected edit neither queues itself nor discards work that was already pending. queued shouldBe List(mkPhysicalOp("already-queued").id) } + it should "report the deployed operator's rejection and leave the pending queue untouched" in { + // The request carries a LimitOpDesc, which would reconfigure happily. Only the operator + // that is actually deployed refuses, so an invalid response proves the service asked the + // plan's operator rather than the one the frontend sent. + // The frontend shows this string in an error notification, so it has to be the reason the + // operator gave -- not an empty string, which is also what the valid arm reports. + assertRejectedEdit( + "rejecting-op", + new StaticReconfigurationOpDesc(Failure(new RuntimeException(rejectionMessage))) + )(_ shouldBe rejectionMessage) + } + it should "report an operator without a reconfiguration override as a rejected edit" in { // DummyOpDesc advertises supportReconfiguration but inherits the LogicalOp base, which // throws UnsupportedOperationException instead of returning Failure. - val opId = "dummy-op" - val deployedOp = new DummyOpDesc - deployedOp.setOperatorId(opId) - - val (response, queued) = modifyWithOnePending(deployedOp, mkLimitOp(opId, 7)) - - response shouldBe a[ModifyLogicResponse] - val rejected = response.asInstanceOf[ModifyLogicResponse] - rejected.opId shouldBe opId - rejected.isValid shouldBe false - rejected.errorMessage should include("DummyOpDesc does not support reconfiguration") - queued shouldBe List(mkPhysicalOp("already-queued").id) + assertRejectedEdit("dummy-op", new DummyOpDesc)( + _ should include("DummyOpDesc does not support reconfiguration") + ) } it should "report an operator whose reconfiguration override throws as a rejected edit" in { - val opId = "throwing-op" - val deployedOp = - new ThrowingReconfigurationOpDesc(() => new IllegalStateException(thrownMessage)) - deployedOp.setOperatorId(opId) - - val (response, queued) = modifyWithOnePending(deployedOp, mkLimitOp(opId, 7)) - - response shouldBe a[ModifyLogicResponse] - val rejected = response.asInstanceOf[ModifyLogicResponse] - rejected.opId shouldBe opId - rejected.isValid shouldBe false - rejected.errorMessage shouldBe thrownMessage - queued shouldBe List(mkPhysicalOp("already-queued").id) + assertRejectedEdit( + "throwing-op", + new StaticReconfigurationOpDesc(throw new IllegalStateException(thrownMessage)) + )(_ shouldBe thrownMessage) } it should "describe a rejection by its exception type when the exception has no message" in { - val opId = "throwing-op" - val deployedOp = new ThrowingReconfigurationOpDesc(() => new IllegalStateException()) - deployedOp.setOperatorId(opId) - - val (response, queued) = modifyWithOnePending(deployedOp, mkLimitOp(opId, 7)) - - response shouldBe a[ModifyLogicResponse] - val rejected = response.asInstanceOf[ModifyLogicResponse] - rejected.isValid shouldBe false - rejected.errorMessage shouldBe "java.lang.IllegalStateException" - queued shouldBe List(mkPhysicalOp("already-queued").id) + assertRejectedEdit( + "throwing-op", + new StaticReconfigurationOpDesc(throw new IllegalStateException()) + )(_ shouldBe "java.lang.IllegalStateException") } it should "let a fatal reconfiguration exception escape instead of reporting a rejection" in { val opId = "throwing-op" - val deployedOp = new ThrowingReconfigurationOpDesc(() => new InterruptedException("stop")) + val deployedOp = new StaticReconfigurationOpDesc(throw new InterruptedException("stop")) deployedOp.setOperatorId(opId) an[InterruptedException] should be thrownBy modifyWithOnePending(deployedOp, mkLimitOp(opId, 7)) @@ -521,43 +493,48 @@ class ExecutionReconfigurationServiceSpec private def refused(name: String): String = s"$name refers to loop variables, so it can't be changed while the workflow runs." + private def assertLoopBlocked( + deployedOp: LogicalOp, + edit: LogicalOp, + opId: String, + opLabel: String + ): Unit = { + val (store, service) = insideLoopBlock(deployedOp) + service.modifyOperatorLogic(ModifyLogicRequest(edit)) shouldBe + ModifyLogicResponse(opId, isValid = false, refused(opLabel)) + queuedReconfigs(store) shouldBe empty + } + "modifyOperatorLogic" should "refuse to modify an operator whose deployed setting refers to a loop variable" in { // The references the compiler left on the deployed operator, as it does inside a loop block. val deployedOp = mkLimitOp("limit-a", 0) deployedOp.stateReferences = Map("/limit" -> "n") - val (stateStore, service) = insideLoopBlock(deployedOp) - - service.modifyOperatorLogic(ModifyLogicRequest(mkLimitOp("limit-a", 7))) shouldBe - ModifyLogicResponse("limit-a", isValid = false, refused("Limit")) - stateStore.reconfigurationStore.getState.unscheduledReconfigurations shouldBe empty + assertLoopBlocked(deployedOp, mkLimitOp("limit-a", 7), "limit-a", "Limit") } it should "refuse an edit that makes an operator inside a loop block refer to a loop variable" in { - val (typedStore, typed) = insideLoopBlock(mkLimitOp("limit-a", 1)) // As the frontend sends it: "$n" in the Int property. val typedEdit = objectMapper.readValue( """{"operatorID":"limit-a","limit":"$n","operatorType":"Limit"}""", classOf[LogicalOp] ) - typed.modifyOperatorLogic(ModifyLogicRequest(typedEdit)) shouldBe - ModifyLogicResponse("limit-a", isValid = false, refused("Limit")) - typedStore.reconfigurationStore.getState.unscheduledReconfigurations shouldBe empty + assertLoopBlocked(mkLimitOp("limit-a", 1), typedEdit, "limit-a", "Limit") // A whole-string "$x" refers to a loop variable inside a loop block; the compiler never saw it. - val (literalStore, literal) = insideLoopBlock(filterOp("filter-a", "Asia")) - literal.modifyOperatorLogic(ModifyLogicRequest(filterOp("filter-a", "$x"))) shouldBe - ModifyLogicResponse("filter-a", isValid = false, refused("Filter")) - literalStore.reconfigurationStore.getState.unscheduledReconfigurations shouldBe empty + assertLoopBlocked( + filterOp("filter-a", "Asia"), + filterOp("filter-a", "$x"), + "filter-a", + "Filter" + ) } it should "still modify an operator inside a loop block that refers to no loop variable, and a '$x' literal outside every block" in { val (insideStore, inside) = insideLoopBlock(mkLimitOp("limit-a", 1)) inside.modifyOperatorLogic(ModifyLogicRequest(mkLimitOp("limit-a", 7))) shouldBe ModifyLogicResponse("limit-a", isValid = true, "") - insideStore.reconfigurationStore.getState.unscheduledReconfigurations.map { - case (op, _) => limitOf(op) - } shouldBe List(7) + queuedReconfigs(insideStore).map { case (op, _) => limitOf(op) } shouldBe List(7) // Outside every block "$x" is the literal it is on main. val outsideStore = new ExecutionStateStore @@ -565,7 +542,7 @@ class ExecutionReconfigurationServiceSpec new WorkflowOnlyService(outsideStore, mkWorkflow(List(filterOp("filter-a", "Asia")))) outside.modifyOperatorLogic(ModifyLogicRequest(filterOp("filter-a", "$x"))) shouldBe ModifyLogicResponse("filter-a", isValid = true, "") - outsideStore.reconfigurationStore.getState.unscheduledReconfigurations should have size 1 + queuedReconfigs(outsideStore) should have size 1 } "performReconfigurationOnResume" should @@ -582,6 +559,12 @@ class ExecutionReconfigurationServiceSpec state.completedReconfigurations shouldBe empty } + private def assertDispatchedOps(request: WorkflowReconfigureRequest, ops: PhysicalOp*): Unit = { + request.reconfiguration.map(_.targetOpId) should contain theSameElementsInOrderAs ops.map(_.id) + request.reconfiguration.map(_.newExecInitInfo) should contain theSameElementsInOrderAs + ops.map(_.opExecInitInfo) + } + it should "mount what an edited operator names and send its bound code" in { val stateStore = new ExecutionStateStore() val service = new RecordingService(stateStore) @@ -590,9 +573,7 @@ class ExecutionReconfigurationServiceSpec override def opExecInitInfo: OpExecInitInfo = bound override def mountLocators: Set[String] = Set("dataset-1:abc123") })) - stateStore.reconfigurationStore.updateState(_ => - ExecutionReconfigurationStore(unscheduledReconfigurations = List((op, None))) - ) + enqueueReconfigs(stateStore, (op, None)) service.performReconfigurationOnResume() @@ -621,14 +602,7 @@ class ExecutionReconfigurationServiceSpec service.captured should have size 1 val request = service.captured.head request.reconfigurationId should not be empty - request.reconfiguration.map(_.targetOpId) should contain theSameElementsInOrderAs Seq( - op1.id, - op2.id - ) - request.reconfiguration.map(_.newExecInitInfo) should contain theSameElementsInOrderAs Seq( - op1.opExecInitInfo, - op2.opExecInitInfo - ) + assertDispatchedOps(request, op1, op2) val state = stateStore.reconfigurationStore.getState state.unscheduledReconfigurations shouldBe empty @@ -645,9 +619,7 @@ class ExecutionReconfigurationServiceSpec val service = new RecordingService(stateStore) def queueAndDispatch(opName: String): String = { - stateStore.reconfigurationStore.updateState(old => - old.copy(unscheduledReconfigurations = List((mkPhysicalOp(opName), None))) - ) + enqueueReconfigs(stateStore, (mkPhysicalOp(opName), None)) service.performReconfigurationOnResume() service.captured.last.reconfigurationId } @@ -665,9 +637,7 @@ class ExecutionReconfigurationServiceSpec withLiveService() { f => val op1 = mkPhysicalOp("dispatched-op-1") val op2 = mkPhysicalOp("dispatched-op-2") - f.stateStore.reconfigurationStore.updateState(_ => - ExecutionReconfigurationStore(unscheduledReconfigurations = List((op1, None), (op2, None))) - ) + enqueueReconfigs(f.stateStore, (op1, None), (op2, None)) f.service.performReconfigurationOnResume() @@ -677,14 +647,7 @@ class ExecutionReconfigurationServiceSpec // the engine through any other one would not start a Fries round. calledMethod shouldBe "reconfigureWorkflow" val captured = payload.asInstanceOf[WorkflowReconfigureRequest] - captured.reconfiguration.map(_.targetOpId) should contain theSameElementsInOrderAs Seq( - op1.id, - op2.id - ) - captured.reconfiguration.map(_.newExecInitInfo) should contain theSameElementsInOrderAs Seq( - op1.opExecInitInfo, - op2.opExecInitInfo - ) + assertDispatchedOps(captured, op1, op2) // The id the engine is told to reconfigure under is the one the store now tracks; a // mismatch would leave the completion diff handler waiting on a round that never reports. f.stateStore.reconfigurationStore.getState.currentReconfigId shouldBe @@ -704,17 +667,14 @@ class ExecutionReconfigurationServiceSpec // duplicate completion is idempotent (Set semantics). service.onWorkerReconfigured(w1) - stateStore.reconfigurationStore.getState.completedReconfigurations should contain theSameElementsAs Set( - w1, - w2 - ) + completedWorkers(stateStore) should contain theSameElementsAs Set(w1, w2) } "the worker completion callback" should "announce the logical operator of every worker that reports in" in { // Driven only through the engine event, so it also pins that the constructor registered the // callback for UpdateExecutorCompleted and passed the reported worker on. - withLiveService(physicalOps = Set(mkPhysicalOp("opA"), mkPhysicalOp("opB"))) { f => + withLiveOpPair() { f => val batches = recordBatches(f.stateStore.reconfigurationStore) { f.client.fire(UpdateExecutorCompleted(mkWorker("opA", 0))) f.client.fire(UpdateExecutorCompleted(mkWorker("opB", 0))) @@ -737,7 +697,7 @@ class ExecutionReconfigurationServiceSpec // the two is wrong; whichever is corrected, this assertion has to be revisited. announcedOps(batches(2)) shouldBe List("opA") - f.stateStore.reconfigurationStore.getState.completedReconfigurations shouldBe + completedWorkers(f.stateStore) shouldBe Set(mkWorker("opA", 0), mkWorker("opB", 0), mkWorker("opA", 1)) } } @@ -748,7 +708,7 @@ class ExecutionReconfigurationServiceSpec // running execution is handed one diff carrying every worker that has finished so far. It is // the only path that produces a multi-worker diff, and therefore the only thing that // distinguishes announcing the whole diff from announcing one of its members. - withLiveService(physicalOps = Set(mkPhysicalOp("opA"), mkPhysicalOp("opB"))) { f => + withLiveOpPair() { f => f.client.fire(UpdateExecutorCompleted(mkWorker("opA", 0))) f.client.fire(UpdateExecutorCompleted(mkWorker("opB", 0))) @@ -765,12 +725,8 @@ class ExecutionReconfigurationServiceSpec it should "announce nothing to a client that attaches after a new round has started" in { // Same catch-up pair, but this time it straddles a round boundary (reconfigId None -> Some). // It is not a completion report, so the joining client must not be told that opA finished. - withLiveService(physicalOps = Set(mkPhysicalOp("opA"), mkPhysicalOp("opB"))) { f => - f.stateStore.reconfigurationStore.updateState(_ => - ExecutionReconfigurationStore(unscheduledReconfigurations = - List((mkPhysicalOp("opA"), None)) - ) - ) + withLiveOpPair() { f => + enqueueReconfigs(f.stateStore, (mkPhysicalOp("opA"), None)) f.service.performReconfigurationOnResume() f.client.fire(UpdateExecutorCompleted(mkWorker("opA", 0))) @@ -809,7 +765,7 @@ class ExecutionReconfigurationServiceSpec it should "stop announcing completions once the service is unsubscribed" in { val workerA = mkWorker("opA", 0) val workerB = mkWorker("opB", 0) - withLiveService(physicalOps = Set(mkPhysicalOp("opA"), mkPhysicalOp("opB"))) { f => + withLiveOpPair() { f => val batches = recordBatches(f.stateStore.reconfigurationStore) { f.client.fire(UpdateExecutorCompleted(workerA)) f.service.unsubscribeAll() @@ -827,19 +783,18 @@ class ExecutionReconfigurationServiceSpec // assertion below confirms -- but the handler is gone, which is only true if the // constructor handed its disposable to the subscription manager. batches.last shouldBe empty - f.stateStore.reconfigurationStore.getState.completedReconfigurations shouldBe - Set(workerA, workerB) + completedWorkers(f.stateStore) shouldBe Set(workerA, workerB) } } it should "release the engine subscription once the service is unsubscribed" in { val workerA = mkWorker("opA", 0) val workerB = mkWorker("opB", 0) - withLiveService(physicalOps = Set(mkPhysicalOp("opA"), mkPhysicalOp("opB"))) { f => + withLiveOpPair() { f => // A live registration first, so a disposed one below cannot be confused with one that was // never made. f.client.fire(UpdateExecutorCompleted(workerA)) - f.stateStore.reconfigurationStore.getState.completedReconfigurations shouldBe Set(workerA) + completedWorkers(f.stateStore) shouldBe Set(workerA) f.service.unsubscribeAll() @@ -854,7 +809,7 @@ class ExecutionReconfigurationServiceSpec // ...and the detached callback is inert: a late engine event does not write into the store // of a finished execution. f.client.fireIfRegistered(UpdateExecutorCompleted(workerB)) - f.stateStore.reconfigurationStore.getState.completedReconfigurations shouldBe Set(workerA) + completedWorkers(f.stateStore) shouldBe Set(workerA) } } } diff --git a/amber/src/test/scala/org/apache/texera/web/service/ExecutionStatsServiceSpec.scala b/amber/src/test/scala/org/apache/texera/web/service/ExecutionStatsServiceSpec.scala index a02208b0e78..39502a278ad 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/ExecutionStatsServiceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/ExecutionStatsServiceSpec.scala @@ -19,7 +19,6 @@ package org.apache.texera.web.service -import io.reactivex.rxjava3.disposables.Disposable import org.apache.pekko.actor.ActorSystem import org.apache.pekko.testkit.TestKit import org.apache.texera.amber.core.storage.model.VirtualDocument @@ -30,10 +29,9 @@ import org.apache.texera.amber.core.virtualidentity.{ ExecutionIdentity, WorkflowIdentity } -import org.apache.texera.amber.core.workflow.{PhysicalPlan, PortIdentity, WorkflowContext} +import org.apache.texera.amber.core.workflow.{PortIdentity, WorkflowContext} import org.apache.texera.amber.core.workflowruntimestate.FatalErrorType.EXECUTION_FAILURE import org.apache.texera.amber.engine.architecture.coordinator.{ - CoordinatorConfig, ExecutionStateUpdate, ExecutionStatsUpdate, FatalError, @@ -47,9 +45,7 @@ import org.apache.texera.amber.engine.architecture.worker.statistics.{ PortTupleMetricsMapping, TupleMetrics } -import org.apache.texera.amber.engine.common.client.AmberClient import org.apache.texera.amber.engine.common.executionruntimestate.{ - ExecutionStatsStore, OperatorMetrics, OperatorStatistics } @@ -74,7 +70,8 @@ import org.apache.texera.web.model.websocket.event.{ TexeraWebSocketEvent, WorkerAssignmentUpdateEvent } -import org.apache.texera.web.storage.{ExecutionStateStore, StateStore} +import org.apache.texera.web.service.RecordingExecutionClient.recordBatches +import org.apache.texera.web.storage.ExecutionStateStore import org.scalatest.BeforeAndAfterAll import org.scalatest.flatspec.AnyFlatSpecLike import org.scalatest.matchers.should.Matchers @@ -82,9 +79,6 @@ import org.scalatest.matchers.should.Matchers import java.net.URI import java.sql.Timestamp import java.util.concurrent.ExecutorService -import scala.collection.mutable -import scala.collection.mutable.ListBuffer -import scala.reflect.ClassTag /** * The service class was entirely uncovered. It owns two independent outputs: the websocket @@ -165,37 +159,8 @@ class ExecutionStatsServiceSpec } finally super.afterAll() } - /** Empty-plan client that captures every callback the service registers, keyed by event type. */ - private final class TestAmberClient - extends AmberClient( - system, - new WorkflowContext(), - PhysicalPlan(Set.empty, Set.empty), - CoordinatorConfig(None, None, None, None), - _ => () - ) { - var shutdownCount = 0 - private val callbacks = mutable.Map.empty[Class[_], Any => Unit] - - override def shutdown(): Unit = { - shutdownCount += 1 - super.shutdown() - } - - override def registerCallback[T](callback: T => Unit)(implicit ct: ClassTag[T]): Disposable = { - callbacks(ct.runtimeClass) = callback.asInstanceOf[Any => Unit] - Disposable.empty() - } - - /** Delivers an engine event the way the client's observable would. */ - def fire[T <: AnyRef](event: T)(implicit ct: ClassTag[T]): Unit = - callbacks(ct.runtimeClass)(event) - - def dispose(): Unit = super.shutdown() - } - private final class Fixture( - val client: TestAmberClient, + val client: RecordingExecutionClient, val stateStore: ExecutionStateStore, val service: ExecutionStatsService ) { @@ -206,7 +171,7 @@ class ExecutionStatsServiceSpec } private def withService(body: Fixture => Unit): Unit = { - val client = new TestAmberClient + val client = new RecordingExecutionClient(system) val stateStore = new ExecutionStateStore val service = new ExecutionStatsService( client, @@ -221,22 +186,6 @@ class ExecutionStatsServiceSpec finally fixture.close() } - /** - * One batch of websocket events is published per state update, so a test that cares about - * what a *second* update publishes has to subscribe before the first one. Subscribing later - * would also replay the diff between the default state and the current one as a batch. - */ - private def recordBatches( - store: StateStore[ExecutionStatsStore] - )(body: => Unit): Seq[Iterable[TexeraWebSocketEvent]] = { - val batches = ListBuffer.empty[Iterable[TexeraWebSocketEvent]] - val subscription = store.getWebsocketEventObservable - .subscribe((batch: Iterable[TexeraWebSocketEvent]) => batches += batch) - try body - finally subscription.dispose() - batches.toSeq - } - private def metrics( state: WorkflowAggregatedState, inputs: Seq[(Int, Long, Long)] = Seq.empty, diff --git a/amber/src/test/scala/org/apache/texera/web/service/ExecutionsMetadataPersistServiceSpec.scala b/amber/src/test/scala/org/apache/texera/web/service/ExecutionsMetadataPersistServiceSpec.scala index 620d8312b3c..1bb151507c6 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/ExecutionsMetadataPersistServiceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/ExecutionsMetadataPersistServiceSpec.scala @@ -24,28 +24,14 @@ import org.apache.texera.amber.engine.architecture.rpc.controlreturns.WorkflowAg import org.apache.texera.amber.engine.common.Utils.maptoStatusCode import org.apache.texera.amber.engine.common.executionruntimestate.ExecutionMetadataStore import org.apache.texera.dao.MockTexeraDB -import org.apache.texera.dao.jooq.generated.Tables._ +import org.apache.texera.dao.jooq.generated.Tables.USER_WAREHOUSE import org.apache.texera.dao.jooq.generated.enums.UserWarehouseFlavorEnum -import org.apache.texera.dao.jooq.generated.tables.daos.{ - UserDao, - WorkflowComputingUnitDao, - WorkflowDao, - WorkflowExecutionsDao, - WorkflowVersionDao -} -import org.apache.texera.dao.jooq.generated.tables.pojos.{ - User, - Workflow, - WorkflowComputingUnit, - WorkflowExecutions, - WorkflowVersion -} +import org.apache.texera.web.ExecutionDatabaseTestSupport import org.apache.texera.web.storage.ExecutionStateStore import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.matchers.should.Matchers import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} -import java.sql.Timestamp import java.util.UUID class ExecutionsMetadataPersistServiceSpec @@ -53,18 +39,14 @@ class ExecutionsMetadataPersistServiceSpec with Matchers with BeforeAndAfterAll with BeforeAndAfterEach - with MockTexeraDB { + with MockTexeraDB + with ExecutionDatabaseTestSupport { // Randomise the seeded wid/uid so a parallel run of unrelated specs that // happen to seed the same ids wouldn't collide on the embedded postgres. private val testWid = 7000 + scala.util.Random.nextInt(1000) private val testUid = 7000 + scala.util.Random.nextInt(1000) - private var workflowDao: WorkflowDao = _ - private var workflowVersionDao: WorkflowVersionDao = _ - private var workflowExecutionsDao: WorkflowExecutionsDao = _ - private var userDao: UserDao = _ - private var computingUnitDao: WorkflowComputingUnitDao = _ private var seededVid: Integer = _ private var seededCuid: Integer = _ @@ -77,89 +59,35 @@ class ExecutionsMetadataPersistServiceSpec } override protected def beforeEach(): Unit = { - val cfg = getDSLContext.configuration() - workflowDao = new WorkflowDao(cfg) - workflowVersionDao = new WorkflowVersionDao(cfg) - workflowExecutionsDao = new WorkflowExecutionsDao(cfg) - userDao = new UserDao(cfg) - computingUnitDao = new WorkflowComputingUnitDao(cfg) cleanup() - - val user = new User - user.setUid(testUid) - user.setName("metadata_persist_spec_user") - user.setEmail(s"user_${UUID.randomUUID()}@example.com") - userDao.insert(user) - - val workflow = new Workflow - workflow.setWid(testWid) - workflow.setName(s"wf_${UUID.randomUUID().toString.substring(0, 8)}") - workflow.setContent("{}") - workflow.setDescription("") - workflow.setCreationTime(new Timestamp(System.currentTimeMillis())) - workflow.setLastModifiedTime(new Timestamp(System.currentTimeMillis())) - workflowDao.insert(workflow) + insertUser(testUid, "metadata_persist_spec_user", s"user_${UUID.randomUUID()}@example.com") + insertWorkflow(testWid, s"wf_${UUID.randomUUID().toString.substring(0, 8)}") // Seed one version explicitly so insertNewExecution's getLatestVersion // takes the happy "max(existing)" branch instead of falling into the // back-compat "insert one for you" branch (which would drag in // WorkflowVersionResource.insertNewVersion's diff/aggregate logic). - val version = new WorkflowVersion - version.setWid(testWid) - version.setContent("{}") - version.setCreationTime(new Timestamp(System.currentTimeMillis())) - workflowVersionDao.insert(version) - seededVid = version.getVid + seededVid = insertWorkflowVersion(testWid).getVid // workflow_executions.cuid has a FK to workflow_computing_unit; seed one // so insertNewExecution's setCuid(...) call doesn't trip the constraint. - val cu = new WorkflowComputingUnit - cu.setUid(testUid) - cu.setName("test-cu") - computingUnitDao.insert(cu) - seededCuid = cu.getCuid + seededCuid = + insertComputingUnit(testUid, "test-cu", unitType = None, uri = None, resource = None).getCuid } override protected def afterEach(): Unit = cleanup() - private def cleanup(): Unit = { - getDSLContext - .deleteFrom(WORKFLOW_EXECUTIONS) - .where( - WORKFLOW_EXECUTIONS.VID.in( - getDSLContext - .select(WORKFLOW_VERSION.VID) - .from(WORKFLOW_VERSION) - .where(WORKFLOW_VERSION.WID.eq(testWid)) - ) - ) - .execute() - getDSLContext - .deleteFrom(WORKFLOW_VERSION) - .where(WORKFLOW_VERSION.WID.eq(testWid)) - .execute() - getDSLContext.deleteFrom(WORKFLOW).where(WORKFLOW.WID.eq(testWid)).execute() - getDSLContext - .deleteFrom(WORKFLOW_COMPUTING_UNIT) - .where(WORKFLOW_COMPUTING_UNIT.UID.eq(testUid)) - .execute() - getDSLContext.deleteFrom(USER).where(USER.UID.eq(testUid)).execute() - } + private def cleanup(): Unit = + cleanupWorkflowRecords(Seq(testWid), uids = Seq(testUid), includeComputingUnits = true) // Helper: insert an execution row tied to the seeded version and return its eid. - private def seedExecution(status: Byte = 0): Integer = { - val exec = new WorkflowExecutions - exec.setVid(seededVid) - exec.setUid(testUid) - exec.setStatus(status) - exec.setResult("") - exec.setStartingTime(new Timestamp(System.currentTimeMillis())) - exec.setBookmarked(false) - exec.setName("seeded execution") - exec.setEnvironmentVersion("test-env") - workflowExecutionsDao.insert(exec) - exec.getEid - } + private def seedExecution(status: Byte = 0): Integer = + insertWorkflowExecution( + vid = seededVid, + uid = testUid, + status = status, + name = "seeded execution" + ).getEid // -- insertNewExecution ----------------------------------------------------- diff --git a/amber/src/test/scala/org/apache/texera/web/service/RecordingExecutionClient.scala b/amber/src/test/scala/org/apache/texera/web/service/RecordingExecutionClient.scala index 8223e349d02..aaa412b5464 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/RecordingExecutionClient.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/RecordingExecutionClient.scala @@ -27,11 +27,28 @@ import org.apache.texera.amber.engine.architecture.coordinator.CoordinatorConfig import org.apache.texera.amber.engine.architecture.rpc.controlreturns.EmptyReturn import org.apache.texera.amber.engine.architecture.rpc.coordinatorservice.CoordinatorServiceFs2Grpc import org.apache.texera.amber.engine.common.client.AmberClient +import org.apache.texera.web.model.websocket.event.TexeraWebSocketEvent +import org.apache.texera.web.storage.StateStore import java.lang.reflect.{InvocationHandler, Method, Proxy} import scala.collection.mutable import scala.reflect.ClassTag +object RecordingExecutionClient { + + /** Collects every websocket event batch emitted by `store` while `body` runs. */ + def recordBatches[S]( + store: StateStore[S] + )(body: => Unit): Seq[Iterable[TexeraWebSocketEvent]] = { + val batches = mutable.ListBuffer.empty[Iterable[TexeraWebSocketEvent]] + val subscription = store.getWebsocketEventObservable + .subscribe((batch: Iterable[TexeraWebSocketEvent]) => batches += batch) + try body + finally subscription.dispose() + batches.toSeq + } +} + /** * Empty-plan `AmberClient` for execution-service unit specs: captures registered callbacks, * honors subscription disposal, and optionally records coordinator RPC invocations. diff --git a/amber/src/test/scala/org/apache/texera/web/service/ResultExportServiceSpec.scala b/amber/src/test/scala/org/apache/texera/web/service/ResultExportServiceSpec.scala index de76bad7f1f..311b00b8a60 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/ResultExportServiceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/ResultExportServiceSpec.scala @@ -41,22 +41,14 @@ import org.apache.texera.amber.core.workflow.{GlobalPortIdentity, PortIdentity} import org.apache.texera.amber.util.ArrowUtils import org.apache.texera.auth.JwtAuth import org.apache.texera.dao.MockTexeraDB -import org.apache.texera.dao.jooq.generated.Tables.{OPERATOR_PORT_EXECUTIONS, WORKFLOW_EXECUTIONS} -import org.apache.texera.dao.jooq.generated.enums.WorkflowComputingUnitTypeEnum -import org.apache.texera.dao.jooq.generated.tables.daos.{ - UserDao, - WorkflowComputingUnitDao, - WorkflowDao, - WorkflowExecutionsDao, - WorkflowVersionDao -} +import org.apache.texera.dao.jooq.generated.Tables.OPERATOR_PORT_EXECUTIONS import org.apache.texera.dao.jooq.generated.tables.pojos.{ User, - Workflow, WorkflowComputingUnit, WorkflowExecutions, WorkflowVersion } +import org.apache.texera.web.ExecutionDatabaseTestSupport import org.apache.texera.web.model.http.request.result.{OperatorExportInfo, ResultExportRequest} import org.apache.texera.web.model.http.response.result.ResultExportResponse import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, PrivateMethodTester} @@ -72,7 +64,6 @@ import java.io.{ } import java.net.{InetSocketAddress, URI, URL} import java.nio.charset.StandardCharsets -import java.sql.Timestamp import java.util.UUID import java.util.zip.{ZipException, ZipInputStream} import javax.ws.rs.WebApplicationException @@ -113,7 +104,8 @@ class ResultExportServiceSpec with PrivateMethodTester with BeforeAndAfterAll with BeforeAndAfterEach - with MockTexeraDB { + with MockTexeraDB + with ExecutionDatabaseTestSupport { private val service = new ResultExportService(WorkflowIdentity(1L), computingUnitId = 0) @@ -402,60 +394,20 @@ class ResultExportServiceSpec private var testUser: User = _ private var testVersion: WorkflowVersion = _ private var testComputingUnit: WorkflowComputingUnit = _ - private var workflowExecutionsDao: WorkflowExecutionsDao = _ override protected def beforeAll(): Unit = { initializeDBAndReplaceDSLContext() - testUser = new User - testUser.setUid(testUserId) - testUser.setName("export_user") - testUser.setEmail("export@example.com") - new UserDao(getDSLContext.configuration()).insert(testUser) - - val workflow = new Workflow - workflow.setWid(testWorkflowWid) - workflow.setName("wf-" + UUID.randomUUID().toString.substring(0, 8)) - workflow.setContent("{}") - workflow.setDescription("") - workflow.setCreationTime(new Timestamp(System.currentTimeMillis())) - workflow.setLastModifiedTime(new Timestamp(System.currentTimeMillis())) - new WorkflowDao(getDSLContext.configuration()).insert(workflow) - - testVersion = new WorkflowVersion - testVersion.setWid(testWorkflowWid) - testVersion.setContent("{}") - testVersion.setCreationTime(new Timestamp(System.currentTimeMillis())) - new WorkflowVersionDao(getDSLContext.configuration()).insert(testVersion) - - testComputingUnit = new WorkflowComputingUnit - testComputingUnit.setUid(testUserId) - testComputingUnit.setName("export-unit") - testComputingUnit.setCreationTime(new Timestamp(System.currentTimeMillis())) - testComputingUnit.setType(WorkflowComputingUnitTypeEnum.local) - testComputingUnit.setUri("local://test") - testComputingUnit.setResource("{}") - new WorkflowComputingUnitDao(getDSLContext.configuration()).insert(testComputingUnit) - - workflowExecutionsDao = new WorkflowExecutionsDao(getDSLContext.configuration()) + testUser = insertUser(testUserId, "export_user", "export@example.com") + insertWorkflow(testWorkflowWid, "wf-" + UUID.randomUUID().toString.substring(0, 8)) + testVersion = insertWorkflowVersion(testWorkflowWid) + testComputingUnit = insertComputingUnit(testUserId, "export-unit") } // Executions are per-test state: several cases below distinguish "no // execution at all" from "an execution with no stored result URI". - override protected def afterEach(): Unit = { - val eids = getDSLContext - .select(WORKFLOW_EXECUTIONS.EID) - .from(WORKFLOW_EXECUTIONS) - .where(WORKFLOW_EXECUTIONS.VID.eq(testVersion.getVid)) - getDSLContext - .deleteFrom(OPERATOR_PORT_EXECUTIONS) - .where(OPERATOR_PORT_EXECUTIONS.WORKFLOW_EXECUTION_ID.in(eids)) - .execute() - getDSLContext - .deleteFrom(WORKFLOW_EXECUTIONS) - .where(WORKFLOW_EXECUTIONS.VID.eq(testVersion.getVid)) - .execute() - } + override protected def afterEach(): Unit = + cleanupExecutionsForVersion(testVersion.getVid, includePortExecutions = true) override protected def afterAll(): Unit = closeConnectionPool() @@ -481,21 +433,15 @@ class ResultExportServiceSpec computingUnitId = testComputingUnit.getCuid ) - private def insertExecution(): WorkflowExecutions = { - val execution = new WorkflowExecutions - execution.setVid(testVersion.getVid) - execution.setUid(testUser.getUid) - execution.setStatus(0.toByte) - execution.setResult("") - execution.setLogLocation("") - execution.setStartingTime(new Timestamp(System.currentTimeMillis())) - execution.setBookmarked(false) - execution.setName("export-execution") - execution.setEnvironmentVersion("test-env-1.0") - execution.setCuid(testComputingUnit.getCuid) - workflowExecutionsDao.insert(execution) - execution - } + private def insertExecution(): WorkflowExecutions = + insertWorkflowExecution( + vid = testVersion.getVid, + uid = testUser.getUid, + cuidOpt = Some(testComputingUnit.getCuid), + logLocation = Some(""), + name = "export-execution", + envVersion = "test-env-1.0" + ) private def insertResultUri(eid: Integer, resultUri: String): Unit = getDSLContext diff --git a/amber/src/test/scala/org/apache/texera/web/service/WorkflowExecutionServiceSpec.scala b/amber/src/test/scala/org/apache/texera/web/service/WorkflowExecutionServiceSpec.scala index 75fb5cc33e2..d84a1cd88c2 100644 --- a/amber/src/test/scala/org/apache/texera/web/service/WorkflowExecutionServiceSpec.scala +++ b/amber/src/test/scala/org/apache/texera/web/service/WorkflowExecutionServiceSpec.scala @@ -34,22 +34,7 @@ import org.apache.texera.amber.engine.architecture.rpc.controlreturns.WorkflowAg } import org.apache.texera.amber.operator.source.scan.csv.CSVScanSourceOpDesc import org.apache.texera.dao.MockTexeraDB -import org.apache.texera.dao.jooq.generated.enums.WorkflowComputingUnitTypeEnum -import org.apache.texera.dao.jooq.generated.tables.daos.{ - UserDao, - WorkflowComputingUnitDao, - WorkflowDao, - WorkflowExecutionsDao, - WorkflowVersionDao -} -import org.apache.texera.dao.jooq.generated.tables.pojos.{ - User, - Workflow, - WorkflowComputingUnit, - WorkflowExecutions, - WorkflowVersion -} -import org.apache.texera.web.WebsocketInput +import org.apache.texera.web.{ExecutionDatabaseTestSupport, WebsocketInput} import org.apache.texera.web.model.websocket.event.{ TexeraWebSocketEvent, WorkflowErrorEvent, @@ -64,7 +49,6 @@ import org.scalatest.flatspec.AnyFlatSpecLike import org.scalatest.matchers.should.Matchers import java.net.URI -import java.sql.{Timestamp => SqlTimestamp} import java.time.Instant import scala.collection.mutable import scala.collection.mutable.ListBuffer @@ -134,7 +118,8 @@ class WorkflowExecutionServiceSpec with AnyFlatSpecLike with Matchers with BeforeAndAfterAll - with MockTexeraDB { + with MockTexeraDB + with ExecutionDatabaseTestSupport { // Distinct from every other suite's ids: the statistics URI is derived from wid/eid and // `createDocument` truncates whatever table already sits at it (ExecutionStatsServiceSpec @@ -158,59 +143,25 @@ class WorkflowExecutionServiceSpec super.beforeAll() initializeDBAndReplaceDSLContext() - // Set explicitly rather than left to the SERIAL default: the computing-unit and execution - // rows below bind `testUid` into their `uid` foreign keys, so a generated value would leave - // them pointing at a user that does not exist. - val user = new User - user.setUid(testUid) - user.setName("workflow-execution-test-user") - user.setEmail(s"u$testUid@example.com") - new UserDao(getDSLContext.configuration()).insert(user) - - val workflow = new Workflow - workflow.setWid(testWid) - workflow.setName(s"workflow-execution-test-$testWid") - workflow.setContent("{}") - workflow.setDescription("") - workflow.setCreationTime(new SqlTimestamp(System.currentTimeMillis())) - workflow.setLastModifiedTime(new SqlTimestamp(System.currentTimeMillis())) - new WorkflowDao(getDSLContext.configuration()).insert(workflow) - - val version = new WorkflowVersion - version.setWid(testWid) - version.setContent("{}") - version.setCreationTime(new SqlTimestamp(System.currentTimeMillis())) - new WorkflowVersionDao(getDSLContext.configuration()).insert(version) - - val computingUnitDao = new WorkflowComputingUnitDao(getDSLContext.configuration()) + insertUser(testUid, "workflow-execution-test-user", s"u$testUid@example.com") + insertWorkflow(testWid, s"workflow-execution-test-$testWid") + val version = insertWorkflowVersion(testWid) + List(testCuid -> "workflow-execution-test-unit", otherCuid -> "workflow-execution-idle-unit") .foreach { - case (cuid, name) => - val unit = new WorkflowComputingUnit - unit.setCuid(cuid) - unit.setUid(testUid) - unit.setName(name) - unit.setCreationTime(new SqlTimestamp(System.currentTimeMillis())) - unit.setType(WorkflowComputingUnitTypeEnum.local) - unit.setUri("local://test") - unit.setResource("{}") - computingUnitDao.insert(unit) + case (cuid, name) => insertComputingUnit(testUid, name, cuidOpt = Some(cuid)) } // `ExecutionStatsService`'s constructor stamps the runtime-statistics URI onto this row, // reaching it from the workflow through workflow_version. `cuid` is set because // `getLatestExecutionID` matches on it and a NULL cuid matches no value at all. - val execution = new WorkflowExecutions - execution.setEid(testEid) - execution.setVid(version.getVid) - execution.setUid(testUid) - execution.setCuid(testCuid) - execution.setStatus(0.toByte) - execution.setStartingTime(new SqlTimestamp(System.currentTimeMillis())) - execution.setBookmarked(false) - execution.setName("workflow-execution-test-execution") - execution.setEnvironmentVersion("test-env") - new WorkflowExecutionsDao(getDSLContext.configuration()).insert(execution) + insertWorkflowExecution( + vid = version.getVid, + uid = testUid, + eidOpt = Some(testEid), + cuidOpt = Some(testCuid), + name = "workflow-execution-test-execution" + ) } override protected def afterAll(): Unit = {