Berkof commented on a change in pull request #191:
URL: https://github.com/apache/ignite-3/pull/191#discussion_r675104949



##########
File path: 
modules/client/src/main/java/org/apache/ignite/internal/client/ReliableChannel.java
##########
@@ -0,0 +1,621 @@
+/*
+ * 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;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+import java.util.function.BiFunction;
+import java.util.function.Consumer;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+
+import org.apache.ignite.client.IgniteClientAuthenticationException;
+import org.apache.ignite.client.IgniteClientConfiguration;
+import org.apache.ignite.client.IgniteClientConnectionException;
+import org.apache.ignite.client.IgniteClientException;
+import org.apache.ignite.internal.client.io.ClientConnectionMultiplexer;
+import 
org.apache.ignite.internal.client.io.netty.NettyClientConnectionMultiplexer;
+
+/**
+ * Communication channel with failover and partition awareness.
+ */
+public final class ReliableChannel implements AutoCloseable {
+    /** Do nothing helper function. */
+    private static final Consumer<Integer> DO_NOTHING = (v) -> {};
+
+    /** Channel factory. */
+    private final BiFunction<ClientChannelConfiguration, 
ClientConnectionMultiplexer, ClientChannel> chFactory;
+
+    /** Client channel holders for each configured address. */
+    private volatile List<ClientChannelHolder> channels;
+
+    /** Index of the current channel. */
+    private volatile int curChIdx = -1;
+
+    /** Client configuration. */
+    private final IgniteClientConfiguration clientCfg;
+
+    /** Node channels. */
+    private final Map<UUID, ClientChannelHolder> nodeChannels = new 
ConcurrentHashMap<>();
+
+    /** Channels reinit was scheduled. */
+    private final AtomicBoolean scheduledChannelsReinit = new AtomicBoolean();
+
+    /** Channel is closed. */
+    private volatile boolean closed;
+
+    /** Fail (disconnect) listeners. */
+    private final ArrayList<Runnable> chFailLsnrs = new ArrayList<>();
+
+    /** Guard channels and curChIdx together. */
+    private final ReadWriteLock curChannelsGuard = new 
ReentrantReadWriteLock();
+
+    /** Connection manager. */
+    private final ClientConnectionMultiplexer connMgr;
+
+    /** Cache addresses returned by {@code ThinClientAddressFinder}. */
+    private volatile String[] prevHostAddrs;
+
+    /**
+     * Constructor.
+     *
+     * @param chFactory Channel factory.
+     * @param clientCfg Client config.
+     */
+    ReliableChannel(BiFunction<ClientChannelConfiguration, 
ClientConnectionMultiplexer, ClientChannel> chFactory,
+            IgniteClientConfiguration clientCfg) {
+        if (chFactory == null)
+            throw new NullPointerException("chFactory");
+
+        if (clientCfg == null)
+            throw new NullPointerException("clientCfg");
+
+        this.clientCfg = clientCfg;
+        this.chFactory = chFactory;
+
+        connMgr = new NettyClientConnectionMultiplexer();
+        connMgr.start();
+    }
+
+    /** {@inheritDoc} */
+    @Override public synchronized void close() {
+        closed = true;
+
+        connMgr.stop();
+
+        List<ClientChannelHolder> holders = channels;
+
+        if (holders != null) {
+            for (ClientChannelHolder hld: holders)
+                hld.close();
+        }
+    }
+
+    /**
+     * Sends request and handles response asynchronously.
+     *
+     * @param opCode Operation code.
+     * @param payloadWriter Payload writer.
+     * @param payloadReader Payload reader.
+     * @param <T> response type.
+     * @return Future for the operation.
+     */
+    public <T> CompletableFuture<T> serviceAsync(
+            int opCode,
+            PayloadWriter payloadWriter,
+            PayloadReader<T> payloadReader
+    ) {
+        CompletableFuture<T> fut = new CompletableFuture<>();
+
+        // Use the only one attempt to avoid blocking async method.
+        handleServiceAsync(fut, opCode, payloadWriter, payloadReader, 1, null);
+
+        return fut;
+    }
+
+    private <T> void handleServiceAsync(final CompletableFuture<T> fut,
+                                        int opCode,
+                                        PayloadWriter payloadWriter,
+                                        PayloadReader<T> payloadReader,
+                                        int attemptsLimit,
+                                        IgniteClientConnectionException 
failure) {
+        ClientChannel ch;
+        // Workaround to store used attempts value within lambda body.
+        var attemptsCnt = new int[1];
+
+        try {
+            ch = applyOnDefaultChannel(channel -> channel, attemptsLimit, v -> 
attemptsCnt[0] = v );
+        } catch (Throwable ex) {

Review comment:
       NL




-- 
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]


Reply via email to