sanpwc commented on code in PR #2825:
URL: https://github.com/apache/ignite-3/pull/2825#discussion_r1395236305
##########
modules/client-handler/src/integrationTest/java/org/apache/ignite/client/handler/TestServer.java:
##########
@@ -133,7 +134,8 @@ ClientHandlerModule start(TestInfo testInfo) {
authenticationManager(),
new HybridClockImpl(),
new AlwaysSyncedSchemaSyncService(),
- mock(CatalogService.class)
+ mock(CatalogService.class),
+ mock(PlacementDriver.class)
Review Comment:
Just in case, there's
org.apache.ignite.internal.placementdriver.TestPlacementDriver that is
sometimes better and easier to use than mocked one.
##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java:
##########
@@ -177,6 +182,7 @@ public ClientHandlerModule(
this.clock = clock;
this.schemaSyncService = schemaSyncService;
this.catalogService = catalogService;
+ this.primaryReplicaTracker = new
ClientPrimaryReplicaTracker(placementDriver, igniteTables, catalogService,
clock);
Review Comment:
Why do we need both igniteTables and catalogService as tracker parameters?
##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java:
##########
@@ -204,6 +212,7 @@ public void start() {
@Override
public void stop() throws Exception {
metricManager.unregisterSource(metrics);
Review Comment:
It's not relevant to current proposal, but why there's no busyLock and
stopGuard?
##########
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.
Review Comment:
There are 4 parameters actually.
##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientHandlerModule.java:
##########
@@ -204,6 +212,7 @@ public void start() {
@Override
public void stop() throws Exception {
metricManager.unregisterSource(metrics);
Review Comment:
Do we need network in order to `metricManager.unregisterSource(metrics);` or
it's a local operation?
##########
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:
Why do you need tableAsync()? TableId is available though catalog.
Partitions is a zone attribute
`org.apache.ignite.internal.catalog.descriptors.CatalogZoneDescriptor#partitions`
where corresponding zoneId is also available in
org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor#zoneId.
Thus you probably need catalog but not table and especially InternalTable.
##########
modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientInboundMessageHandler.java:
##########
@@ -690,21 +689,23 @@ private void processOperation(ChannelHandlerContext ctx,
ClientMessageUnpacker i
}
private void writeFlags(ClientMessagePacker out, ChannelHandlerContext
ctx) {
- boolean assignmentChanged =
partitionAssignmentChanged.compareAndSet(true, false);
-
- if (assignmentChanged && LOG.isInfoEnabled()) {
- LOG.info("Partition assignment changed, notifying client
[connectionId=" + connectionId + ", remoteAddress="
+ // Notify the client about primary replica change that happened for
ANY table since the last request.
+ // We can't assume that the client only uses uses a particular table
(e.g. the one present in the replica tracker), because
+ // the client can be connected to multiple nodes.
+ long localUpdateCount = primaryReplicaUpdateCount.get();
+ long updateCount = primaryReplicaTracker.updateCount();
+ boolean primaryReplicasChanged = localUpdateCount < updateCount
+ && primaryReplicaUpdateCount.compareAndSet(localUpdateCount,
updateCount);
+
+ if (primaryReplicasChanged && LOG.isInfoEnabled()) {
+ LOG.info("Partition primary replica changed, notifying client
[connectionId=" + connectionId + ", remoteAddress="
+ ctx.channel().remoteAddress() + ']');
}
- var flags = ResponseFlags.getFlags(assignmentChanged);
+ var flags = ResponseFlags.getFlags(primaryReplicasChanged);
Review Comment:
Why not to send primaryReplicaUpdateCount backwards in order to prevent
schema notifications reordering?
--
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]