Skip to content

Commit

Permalink
avoid mima filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Roiocam committed Jul 8, 2024
1 parent 7793feb commit f48f2dd
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static Behavior<Void> showPoolRouting() {
Routers.pool(
poolSize,
// make sure the workers are restarted if they fail
Behaviors.supervise(Worker.create()).onAnyFailure(SupervisorStrategy.restart()));
Behaviors.supervise(Worker.create()).onFailure(SupervisorStrategy.restart()));
ActorRef<Worker.Command> router = context.spawn(pool, "worker-pool");

for (int i = 0; i < 10; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public Got(int n) {

// #top-level
public static Behavior<Command> create() {
return Behaviors.supervise(counter(1)).onAnyFailure(SupervisorStrategy.restart());
return Behaviors.supervise(counter(1)).onFailure(SupervisorStrategy.restart());
}
// #top-level

Expand Down Expand Up @@ -114,7 +114,7 @@ static Behavior<String> parent() {
return Behaviors.same();
});
}))
.onAnyFailure(SupervisorStrategy.restart());
.onFailure(SupervisorStrategy.restart());
}
// #restart-stop-children

Expand All @@ -135,7 +135,7 @@ static Behavior<String> parent2() {
child2.tell(parts[1]);
return Behaviors.same();
}))
.onAnyFailure(SupervisorStrategy.restart().withStopChildren(false));
.onFailure(SupervisorStrategy.restart().withStopChildren(false));
});
}
// #restart-keep-children
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
ProblemFilters.exclude[MissingFieldProblem]("org.apache.pekko.actor.typed.scaladsl.Behaviors.Supervise")
ProblemFilters.exclude[MissingClassProblem]("org.apache.pekko.actor.typed.scaladsl.Behaviors$Supervise$")
ProblemFilters.exclude[MissingClassProblem]("org.apache.pekko.actor.typed.scaladsl.Behaviors$Supervise")
ProblemFilters.exclude[MissingClassProblem]("org.apache.pekko.actor.typed.javadsl.Behaviors$Supervise")
ProblemFilters.exclude[IncompatibleResultTypeProblem]("org.apache.pekko.actor.typed.scaladsl.Behaviors.supervise")
ProblemFilters.exclude[IncompatibleResultTypeProblem]("org.apache.pekko.actor.typed.javadsl.Behaviors.supervise")
# 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.

# Change the return type of `Behaviors.supervise` to support flattened supervision
ProblemFilters.exclude[IncompatibleResultTypeProblem]("org.apache.pekko.actor.typed.javadsl.Behaviors#Supervise.onFailure")
ProblemFilters.exclude[IncompatibleResultTypeProblem]("org.apache.pekko.actor.typed.scaladsl.Behaviors#Supervise.onFailure")
ProblemFilters.exclude[IncompatibleResultTypeProblem]("org.apache.pekko.actor.typed.scaladsl.Behaviors#Supervise.onFailure$extension")
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ abstract class Behavior[T](private[pekko] val _tag: Int) { behavior =>
* A behavior type that could be supervised, Not for user extension.
*/
@InternalApi
final class SuperviseBehavior[T] private[pekko] (
class SuperviseBehavior[T] private[pekko] (
val wrapped: Behavior[T]) extends Behavior[T](BehaviorTags.SuperviseBehavior) {
private final val ThrowableClassTag = ClassTag(classOf[Throwable])

Expand All @@ -86,24 +86,12 @@ final class SuperviseBehavior[T] private[pekko] (
}

/**
* Java API:
* Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws.
*
* Only exceptions of the given type (and their subclasses) will be handled by this supervision behavior.
*/
def onFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] = {
def onFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] =
onFailure(strategy)(ClassTag(clazz))
}

/**
* Java API:
* Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws.
*
* Only exceptions of the given type (and their subclasses) will be handled by this supervision behavior.
*/
def onAnyFailure[Thr <: Throwable](strategy: SupervisorStrategy): SuperviseBehavior[T] = {
onFailure(classOf[Exception], strategy)
}

private[pekko] def unwrap: Behavior[T] = wrapped
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,27 @@ object Behaviors {
* .onFailure[IndexOutOfBoundsException](SupervisorStrategy.resume) // resume for IndexOutOfBoundsException exceptions
* }}}
*/
def supervise[T](wrapped: Behavior[T]): SuperviseBehavior[T] =
scaladsl.Behaviors.supervise(wrapped)
def supervise[T](wrapped: Behavior[T]): Supervise[T] =
new Supervise[T](wrapped)

