Skip to content

Commit bb46d20

Browse files
committed
refactor(amber): remove the materialization reader thread's finished flag
After the fix, nothing outside tests reads InputPortMaterializationReaderThread .finished, and it is exactly the signal that must not stand for port completion. Remove it. The tests that read it now check what it stood for: the END_CHANNEL the reader enqueued, or the thread's own state.
1 parent dea6c1a commit bb46d20

5 files changed

Lines changed: 42 additions & 58 deletions

File tree

‎amber/src/main/scala/org/apache/texera/amber/engine/architecture/messaginglayer/InputManager.scala‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,8 @@ class InputManager(
117117

118118
/**
119119
* Whether END_CHANNEL has been processed on every channel of the port (`EndChannelHandler` then
120-
* marks it completed), for a port read from materialization as well. Its reader thread flags
121-
* itself finished only after enqueuing END, so the DP thread can process END before the flag is
122-
* set, and the flag can be set while END is still queued.
120+
* marks it completed), for a port read from materialization as well: what its reader thread has
121+
* done says nothing about whether the DP thread has processed the END it enqueued.
123122
*/
124123
def isPortCompleted(portId: PortIdentity): Boolean = this.getPort(portId).completed
125124

‎amber/src/main/scala/org/apache/texera/amber/engine/architecture/worker/managers/InputPortMaterializationReaderThread.scala‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ import org.apache.texera.amber.util.VirtualIdentityUtils.getFromActorIdForInputP
5555

5656
import java.net.URI
5757
import java.util.concurrent.LinkedBlockingQueue
58-
import java.util.concurrent.atomic.{AtomicBoolean, AtomicLong}
58+
import java.util.concurrent.atomic.AtomicLong
5959
import scala.collection.mutable.ArrayBuffer
6060

6161
class InputPortMaterializationReaderThread(
@@ -75,12 +75,6 @@ class InputPortMaterializationReaderThread(
7575
}
7676
private val partitioner = toPartitioner(partitioning, workerActorId)
7777
private val batchSize = ApplicationConfig.defaultDataTransferBatchSize
78-
private val isFinished = new AtomicBoolean(false)
79-
80-
/**
81-
* Whether the reader thread has completed.
82-
*/
83-
def finished: Boolean = isFinished.get()
8478

8579
/**
8680
* Read from the materialization storage, and mimics the behavior of an upstream worker's output manager.
@@ -141,7 +135,6 @@ class InputPortMaterializationReaderThread(
141135
// Flush any remaining tuples in the buffer.
142136
if (buffer.nonEmpty) flush()
143137
emitECM(METHOD_END_CHANNEL, PORT_ALIGNMENT)
144-
isFinished.set(true)
145138
} catch {
146139
case e: Exception =>
147140
throw new RuntimeException(s"Error reading input port materializations: ${e.getMessage}", e)

‎amber/src/test/scala/org/apache/texera/amber/engine/architecture/messaginglayer/InputManagerSpec.scala‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,41 +219,42 @@ class InputManagerSpec extends AnyFlatSpec {
219219
// isPortCompleted — materialized path
220220
// ---------------------------------------------------------------------------
221221
//
222-
// A port read from materialization is fed by a reader thread that enqueues END_CHANNEL and only
223-
// then flags itself `finished`. The DP thread may process that END before the flag flips, so
224-
// the port is completed when END has been processed (`WorkerPort.completed`, set by
225-
// EndChannelHandler), whatever the reader thread reports.
222+
// A port read from materialization is fed by a reader thread that enqueues END_CHANNEL. The DP
223+
// thread may process that END before the thread returns, and the thread may return while END is
224+
// still queued, so the port is completed when END has been processed (`WorkerPort.completed`,
225+
// set by EndChannelHandler), whatever state the reader thread is in.
226226

227227
private val materializedUri = new URI("file:///nowhere")
228228

229229
private def broadcastToSelf: Partitioning =
230230
BroadcastPartitioning(batchSize = 1, channels = Seq(channelId("upstream", "worker-1")))
231231

232232
"InputManager.isPortCompleted (materialized)" should
233-
"be true once END has been processed, before the reader thread flags itself finished" in {
233+
"be true once END has been processed, before the reader thread has returned" in {
234234
val mgr = freshManager
235235
val portId = PortIdentity(0)
236236
mgr.addPort(portId, schema, List(materializedUri), List(broadcastToSelf))
237237
val reader = mgr.getInputPortReaderThreads(portId).head
238238
assert(!mgr.isPortCompleted(portId))
239239

240-
// What EndChannelHandler does on END; the reader thread has not set its flag yet.
240+
// What EndChannelHandler does on END, while the reader thread has not even started.
241241
mgr.getPort(portId).completed = true
242242

243-
assert(!reader.finished)
243+
assert(reader.getState == Thread.State.NEW)
244244
assert(mgr.isPortCompleted(portId))
245245
}
246246

247-
it should "be false while END is still queued, even after the reader thread flags itself finished" in {
247+
it should "be false while END is still queued, even after the reader thread has returned" in {
248248
val mgr = freshManager
249249
val portId = PortIdentity(0)
250250
mgr.addPort(portId, schema, List.empty, List.empty)
251-
val finishedReader = new NoOpReaderThread {
252-
override def finished: Boolean = true
253-
}
254-
installReaderThreads(mgr, portId, List(finishedReader))
251+
val returnedReader = new NoOpReaderThread
252+
returnedReader.start()
253+
returnedReader.join()
254+
installReaderThreads(mgr, portId, List(returnedReader))
255255

256256
// The reader enqueued END and returned, but the DP thread has not processed END yet.
257+
assert(returnedReader.getState == Thread.State.TERMINATED)
257258
assert(!mgr.isPortCompleted(portId))
258259
}
259260

‎amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/managers/InputPortMaterializationReaderThreadSpec.scala‎

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -157,20 +157,15 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
157157
hashAttributeNames = Seq("id")
158158
)
159159

160-
/**
161-
* Runs the reader inline and returns everything it enqueued, in order, together with the reader
162-
* itself so its completion flag can be read.
163-
*/
160+
/** Runs the reader inline and returns everything it enqueued, in order. */
164161
private def replay(
165162
uri: URI,
166163
partitioning: Partitioning,
167164
readerFor: ActorVirtualIdentity = workerId
168-
): (InputPortMaterializationReaderThread, Seq[WorkflowFIFOMessage]) = {
165+
): Seq[WorkflowFIFOMessage] = {
169166
val queue = new LinkedBlockingQueue[DPInputQueueElement]()
170-
val reader =
171-
new InputPortMaterializationReaderThread(uri, queue, readerFor, partitioning)
172-
reader.run()
173-
(reader, drain(queue))
167+
new InputPortMaterializationReaderThread(uri, queue, readerFor, partitioning).run()
168+
drain(queue)
174169
}
175170

176171
private def drain(queue: LinkedBlockingQueue[DPInputQueueElement]): Seq[WorkflowFIFOMessage] = {
@@ -197,9 +192,9 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
197192

198193
behavior of "InputPortMaterializationReaderThread"
199194

200-
it should "bracket the replay with START_CHANNEL and END_CHANNEL and mark itself finished" in {
195+
it should "bracket the replay with START_CHANNEL and END_CHANNEL" in {
201196
val uri = materialize("bracket", tuples = Seq(tuple(1)))
202-
val (reader, messages) = replay(uri, broadcastTo(workerId))
197+
val messages = replay(uri, broadcastTo(workerId))
203198

204199
// The markers are the first and last things on the channel: a downstream input port that saw
205200
// data before START_CHANNEL, or END_CHANNEL before the last batch, would mis-order the port.
@@ -218,26 +213,24 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
218213
}
219214
assert(start.ecmType == NO_ALIGNMENT)
220215
assert(end.ecmType == PORT_ALIGNMENT)
221-
assert(reader.finished)
222216
}
223217

224-
it should "not report itself finished before the replay runs" in {
218+
it should "enqueue nothing before the replay runs" in {
225219
val queue = new LinkedBlockingQueue[DPInputQueueElement]()
226-
val reader = new InputPortMaterializationReaderThread(
220+
new InputPortMaterializationReaderThread(
227221
baseURI("unstarted"),
228222
queue,
229223
workerId,
230224
broadcastTo(workerId)
231225
)
232226

233-
assert(!reader.finished)
234227
assert(queue.isEmpty)
235228
}
236229

237230
it should "send every message on one channel from a URI-derived sender, numbered from zero" in {
238231
val uri =
239232
materialize("channel", tuples = Seq(tuple(1)), states = Seq((State(Map("k" -> 1)), 0L, "")))
240-
val (_, messages) = replay(uri, broadcastTo(workerId))
233+
val messages = replay(uri, broadcastTo(workerId))
241234

242235
// The dummy sender is derived from the URI *and* the receiving worker, so two workers reading
243236
// the same materialization do not share a channel (and neither collides with a real upstream).
@@ -260,7 +253,7 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
260253
)
261254
val uri = materialize("states", tuples = Seq(tuple(1), tuple(2)), states = states)
262255

263-
val (_, messages) = replay(uri, broadcastTo(workerId))
256+
val messages = replay(uri, broadcastTo(workerId))
264257

265258
// Downstream operators need their state set up before the tuples arrive, so every state
266259
// precedes every tuple regardless of how the two tables were written.
@@ -277,7 +270,7 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
277270
it should "emit no state frame when the port materialized no state" in {
278271
val uri = materialize("nostate", tuples = Seq(tuple(1)))
279272

280-
val (_, messages) = replay(uri, broadcastTo(workerId))
273+
val messages = replay(uri, broadcastTo(workerId))
281274

282275
assert(stateFrames(messages).isEmpty)
283276
assert(deliveredIds(messages) == Seq(1))
@@ -287,7 +280,7 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
287280
val ids = 1 to 5
288281
val uri = materialize("broadcast", tuples = ids.map(tuple))
289282

290-
val (_, messages) = replay(uri, broadcastTo(workerId, siblingWorkerId))
283+
val messages = replay(uri, broadcastTo(workerId, siblingWorkerId))
291284

292285
// Under the batch size, everything leaves in the single trailing flush.
293286
assert(dataFrames(messages).size == 1)
@@ -299,7 +292,7 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
299292

300293
// Broadcast to the sibling only: `allReceivers` never contains this worker, so the membership
301294
// test fails for every tuple.
302-
val (reader, messages) = replay(uri, broadcastTo(siblingWorkerId))
295+
val messages = replay(uri, broadcastTo(siblingWorkerId))
303296

304297
assert(deliveredIds(messages).isEmpty)
305298
assert(dataFrames(messages).isEmpty)
@@ -310,16 +303,15 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
310303
EmbeddedControlMessageIdentity("EndChannel")
311304
)
312305
)
313-
assert(reader.finished)
314306
}
315307

316308
it should "split the materialization between the workers of a hash-shuffled link" in {
317309
val ids = 1 to 20
318310
val uri = materialize("shuffle", tuples = ids.map(tuple))
319311
val partitioning = hashShuffleTo(workerId, siblingWorkerId)
320312

321-
val (_, mine) = replay(uri, partitioning)
322-
val (_, siblings) = replay(uri, partitioning, readerFor = siblingWorkerId)
313+
val mine = replay(uri, partitioning)
314+
val siblings = replay(uri, partitioning, readerFor = siblingWorkerId)
323315

324316
// Each worker's reader takes exactly its own buckets: together they cover the materialization
325317
// once. A filter that ignored the partitioner would duplicate every tuple; one that dropped
@@ -337,7 +329,7 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
337329
val ids = 1 to (batchSize + 1)
338330
val uri = materialize("batching", tuples = ids.map(tuple))
339331

340-
val (_, messages) = replay(uri, broadcastTo(workerId))
332+
val messages = replay(uri, broadcastTo(workerId))
341333

342334
assert(dataFrames(messages).map(_.frame.length) == Seq(batchSize, 1))
343335
assert(deliveredIds(messages) == ids)
@@ -358,9 +350,8 @@ class InputPortMaterializationReaderThreadSpec extends AnyFlatSpec {
358350
assert(failure.getMessage.startsWith("Error reading input port materializations: "))
359351
assert(failure.getCause != null)
360352
assert(failure.getMessage.endsWith(failure.getCause.getMessage))
361-
// START_CHANNEL is emitted before the try, so the marker is already on the queue...
353+
// START_CHANNEL is emitted before the try, so the marker is already on the queue, but
354+
// END_CHANNEL never is: the port is never completed.
362355
assert(ecmIds(drain(queue)) == Seq(EmbeddedControlMessageIdentity("StartChannel")))
363-
// ...but the channel never completes, which is what stops the port being called complete.
364-
assert(!reader.finished)
365356
}
366357
}

‎amber/src/test/scala/org/apache/texera/amber/engine/architecture/worker/promisehandlers/EndChannelHandlerSpec.scala‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ import scala.util.control.ControlThrowable
9393
* other input port is still open must NOT finalize its output, or the downstream region would
9494
* see the executor finish while data is still arriving on the second port. The fixture pins that
9595
* `forall`. A port fed from materialization is completed the same way, by the END this handler
96-
* processes: its reader thread flags itself `finished` only after enqueuing that END, so the
97-
* fixture can register such a port with a reader thread that never starts and still expect the
98-
* output to be finalized.
96+
* processes, whatever its reader thread has done since enqueuing that END, so the fixture can
97+
* register such a port with a reader thread that never starts and still expect the output to be
98+
* finalized.
9999
*
100100
* `produceStateOnFinish` has no override in main today — `OperatorExecutor` returns `None` for
101101
* every port — so the "operator produced a state" arm is reachable only through a test executor.
@@ -145,7 +145,7 @@ class EndChannelHandlerSpec extends AnyFlatSpec {
145145
* being ended is the last one open.
146146
* @param currentPortMaterialized whether the ended port is read from materialization, by a
147147
* reader thread that is never started here, so it has not
148-
* flagged itself finished when END is processed.
148+
* returned when END is processed.
149149
*/
150150
private class Fixture(
151151
executor: OperatorExecutor,
@@ -443,15 +443,15 @@ class EndChannelHandlerSpec extends AnyFlatSpec {
443443
assert(executor.calls.map(c => (c._1, c._2)) == Seq(("state", 3), ("finish", 3)))
444444
}
445445

446-
it should "finalize the output when the last input port is read from materialization, before its reader thread flags itself finished" in {
446+
it should "finalize the output when the last input port is read from materialization, before its reader thread has returned" in {
447447
val fixture = new Fixture(new RecordingExecutor(), currentPortMaterialized = true)
448448
val reader = fixture.dp.inputManager.getInputPortReaderThreads(currentPortId).head
449449

450450
fixture.endChannel()
451451

452-
// The reader thread enqueues END and only then sets its flag, so the DP thread can get here
453-
// first. Leaving the output unfinalized then would hang the region: nothing ends it later.
454-
assert(!reader.finished)
452+
// The DP thread can process END before the reader thread that enqueued it returns. Leaving
453+
// the output unfinalized then would hang the region: nothing ends it later.
454+
assert(reader.getState == Thread.State.NEW)
455455
assert(
456456
fixture.drainOutput() == List(
457457
FinalizePort(currentPortId, input = true),

0 commit comments

Comments
 (0)