Skip to content

Commit f317aaf

Browse files
committed
rewrite mailbox selection logic, see #3342
- add “mailbox-requirement” key to dispatcher section - split out mailbox section, add akka.actor.default-mailbox - rewrite findMarker method and use it for Props.create() and getting the required mailbox of an actor - add ProducesMessageQueue trait for MailboxType so that requirements can be checked before trying to create the actor for real - verify actor as well as dispatcher requirements for message queue before creation, even in remote-deployed case - change MessageDispatcher constructor to take a Configurator, add that to migration guide
1 parent 51ed174 commit f317aaf

37 files changed

+843
-419
lines changed

akka-actor-tests/src/test/java/akka/actor/ActorCreationTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,14 @@ public T create() {
4747
}
4848
}
4949

50+
static interface I<T> extends Creator<UntypedActor> {}
51+
static class F implements I<Object> {
52+
@Override
53+
public UntypedActor create() {
54+
return null;
55+
}
56+
}
57+
5058
@Test
5159
public void testRightCreator() {
5260
final Props p = Props.create(new C());
@@ -65,4 +73,10 @@ public void testBoundedCreator() {
6573
assertEquals(UntypedActor.class, p.actorClass());
6674
}
6775

76+
@Test
77+
public void testSuperinterface() {
78+
final Props p = Props.create(new F());
79+
assertEquals(UntypedActor.class, p.actorClass());
80+
}
81+
6882
}

akka-actor-tests/src/test/scala/akka/actor/ActorMailboxSpec.scala

