Skip to content

Commit 095018a

Browse files
Isolate gRPC code (#195)
* Add InvocationId to Syscalls * Rename GrpcContextDataProvider to RestateContextDataProvider * Add helper method to get poll input stream entry * Rename RestateGrpcServer to RestateEndpoint * Introduce RpcHandler. This is the well defined interface we can target for code-generation * Add ability to register close callback on state machine, to get notified when the state machine is closed. * Refactor existing gRPC adapter code, encapsulating it into GrpcUnaryRpcHandler. Use the GrpcUnaryRpcHandler in RestateEndpoint
1 parent f27c278 commit 095018a

21 files changed

+726
-701
lines changed

gradlew.bat

Lines changed: 92 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,92 +1,92 @@
1-
@rem
2-
@rem Copyright 2015 the original author or authors.
3-
@rem
4-
@rem Licensed under the Apache License, Version 2.0 (the "License");
5-
@rem you may not use this file except in compliance with the License.
6-
@rem You may obtain a copy of the License at
7-
@rem
8-
@rem https://www.apache.org/licenses/LICENSE-2.0
9-
@rem
10-
@rem Unless required by applicable law or agreed to in writing, software
11-
@rem distributed under the License is distributed on an "AS IS" BASIS,
12-
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
@rem See the License for the specific language governing permissions and
14-
@rem limitations under the License.
15-
@rem
16-
17-
@if "%DEBUG%"=="" @echo off
18-
@rem ##########################################################################
19-
@rem
20-
@rem Gradle startup script for Windows
21-
@rem
22-
@rem ##########################################################################
23-
24-
@rem Set local scope for the variables with windows NT shell
25-
if "%OS%"=="Windows_NT" setlocal
26-
27-
set DIRNAME=%~dp0
28-
if "%DIRNAME%"=="" set DIRNAME=.
29-
@rem This is normally unused
30-
set APP_BASE_NAME=%~n0
31-
set APP_HOME=%DIRNAME%
32-
33-
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
34-
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35-
36-
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37-
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38-
39-
@rem Find java.exe
40-
if defined JAVA_HOME goto findJavaFromJavaHome
41-
42-
set JAVA_EXE=java.exe
43-
%JAVA_EXE% -version >NUL 2>&1
44-
if %ERRORLEVEL% equ 0 goto execute
45-
46-
echo.
47-
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48-
echo.
49-
echo Please set the JAVA_HOME variable in your environment to match the
50-
echo location of your Java installation.
51-
52-
goto fail
53-
54-
:findJavaFromJavaHome
55-
set JAVA_HOME=%JAVA_HOME:"=%
56-
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57-
58-
if exist "%JAVA_EXE%" goto execute
59-
60-
echo.
61-
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62-
echo.
63-
echo Please set the JAVA_HOME variable in your environment to match the
64-
echo location of your Java installation.
65-
66-
goto fail
67-
68-
:execute
69-
@rem Setup the command line
70-
71-
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72-
73-
74-
@rem Execute Gradle
75-
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76-
77-
:end
78-
@rem End local scope for the variables with windows NT shell
79-
if %ERRORLEVEL% equ 0 goto mainEnd
80-
81-
:fail
82-
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83-
rem the _cmd.exe /c_ return code!
84-
set EXIT_CODE=%ERRORLEVEL%
85-
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86-
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87-
exit /b %EXIT_CODE%
88-
89-
:mainEnd
90-
if "%OS%"=="Windows_NT" endlocal
91-
92-
:omega
1+
@rem
2+
@rem Copyright 2015 the original author or authors.
3+
@rem
4+
@rem Licensed under the Apache License, Version 2.0 (the "License");
5+
@rem you may not use this file except in compliance with the License.
6+
@rem You may obtain a copy of the License at
7+
@rem
8+
@rem https://www.apache.org/licenses/LICENSE-2.0
9+
@rem
10+
@rem Unless required by applicable law or agreed to in writing, software
11+
@rem distributed under the License is distributed on an "AS IS" BASIS,
12+
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
@rem See the License for the specific language governing permissions and
14+
@rem limitations under the License.
15+
@rem
16+
17+
@if "%DEBUG%"=="" @echo off
18+
@rem ##########################################################################
19+
@rem
20+
@rem Gradle startup script for Windows
21+
@rem
22+
@rem ##########################################################################
23+
24+
@rem Set local scope for the variables with windows NT shell
25+
if "%OS%"=="Windows_NT" setlocal
26+
27+
set DIRNAME=%~dp0
28+
if "%DIRNAME%"=="" set DIRNAME=.
29+
@rem This is normally unused
30+
set APP_BASE_NAME=%~n0
31+
set APP_HOME=%DIRNAME%
32+
33+
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
34+
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
35+
36+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
37+
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
38+
39+
@rem Find java.exe
40+
if defined JAVA_HOME goto findJavaFromJavaHome
41+
42+
set JAVA_EXE=java.exe
43+
%JAVA_EXE% -version >NUL 2>&1
44+
if %ERRORLEVEL% equ 0 goto execute
45+
46+
echo.
47+
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
48+
echo.
49+
echo Please set the JAVA_HOME variable in your environment to match the
50+
echo location of your Java installation.
51+
52+
goto fail
53+
54+
:findJavaFromJavaHome
55+
set JAVA_HOME=%JAVA_HOME:"=%
56+
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
57+
58+
if exist "%JAVA_EXE%" goto execute
59+
60+
echo.
61+
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
62+
echo.
63+
echo Please set the JAVA_HOME variable in your environment to match the
64+
echo location of your Java installation.
65+
66+
goto fail
67+
68+
:execute
69+
@rem Setup the command line
70+
71+
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
72+
73+
74+
@rem Execute Gradle
75+
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
76+
77+
:end
78+
@rem End local scope for the variables with windows NT shell
79+
if %ERRORLEVEL% equ 0 goto mainEnd
80+
81+
:fail
82+
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83+
rem the _cmd.exe /c_ return code!
84+
set EXIT_CODE=%ERRORLEVEL%
85+
if %EXIT_CODE% equ 0 set EXIT_CODE=1
86+
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
87+
exit /b %EXIT_CODE%
88+
89+
:mainEnd
90+
if "%OS%"=="Windows_NT" endlocal
91+
92+
:omega

sdk-common/src/main/java/dev/restate/sdk/common/syscalls/Syscalls.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import com.google.protobuf.ByteString;
1212
import com.google.protobuf.MessageLite;
13+
import dev.restate.sdk.common.InvocationId;
1314
import dev.restate.sdk.common.TerminalException;
1415
import io.grpc.Context;
1516
import io.grpc.MethodDescriptor;
@@ -41,6 +42,8 @@ static Syscalls current() {
4142
+ Thread.currentThread().getName());
4243
}
4344

