Skip to content

Commit f6dc3c5

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 3d48931 commit f6dc3c5

File tree

19 files changed

+1741
-2
lines changed

19 files changed

+1741
-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: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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.task.RuntimeDef;
30+
import org.apache.pekko.japi.function.Creator;
31+
import org.apache.pekko.stream.javadsl.Sink;
32+
import org.apache.pekko.stream.javadsl.Source;
33+
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.atomic.AtomicLong;
37+
38+
import java.util.Optional;
39+
import java.time.Duration;
40+
41+
import static org.junit.Assert.assertEquals;
42+
import static org.junit.Assert.assertTrue;
43+
44+
public class TaskTest extends StreamTest{
45+
private final RuntimeDef runtime = RuntimeDef.create(Materializer.createMaterializer(system));
46+
47+
public TaskTest() {
48+
super(actorSystemResource);
49+
}
50+
51+
@ClassRule
52+
public static PekkoJUnitActorSystemResource actorSystemResource =
53+
new PekkoJUnitActorSystemResource("TaskTest", PekkoSpec.testConf());
54+
55+
private <T> T run(Task<T> task) throws Exception {
56+
return runtime.runAsync(task).get(2, TimeUnit.SECONDS);
57+
}
58+
59+
@Test
60+
public void can_run_task_from_lambda() throws Exception {
61+
assertEquals("Hello", run(Task.run(() -> "Hello")));
62+
}
63+
64+
@Test
65+
public void can_map() throws Exception {
66+
assertEquals(25, run(Task.run(() -> "25").map(Integer::parseInt)).intValue());
67+
}
68+
69+
@Test
70+
public void can_flatMap_to_run() throws Exception {
71+
assertEquals(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 Exception {
76+
Task<String> task = Task.run(() -> {
77+
return "Hello";
78+
});
79+
assertEquals("HelloHello", run(task.zipPar(task, (s1,s2) -> s1 + s2)));
80+
}
81+
@Test
82+
public void can_interrupt_forked_task() throws Exception {
83+
AtomicLong check = new AtomicLong();
84+
Task<Long> task = Task.run(() -> check.incrementAndGet()).delayed(Duration.ofMillis(100));
85+
run(task.forkDaemon().flatMap(fiber ->
86+
fiber.interrupt().map(cancelled ->
87+
"cancelled"
88+
)
89+
));
90+
assertEquals(0, check.get());
91+
}
92+
93+
@Test(expected=ExecutionException.class)
94+
public void joining_interrupted_fiber_yields_exception() throws Exception {
95+
Task<Long> task = Task.succeed(42L).delayed(Duration.ofMillis(100));
96+
run(task.forkDaemon().flatMap(fiber ->
97+
fiber.interrupt().flatMap(cancelled ->
98+
fiber.join()
99+
)
100+
));
101+
}
102+
103+
@Test
104+
public void can_run_graph() throws Exception {
105+
assertEquals(Optional.of("hello"),
106+
run(Task.connect(Source.single("hello"), Sink.headOption())));
107+
}
108+
109+
@Test
110+
public void can_interrupt_graph() throws Exception {
111+
AtomicLong check = new AtomicLong();
112+
assertEquals(Done.getInstance(), run(
113+
Task.connect(
114+
Source.tick(Duration.ofMillis(1), Duration.ofMillis(1), ""),
115+
Sink.foreach(s -> check.incrementAndGet())
116+
).forkDaemon().flatMap(fiber -> fiber.interrupt())
117+
));
118+
Thread.sleep(100);
119+
assertTrue(check.get() < 10);
120+
}
121+
122+
@Test
123+
public void resource_is_acquired_and_released() throws Exception {
124+
AtomicLong check = new AtomicLong();
125+
Resource<Long> res = Resource.acquireRelease(Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet()));
126+
Task<Long> task = res.use(i -> Task.succeed(i));
127+
assertEquals(1L, run(task).longValue());
128+
assertEquals(0L, check.get());
129+
}
130+
131+
@Test
132+
public void resource_is_released_on_failure() throws Exception {
133+
AtomicLong check = new AtomicLong();
134+
Resource<Long> res = Resource.acquireRelease(Task.run(() -> check.incrementAndGet()), i -> Task.run(() -> check.decrementAndGet()));
135+
Task<Long> task = res.use(i -> Task.fail(new RuntimeException("Simulated failure")));
136+
try { run(task); } catch (Exception ignored) {}
137+
assertEquals(0L, check.get());
138+
}
139+
140+
@Test
141+
public void resource_is_released_when_interrupted() throws Exception {
142+
AtomicLong check = new AtomicLong();
143+
AtomicLong started = new AtomicLong();
144+
145+
Resource<Long> res = Resource.acquireRelease(Task.run(() -> {
146+
return check.incrementAndGet();
147+
}), i -> Task.run(() -> {
148+
return check.decrementAndGet();
149+
}));
150+
151+
Task<Long> task = res.use(i ->
152+
Task.run(() -> started.incrementAndGet())
153+
.before(Clock.sleep(Duration.ofMillis(100))));
154+
run(task.forkDaemon().flatMap(fiber -> fiber.interrupt().delayed(Duration.ofMillis(50))));
155+
156+
assertEquals(0L, check.get());
157+
assertEquals(1L, started.get());
158+
}
159+
160+
@Test
161+
public void can_create_and_complete_promise() throws Exception {
162+
Task<Integer> task = Promise.<Integer>make().flatMap(promise ->
163+
promise.await().forkDaemon().flatMap(fiber ->
164+
promise.succeed(42).andThen(fiber.join())
165+
)
166+
);
167+
assertEquals(42, run(task).intValue());
168+
}
169+
170+
@Test
171+
public void can_race_two_tasks() throws Exception {
172+
Task<Integer> task1 = Task.succeed(0).delayed(Duration.ofMillis(100));
173+
Task<Integer> task2 = Task.succeed(42);
174+
Task<Integer> task = Task.raceAll(task1, task2);
175+
assertEquals(42, run(task).intValue());
176+
}
177+
178+
}
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.util.ArrayList;
21+
import java.util.function.Function;
22+
23+
/**
24+
* Functional helpers for collections. Prefixed with "c" so they don't conflict with local methods
25+
* of the same name (in Java 8's limited name resolution)
26+
*/
27+
public class CollectionHelpers {
28+
public static <T, U> ArrayList<U> cmap(
29+
Iterable<? extends T> src, Function<? super T, ? extends U> fn) {
30+
ArrayList<U> list = new ArrayList<>();
31+
for (T t : src) {
32+
list.add(fn.apply(t));
33+
}
34+
return list;
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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.task.FiberDef;
21+
import org.apache.pekko.task.AwaitDef;
22+
import org.apache.pekko.task.InterruptDef;
23+
import org.apache.pekko.Done;
24+
25+
/**
26+
* A fiber represents the ongoing execution of a Task, eventually resulting in a value T or failing
27+
* with an exception.
28+
*/
29+
public class Fiber<T> {
30+
private final FiberDef<T> definition;
31+
32+
public Fiber(FiberDef<T> definition) {
33+
this.definition = definition;
34+
}
35+
36+
/** Returns a Task that will complete when this fiber does. */
37+
public Task<T> join() {
38+
return new Task<>(new AwaitDef<>(definition.result()));
39+
}
40+
41+
/**
42+
* Returns a Task that will interrupt this fiber, causing its execution to stop. The task will
43+
* complete with Done when this fiber has fully stopped.
44+
*/
45+
public Task<Done> interrupt() {
46+
return new Task<>(new InterruptDef<>(definition)).asDone();
47+
}
48+
}

0 commit comments

Comments
 (0)