tkalkirill commented on code in PR #2787: URL: https://github.com/apache/ignite-3/pull/2787#discussion_r1383156119
########## modules/table/src/main/java/org/apache/ignite/internal/table/distributed/schema/CatalogValidationSchemasSource.java: ########## @@ -0,0 +1,192 @@ +/* + * 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.table.distributed.schema; + +import static java.util.stream.Collectors.toList; + +import java.util.List; +import java.util.Objects; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.function.Predicate; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import org.apache.ignite.internal.catalog.CatalogService; +import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor; +import org.apache.ignite.internal.hlc.HybridTimestamp; +import org.apache.ignite.internal.schema.SchemaManager; + +/** + * An implementation over {@link CatalogService}. + */ +public class CatalogValidationSchemasSource implements ValidationSchemasSource { + private final CatalogService catalogService; + + private final SchemaManager schemaManager; + + private final ConcurrentMap<CatalogVersionsSpan, List<FullTableSchema>> catalogVersionSpansCache = new ConcurrentHashMap<>(); + + // TODO: Remove entries from cache when compacting Catalog https://issues.apache.org/jira/browse/IGNITE-20790 + // TODO: Remove entries from cache when compacting schemas in SchemaManager https://issues.apache.org/jira/browse/IGNITE-20789 + private final ConcurrentMap<CatalogVersionToTableVersionSpan, List<FullTableSchema>> catalogVersionToTableVersionSpansCache + = new ConcurrentHashMap<>(); + + /** Constructor. */ + public CatalogValidationSchemasSource(CatalogService catalogService, SchemaManager schemaManager) { + this.catalogService = catalogService; + this.schemaManager = schemaManager; + } + + @Override + public CompletableFuture<Void> waitForSchemaAvailability(int tableId, int schemaVersion) { + return schemaManager.schemaRegistry(tableId) + .schemaAsync(schemaVersion) + .thenApply(unused -> null); + } + + @Override + public List<FullTableSchema> tableSchemaVersionsBetween(int tableId, HybridTimestamp fromIncluding, HybridTimestamp toIncluding) { + int fromCatalogVersion = catalogService.activeCatalogVersion(fromIncluding.longValue()); + int toCatalogVersion = catalogService.activeCatalogVersion(toIncluding.longValue()); + + return catalogVersionSpansCache.computeIfAbsent( + new CatalogVersionsSpan(tableId, fromCatalogVersion, toCatalogVersion), + key -> tableSchemaVersionsBetweenCatalogVersions(tableId, fromCatalogVersion, toCatalogVersion) + ); + } + + @Override + public List<FullTableSchema> tableSchemaVersionsBetween(int tableId, HybridTimestamp fromIncluding, int toIncluding) { + int fromCatalogVersion = catalogService.activeCatalogVersion(fromIncluding.longValue()); + + return catalogVersionToTableVersionSpansCache.computeIfAbsent( + new CatalogVersionToTableVersionSpan(tableId, fromCatalogVersion, toIncluding), + key -> tableSchemaVersionsBetweenCatalogAndTableVersions(tableId, fromCatalogVersion, toIncluding) + ); + } + + private List<FullTableSchema> tableSchemaVersionsBetweenCatalogVersions(int tableId, int fromCatalogVersion, int toCatalogVersion) { + return tableVersionsBetween(tableId, fromCatalogVersion, toCatalogVersion) + .map(CatalogValidationSchemasSource::fullSchemaFromTableDescriptor) + .collect(toList()); + } + + // It's ok to use Stream as the results of the methods that call this are cached. + private Stream<CatalogTableDescriptor> tableVersionsBetween( + int tableId, + int fromCatalogVersionIncluding, + int toCatalogVersionIncluding + ) { + return IntStream.rangeClosed(fromCatalogVersionIncluding, toCatalogVersionIncluding) + .mapToObj(catalogVersion -> catalogService.table(tableId, catalogVersion)) + .filter(new Predicate<>() { + int prevVersion = Integer.MIN_VALUE; + + @Override + public boolean test(CatalogTableDescriptor tableDescriptor) { + if (tableDescriptor.tableVersion() == prevVersion) { + return false; + } + + assert prevVersion == Integer.MIN_VALUE || tableDescriptor.tableVersion() == prevVersion + 1; + + prevVersion = tableDescriptor.tableVersion(); + + return true; + } + }); + } + + private List<FullTableSchema> tableSchemaVersionsBetweenCatalogAndTableVersions( + int tableId, + int fromCatalogVersion, + int toTableVersion + ) { + return tableVersionsBetween(tableId, fromCatalogVersion, catalogService.latestCatalogVersion()) + .takeWhile(tableDescriptor -> tableDescriptor.tableVersion() <= toTableVersion) + .map(CatalogValidationSchemasSource::fullSchemaFromTableDescriptor) + .collect(toList()); + } + + private static FullTableSchema fullSchemaFromTableDescriptor(CatalogTableDescriptor tableDescriptor) { + return new FullTableSchema( + tableDescriptor.tableVersion(), + tableDescriptor.id(), + tableDescriptor.columns() + ); + } + + private static class CatalogVersionsSpan { Review Comment: ok -- 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]
