rpuch commented on a change in pull request #534: URL: https://github.com/apache/ignite-3/pull/534#discussion_r781127969
########## File path: modules/compute/src/main/java/org/apache/ignite/compute/internal/IgniteComputeImpl.java ########## @@ -0,0 +1,302 @@ +/* + * 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.compute.internal; + +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.compute.internal.util.GenericUtils.getTypeArguments; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Type; +import java.util.Collection; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.apache.ignite.compute.ComputeException; +import org.apache.ignite.compute.ComputeTask; +import org.apache.ignite.compute.IgniteCompute; +import org.apache.ignite.compute.internal.message.ComputeMessagesFactory; +import org.apache.ignite.compute.internal.message.ComputeMessagesType; +import org.apache.ignite.compute.internal.message.ExecuteMessage; +import org.apache.ignite.compute.internal.message.ResultMessage; +import org.apache.ignite.internal.manager.IgniteComponent; +import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.network.ClusterNode; +import org.apache.ignite.network.ClusterService; +import org.apache.ignite.network.MessagingService; +import org.apache.ignite.network.NetworkAddress; +import org.apache.ignite.network.NetworkMessage; +import org.apache.ignite.network.NetworkMessageHandler; +import org.apache.ignite.network.TopologyEventHandler; +import org.jetbrains.annotations.NotNull; + +/** + * Implementation of the compute service. + */ +public class IgniteComputeImpl implements IgniteCompute, IgniteComponent { + private final ClusterService clusterService; + + private final ExecutorService computeExecutor = Executors.newSingleThreadExecutor(); + + private final ComputeMessagesFactory messagesFactory = new ComputeMessagesFactory(); + + private final ConcurrentMap<UUID, Execution<?>> executions = new ConcurrentHashMap<>(); + + public IgniteComputeImpl(ClusterService clusterService) { + this.clusterService = clusterService; + } + + /** {@inheritDoc} */ + @Override + public <T, R, F> F execute(Class<? extends ComputeTask<T, R, F>> clazz, T argument) throws ComputeException { + UUID executionId = UUID.randomUUID(); + + Execution<R> execution = registerExecution(clazz, argument, executionId); + + Collection<ExecutionResult<R>> executionResults = execution.getResult(); + + executions.remove(executionId); + + ComputeTask<T, R, F> computeTask; + try { + Constructor<? extends ComputeTask<T, R, F>> declaredConstructor = clazz.getDeclaredConstructor(); + declaredConstructor.setAccessible(true); + computeTask = declaredConstructor.newInstance(); + } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new ComputeException("Failed to execute reduce: " + e.getMessage(), e); + } + + return computeTask.reduce(executionResults.stream().map(ExecutionResult::result).collect(toList())); + } + + /** {@inheritDoc} */ + @Override + public void start() { + clusterService.messagingService().addMessageHandler(ComputeMessagesType.class, new ComputeNetworkHandler()); + + clusterService.topologyService().addEventHandler(new TopologyHandler()); + } + + /** {@inheritDoc} */ + @Override + public void stop() throws Exception { + IgniteUtils.shutdownAndAwaitTermination(computeExecutor, 5, TimeUnit.SECONDS); + } + + /** + * Creates new execution based on a task, an argument and an id. + * + * @param clazz Task's class. + * @param argument Task's argument. + * @param executionId Execution id. + * @param <T> Argument's type. + * @param <R> Intermediate result's type. + * @param <F> Result's type. + * @return Execution. + */ + @NotNull + private <T, R, F> Execution<R> registerExecution(Class<? extends ComputeTask<T, R, F>> clazz, T argument, UUID executionId) { + Collection<ClusterNode> currentTopology = this.clusterService.topologyService().allMembers(); + + List<String> nodeIds = currentTopology.stream().map(ClusterNode::id).collect(toList()); + + var execution = new Execution<R>(nodeIds); + + Execution<?> prev = executions.putIfAbsent(executionId, execution); + assert prev == null; + + MessagingService messagingService = this.clusterService.messagingService(); + + ExecuteMessage executeMessage = messagesFactory.executeMessage() + .executionId(executionId).className(clazz.getName()).argument(argument).build(); + + currentTopology.forEach(clusterNode -> { + try { + messagingService.send(clusterNode, executeMessage).get(3, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { Review comment: How about separating try/catch block into two: one for `#send()` (which is our own and can throw anything, like some `RuntimeException` due to a programming error), where we'd try to make sure that the execution never gets stuck even if some `IllegalStateException` is thrown, and the second one, for `.get()` (to which we can... hmmm... trust)? -- 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]
