ptupitsyn commented on code in PR #2825: URL: https://github.com/apache/ignite-3/pull/2825#discussion_r1400992117
########## modules/client-handler/src/main/java/org/apache/ignite/client/handler/ClientPrimaryReplicaTracker.java: ########## @@ -0,0 +1,327 @@ +/* + * 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 static org.apache.ignite.lang.ErrorGroups.Table.TABLE_NOT_FOUND_ERR; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import org.apache.ignite.internal.catalog.CatalogService; +import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor; +import org.apache.ignite.internal.catalog.events.CatalogEvent; +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.hlc.HybridClock; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.placementdriver.PlacementDriver; +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.distributed.schema.SchemaSyncService; +import org.apache.ignite.lang.TableNotFoundException; +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. + * + * <p>Every "assignment" (set of primary replicas per partition) is identified by a maxStartTime - latest known lease start time. + * + * <p>Assumptions: + * - Primary replicas are not changed often. + * - We do "best effort" partition awareness - it is ok if we don't have the latest primary replicas at some point or don't have them + * at all. What matters is that we have the correct assignment eventually. + * - It is allowed to return incomplete assignment (null for some partitions) - better than nothing. + * - We don't know which tables the client is going to use, so we track a common maxStartTime for all tables. + * + * <p>Tracking logic: + * - Listen to election events from placement driver, update primary replicas. This is the main source of information. + * - When we have not yet received events for all partitions of a certain table, and the client requests the assignment, + * load it from the placement driver. Wait for a limited amount of time (in getPrimaryReplica) and return what we have. + * Don't block the client for too long, it is better to miss the primary than to delay the request. + */ +public class ClientPrimaryReplicaTracker implements EventListener<EventParameters> { + private final ConcurrentHashMap<TablePartitionId, ReplicaHolder> primaryReplicas = new ConcurrentHashMap<>(); + + private final AtomicLong maxStartTime = new AtomicLong(); + + private final PlacementDriver placementDriver; + + private final HybridClock clock; + + private final CatalogService catalogService; + + private final SchemaSyncService schemaSyncService; + + /** + * Constructor. + * + * @param placementDriver Placement driver. + * @param catalogService Catalog. + * @param clock Hybrid clock. + * @param schemaSyncService Schema synchronization service. + */ + public ClientPrimaryReplicaTracker( + PlacementDriver placementDriver, + CatalogService catalogService, + HybridClock clock, + SchemaSyncService schemaSyncService) { + this.placementDriver = placementDriver; + this.catalogService = catalogService; + this.clock = clock; + this.schemaSyncService = schemaSyncService; + } + + /** + * Gets primary replicas by partition for the table. + * + * @param tableId Table ID. + * @param maxStartTime Timestamp. + * @return Primary replicas for the table, or null when not yet known. + */ + public CompletableFuture<PrimaryReplicasResult> primaryReplicasAsync(int tableId, @Nullable Long maxStartTime) { + HybridTimestamp timestamp = clock.now(); + + if (maxStartTime == null) { + maxStartTime = this.maxStartTime.get(); + } else { + // If the client provides an old maxStartTime, ignore it and use the current one. + maxStartTime = Math.max(maxStartTime, this.maxStartTime.get()); + } + + // Check happy path: if we already have all replicas, and maxStartTime > timestamp, return synchronously. Review Comment: > `methodParam.maxStartTime <= local.MaxStartTime` Yes, that's what we check in `primaryReplicasNoWait`. The comment was a bit misleading, I've fixed it. -- 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]
