ptupitsyn commented on code in PR #2825:
URL: https://github.com/apache/ignite-3/pull/2825#discussion_r1395689486


##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientPrimaryReplicaTracker.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.client.handler;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.ignite.internal.catalog.events.CatalogEvent;
+import org.apache.ignite.internal.catalog.events.CatalogEventParameters;
+import org.apache.ignite.internal.catalog.events.DropTableEventParameters;
+import org.apache.ignite.internal.event.EventListener;
+import org.apache.ignite.internal.event.EventParameters;
+import org.apache.ignite.internal.event.EventProducer;
+import org.apache.ignite.internal.hlc.HybridClock;
+import org.apache.ignite.internal.hlc.HybridTimestamp;
+import org.apache.ignite.internal.lang.NodeStoppingException;
+import org.apache.ignite.internal.placementdriver.PlacementDriver;
+import org.apache.ignite.internal.placementdriver.ReplicaMeta;
+import org.apache.ignite.internal.placementdriver.event.PrimaryReplicaEvent;
+import 
org.apache.ignite.internal.placementdriver.event.PrimaryReplicaEventParameters;
+import org.apache.ignite.internal.replicator.TablePartitionId;
+import org.apache.ignite.internal.table.IgniteTablesInternal;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Primary partition replica tracker. Shared by all instances of {@link 
ClientInboundMessageHandler}.
+ *
+ * <p>Keeps up-to-date lists of primary replicas by partition for every table, 
avoiding expensive placement driver calls in most cases.
+ */
+public class ClientPrimaryReplicaTracker implements 
EventListener<EventParameters> {
+    private static final int LRU_CHECK_FREQ_MILLIS = 60 * 60 * 1000;
+
+    private final ConcurrentHashMap<Integer, Holder> primaryReplicas = new 
ConcurrentHashMap<>();
+
+    private final AtomicLong updateCount = new AtomicLong();
+
+    private final PlacementDriver placementDriver;
+
+    private final IgniteTablesInternal igniteTables;
+
+    private final HybridClock clock;
+
+    private final AtomicLong lruCheckTime = new AtomicLong(0);
+
+    private final EventProducer<CatalogEvent, CatalogEventParameters> 
catalogEventProducer;
+
+    /**
+     * Constructor.
+     *
+     * @param placementDriver Placement driver.
+     * @param igniteTables Ignite tables.
+     * @param clock Hybrid clock.
+     */
+    public ClientPrimaryReplicaTracker(
+            PlacementDriver placementDriver,
+            IgniteTablesInternal igniteTables,
+            EventProducer<CatalogEvent, CatalogEventParameters> 
catalogEventProducer,
+            HybridClock clock) {
+        this.placementDriver = placementDriver;
+        this.igniteTables = igniteTables;
+        this.catalogEventProducer = catalogEventProducer;
+        this.clock = clock;
+    }
+
+    /**
+     * Gets primary replicas by partition for the table.
+     *
+     * @param tableId Table ID.
+     * @return Primary replicas for the table, or null when not yet known.
+     */
+    public CompletableFuture<List<ReplicaHolder>> primaryReplicasAsync(int 
tableId) {
+        return primaryReplicas.compute(tableId, (id, hld) -> {
+            if (hld == null || hld.replicas.isCompletedExceptionally()) {
+                return new Holder(initReplicasForTableAsync(id));
+            }
+
+            hld.lastAccessTime = System.currentTimeMillis();
+            return hld;
+        }).replicas;
+    }
+
+    long updateCount() {
+        return updateCount.get();
+    }
+
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    void start() {
+        placementDriver.listen(PrimaryReplicaEvent.PRIMARY_REPLICA_ELECTED, 
(EventListener) this);
+        catalogEventProducer.listen(CatalogEvent.TABLE_DROP, (EventListener) 
this);
+    }
+
+    @SuppressWarnings({"rawtypes", "unchecked"})
+    void stop() {
+        catalogEventProducer.removeListener(CatalogEvent.TABLE_DROP, 
(EventListener) this);
+        
placementDriver.removeListener(PrimaryReplicaEvent.PRIMARY_REPLICA_ELECTED, 
(EventListener) this);
+        primaryReplicas.clear();
+    }
+
+    private CompletableFuture<List<ReplicaHolder>> 
initReplicasForTableAsync(Integer tableId) {
+        try {
+            // Initially, request all primary replicas for the table.
+            // Then keep them updated via PRIMARY_REPLICA_ELECTED events.
+            return igniteTables
+                    .tableAsync(tableId)

Review Comment:
   Thank you, good point. I've added catalog later and did not notice that 
`partitions` is available from there.



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