Skip to content

Commit c1cff1c

Browse files
authored
Data Converters now have method deserializing all the Payloads at once (#1726)
Issue #1345
1 parent 1e49493 commit c1cff1c

File tree

10 files changed

+84
-61
lines changed

10 files changed

+84
-61
lines changed

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
buildscript {
22
ext {
33
palantirGitVersionVersion = "${JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_11) ? '0.15.0' : '0.13.0'}"
4-
kotlinVersion = "${project.hasProperty("edgeDepsTest") ? '1.8.10' : (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16) ? '1.5.32' : '1.4.32')}"
4+
kotlinVersion = "${project.hasProperty("edgeDepsTest") ? '1.8.20' : (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_16) ? '1.5.32' : '1.4.32')}"
55
}
66
}
77

@@ -10,14 +10,14 @@ plugins {
1010
id 'org.cadixdev.licenser' version '0.6.1'
1111
id 'com.palantir.git-version' version "${palantirGitVersionVersion}" apply false
1212
id 'io.github.gradle-nexus.publish-plugin' version '1.3.0'
13-
id 'com.diffplug.spotless' version '6.17.0' apply false
13+
id 'com.diffplug.spotless' version '6.18.0' apply false
1414
id 'com.github.nbaztec.coveralls-jacoco' version "1.2.15" apply false
1515

1616
// id 'org.jetbrains.kotlin.jvm' version '1.4.32'
1717
// id 'org.jetbrains.kotlin.jvm' version '1.5.32'
1818
// id 'org.jetbrains.kotlin.jvm' version '1.6.21'
1919
// id 'org.jetbrains.kotlin.jvm' version '1.7.22'
20-
// id 'org.jetbrains.kotlin.jvm' version '1.8.10'
20+
// id 'org.jetbrains.kotlin.jvm' version '1.8.20'
2121
id 'org.jetbrains.kotlin.jvm' version "${kotlinVersion}" apply false
2222
id 'base'
2323
}
@@ -30,7 +30,7 @@ allprojects {
3030

3131
ext {
3232
// Platforms
33-
grpcVersion = '1.54.0' // [1.38.0,) Needed for io.grpc.protobuf.services.HealthStatusManager
33+
grpcVersion = '1.54.1' // [1.38.0,) Needed for io.grpc.protobuf.services.HealthStatusManager
3434
jacksonVersion = '2.14.2' // [2.9.0,)
3535
// we don't upgrade to 1.10.x because it requires kotlin 1.6. Users may use 1.10.x in their environments though.
3636
micrometerVersion = project.hasProperty("edgeDepsTest") ? '1.10.5' : '1.9.9' // [1.0.0,)
@@ -50,7 +50,7 @@ ext {
5050

5151
gsonVersion = '2.10.1' // [2.0,)
5252

53-
jsonPathVersion = '2.7.0' // compileOnly
53+
jsonPathVersion = '2.8.0' // compileOnly
5454

5555
cronUtilsVersion = '9.2.1' // for test server only
5656

gradle/jacoco.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ subprojects {
5151
apply plugin: 'jacoco'
5252

5353
jacoco {
54-
toolVersion = "0.8.8"
54+
toolVersion = "0.8.9"
5555
}
5656

5757
jacocoTestReport {

temporal-sdk/src/main/java/io/temporal/common/converter/CodecDataConverter.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ public <T> T fromPayloads(
175175
.fromPayloads(index, content, valueType, valueGenericType);
176176
}
177177

178+
@Override
179+
public Object[] fromPayloads(
180+
Optional<Payloads> content, Class<?>[] parameterTypes, Type[] genericParameterTypes)
181+
throws DataConverterException {
182+
if (content.isPresent()) {
183+
content = Optional.of(decodePayloads(content.get()));
184+
}
185+
return ConverterUtils.withContext(dataConverter, serializationContext)
186+
.fromPayloads(content, parameterTypes, genericParameterTypes);
187+
}
188+
178189
@Override
179190
@Nonnull
180191
public Failure exceptionToFailure(@Nonnull Throwable throwable) {

temporal-sdk/src/main/java/io/temporal/common/converter/DataConverter.java

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,12 @@ <T> T fromPayload(Payload payload, Class<T> valueClass, Type valueType)
9999
Optional<Payloads> toPayloads(Object... values) throws DataConverterException;
100100

101101
/**
102-
* Implements conversion of an array of values of different types. Useful for deserializing
103-
* arguments of function invocations.
102+
* Implements conversion of a single {@link Payload} from the serialized {@link Payloads}.
104103
*
105104
* @param index index of the value in the payloads
106105
* @param content serialized value to convert to Java objects.
107-
* @param valueType type of the value stored in the content
108-
* @param valueGenericType generic type of the value stored in the content
106+
* @param valueType type of the value stored in the {@code content}
107+
* @param valueGenericType generic type of the value stored in the {@code content}
109108
* @return converted Java object
110109
* @throws DataConverterException if conversion of the data passed as parameter failed for any
111110
* reason.
@@ -115,32 +114,23 @@ <T> T fromPayloads(
115114
throws DataConverterException;
116115

117116
/**
118-
* Instantiate an appropriate Java Exception from a serialized Failure object. The default
119-
* implementation delegates the conversion process to an instance of {@link FailureConverter},
120-
* using this data converter for payload decoding.
117+
* Implements conversion of the whole {@code content} {@link Payloads} into an array of values of
118+
* different types.
121119
*
122-
* @param failure Failure protobuf object to deserialize into an exception
123-
* @throws NullPointerException if failure is null
124-
*/
125-
@Nonnull
126-
TemporalFailure failureToException(@Nonnull Failure failure);
127-
128-
/**
129-
* Serialize an existing Throwable object into a Failure object. The default implementation
130-
* delegates the conversion process to an instance of {@link FailureConverter}, using this data
131-
* converter for payload encoding.
120+
* <p>Implementation note<br>
121+
* This method is expected to return an array of the same length as {@code parameterTypes}. If
122+
* {@code content} has not enough {@link Payload} elements, this method provides default
123+
* instances.
132124
*
133-
* @param throwable a Throwable object to serialize into a Failure protobuf object
134-
* @throws NullPointerException if throwable is null
125+
* @param content serialized value to convert to Java objects.
126+
* @param parameterTypes types of the values stored in the @code content}
127+
* @param genericParameterTypes generic types of the values stored in the {@code content}
128+
* @return array if converted Java objects
129+
* @throws DataConverterException if conversion of the data passed as parameter failed for any
130+
* reason.
135131
*/
136-
@Nonnull
137-
Failure exceptionToFailure(@Nonnull Throwable throwable);
138-
139-
static Object[] arrayFromPayloads(
140-
DataConverter converter,
141-
Optional<Payloads> content,
142-
Class<?>[] parameterTypes,
143-
Type[] genericParameterTypes)
132+
default Object[] fromPayloads(
133+
Optional<Payloads> content, Class<?>[] parameterTypes, Type[] genericParameterTypes)
144134
throws DataConverterException {
145135
if (parameterTypes != null
146136
&& (genericParameterTypes == null
@@ -152,8 +142,8 @@ static Object[] arrayFromPayloads(
152142
+ Arrays.toString(genericParameterTypes));
153143
}
154144

155-
int length = parameterTypes.length;
156-
Object[] result = new Object[length];
145+
int totalLength = parameterTypes.length;
146+
Object[] result = new Object[totalLength];
157147
if (!content.isPresent()) {
158148
// Return defaults for all the parameters
159149
for (int i = 0; i < parameterTypes.length; i++) {
@@ -169,12 +159,34 @@ static Object[] arrayFromPayloads(
169159
if (i >= count) {
170160
result[i] = Defaults.defaultValue((Class<?>) gt);
171161
} else {
172-
result[i] = converter.fromPayload(payloads.getPayloads(i), pt, gt);
162+
result[i] = this.fromPayload(payloads.getPayloads(i), pt, gt);
173163
}
174164
}
175165
return result;
176166
}
177167

168+
/**
169+
* Instantiate an appropriate Java Exception from a serialized Failure object. The default
170+
* implementation delegates the conversion process to an instance of {@link FailureConverter},
171+
* using this data converter for payload decoding.
172+
*
173+
* @param failure Failure protobuf object to deserialize into an exception
174+
* @throws NullPointerException if failure is null
175+
*/
176+
@Nonnull
177+
TemporalFailure failureToException(@Nonnull Failure failure);
178+
179+
/**
180+
* Serialize an existing Throwable object into a Failure object. The default implementation
181+
* delegates the conversion process to an instance of {@link FailureConverter}, using this data
182+
* converter for payload encoding.
183+
*
184+
* @param throwable a Throwable object to serialize into a Failure protobuf object
185+
* @throws NullPointerException if throwable is null
186+
*/
187+
@Nonnull
188+
Failure exceptionToFailure(@Nonnull Throwable throwable);
189+
178190
/**
179191
* A correct implementation of this interface should have a fully functional "contextless"
180192
* implementation. Temporal SDK will call this method when a knowledge of the context exists, but
@@ -197,4 +209,18 @@ static Object[] arrayFromPayloads(
197209
default DataConverter withContext(@Nonnull SerializationContext context) {
198210
return this;
199211
}
212+
213+
/**
214+
* @deprecated use {@link DataConverter#fromPayloads(int, Optional, Class, Type)}. This is an SDK
215+
* implementation detail and never was expected to be exposed to users.
216+
*/
217+
@Deprecated
218+
static Object[] arrayFromPayloads(
219+
DataConverter converter,
220+
Optional<Payloads> content,
221+
Class<?>[] parameterTypes,
222+
Type[] genericParameterTypes)
223+
throws DataConverterException {
224+
return converter.fromPayloads(content, parameterTypes, genericParameterTypes);
225+
}
200226
}

temporal-sdk/src/main/java/io/temporal/internal/activity/ActivityTaskExecutors.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,8 @@ ActivityInboundCallsInterceptor createRootInboundInterceptor() {
200200

201201
@Override
202202
Object[] provideArgs(Optional<Payloads> input, DataConverter dataConverterWithActivityContext) {
203-
return DataConverter.arrayFromPayloads(
204-
dataConverterWithActivityContext,
205-
input,
206-
method.getParameterTypes(),
207-
method.getGenericParameterTypes());
203+
return dataConverterWithActivityContext.fromPayloads(
204+
input, method.getParameterTypes(), method.getGenericParameterTypes());
208205
}
209206

210207
@Override

temporal-sdk/src/main/java/io/temporal/internal/sync/POJOWorkflowImplementationFactory.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -306,11 +306,8 @@ public Optional<Payloads> execute(Header header, Optional<Payloads> input)
306306
throws CanceledFailure, WorkflowExecutionException {
307307

308308
Object[] args =
309-
DataConverter.arrayFromPayloads(
310-
dataConverterWithWorkflowContext,
311-
input,
312-
workflowMethod.getParameterTypes(),
313-
workflowMethod.getGenericParameterTypes());
309+
dataConverterWithWorkflowContext.fromPayloads(
310+
input, workflowMethod.getParameterTypes(), workflowMethod.getGenericParameterTypes());
314311
Preconditions.checkNotNull(workflowInvoker, "initialize not called");
315312
WorkflowInboundCallsInterceptor.WorkflowOutput result =
316313
workflowInvoker.execute(new WorkflowInboundCallsInterceptor.WorkflowInput(header, args));

temporal-sdk/src/main/java/io/temporal/internal/sync/QueryDispatcher.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,8 @@ public Optional<Payloads> handleQuery(String queryName, Optional<Payloads> input
8080
args = new Object[] {new EncodedValues(input, dataConverterWithWorkflowContext)};
8181
} else {
8282
args =
83-
DataConverter.arrayFromPayloads(
84-
dataConverterWithWorkflowContext,
85-
input,
86-
handler.getArgTypes(),
87-
handler.getGenericArgTypes());
83+
dataConverterWithWorkflowContext.fromPayloads(
84+
input, handler.getArgTypes(), handler.getGenericArgTypes());
8885
}
8986
Object result =
9087
inboundCallsInterceptor

temporal-sdk/src/main/java/io/temporal/internal/sync/SignalDispatcher.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,8 @@ public void handleSignal(String signalName, Optional<Payloads> input, long event
8989
} else {
9090
try {
9191
args =
92-
DataConverter.arrayFromPayloads(
93-
dataConverterWithWorkflowContext,
94-
input,
95-
handler.getArgTypes(),
96-
handler.getGenericArgTypes());
92+
dataConverterWithWorkflowContext.fromPayloads(
93+
input, handler.getArgTypes(), handler.getGenericArgTypes());
9794
} catch (DataConverterException e) {
9895
logSerializationException(signalName, eventId, e);
9996
return;

temporal-sdk/src/test/java/io/temporal/common/converter/JsonDataConverterTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ public void AdditionalInputArgumentsAreIgnored() throws NoSuchMethodException {
117117
Optional<Payloads> data =
118118
converter.toPayloads(1234, struct1, "a string", list, "an extra string :o!!!");
119119
Object[] deserializedArguments =
120-
DataConverter.arrayFromPayloads(
121-
converter, data, m.getParameterTypes(), m.getGenericParameterTypes());
120+
converter.fromPayloads(data, m.getParameterTypes(), m.getGenericParameterTypes());
122121
assertEquals(4, deserializedArguments.length);
123122
assertEquals(1234, (int) deserializedArguments[0]);
124123
assertEquals(struct1, deserializedArguments[1]);
@@ -136,8 +135,7 @@ public void MissingInputArgumentsArePopulatedWithDefaultValues() throws NoSuchMe
136135
Optional<Payloads> data = converter.toPayloads(1);
137136
@SuppressWarnings("unchecked")
138137
Object[] deserializedArguments =
139-
DataConverter.arrayFromPayloads(
140-
converter, data, m.getParameterTypes(), m.getGenericParameterTypes());
138+
converter.fromPayloads(data, m.getParameterTypes(), m.getGenericParameterTypes());
141139
assertEquals(5, deserializedArguments.length);
142140
assertEquals(1, (int) deserializedArguments[0]);
143141
assertEquals(null, deserializedArguments[1]);

temporal-spring-boot-autoconfigure-alpha/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
description = '''Spring Boot AutoConfigure for Temporal Java SDK'''
22

33
ext {
4-
otelVersion = '1.24.0'
4+
otelVersion = '1.25.0'
55
otShimVersion = "${otelVersion}-alpha"
66
}
77

0 commit comments

Comments
 (0)