ibessonov commented on code in PR #4769: URL: https://github.com/apache/ignite-3/pull/4769#discussion_r1875759090
########## modules/distribution-zones/src/main/java/org/apache/ignite/internal/distributionzones/rebalance/RebalanceMinimumRequiredTimeProviderImpl.java: ########## @@ -0,0 +1,294 @@ +/* + * 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.distributionzones.rebalance; + +import static java.lang.Math.min; +import static java.util.Collections.emptyMap; +import static java.util.stream.Collectors.toMap; +import static org.apache.ignite.internal.distributionzones.rebalance.RebalanceUtil.PENDING_ASSIGNMENTS_PREFIX_BYTES; +import static org.apache.ignite.internal.distributionzones.rebalance.RebalanceUtil.PENDING_CHANGE_TRIGGER_PREFIX_BYTES; +import static org.apache.ignite.internal.distributionzones.rebalance.RebalanceUtil.STABLE_ASSIGNMENTS_PREFIX_BYTES; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.Map; +import java.util.NavigableMap; +import java.util.Set; +import java.util.TreeMap; +import org.apache.ignite.internal.catalog.Catalog; +import org.apache.ignite.internal.catalog.CatalogService; +import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor; +import org.apache.ignite.internal.catalog.descriptors.CatalogZoneDescriptor; +import org.apache.ignite.internal.lang.ByteArray; +import org.apache.ignite.internal.metastorage.Entry; +import org.apache.ignite.internal.metastorage.MetaStorageManager; +import org.apache.ignite.internal.partitiondistribution.Assignments; +import org.apache.ignite.internal.replicator.TablePartitionId; +import org.apache.ignite.internal.util.ByteUtils; +import org.apache.ignite.internal.util.Cursor; + +/** + * {@link RebalanceMinimumRequiredTimeProvider} implementation for the current implementation of assignments. Assumes that each table has + * its own assignments, but assignments within a zone are still somehow coordinated. + */ +public class RebalanceMinimumRequiredTimeProviderImpl implements RebalanceMinimumRequiredTimeProvider { + private final MetaStorageManager metaStorageManager; + private final CatalogService catalogService; + + /** + * Constructor. + */ + public RebalanceMinimumRequiredTimeProviderImpl(MetaStorageManager metaStorageManager, CatalogService catalogService) { + this.metaStorageManager = metaStorageManager; + this.catalogService = catalogService; + } + + @Override + public long minimumRequiredTime() { + // Use the same revision to read all the data, in order to guarantee consistency of data. + long appliedRevision = metaStorageManager.appliedRevision(); + + // Ignore the real safe time, having a time associated with revision is enough. Also, acquiring real safe time would be + // unnecessarily more complicated due to handling of possible data races. + long metaStorageSafeTime = metaStorageManager.timestampByRevisionLocally(appliedRevision).longValue(); + + long minTimestamp = metaStorageSafeTime; + + Map<Integer, Map<Integer, Assignments>> stableAssignments = readAssignments(STABLE_ASSIGNMENTS_PREFIX_BYTES, appliedRevision); + Map<Integer, Map<Integer, Assignments>> pendingAssignments = readAssignments(PENDING_ASSIGNMENTS_PREFIX_BYTES, appliedRevision); + + Map<Integer, Long> pendingChangeTriggerRevisions = readPendingChangeTriggerRevisions( + PENDING_CHANGE_TRIGGER_PREFIX_BYTES, + appliedRevision + ); + + int earliestCatalogVersion = catalogService.earliestCatalogVersion(); + int latestCatalogVersion = catalogService.latestCatalogVersion(); + + Map<Integer, Integer> tableIdToZoneIdMap = tableIdToZoneIdMap(earliestCatalogVersion, latestCatalogVersion); + Map<Integer, NavigableMap<Long, CatalogZoneDescriptor>> allZonesByTimestamp = allZonesByTimestamp( + earliestCatalogVersion, + latestCatalogVersion + ); + Map<Integer, NavigableMap<Long, CatalogZoneDescriptor>> allZonesByRevision = allZonesByRevision(allZonesByTimestamp); + Map<Integer, Long> zoneDeletionTimestamps = zoneDeletionTimestamps(earliestCatalogVersion, latestCatalogVersion); + + for (Map.Entry<Integer, Integer> entry : tableIdToZoneIdMap.entrySet()) { + Integer tableId = entry.getKey(); + Integer zoneId = entry.getValue(); + + NavigableMap<Long, CatalogZoneDescriptor> zoneDescriptors = allZonesByTimestamp.get(zoneId); + int zonePartitions = zoneDescriptors.lastEntry().getValue().partitions(); + + Long pendingChangeTriggerKey = pendingChangeTriggerRevisions.get(tableId); + + // +-1 here ir required for 2 reasons: + // - we need timestamp right before deletion, if zone is deleted, thus we must subtract 1; + // - we need a "metaStorageSafeTime" if zone is not deleted, without any subtractions. + long latestTimestamp = zoneDeletionTimestamps.getOrDefault(zoneId, metaStorageSafeTime + 1) - 1; + + long zoneRevision = pendingChangeTriggerKey == null Review Comment: I should add such test and assert what we want to see, if it doesn't exist yer. My assumption is - we should be able see the earliest catalog descriptor for a zone. -- 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