Skip to content

Commit 257eeec

Browse files
committed
Introduce Task monad
This commit introduces Task, a data structure that represents a recipe, or program, for producing a value of type T (or failing with an exception). It is similar in semantics to RunnableGraph[T], but intended as first-class building block. It has the following properties: - A task can have resources associated to it, which are guaranteed to be released if the task is cancelled or fails - Tasks can be forked so multiple ones can run concurrently - Such forked tasks can be cancelled A Task can be created from a RunnableGraph which has a KillSwitch, by connecting a Source and a Sink through a KillSwitch, or by direct lambda functions.
1 parent 0186524 commit 257eeec

File tree

24 files changed

+2187
-2
lines changed

24 files changed

+2187
-2
lines changed

actor/src/main/scala/org/apache/pekko/japi/function/Function.scala

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ import scala.annotation.nowarn
2525
@nowarn("msg=@SerialVersionUID has no effect")
2626
@SerialVersionUID(1L)
2727
@FunctionalInterface
28-
trait Function[-T, +R] extends java.io.Serializable {
28+
trait Function[-T, +R] extends java.io.Serializable { outer =>
2929
@throws(classOf[Exception])
3030
def apply(param: T): R
31+
32+
/** Returns a function that applies [fn] to the result of this function. */
33+
def andThen[U](fn: Function[R, U]): Function[T, U] = new Function[T, U] {
34+
override def apply(param: T) = fn(outer.apply(param))
35+
}
36+
37+
/** Returns a Scala function representation for this function. */
38+
def toScala[T1 <: T, R1 >: R]: T1 => R1 = t => apply(t)
3139
}
3240

3341
object Function {
@@ -63,6 +71,21 @@ trait Function2[-T1, -T2, +R] extends java.io.Serializable {
6371
trait Procedure[-T] extends java.io.Serializable {
6472
@throws(classOf[Exception])
6573
def apply(param: T): Unit
74+
75+
def toScala[T1 <: T]: T1 => Unit = t => apply(t)
76+
}
77+
78+
/**
79+
* A BiProcedure is like a BiFunction, but it doesn't produce a return value.
80+
* `Serializable` is needed to be able to grab line number for Java 8 lambdas.
81+
* Supports throwing `Exception` in the apply, which the `java.util.function.Consumer` counterpart does not.
82+
*/
83+
@nowarn("msg=@SerialVersionUID has no effect")
84+
@SerialVersionUID(1L)
85+
@FunctionalInterface
86+
trait BiProcedure[-T1, -T2] extends java.io.Serializable {
87+
@throws(classOf[Exception])
88+
def apply(t1: T1, t2: T2): Unit
6689
}
6790

6891
/**
@@ -77,6 +100,9 @@ trait Effect extends java.io.Serializable {
77100

78101
@throws(classOf[Exception])
79102
def apply(): Unit
103+
104+
/** Returns a Scala function representation for this function. */
105+
def toScala: () => Unit = () => apply()
80106
}
81107

82108
/**
@@ -98,11 +124,19 @@ trait Predicate[-T] extends java.io.Serializable {
98124
@nowarn("msg=@SerialVersionUID has no effect")
99125
@SerialVersionUID(1L)
100126
@FunctionalInterface
101-
trait Creator[+T] extends Serializable {
127+
trait Creator[+T] extends Serializable { outer =>
102128

103129
/**
104130
* This method must return a different instance upon every call.
105131
*/
106132
@throws(classOf[Exception])
107133
def create(): T
134+
135+
/** Returns a function that applies [fn] to the result of this function. */
136+
def andThen[U](fn: Function[T, U]): Creator[U] = new Creator[U] {
137+
override def create() = fn(outer.create())
138+
}
139+
140+
/** Returns a Scala function representation for this function. */
141+
def toScala[T1 >: T]: () => T1 = () => create()
108142
}
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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.task.javadsl;
19+
20+
import org.apache.pekko.stream.StreamTest;
21+
import org.apache.pekko.testkit.PekkoJUnitActorSystemResource;
22+
import org.apache.pekko.testkit.PekkoSpec;
23+
import org.apache.pekko.stream.Materializer;
24+
import org.apache.pekko.Done;
25+
26+
import org.junit.ClassRule;
27+
import org.junit.Test;
28+
29+
import org.apache.pekko.japi.function.Creator;
30+
import org.apache.pekko.stream.javadsl.Sink;
31+
import org.apache.pekko.stream.javadsl.Source;
32+
33+
import java.util.concurrent.TimeUnit;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.atomic.AtomicLong;
36+
37+
import java.util.Optional;
38+
import java.time.Duration;
39+
40+
import static org.junit.Assert.assertEquals;
41+
import static org.junit.Assert.assertTrue;
42+
43+
public class TaskTest extends StreamTest {
44+
private final Runtime runtime = Runtime.create(Materializer.createMaterializer(system));
45+
46+
public TaskTest() {
47+
super(actorSystemResource);
48+
}
49+
50+
@ClassRule
51+
public static PekkoJUnitActorSystemResource actorSystemResource =
52+
new PekkoJUnitActorSystemResource("TaskTest", PekkoSpec.testConf());
53+
54+
private <T> T run(Task<T> task) throws Throwable {
55+
return runtime.run(task.timeout(Duration.ofSeconds(2)));
56+
}
57+
58+
@Test
59+
public void can_run_task_from_lambda() throws Throwable {
60+
assertEquals("Hello", run(Task.run(() -> "Hello")));
61+
}
62+
63+
@Test
64+
public void can_map() throws Throwable {
65+
assertEquals(25, run(Task.run(() -> "25").map(Integer::parseInt)).intValue());
66+
}
67+
68+
@Test
69+
public void can_flatMap_to_run() throws Throwable {
70+
assertEquals(
71+
25, run(Task.run(() -> "25").flatMap(s -> Task.run(() -> Integer.parseInt(s)))).intValue());
72+
}
73+
74+
@Test
75+
public void can_zipPar_two_tasks() throws Throwable {
76+
Task<String> task =
77+
Task.run(
78+
() -> {
79+
return "Hello";
80+
});
81+
assertEquals("HelloHello", run(task.zipPar(task, (s1, s2) -> s1 + s2)));
82+
}
83+
84+
@Test
85+
public void zipPar_interrupts_first_on_error_in_second() throws Throwable {
86+
AtomicLong check = new AtomicLong();
87+
Task<String> task1 =
88+
Task.succeed("A").delayed(Duration.ofMillis(100)).before(Task.run(check::incrementAndGet));
89+
Task<String> task2 = Task.fail(new RuntimeException("simulated failure"));
90+
org.junit.Assert.assertThrows(
91+
RuntimeException.class, () -> run(task1.zipPar(task2, (a, b) -> a + b)));
92+
assertEquals(0, check.get());
93+
}
94+
95+
@Test
96+
public void zipPar_interrupts_second_on_error_in_first() throws Throwable {
97+
AtomicLong check = new AtomicLong();
98+
Task<String> task1 =
99+
Task.succeed("A").delayed(Duration.ofMillis(100)).before(Task.run(check::incrementAndGet));
100+
Task<String> task2 = Task.fail(new RuntimeException("simulated failure"));
101+
org.junit.Assert.assertThrows(
102+
RuntimeException.class, () -> run(task2.zipPar(task1, (a, b) -> a + b)));
103+
assertEquals(0, check.get());
104+
}
105+
106+
@Test
107+
public void can_interrupt_forked_task() throws Throwable {
108+
AtomicLong check = new AtomicLong();
109+
Task<Long> task = Task.run(() -> check.incrementAndGet()).delayed(Duration.ofMillis(100));
110+
run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().map(cancelled -> "cancelled")));
111+
assertEquals(0, check.get());
112+
}
113+
114+
@Test(expected = InterruptedException.class)
115+
public void joining_interrupted_fiber_yields_exception() throws Throwable {
116+
Task<Long> task = Task.succeed(42L).delayed(Duration.ofMillis(100));
117+
run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().flatMap(cancelled -> fiber.join())));
118+
}
119+
120+
@Test
121+
public void can_run_graph() throws Throwable {
122+
assertEquals(
123+
Optional.of("hello"), run(Task.connect(Source.single("hello"), Sink.headOption())));
124+
}
125+
126+
@Test
127+
public void can_interrupt_graph() throws Throwable {
128+
AtomicLong check = new AtomicLong();
129+
assertEquals(
130+
Done.getInstance(),
131+
run(
132+
Task.connect(
133+
Source.tick(Duration.ofMillis(1), Duration.ofMillis(1), ""),
134+
Sink.foreach(s -> check.incrementAndGet()))
135+
.forkDaemon()
136+
.flatMap(fiber -> fiber.interrupt())));
137+
Thread.sleep(100);
138+
assertTrue(check.get() < 10);
139+
}
140+
141+
@Test
142+
public void resource_is_acquired_and_released() throws Throwable {
143+
AtomicLong check = new AtomicLong();
144+
Resource<Long> res =
145+
Resource.acquireRelease(
146+
Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet()));
147+
Task<Long> task = res.use(i -> Task.succeed(i));
148+
assertEquals(1L, run(task).longValue());
149+
assertEquals(0L, check.get());
150+
}
151+
152+
@Test
153+
public void resource_is_released_on_failure() throws Throwable {
154+
AtomicLong check = new AtomicLong();
155+
Resource<Long> res =
156+
Resource.acquireRelease(
157+
Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet()));
158+
Task<Long> task = res.use(i -> Task.fail(new RuntimeException("Simulated failure")));
159+
try {
160+
run(task);
161+
} catch (Exception ignored) {
162+
}
163+
assertEquals(0L, check.get());
164+
}
165+
166+
@Test
167+
public void resource_closes_AutoCloseable() throws Throwable {
168+
AtomicLong created = new AtomicLong();
169+
AtomicLong closed = new AtomicLong();
170+
Resource<AutoCloseable> res =
171+
Resource.autoCloseable(
172+
Task.run(
173+
() -> {
174+
created.incrementAndGet();
175+
return () -> closed.incrementAndGet();
176+
}));
177+
run(res.use(ac -> Task.done));
178+
assertEquals(1L, created.get());
179+
assertEquals(1L, closed.get());
180+
}
181+
182+
@Test
183+
public void resource_is_released_when_interrupted() throws Throwable {
184+
AtomicLong check = new AtomicLong();
185+
AtomicLong started = new AtomicLong();
186+
187+
Resource<Long> res =
188+
Resource.acquireRelease(
189+
Task.run(
190+
() -> {
191+
return check.incrementAndGet();
192+
}),
193+
i ->
194+
Task.run(
195+
() -> {
196+
return check.decrementAndGet();
197+
}));
198+
199+
Task<Long> task =
200+
res.use(
201+
i ->
202+
Task.run(() -> started.incrementAndGet())
203+
.before(Clock.sleep(Duration.ofMillis(100))));
204+
run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().delayed(Duration.ofMillis(50))));
205+
206+
assertEquals(0L, check.get());
207+
assertEquals(1L, started.get());
208+
}
209+
210+
@Test
211+
public void resource_can_fork() throws Throwable {
212+
AtomicLong check = new AtomicLong();
213+
Resource<Long> res =
214+
Resource.acquireRelease(Task.run(() -> check.incrementAndGet()), i -> Task.done);
215+
Task<Long> task = res.fork().use(fiber -> fiber.join());
216+
run(task);
217+
assertEquals(1L, check.get());
218+
}
219+
220+
@Test
221+
public void resource_is_released_when_fork_is_interrupted() throws Throwable {
222+
AtomicLong check = new AtomicLong();
223+
Resource<Long> res =
224+
Resource.acquireRelease(
225+
Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet()));
226+
Task<Done> task = res.fork().use(fiber -> fiber.interrupt());
227+
run(task);
228+
assertEquals(0L, check.get());
229+
}
230+
231+
@Test
232+
public void resource_is_released_when_fork_is_completed() throws Throwable {
233+
AtomicLong check = new AtomicLong();
234+
Resource<Long> res =
235+
Resource.acquireRelease(
236+
Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet()));
237+
Task<Long> task = res.fork().use(fiber -> fiber.join());
238+
run(task);
239+
assertEquals(0L, check.get());
240+
}
241+
242+
@Test
243+
public void can_create_and_complete_promise() throws Throwable {
244+
Task<Integer> task =
245+
Promise.<Integer>make()
246+
.flatMap(
247+
promise ->
248+
promise
249+
.await()
250+
.forkDaemon()
251+
.flatMap(fiber -> promise.succeed(42).andThen(fiber.join())));
252+
assertEquals(42, run(task).intValue());
253+
}
254+
255+
@Test
256+
public void can_race_two_tasks() throws Throwable {
257+
Task<Integer> task1 = Task.succeed(0).delayed(Duration.ofMillis(100));
258+
Task<Integer> task2 = Task.succeed(42);
259+
Task<Integer> task = Task.raceAll(task1, task2);
260+
assertEquals(42, run(task).intValue());
261+
}
262+
263+
@Test
264+
public void can_race_task_with_never() throws Throwable {
265+
Task<Integer> task1 = Task.succeed(42).delayed(Duration.ofMillis(100));
266+
Task<Integer> task2 = Task.never();
267+
Task<Integer> task = Task.raceAll(task1, task2);
268+
assertEquals(42, run(task).intValue());
269+
}
270+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.task.javadsl;
19+
20+
import java.time.Duration;
21+
import java.time.Instant;
22+
23+
import org.apache.pekko.task.GetRuntimeDef$;
24+
import org.apache.pekko.task.ClockDef$;
25+
import org.apache.pekko.task.TaskDef;
26+
import org.apache.pekko.Done;
27+
28+
public class Clock {
29+
public static final Task<Long> nanoTime = new Task<>(ClockDef$.MODULE$.nanoTime());
30+
public static final Task<Instant> now = new Task<>(ClockDef$.MODULE$.now());
31+
32+
public static Task<Done> sleep(Duration duration) {
33+
return new Task<>(ClockDef$.MODULE$.sleep(duration)).asDone();
34+
}
35+
}

0 commit comments

Comments
 (0)