-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
POC +str Add StreamCollector api for unsafe transformation. #628
Draft
He-Pin
wants to merge
1
commit into
apache:main
Choose a base branch
from
He-Pin:flowEmiter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
stream/src/main/scala/org/apache/pekko/stream/StreamCollector.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* 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.pekko.stream | ||
|
||
import org.apache.pekko | ||
import pekko.actor.{ DeadLetterSuppression, NoSerializationVerificationNeeded } | ||
import pekko.annotation.InternalApi | ||
|
||
import scala.util.{ Failure, Success, Try } | ||
|
||
object StreamCollectorOps { | ||
def emit[T](value: T)(implicit collector: StreamCollector[T]): Unit = collector.emit(value) | ||
def fail(throwable: Throwable)(implicit collector: StreamCollector[_]): Unit = collector.fail(throwable) | ||
def complete()(implicit collector: StreamCollector[_]): Unit = collector.complete() | ||
def handle[T](result: Try[T])(implicit collector: StreamCollector[T]): Unit = collector.handle(result) | ||
def handle[T](result: Either[Throwable, T])(implicit collector: StreamCollector[T]): Unit = collector.handle(result) | ||
} | ||
|
||
object StreamCollectorUnsafeOps { | ||
def emitSync[T](value: T)(implicit collector: StreamCollector[T]): Unit = | ||
collector.asInstanceOf[UnsafeStreamCollector[T]].emitSync(value) | ||
def failSync(throwable: Throwable)(implicit collector: StreamCollector[_]): Unit = | ||
collector.asInstanceOf[UnsafeStreamCollector[_]].failSync(throwable) | ||
def completeSync()(implicit collector: StreamCollector[_]): Unit = | ||
collector.asInstanceOf[UnsafeStreamCollector[_]].completeSync() | ||
def handleSync[T](result: Try[T])(implicit collector: StreamCollector[T]): Unit = | ||
collector.asInstanceOf[UnsafeStreamCollector[T]].handleSync(result) | ||
def handleSync[T](result: Either[Throwable, T])(implicit collector: StreamCollector[T]): Unit = | ||
collector.asInstanceOf[UnsafeStreamCollector[T]].handleSync(result) | ||
} | ||
|
||
object StreamCollector { | ||
sealed trait StreamCollectorCommand | ||
extends DeadLetterSuppression | ||
with NoSerializationVerificationNeeded | ||
|
||
case class EmitNext[T](value: T) extends StreamCollectorCommand | ||
|
||
case class Fail(throwable: Throwable) extends StreamCollectorCommand | ||
|
||
object Complete extends StreamCollectorCommand | ||
|
||
object TryPull extends StreamCollectorCommand | ||
} | ||
|
||
trait StreamCollector[T] { | ||
|
||
def emit(value: T): Unit | ||
|
||
def tryPull(): Unit = () | ||
|
||
def fail(throwable: Throwable): Unit | ||
|
||
def complete(): Unit | ||
|
||
def handle(result: Try[T]): Unit = result match { | ||
case Success(value) => emit(value) | ||
case Failure(ex) => fail(ex) | ||
} | ||
|
||
def handle(result: Either[Throwable, T]): Unit = result match { | ||
case Right(value) => emit(value) | ||
case Left(ex) => fail(ex) | ||
} | ||
} | ||
|
||
@InternalApi | ||
private[pekko] trait UnsafeStreamCollector[T] extends StreamCollector[T] { | ||
def emitSync(value: T): Unit | ||
def failSync(throwable: Throwable): Unit | ||
|
||
def completeSync(): Unit | ||
def handleSync(result: Try[T]): Unit = result match { | ||
case Success(value) => emitSync(value) | ||
case Failure(ex) => failSync(ex) | ||
} | ||
def handleSync(result: Either[Throwable, T]): Unit = result match { | ||
case Right(value) => emitSync(value) | ||
case Left(ex) => failSync(ex) | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
stream/src/main/scala/org/apache/pekko/stream/impl/fusing/UnsafeTransformUnordered.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* 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.pekko.stream.impl.fusing | ||
|
||
import org.apache.pekko.stream.ActorAttributes.SupervisionStrategy | ||
import org.apache.pekko.stream.Attributes.SourceLocation | ||
import org.apache.pekko.stream.impl.Stages.DefaultAttributes | ||
import org.apache.pekko.stream.stage.{ GraphStage, GraphStageLogic, InHandler, OutHandler } | ||
import org.apache.pekko.stream._ | ||
|
||
import scala.util.control.NonFatal | ||
|
||
private[pekko] class UnsafeTransformUnordered[In, Out]( | ||
parallelism: Int, | ||
transform: (In, StreamCollector[Out]) => Unit) | ||
extends GraphStage[FlowShape[In, Out]] { | ||
private val in = Inlet[In]("UnsafeTransformOrdered.in") | ||
private val out = Outlet[Out]("UnsafeTransformOrdered.out") | ||
|
||
override def initialAttributes = DefaultAttributes.mapAsyncUnordered and SourceLocation.forLambda(transform) | ||
|
||
override val shape = FlowShape(in, out) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = | ||
new GraphStageLogic(shape) with InHandler with OutHandler with UnsafeStreamCollector[Out] { | ||
override def toString = s"UnsafeTransformOrdered.Logic(inFlight=$inFlight, buffer=$buffer)" | ||
import org.apache.pekko.stream.impl.{ Buffer => BufferImpl } | ||
|
||
private val decider = | ||
inheritedAttributes.mandatoryAttribute[SupervisionStrategy].decider | ||
private var inFlight = 0 | ||
private var buffer: org.apache.pekko.stream.impl.Buffer[Out] = _ | ||
|
||
import StreamCollector._ | ||
|
||
private val callback: StreamCollectorCommand => Unit = getAsyncCallback[StreamCollectorCommand](handle).invoke | ||
|
||
override def emitSync(value: Out): Unit = handle(EmitNext(value)) | ||
override def failSync(throwable: Throwable): Unit = handle(Fail(throwable)) | ||
override def completeSync(): Unit = completeStage() | ||
|
||
// TODO check permit | ||
override final def emit(value: Out): Unit = callback(EmitNext(value)) | ||
override final def fail(throwable: Throwable): Unit = callback(Fail(throwable)) | ||
override final def complete(): Unit = callback(Complete) | ||
|
||
// | ||
private[this] def todo: Int = inFlight + buffer.used | ||
|
||
override def preStart(): Unit = buffer = BufferImpl(parallelism, inheritedAttributes) | ||
|
||
private def isCompleted = isClosed(in) && todo == 0 | ||
|
||
def handle(msg: StreamCollectorCommand): Unit = { | ||
inFlight -= 1 | ||
|
||
msg match { | ||
case EmitNext(elem: Out @unchecked) if elem != null => | ||
if (isAvailable(out)) { | ||
if (!hasBeenPulled(in)) tryPull(in) | ||
push(out, elem) | ||
if (isCompleted) completeStage() | ||
} else buffer.enqueue(elem) | ||
case EmitNext(_) => | ||
if (isCompleted) completeStage() | ||
else if (!hasBeenPulled(in)) tryPull(in) | ||
case TryPull => | ||
if (!hasBeenPulled(in)) tryPull(in) | ||
case Complete => | ||
completeStage() | ||
case Fail(ex) => | ||
if (decider(ex) == Supervision.Stop) failStage(ex) | ||
else if (isCompleted) completeStage() | ||
else if (!hasBeenPulled(in)) tryPull(in) | ||
} | ||
} | ||
|
||
override def onPush(): Unit = { | ||
try { | ||
val elem = grab(in) | ||
transform(elem, this) | ||
inFlight += 1 | ||
} catch { | ||
case NonFatal(ex) => if (decider(ex) == Supervision.Stop) failStage(ex) | ||
} | ||
if (todo < parallelism && !hasBeenPulled(in)) tryPull(in) | ||
} | ||
|
||
override def onUpstreamFinish(): Unit = { | ||
if (todo == 0) completeStage() | ||
} | ||
|
||
override def onPull(): Unit = { | ||
if (!buffer.isEmpty) push(out, buffer.dequeue()) | ||
|
||
val leftTodo = todo | ||
if (isClosed(in) && leftTodo == 0) completeStage() | ||
else if (leftTodo < parallelism && !hasBeenPulled(in)) tryPull(in) | ||
} | ||
|
||
setHandlers(in, out, this) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for demo