Pochatkin commented on code in PR #3764:
URL: https://github.com/apache/ignite-3/pull/3764#discussion_r1607936979


##########
modules/client/src/main/java/org/apache/ignite/internal/client/table/ClientPartitionManager.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.table;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static 
org.apache.ignite.internal.client.TcpIgniteClient.unpackClusterNode;
+import static 
org.apache.ignite.internal.client.table.ClientTupleSerializer.getPartitionAwarenessProvider;
+
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.client.proto.ClientMessageUnpacker;
+import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.table.partition.HashPartition;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.table.Tuple;
+import org.apache.ignite.table.mapper.Mapper;
+import org.apache.ignite.table.partition.Partition;
+import org.apache.ignite.table.partition.PartitionManager;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Client partition manager implementation.
+ */
+public class ClientPartitionManager implements PartitionManager {
+    private final ClientTable tbl;
+
+    private final Lock lock = new ReentrantLock();
+
+    private final Map<Partition, ClusterNode> cache = new HashMap<>();
+
+    private @Nullable Instant aliveUntil;
+
+    ClientPartitionManager(ClientTable clientTable) {
+        this.tbl = clientTable;
+    }
+
+    @Override
+    public CompletableFuture<ClusterNode> primaryReplicaAsync(Partition 
partition) {
+        if (!(partition instanceof HashPartition)) {
+            throw new IllegalArgumentException("Unsupported partition type: " 
+ partition);
+        }
+
+        ClusterNode clusterNode = getClusterNode(partition);
+
+        if (clusterNode != null) {
+            return completedFuture(clusterNode);
+        }
+
+        return primaryReplicasAsync().thenApply(map -> {
+            lock.lock();
+            try {
+                cache.putAll(map);
+                aliveUntil = Instant.now().plus(1, ChronoUnit.MINUTES);
+                return map.get(partition);
+            } finally {
+                lock.unlock();
+            }
+        });
+    }
+
+    @Override
+    @SuppressWarnings("resource")
+    public CompletableFuture<Map<Partition, ClusterNode>> 
primaryReplicasAsync() {
+        return tbl.channel().serviceAsync(ClientOp.PRIMARY_REPLICAS_GET,
+                w -> w.out().packInt(tbl.tableId()),
+                r -> {
+                    ClientMessageUnpacker in = r.in();
+
+                    if (in.tryUnpackNil()) {
+                        return null;
+                    }
+
+                    int size = in.unpackInt();
+
+                    Map<Partition, ClusterNode> res = new HashMap<>(size);
+
+                    for (int i = 0; i < size; i++) {
+                        int partition = in.unpackInt();
+                        res.put(new HashPartition(partition), 
unpackClusterNode(r));
+                    }
+
+                    return res;
+                });
+    }
+
+    @Override
+    public <K> CompletableFuture<Partition> partitionAsync(K key, Mapper<K> 
mapper) {

Review Comment:
   Done



##########
modules/client/src/main/java/org/apache/ignite/internal/client/table/ClientPartitionManager.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.table;
+
+import static java.util.concurrent.CompletableFuture.completedFuture;
+import static 
org.apache.ignite.internal.client.TcpIgniteClient.unpackClusterNode;
+import static 
org.apache.ignite.internal.client.table.ClientTupleSerializer.getPartitionAwarenessProvider;
+
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.ignite.internal.client.proto.ClientMessageUnpacker;
+import org.apache.ignite.internal.client.proto.ClientOp;
+import org.apache.ignite.internal.table.partition.HashPartition;
+import org.apache.ignite.network.ClusterNode;
+import org.apache.ignite.table.Tuple;
+import org.apache.ignite.table.mapper.Mapper;
+import org.apache.ignite.table.partition.Partition;
+import org.apache.ignite.table.partition.PartitionManager;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Client partition manager implementation.
+ */
+public class ClientPartitionManager implements PartitionManager {
+    private final ClientTable tbl;
+
+    private final Lock lock = new ReentrantLock();
+
+    private final Map<Partition, ClusterNode> cache = new HashMap<>();
+
+    private @Nullable Instant aliveUntil;
+
+    ClientPartitionManager(ClientTable clientTable) {
+        this.tbl = clientTable;
+    }
+
+    @Override
+    public CompletableFuture<ClusterNode> primaryReplicaAsync(Partition 
partition) {
+        if (!(partition instanceof HashPartition)) {
+            throw new IllegalArgumentException("Unsupported partition type: " 
+ partition);
+        }
+
+        ClusterNode clusterNode = getClusterNode(partition);
+
+        if (clusterNode != null) {
+            return completedFuture(clusterNode);
+        }
+
+        return primaryReplicasAsync().thenApply(map -> {
+            lock.lock();
+            try {
+                cache.putAll(map);
+                aliveUntil = Instant.now().plus(1, ChronoUnit.MINUTES);
+                return map.get(partition);
+            } finally {
+                lock.unlock();
+            }
+        });
+    }
+
+    @Override
+    @SuppressWarnings("resource")
+    public CompletableFuture<Map<Partition, ClusterNode>> 
primaryReplicasAsync() {
+        return tbl.channel().serviceAsync(ClientOp.PRIMARY_REPLICAS_GET,
+                w -> w.out().packInt(tbl.tableId()),
+                r -> {
+                    ClientMessageUnpacker in = r.in();
+
+                    if (in.tryUnpackNil()) {
+                        return null;
+                    }
+
+                    int size = in.unpackInt();
+
+                    Map<Partition, ClusterNode> res = new HashMap<>(size);
+
+                    for (int i = 0; i < size; i++) {
+                        int partition = in.unpackInt();
+                        res.put(new HashPartition(partition), 
unpackClusterNode(r));
+                    }
+
+                    return res;
+                });
+    }
+
+    @Override
+    public <K> CompletableFuture<Partition> partitionAsync(K key, Mapper<K> 
mapper) {
+        return getPartition(getPartitionAwarenessProvider(null, mapper, key));
+    }
+
+    @Override
+    public CompletableFuture<Partition> partitionAsync(Tuple key) {

Review Comment:
   Done



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to