45+
InvocationId invocationId();
46+
4447
/**
4548
* @return true if it's inside a side effect block.
4649
*/
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH
2+
//
3+
// This file is part of the Restate Java SDK,
4+
// which is released under the MIT license.
5+
//
6+
// You can find a copy of the license in file LICENSE in the root
7+
// directory of this repository or package, or at
8+
// https://github.com/restatedev/sdk-java/blob/main/LICENSE
9+
package dev.restate.sdk.core;
10+
11+
import com.google.protobuf.ByteString;
12+
import com.google.protobuf.MessageLite;
13+
import dev.restate.sdk.common.InvocationId;
14+
import dev.restate.sdk.common.TerminalException;
15+
import dev.restate.sdk.common.syscalls.DeferredResult;
16+
import dev.restate.sdk.common.syscalls.EnterSideEffectSyscallCallback;
17+
import dev.restate.sdk.common.syscalls.ExitSideEffectSyscallCallback;
18+
import dev.restate.sdk.common.syscalls.SyscallCallback;
19+
import io.grpc.MethodDescriptor;
20+
import java.time.Duration;
21+
import java.util.Map;
22+
import java.util.concurrent.Executor;
23+
import java.util.function.Function;
24+
25+
class ExecutorSwitchingSyscalls implements SyscallsInternal {
26+
27+
private final SyscallsInternal syscalls;
28+
private final Executor syscallsExecutor;
29+
30+
ExecutorSwitchingSyscalls(SyscallsInternal syscalls, Executor syscallsExecutor) {
31+
this.syscalls = syscalls;
32+
this.syscallsExecutor = syscallsExecutor;
33+
}
34+
35+
@Override
36+
public <T extends MessageLite> void pollInput(
37+
Function<ByteString, T> mapper, SyscallCallback<DeferredResult<T>> callback) {
38+
syscallsExecutor.execute(() -> syscalls.pollInput(mapper, callback));
39+
}
40+
41+
@Override
42+
public <T extends MessageLite> void writeOutput(T value, SyscallCallback<Void> callback) {
43+
syscallsExecutor.execute(() -> syscalls.writeOutput(value, callback));
44+
}
45+
46+
@Override
47+
public void writeOutput(TerminalException throwable, SyscallCallback<Void> callback) {
48+
syscallsExecutor.execute(() -> syscalls.writeOutput(throwable, callback));
49+
}
50+
51+
@Override
52+
public void get(String name, SyscallCallback<DeferredResult<ByteString>> callback) {
53+
syscallsExecutor.execute(() -> syscalls.get(name, callback));
54+
}
55+
56+
@Override
57+
public void clear(String name, SyscallCallback<Void> callback) {
58+
syscallsExecutor.execute(() -> syscalls.clear(name, callback));
59+
}
60+
61+
@Override
62+
public void set(String name, ByteString value, SyscallCallback<Void> callback) {
63+
syscallsExecutor.execute(() -> syscalls.set(name, value, callback));
64+
}
65+
66+
@Override
67+
public void sleep(Duration duration, SyscallCallback<DeferredResult<Void>> callback) {
68+
syscallsExecutor.execute(() -> syscalls.sleep(duration, callback));
69+
}
70+
71+
@Override
72+
public <T, R> void call(
73+
MethodDescriptor<T, R> methodDescriptor,
74+
T parameter,
75+
SyscallCallback<DeferredResult<R>> callback) {
76+
syscallsExecutor.execute(() -> syscalls.call(methodDescriptor, parameter, callback));
77+
}
78+
79+
@Override
80+
public <T> void backgroundCall(
81+
MethodDescriptor<T, ?> methodDescriptor,
82+
T parameter,
83+
Duration delay,
84+
SyscallCallback<Void> requestCallback) {
85+
syscallsExecutor.execute(
86+
() -> syscalls.backgroundCall(methodDescriptor, parameter, delay, requestCallback));
87+
}
88+
89+
@Override
90+
public void enterSideEffectBlock(EnterSideEffectSyscallCallback callback) {
91+
syscallsExecutor.execute(() -> syscalls.enterSideEffectBlock(callback));
92+
}
93+
94+
@Override
95+
public void exitSideEffectBlock(ByteString toWrite, ExitSideEffectSyscallCallback callback) {
96+
syscallsExecutor.execute(() -> syscalls.exitSideEffectBlock(toWrite, callback));
97+
}
98+
99+
@Override
100+
public void exitSideEffectBlockWithTerminalException(
101+
TerminalException toWrite, ExitSideEffectSyscallCallback callback) {
102+
syscallsExecutor.execute(
103+
() -> syscalls.exitSideEffectBlockWithTerminalException(toWrite, callback));
104+
}
105+
106+
@Override
107+
public void awakeable(SyscallCallback<Map.Entry<String, DeferredResult<ByteString>>> callback) {
108+
syscallsExecutor.execute(() -> syscalls.awakeable(callback));
109+
}
110+
111+
@Override
112+
public void resolveAwakeable(
113+
String id, ByteString payload, SyscallCallback<Void> requestCallback) {
114+
syscallsExecutor.execute(() -> syscalls.resolveAwakeable(id, payload, requestCallback));
115+
}
116+
117+
@Override
118+
public void rejectAwakeable(String id, String reason, SyscallCallback<Void> requestCallback) {
119+
syscallsExecutor.execute(() -> syscalls.rejectAwakeable(id, reason, requestCallback));
120+
}
121+
122+
@Override
123+
public <T> void resolveDeferred(
124+
DeferredResult<T> deferredToResolve, SyscallCallback<Void> callback) {
125+
syscallsExecutor.execute(() -> syscalls.resolveDeferred(deferredToResolve, callback));
126+
}
127+
128+
@Override
129+
public String getFullyQualifiedMethodName() {
130+
// We can read this from another thread
131+
return syscalls.getFullyQualifiedMethodName();
132+
}
133+
134+
@Override
135+
public InvocationState getInvocationState() {
136+
// We can read this from another thread
137+
return syscalls.getInvocationState();
138+
}
139+
140+
@Override
141+
public InvocationId invocationId() {
142+
// This is immutable once set
143+
return syscalls.invocationId();
144+
}
145+
146+
@Override
147+
public boolean isInsideSideEffect() {
148+
// We can read this from another thread
149+
return syscalls.isInsideSideEffect();
150+
}
151+
152+
@Override
153+
public void close() {
154+
syscallsExecutor.execute(syscalls::close);
155+
}
156+
157+
@Override
158+
public void fail(Throwable cause) {
159+
syscallsExecutor.execute(() -> syscalls.fail(cause));
160+
}
161+
}

0 commit comments

Comments
 (0)