final class Supervise[T] private[pekko] (wrapped: Behavior[T]) {

/**
* Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws.
*
* Only exceptions of the given type (and their subclasses) will be handled by this supervision behavior.
*/
def onFailure[Thr <: Throwable](clazz: Class[Thr], strategy: SupervisorStrategy): SuperviseBehavior[T] =
new SuperviseBehavior[T](wrapped).onFailure(clazz, strategy)

/**
* Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws.
*
* All non-fatal (see [[scala.util.control.NonFatal]]) exceptions types will be handled using the given strategy.
*/
def onFailure(strategy: SupervisorStrategy): Behavior[T] =
new SuperviseBehavior[T](wrapped).onFailure(strategy)
}

/**
* Transform the incoming messages by placing a funnel in front of the wrapped `Behavior`: the supplied
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ package org.apache.pekko.actor.typed
package scaladsl

import scala.reflect.ClassTag

import org.apache.pekko
import org.apache.pekko.actor.typed.SuperviseBehavior
import pekko.actor.typed.internal._
import pekko.annotation.{ DoNotInherit, InternalApi }

Expand Down Expand Up @@ -225,8 +225,18 @@ object Behaviors {
* .onFailure[IndexOutOfBoundsException](SupervisorStrategy.resume) // resume for IndexOutOfBoundsException exceptions
* }}}
*/
def supervise[T](wrapped: Behavior[T]): SuperviseBehavior[T] =
new SuperviseBehavior[T](wrapped)
def supervise[T](wrapped: Behavior[T]): Supervise[T] =
new Supervise[T](wrapped)

private final val ThrowableClassTag = ClassTag(classOf[Throwable])
final class Supervise[T] private[pekko] (val wrapped: Behavior[T]) extends AnyVal {

/** Specify the [[SupervisorStrategy]] to be invoked when the wrapped behavior throws. */
def onFailure[Thr <: Throwable](strategy: SupervisorStrategy)(
implicit tag: ClassTag[Thr] = ThrowableClassTag): SuperviseBehavior[T] = {
new SuperviseBehavior[T](wrapped).onFailure(strategy)(tag)
}
}

/**
* Support for scheduled `self` messages in an actor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static void backoff() {
singleton.init(
SingletonActor.of(
Behaviors.supervise(Counter.create())
.onAnyFailure(
.onFailure(
SupervisorStrategy.restartWithBackoff(
Duration.ofSeconds(1), Duration.ofSeconds(10), 0.2)),
"GlobalCounter"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,7 @@ private SupervisingActor(ActorContext<String> context) {
super(context);
child =
context.spawn(
Behaviors.supervise(SupervisedActor.create())
.onAnyFailure(SupervisorStrategy.restart()),
Behaviors.supervise(SupervisedActor.create()).onFailure(SupervisorStrategy.restart()),
"supervised-actor");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ protected void log() {
public void workWhenWrappedInOtherBehavior() {
Behavior<Command> behavior =
Behaviors.supervise(counter(PersistenceId.ofUniqueId("c6")))
.onAnyFailure(
.onFailure(
SupervisorStrategy.restartWithBackoff(
Duration.ofSeconds(1), Duration.ofSeconds(10), 0.1));
ActorRef<Command> c = testKit.spawn(behavior);
Expand Down

0 comments on commit f48f2dd

Please sign in to comment.