ptupitsyn commented on code in PR #4438:
URL: https://github.com/apache/ignite-3/pull/4438#discussion_r1774599750
##########
modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientComputeJobPacker.java:
##########
@@ -17,13 +17,30 @@
package org.apache.ignite.internal.client.proto;
+import static
org.apache.ignite.internal.client.proto.ComputeJobType.MARSHALLED_CUSTOM;
+import static
org.apache.ignite.internal.client.proto.ComputeJobType.MARSHALLED_POJO;
+import static
org.apache.ignite.internal.client.proto.ComputeJobType.MARSHALLED_TUPLE;
+import static org.apache.ignite.internal.client.proto.ComputeJobType.NATIVE;
+import static
org.apache.ignite.internal.client.proto.pojo.PojoConverter.toTuple;
+
+import java.util.Arrays;
+import java.util.Set;
+import java.util.stream.Collectors;
import
org.apache.ignite.internal.binarytuple.inlineschema.TupleWithSchemaMarshalling;
+import org.apache.ignite.internal.client.proto.pojo.PojoConversionException;
import org.apache.ignite.marshalling.Marshaller;
+import org.apache.ignite.marshalling.MarshallingException;
+import org.apache.ignite.sql.ColumnType;
import org.apache.ignite.table.Tuple;
import org.jetbrains.annotations.Nullable;
/** Packs job arguments and results. */
public final class ClientComputeJobPacker {
+
Review Comment:
```suggestion
```
##########
modules/api/src/main/java/org/apache/ignite/compute/JobDescriptor.java:
##########
@@ -100,12 +104,16 @@ public static <T, R> Builder<T, R> builder(Class<?
extends ComputeJob<T, R>> job
return new Builder<>(jobClass.getName());
}
+ public @Nullable Marshaller<T, byte[]> argumentMarshaller() {
+ return argumentMarshaller;
+ }
+
public @Nullable Marshaller<R, byte[]> resultMarshaller() {
return resultMarshaller;
}
- public @Nullable Marshaller<T, byte[]> argumentMarshaller() {
- return argumentMarshaller;
+ public @Nullable Class<?> resultClass() {
Review Comment:
So we can infer job argument type from the overridden method, but we can't
do it for the result because of type erasure, and we have to ask the user to
provide the class explicitly - is my understanding correct?
##########
modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/pojo/PojoConverter.java:
##########
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.client.proto.pojo;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.math.BigDecimal;
+import java.time.Duration;
+import java.time.Instant;
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.LocalTime;
+import java.time.Period;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.UUID;
+import org.apache.ignite.table.Tuple;
+import org.jetbrains.annotations.Nullable;
+
+/** Converts POJO to Tuple and back. */
+public class PojoConverter {
+
+ /**
+ * Converts POJO to Tuple. Supports public non-static fields, public
non-static getter methods starting with "get" (or "is" for boolean
+ * type) followed by the capital letter.
+ *
+ * @param obj POJO to convert.
+ * @return Tuple with columns corresponding to supported fields of the
POJO.
+ * @throws PojoConversionException If conversion failed.
+ */
+ public static Tuple toTuple(Object obj) throws PojoConversionException {
+ Class<?> clazz = obj.getClass();
+
+ // TODO https://issues.apache.org/jira/browse/IGNITE-23092
+ if (clazz.getSuperclass() != Object.class) {
+ throw new PojoConversionException("Can't convert subclasses");
+ }
+
+ Map<String, Getter> getters = new HashMap<>();
+
+ for (Field field : clazz.getDeclaredFields()) {
+ if (isSupportedType(field.getType())) {
+ int modifiers = field.getModifiers();
+ if (Modifier.isPublic(modifiers) &&
!Modifier.isStatic(modifiers)) {
+ getters.put(field.getName(), () -> field.get(obj));
+ }
+ }
+ }
+
+ for (Method method : clazz.getDeclaredMethods()) {
Review Comment:
Not only on the server. I think this code can be shared across client and
server.
##########
modules/api/src/main/java/org/apache/ignite/compute/JobDescriptor.java:
##########
@@ -33,22 +33,26 @@ public class JobDescriptor<T, R> {
private final JobExecutionOptions options;
+ private final @Nullable Marshaller<T, byte[]> argumentMarshaller;
+
private final @Nullable Marshaller<R, byte[]> resultMarshaller;
- private final @Nullable Marshaller<T, byte[]> argumentMarshaller;
+ private final Class<?> resultClass;
Review Comment:
```suggestion
private final Class<R> resultClass;
```
##########
modules/compute/src/main/java/org/apache/ignite/internal/compute/executor/ComputeExecutorImpl.java:
##########
@@ -137,6 +159,41 @@ private static <T, R> Callable<CompletableFuture<R>>
unmarshalExecMarshal(
);
}
+ static <T, R> @Nullable Class<?> getArgumentType(Class<? extends
ComputeJob<T, R>> jobClass) {
+ for (Method method : jobClass.getDeclaredMethods()) {
+ if (method.getParameterCount() == 2
+ && method.getParameterTypes()[0] ==
JobExecutionContext.class
+ && method.getParameterTypes()[1] != Object.class // skip
type erased method
+ && method.getReturnType() == CompletableFuture.class
+ && "executeAsync".equals(method.getName())
+ ) {
+ return method.getParameterTypes()[1];
+ }
+ }
+ return null;
+ }
+
+ private static Object unmarshallPojo(Class<?> actualArgumentType, Tuple
input) {
Review Comment:
```suggestion
private static Object unmarshalPojo(Class<?> actualArgumentType, Tuple
input) {
```
##########
modules/api/src/main/java/org/apache/ignite/compute/JobDescriptor.java:
##########
@@ -33,22 +33,26 @@ public class JobDescriptor<T, R> {
private final JobExecutionOptions options;
+ private final @Nullable Marshaller<T, byte[]> argumentMarshaller;
+
private final @Nullable Marshaller<R, byte[]> resultMarshaller;
- private final @Nullable Marshaller<T, byte[]> argumentMarshaller;
+ private final Class<?> resultClass;
private JobDescriptor(
String jobClassName,
List<DeploymentUnit> units,
JobExecutionOptions options,
@Nullable Marshaller<T, byte[]> argumentMarshaller,
- @Nullable Marshaller<R, byte[]> resultMarshaller
+ @Nullable Marshaller<R, byte[]> resultMarshaller,
+ @Nullable Class<?> resultClass
Review Comment:
```suggestion
@Nullable Class<R> resultClass
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]