Lines changed: 126 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ object ActorMailboxSpec {
2222
mailbox-type = "akka.dispatch.BoundedMailbox"
2323
}
2424
25+
requiring-bounded-dispatcher {
26+
mailbox-capacity = 1000
27+
mailbox-push-timeout-time = 10s
28+
mailbox-type = "akka.dispatch.BoundedMailbox"
29+
mailbox-requirement = "akka.dispatch.BoundedMessageQueueSemantics"
30+
}
31+
2532
unbounded-mailbox {
2633
mailbox-type = "akka.dispatch.UnboundedMailbox"
2734
}
@@ -80,10 +87,32 @@ object ActorMailboxSpec {
8087
dispatcher = bounded-dispatcher
8188
mailbox = unbounded-mailbox
8289
}
83-
}
84-
85-
akka.actor.mailbox.requirements {
86-
"akka.dispatch.BoundedMessageQueueSemantics" = bounded-mailbox
90+
/bounded-deque-requirements-configured {
91+
dispatcher = requiring-bounded-dispatcher
92+
mailbox = akka.actor.mailbox.bounded-deque-based
93+
}
94+
/bounded-deque-require-unbounded-configured {
95+
dispatcher = requiring-bounded-dispatcher
96+
mailbox = akka.actor.mailbox.unbounded-deque-based
97+
}
98+
/bounded-deque-require-unbounded-unconfigured {
99+
dispatcher = requiring-bounded-dispatcher
100+
}
101+
/bounded-deque-requirements-configured-props-disp {
102+
mailbox = akka.actor.mailbox.bounded-deque-based
103+
}
104+
/bounded-deque-require-unbounded-configured-props-disp {
105+
mailbox = akka.actor.mailbox.unbounded-deque-based
106+
}
107+
/bounded-deque-requirements-configured-props-mail {
108+
dispatcher = requiring-bounded-dispatcher
109+
}
110+
/bounded-deque-require-unbounded-configured-props-mail {
111+
dispatcher = requiring-bounded-dispatcher
112+
}
113+
/bounded-deque-require-unbounded-unconfigured-props-mail {
114+
dispatcher = requiring-bounded-dispatcher
115+
}
87116
}
88117
""")
89118

@@ -97,10 +126,16 @@ object ActorMailboxSpec {
97126

98127
class StashQueueReportingActor extends QueueReportingActor with Stash
99128

100-
val UnboundedMailboxTypes = Seq(classOf[QueueBasedMessageQueue], classOf[UnboundedMessageQueueSemantics])
101-
val BoundedMailboxTypes = Seq(classOf[QueueBasedMessageQueue], classOf[BoundedMessageQueueSemantics])
102-
val UnboundedDeqMailboxTypes = Seq(classOf[QueueBasedMessageQueue], classOf[DequeBasedMessageQueue],
129+
val UnboundedMailboxTypes = Seq(classOf[UnboundedMessageQueueSemantics])
130+
val BoundedMailboxTypes = Seq(classOf[BoundedMessageQueueSemantics])
131+
val UnboundedDeqMailboxTypes = Seq(
132+
classOf[DequeBasedMessageQueueSemantics],
133+
classOf[UnboundedMessageQueueSemantics],
103134
classOf[UnboundedDequeBasedMessageQueueSemantics])
135+
val BoundedDeqMailboxTypes = Seq(
136+
classOf[DequeBasedMessageQueueSemantics],
137+
classOf[BoundedMessageQueueSemantics],
138+
classOf[BoundedDequeBasedMessageQueueSemantics])
104139
}
105140

106141
class ActorMailboxSpec extends AkkaSpec(ActorMailboxSpec.mailboxConf) with DefaultTimeout with ImplicitSender {
@@ -122,7 +157,7 @@ class ActorMailboxSpec extends AkkaSpec(ActorMailboxSpec.mailboxConf) with Defau
122157
checkMailboxQueue(Props[QueueReportingActor], "default-default", UnboundedMailboxTypes)
123158
}
124159

125-
"get an unbounded dequeu message queue when it is only configured on the props" in {
160+
"get an unbounded deque message queue when it is only configured on the props" in {
126161
checkMailboxQueue(Props[QueueReportingActor].withMailbox("akka.actor.mailbox.unbounded-deque-based"),
127162
"default-override-from-props", UnboundedDeqMailboxTypes)
128163
}
@@ -132,7 +167,7 @@ class ActorMailboxSpec extends AkkaSpec(ActorMailboxSpec.mailboxConf) with Defau
132167
"default-override-from-trait", BoundedMailboxTypes)
133168
}
134169

135-
"get an unbounded dequeu message queue when it's only mixed with Stash" in {
170+
"get an unbounded deque message queue when it's only mixed with Stash" in {
136171
checkMailboxQueue(Props[StashQueueReportingActor],
137172
"default-override-from-stash", UnboundedDeqMailboxTypes)
138173
}
@@ -141,24 +176,20 @@ class ActorMailboxSpec extends AkkaSpec(ActorMailboxSpec.mailboxConf) with Defau
141176
checkMailboxQueue(Props[QueueReportingActor], "default-bounded", BoundedMailboxTypes)
142177
}
143178

144-
"get an unbounded dequeu message queue when it's configured as mailbox" in {
179+
"get an unbounded deque message queue when it's configured as mailbox" in {
145180
checkMailboxQueue(Props[QueueReportingActor], "default-unbounded-deque", UnboundedDeqMailboxTypes)
146181
}
147182

148183
"fail to create actor when an unbounded dequeu message queue is configured as mailbox overriding RequestMailbox" in {
149-
filterEvents(EventFilter[ActorInitializationException]()) {
150-
verifyActorTermination(system.actorOf(Props[BoundedQueueReportingActor], "default-unbounded-deque-override-trait"))
151-
}
184+
intercept[IllegalArgumentException](system.actorOf(Props[BoundedQueueReportingActor], "default-unbounded-deque-override-trait"))
152185
}
153186

154187
"get an unbounded message queue when defined in dispatcher" in {
155188
checkMailboxQueue(Props[QueueReportingActor], "unbounded-default", UnboundedMailboxTypes)
156189
}
157190

158191
"fail to create actor when an unbounded message queue is defined in dispatcher overriding RequestMailbox" in {
159-
filterEvents(EventFilter[ActorInitializationException]()) {
160-
verifyActorTermination(system.actorOf(Props[BoundedQueueReportingActor], "unbounded-default-override-trait"))
161-
}
192+
intercept[IllegalArgumentException](system.actorOf(Props[BoundedQueueReportingActor], "unbounded-default-override-trait"))
162193
}
163194

164195
"get a bounded message queue when it's configured as mailbox overriding unbounded in dispatcher" in {
@@ -183,5 +214,84 @@ class ActorMailboxSpec extends AkkaSpec(ActorMailboxSpec.mailboxConf) with Defau
183214
"bounded-unbounded-override-props", UnboundedMailboxTypes)
184215
}
185216

217+
"get a bounded deque-based message queue if configured and required" in {
218+
checkMailboxQueue(Props[StashQueueReportingActor], "bounded-deque-requirements-configured", BoundedDeqMailboxTypes)
219+
}
220+
221+
"fail with a unbounded deque-based message queue if configured and required" in {
222+
intercept[IllegalArgumentException](system.actorOf(Props[StashQueueReportingActor], "bounded-deque-require-unbounded-configured"))
223+
}
224+
225+
"fail with a bounded deque-based message queue if not configured" in {
226+
intercept[IllegalArgumentException](system.actorOf(Props[StashQueueReportingActor], "bounded-deque-require-unbounded-unconfigured"))
227+
}
228+
229+
"get a bounded deque-based message queue if configured and required with Props" in {
230+
checkMailboxQueue(
231+
Props[StashQueueReportingActor]
232+
.withDispatcher("requiring-bounded-dispatcher")
233+
.withMailbox("akka.actor.mailbox.bounded-deque-based"),
234+
"bounded-deque-requirements-configured-props",
235+
BoundedDeqMailboxTypes)
236+
}
237+
238+
"fail with a unbounded deque-based message queue if configured and required with Props" in {
239+
intercept[IllegalArgumentException](system.actorOf(
240+
Props[StashQueueReportingActor]
241+
.withDispatcher("requiring-bounded-dispatcher")
242+
.withMailbox("akka.actor.mailbox.unbounded-deque-based"),
243+
"bounded-deque-require-unbounded-configured-props"))
244+
}
245+
246+
"fail with a bounded deque-based message queue if not configured with Props" in {
247+
intercept[IllegalArgumentException](system.actorOf(
248+
Props[StashQueueReportingActor]
249+
.withDispatcher("requiring-bounded-dispatcher"),
250+
"bounded-deque-require-unbounded-unconfigured-props"))
251+
}
252+
253+
"get a bounded deque-based message queue if configured and required with Props (dispatcher)" in {
254+
checkMailboxQueue(
255+
Props[StashQueueReportingActor]
256+
.withDispatcher("requiring-bounded-dispatcher"),
257+
"bounded-deque-requirements-configured-props-disp",
258+
BoundedDeqMailboxTypes)
259+
}
260+
261+
"fail with a unbounded deque-based message queue if configured and required with Props (dispatcher)" in {
262+
intercept[IllegalArgumentException](system.actorOf(
263+
Props[StashQueueReportingActor]
264+
.withDispatcher("requiring-bounded-dispatcher"),
265+
"bounded-deque-require-unbounded-configured-props-disp"))
266+
}
267+
268+
"fail with a bounded deque-based message queue if not configured with Props (dispatcher)" in {
269+
intercept[IllegalArgumentException](system.actorOf(
270+
Props[StashQueueReportingActor]
271+
.withDispatcher("requiring-bounded-dispatcher"),
272+
"bounded-deque-require-unbounded-unconfigured-props-disp"))
273+
}
274+
275+
"get a bounded deque-based message queue if configured and required with Props (mailbox)" in {
276+
checkMailboxQueue(
277+
Props[StashQueueReportingActor]
278+
.withMailbox("akka.actor.mailbox.bounded-deque-based"),
279+
"bounded-deque-requirements-configured-props-mail",
280+
BoundedDeqMailboxTypes)
281+
}
282+
283+
"fail with a unbounded deque-based message queue if configured and required with Props (mailbox)" in {
284+
intercept[IllegalArgumentException](system.actorOf(
285+
Props[StashQueueReportingActor]
286+
.withMailbox("akka.actor.mailbox.unbounded-deque-based"),
287+
"bounded-deque-require-unbounded-configured-props-mail"))
288+
}
289+
290+
"fail with a bounded deque-based message queue if not configured with Props (mailbox)" in {
291+
intercept[IllegalArgumentException](system.actorOf(
292+
Props[StashQueueReportingActor],
293+
"bounded-deque-require-unbounded-unconfigured-props-mail"))
294+
}
295+
186296
}
187297
}

akka-actor-tests/src/test/scala/akka/actor/ActorSystemSpec.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ object ActorSystemSpec {
8484

8585
class SlowDispatcher(_config: Config, _prerequisites: DispatcherPrerequisites) extends MessageDispatcherConfigurator(_config, _prerequisites) {
8686
private val instance = new Dispatcher(
87-
prerequisites,
87+
this,
8888
config.getString("id"),
8989
config.getInt("throughput"),
9090
Duration(config.getNanoseconds("throughput-deadline-time"), TimeUnit.NANOSECONDS),
91-
mailboxType,
92-
mailBoxTypeConfigured,
9391
configureExecutor(),
9492
Duration(config.getMilliseconds("shutdown-timeout"), TimeUnit.MILLISECONDS)) {
9593
val doneIt = new Switch

akka-actor-tests/src/test/scala/akka/actor/IOActor.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ class IOActorSpec extends AkkaSpec with DefaultTimeout {
269269
case Failure(e) if check(n, e)
270270
if (delay.isDefined) {
271271
executor match {
272-
case m: MessageDispatcher m.prerequisites.scheduler.scheduleOnce(delay.get)(run(n + 1))
272+
case m: MessageDispatcher m.configurator.prerequisites.scheduler.scheduleOnce(delay.get)(run(n + 1))
273273
case _ // Thread.sleep, ignore, or other?
274274
}
275275
} else run(n + 1)

akka-actor-tests/src/test/scala/akka/actor/SupervisorHierarchySpec.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,10 @@ object SupervisorHierarchySpec {
7878
extends DispatcherConfigurator(config, prerequisites) {
7979

8080
private val instance: MessageDispatcher =
81-
new Dispatcher(prerequisites,
81+
new Dispatcher(this,
8282
config.getString("id"),
8383
config.getInt("throughput"),
8484
Duration(config.getNanoseconds("throughput-deadline-time"), TimeUnit.NANOSECONDS),
85-
mailboxType,
86-
mailBoxTypeConfigured,
8785
configureExecutor(),
8886
Duration(config.getMilliseconds("shutdown-timeout"), TimeUnit.MILLISECONDS)) {
8987

akka-actor-tests/src/test/scala/akka/actor/dispatch/ActorModelSpec.scala

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -529,12 +529,10 @@ object DispatcherModelSpec {
529529
extends MessageDispatcherConfigurator(config, prerequisites) {
530530

531531
private val instance: MessageDispatcher =
532-
new Dispatcher(prerequisites,
532+
new Dispatcher(this,
533533
config.getString("id"),
534534
config.getInt("throughput"),
535535
Duration(config.getNanoseconds("throughput-deadline-time"), TimeUnit.NANOSECONDS),
536-
mailboxType,
537-
mailBoxTypeConfigured,
538536
configureExecutor(),
539537
Duration(config.getMilliseconds("shutdown-timeout"), TimeUnit.MILLISECONDS)) with MessageDispatcherInterceptor
540538

@@ -600,20 +598,17 @@ object BalancingDispatcherModelSpec {
600598
}
601599

602600
class BalancingMessageDispatcherInterceptorConfigurator(config: Config, prerequisites: DispatcherPrerequisites)
603-
extends MessageDispatcherConfigurator(config, prerequisites) {
601+
extends BalancingDispatcherConfigurator(config, prerequisites) {
604602

605-
private val instance: MessageDispatcher =
606-
new BalancingDispatcher(prerequisites,
603+
override protected def create(mailboxType: MailboxType): BalancingDispatcher =
604+
new BalancingDispatcher(this,
607605
config.getString("id"),
608606
config.getInt("throughput"),
609607
Duration(config.getNanoseconds("throughput-deadline-time"), TimeUnit.NANOSECONDS),
610608
mailboxType,
611-
mailBoxTypeConfigured,
612609
configureExecutor(),
613610
Duration(config.getMilliseconds("shutdown-timeout"), TimeUnit.MILLISECONDS),
614611
config.getBoolean("attempt-teamwork")) with MessageDispatcherInterceptor
615-
616-
override def dispatcher(): MessageDispatcher = instance
617612
}
618613
}
619614

akka-actor-tests/src/test/scala/akka/config/ConfigSpec.scala

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ class ConfigSpec extends AkkaSpec(ConfigFactory.defaultReference(ActorSystem.fin
6363
{
6464
c.getString("type") must equal("Dispatcher")
6565
c.getString("executor") must equal("fork-join-executor")
66-
c.getInt("mailbox-capacity") must equal(-1)
67-
c.getMilliseconds("mailbox-push-timeout-time") must equal(10 * 1000)
68-
c.getString("mailbox-type") must be("")
6966
c.getMilliseconds("shutdown-timeout") must equal(1 * 1000)
7067
c.getInt("throughput") must equal(5)
7168
c.getMilliseconds("throughput-deadline-time") must equal(0)
@@ -134,6 +131,18 @@ class ConfigSpec extends AkkaSpec(ConfigFactory.defaultReference(ActorSystem.fin
134131
io.getInt("default-backlog") must be(ioExtSettings.defaultBacklog)
135132
}
136133
}
134+
135+
{
136+
val c = config.getConfig("akka.actor.default-mailbox")
137+
138+
// general mailbox config
139+
140+
{
141+
c.getInt("mailbox-capacity") must equal(1000)
142+
c.getMilliseconds("mailbox-push-timeout-time") must equal(10 * 1000)
143+
c.getString("mailbox-type") must be("akka.dispatch.UnboundedMailbox")
144+
}
145+
}
137146
}
138147
}
139148
}

akka-actor-tests/src/test/scala/akka/dispatch/MailboxConfigSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ object CustomMailboxSpec {
209209
}
210210
}
211211

212-
class MyMailbox(owner: ActorRef) extends QueueBasedMessageQueue with UnboundedMessageQueueSemantics {
212+
class MyMailbox(owner: ActorRef) extends UnboundedQueueBasedMessageQueue {
213213
final val queue = new ConcurrentLinkedQueue[Envelope]()
214214
}
215215
}

akka-actor-tests/src/test/scala/akka/testkit/CallingThreadDispatcherModelSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ object CallingThreadDispatcherModelSpec {
3232
extends MessageDispatcherConfigurator(config, prerequisites) {
3333

3434
private val instance: MessageDispatcher =
35-
new CallingThreadDispatcher(prerequisites, UnboundedMailbox(), false) with MessageDispatcherInterceptor {
35+
new CallingThreadDispatcher(this) with MessageDispatcherInterceptor {
3636
override def id: String = config.getString("id")
3737
}
3838

0 commit comments

Comments
 (0)