Skip to content

Commit f9e37ae

Browse files
committed
init work on RandomNumberGenerator
scalafmt some compile issues Update ReplicatedShardingTest.java stub of JEP 356 generator Update RandomNumberGenerator.scala java version check Update RandomNumberGenerator.scala add test Update RandomNumberGenerator.scala add test Update RandomNumberGeneratorJava21Spec.scala merge issue scalafmt
1 parent 5db362f commit f9e37ae

File tree

27 files changed

+250
-72
lines changed

27 files changed

+250
-72
lines changed

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/internal/StubbedActorContext.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ import pekko.actor.typed._
1919
import pekko.actor.typed.internal._
2020
import pekko.actor.{ ActorPath, ActorRefProvider, InvalidMessageException }
2121
import pekko.annotation.InternalApi
22-
import pekko.util.Helpers
22+
import pekko.util.{ Helpers, RandomNumberGenerator }
2323
import pekko.{ actor => classic }
2424
import org.slf4j.{ Logger, Marker }
2525
import org.slf4j.helpers.{ MessageFormatter, SubstituteLoggerFactory }
2626

27-
import java.util.concurrent.ThreadLocalRandom.{ current => rnd }
2827
import scala.collection.immutable.TreeMap
2928
import scala.concurrent.ExecutionContextExecutor
3029
import scala.concurrent.duration.FiniteDuration
@@ -75,7 +74,7 @@ private[pekko] final class FunctionRef[-T](override val path: ActorPath, send: (
7574
extends ActorContextImpl[T] {
7675

7776
def this(system: ActorSystemStub, name: String, currentBehaviorProvider: () => Behavior[T]) = {
78-
this(system, (system.path / name).withUid(rnd().nextInt()), currentBehaviorProvider)
77+
this(system, (system.path / name).withUid(RandomNumberGenerator.get().nextInt()), currentBehaviorProvider)
7978
}
8079

8180
def this(name: String, currentBehaviorProvider: () => Behavior[T]) = {
@@ -111,7 +110,8 @@ private[pekko] final class FunctionRef[-T](override val path: ActorPath, send: (
111110

112111
override def spawnAnonymous[U](behavior: Behavior[U], props: Props = Props.empty): ActorRef[U] = {
113112
checkCurrentActorThread()
114-
val btk = new BehaviorTestKitImpl[U](system, (path / childName.next()).withUid(rnd().nextInt()), behavior)
113+
val btk = new BehaviorTestKitImpl[U](system,
114+
(path / childName.next()).withUid(RandomNumberGenerator.get().nextInt()), behavior)
115115
_children += btk.context.self.path.name -> btk
116116
btk.context.self
117117
}
@@ -120,7 +120,8 @@ private[pekko] final class FunctionRef[-T](override val path: ActorPath, send: (
120120
_children.get(name) match {
121121
case Some(_) => throw classic.InvalidActorNameException(s"actor name $name is already taken")
122122
case None =>
123-
val btk = new BehaviorTestKitImpl[U](system, (path / name).withUid(rnd().nextInt()), behavior)
123+
val btk =
124+
new BehaviorTestKitImpl[U](system, (path / name).withUid(RandomNumberGenerator.get().nextInt()), behavior)
124125
_children += name -> btk
125126
btk.context.self
126127
}
@@ -172,7 +173,7 @@ private[pekko] final class FunctionRef[-T](override val path: ActorPath, send: (
172173
@InternalApi private[pekko] def internalSpawnMessageAdapter[U](f: U => T, name: String): ActorRef[U] = {
173174

174175
val n = if (name != "") s"${childName.next()}-$name" else childName.next()
175-
val p = (path / n).withUid(rnd().nextInt())
176+
val p = (path / n).withUid(RandomNumberGenerator.get().nextInt())
176177
val i = new BehaviorTestKitImpl[U](system, p, BehaviorImpl.ignore)
177178
_children += p.name -> i
178179

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/BehaviorTestKit.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ import pekko.actor.testkit.typed.{ CapturedLogEvent, Effect }
1919
import pekko.actor.typed.receptionist.Receptionist
2020
import pekko.actor.typed.{ ActorRef, Behavior, Signal }
2121
import pekko.annotation.{ ApiMayChange, DoNotInherit }
22-
import com.typesafe.config.Config
22+
import pekko.util.RandomNumberGenerator
2323

24-
import java.util.concurrent.ThreadLocalRandom
24+
import com.typesafe.config.Config
2525

2626
object BehaviorTestKit {
2727

@@ -37,7 +37,7 @@ object BehaviorTestKit {
3737
@ApiMayChange
3838
def create[T](initialBehavior: Behavior[T], name: String, config: Config): BehaviorTestKit[T] = {
3939
val system = new ActorSystemStub("StubbedActorContext", config)
40-
val uid = ThreadLocalRandom.current().nextInt()
40+
val uid = RandomNumberGenerator.get().nextInt()
4141
new BehaviorTestKitImpl(system, (system.path / name).withUid(uid), initialBehavior)
4242
}
4343

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/javadsl/TestInbox.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,24 @@
1313

1414
package org.apache.pekko.actor.testkit.typed.javadsl
1515

16-
import java.util.concurrent.ThreadLocalRandom
17-
1816
import scala.collection.immutable
1917

2018
import org.apache.pekko
2119
import pekko.actor.testkit.typed.internal.TestInboxImpl
2220
import pekko.actor.typed.ActorRef
2321
import pekko.annotation.DoNotInherit
2422
import pekko.util.ccompat.JavaConverters._
23+
import pekko.util.RandomNumberGenerator
2524

2625
object TestInbox {
2726
import pekko.actor.testkit.typed.scaladsl.TestInbox.address
2827

2928
def create[T](name: String): TestInbox[T] = {
30-
val uid = ThreadLocalRandom.current().nextInt()
29+
val uid = RandomNumberGenerator.get().nextInt()
3130
new TestInboxImpl((address / name).withUid(uid))
3231
}
3332
def create[T](): TestInbox[T] = {
34-
val uid = ThreadLocalRandom.current().nextInt()
33+
val uid = RandomNumberGenerator.get().nextInt()
3534
new TestInboxImpl((address / "inbox").withUid(uid))
3635
}
3736
}

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/BehaviorTestKit.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ import pekko.actor.testkit.typed.{ CapturedLogEvent, Effect }
1919
import pekko.actor.typed.receptionist.Receptionist
2020
import pekko.actor.typed.{ ActorRef, Behavior, Signal, TypedActorContext }
2121
import pekko.annotation.{ ApiMayChange, DoNotInherit }
22+
import pekko.util.RandomNumberGenerator
23+
2224
import com.typesafe.config.Config
2325

24-
import java.util.concurrent.ThreadLocalRandom
2526
import scala.collection.immutable
2627
import scala.reflect.ClassTag
2728

@@ -32,7 +33,7 @@ object BehaviorTestKit {
3233

3334
def apply[T](initialBehavior: Behavior[T], name: String, config: Config): BehaviorTestKit[T] = {
3435
val system = new ActorSystemStub("StubbedActorContext", config)
35-
val uid = ThreadLocalRandom.current().nextInt()
36+
val uid = RandomNumberGenerator.get().nextInt()
3637
new BehaviorTestKitImpl(system, (system.path / name).withUid(uid), initialBehavior)
3738
}
3839
def apply[T](initialBehavior: Behavior[T], name: String): BehaviorTestKit[T] = {

actor-testkit-typed/src/main/scala/org/apache/pekko/actor/testkit/typed/scaladsl/TestInbox.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313

1414
package org.apache.pekko.actor.testkit.typed.scaladsl
1515

16-
import java.util.concurrent.ThreadLocalRandom
17-
1816
import scala.collection.immutable
1917

2018
import org.apache.pekko
2119
import pekko.actor.{ Address, RootActorPath }
2220
import pekko.actor.testkit.typed.internal.TestInboxImpl
2321
import pekko.actor.typed.ActorRef
2422
import pekko.annotation.{ ApiMayChange, DoNotInherit }
23+
import pekko.util.RandomNumberGenerator
2524

2625
@ApiMayChange
2726
object TestInbox {
2827
def apply[T](name: String = "inbox"): TestInbox[T] = {
29-
val uid = ThreadLocalRandom.current().nextInt()
28+
val uid = RandomNumberGenerator.get().nextInt()
3029
new TestInboxImpl((address / name).withUid(uid))
3130
}
3231

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.pekko.util
19+
20+
import com.typesafe.config.ConfigFactory
21+
import org.scalatest.matchers.should.Matchers
22+
import org.scalatest.wordspec.AnyWordSpec
23+
24+
class RandomNumberGeneratorJava21Spec extends AnyWordSpec with Matchers {
25+
26+
"RandomNumberGenerator (Java 21+)" should {
27+
28+
"support config" in {
29+
val config = ConfigFactory.parseString(
30+
"""pekko.random.generator-implementation = "Xoroshiro128PlusPlus"""")
31+
val rng = RandomNumberGenerator.createGenerator(config)
32+
rng shouldBe a[Jep356RandomNumberGenerator]
33+
rng.nextInt(10) should (be >= 0 and be < 10)
34+
}
35+
36+
}
37+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.pekko.util
19+
20+
import org.scalatest.matchers.should.Matchers
21+
import org.scalatest.wordspec.AnyWordSpec
22+
23+
class RandomNumberGeneratorSpec extends AnyWordSpec with Matchers {
24+
25+
"RandomNumberGenerator" should {
26+
27+
"default to ThreadLocalRandom" in {
28+
val rng = RandomNumberGenerator.get()
29+
rng shouldEqual ThreadLocalRandomNumberGenerator
30+
rng.nextInt(10) should (be >= 0 and be < 10)
31+
}
32+
33+
}
34+
}

actor-typed/src/main/scala/org/apache/pekko/actor/typed/delivery/internal/WorkPullingProducerControllerImpl.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package org.apache.pekko.actor.typed.delivery.internal
1515

1616
import java.util.UUID
17-
import java.util.concurrent.ThreadLocalRandom
1817
import java.util.concurrent.TimeoutException
1918

2019
import scala.reflect.ClassTag
@@ -39,7 +38,7 @@ import pekko.actor.typed.scaladsl.Behaviors
3938
import pekko.actor.typed.scaladsl.LoggerOps
4039
import pekko.actor.typed.scaladsl.StashBuffer
4140
import pekko.annotation.InternalApi
42-
import pekko.util.Timeout
41+
import pekko.util.{ RandomNumberGenerator, Timeout }
4342

4443
/**
4544
* INTERNAL API
@@ -404,7 +403,7 @@ private class WorkPullingProducerControllerImpl[A: ClassTag](
404403
if (workers.isEmpty) {
405404
None
406405
} else {
407-
val i = ThreadLocalRandom.current().nextInt(workers.size)
406+
val i = RandomNumberGenerator.get().nextInt(workers.size)
408407
Some(workers(i))
409408
}
410409
}

actor-typed/src/main/scala/org/apache/pekko/actor/typed/internal/Supervision.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
package org.apache.pekko.actor.typed
1515
package internal
1616

17-
import java.util.concurrent.ThreadLocalRandom
18-
1917
import scala.concurrent.duration.Deadline
2018
import scala.concurrent.duration.FiniteDuration
2119
import scala.reflect.ClassTag
@@ -33,7 +31,7 @@ import pekko.actor.typed.scaladsl.Behaviors
3331
import pekko.actor.typed.scaladsl.StashBuffer
3432
import pekko.annotation.InternalApi
3533
import pekko.event.Logging
36-
import pekko.util.OptionVal
34+
import pekko.util.{ OptionVal, RandomNumberGenerator }
3735
import pekko.util.unused
3836

3937
import scala.util.Try
@@ -187,7 +185,7 @@ private object RestartSupervisor {
187185
minBackoff: FiniteDuration,
188186
maxBackoff: FiniteDuration,
189187
randomFactor: Double): FiniteDuration = {
190-
val rnd = 1.0 + ThreadLocalRandom.current().nextDouble() * randomFactor
188+
val rnd = 1.0 + RandomNumberGenerator.get().nextDouble() * randomFactor
191189
val calculatedDuration = Try(maxBackoff.min(minBackoff * math.pow(2, restartCount)) * rnd).getOrElse(maxBackoff)
192190
calculatedDuration match {
193191
case f: FiniteDuration => f

actor-typed/src/main/scala/org/apache/pekko/actor/typed/internal/routing/RoutingLogic.scala

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313

1414
package org.apache.pekko.actor.typed.internal.routing
1515

16-
import java.util.concurrent.ThreadLocalRandom
17-
1816
import org.apache.pekko
1917
import pekko.actor.Address
2018
import pekko.actor.typed.ActorRef
2119
import pekko.annotation.InternalApi
2220
import pekko.routing.ConsistentHash
21+
import pekko.util.RandomNumberGenerator
2322

2423
/**
2524
* Kept in the behavior, not shared between instances, meant to be stateful.
@@ -89,7 +88,7 @@ private[pekko] object RoutingLogics {
8988
private var currentRoutees: Array[ActorRef[T]] = _
9089

9190
override def selectRoutee(msg: T): ActorRef[T] = {
92-
val selectedIdx = ThreadLocalRandom.current().nextInt(currentRoutees.length)
91+
val selectedIdx = RandomNumberGenerator.get().nextInt(currentRoutees.length)
9392
currentRoutees(selectedIdx)
9493
}
9594

0 commit comments

Comments
 (0)