This is an automated email from the ASF dual-hosted git repository. ppa pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push: new 7286d7acb8 IGNITE-21676 Sql. Move system view definitions to a separate package of a catalog module (#3766) 7286d7acb8 is described below commit 7286d7acb8e61590161a58dc509423db0d5f381e Author: Max Zhuravkov <shh...@gmail.com> AuthorDate: Fri May 24 14:17:21 2024 +0300 IGNITE-21676 Sql. Move system view definitions to a separate package of a catalog module (#3766) --- .../internal/catalog/CatalogManagerImpl.java | 195 +-------------------- .../catalog/CatalogSystemViewProvider.java | 34 ++++ .../catalog/CatalogSystemViewRegistry.java | 52 ++++++ .../systemviews/IndexSystemViewProvider.java | 104 +++++++++++ .../systemviews/SystemViewViewProvider.java | 126 +++++++++++++ .../systemviews/ZonesSystemViewProvider.java | 77 ++++++++ 6 files changed, 397 insertions(+), 191 deletions(-) diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java index eacb159c1c..1844193f6e 100644 --- a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java +++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogManagerImpl.java @@ -20,7 +20,6 @@ package org.apache.ignite.internal.catalog; import static java.util.concurrent.CompletableFuture.allOf; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.CompletableFuture.failedFuture; -import static java.util.stream.Collectors.joining; import static org.apache.ignite.internal.catalog.commands.CatalogUtils.DEFAULT_FILTER; import static org.apache.ignite.internal.catalog.commands.CatalogUtils.DEFAULT_PARTITION_COUNT; import static org.apache.ignite.internal.catalog.commands.CatalogUtils.DEFAULT_REPLICA_COUNT; @@ -28,35 +27,24 @@ import static org.apache.ignite.internal.catalog.commands.CatalogUtils.IMMEDIATE import static org.apache.ignite.internal.catalog.commands.CatalogUtils.INFINITE_TIMER_VALUE; import static org.apache.ignite.internal.catalog.commands.CatalogUtils.clusterWideEnsuredActivationTsSafeForRoReads; import static org.apache.ignite.internal.catalog.commands.CatalogUtils.defaultZoneIdOpt; -import static org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor.CatalogIndexDescriptorType.HASH; -import static org.apache.ignite.internal.type.NativeTypes.BOOLEAN; -import static org.apache.ignite.internal.type.NativeTypes.INT32; -import static org.apache.ignite.internal.type.NativeTypes.STRING; -import static org.apache.ignite.internal.type.NativeTypes.stringOf; import static org.apache.ignite.internal.util.CollectionUtils.nullOrEmpty; import static org.apache.ignite.internal.util.CompletableFutures.nullCompletedFuture; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map.Entry; import java.util.NavigableMap; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentSkipListMap; -import java.util.concurrent.Flow.Publisher; import java.util.function.LongSupplier; import org.apache.ignite.internal.catalog.commands.AlterZoneSetDefaultCommand; import org.apache.ignite.internal.catalog.commands.CreateSchemaCommand; import org.apache.ignite.internal.catalog.commands.CreateZoneCommand; import org.apache.ignite.internal.catalog.commands.StorageProfileParams; -import org.apache.ignite.internal.catalog.descriptors.CatalogHashIndexDescriptor; import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor; import org.apache.ignite.internal.catalog.descriptors.CatalogObjectDescriptor; import org.apache.ignite.internal.catalog.descriptors.CatalogSchemaDescriptor; -import org.apache.ignite.internal.catalog.descriptors.CatalogSortedIndexDescriptor; -import org.apache.ignite.internal.catalog.descriptors.CatalogSystemViewDescriptor; -import org.apache.ignite.internal.catalog.descriptors.CatalogTableColumnDescriptor; import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor; import org.apache.ignite.internal.catalog.descriptors.CatalogZoneDescriptor; import org.apache.ignite.internal.catalog.events.CatalogEvent; @@ -78,12 +66,9 @@ import org.apache.ignite.internal.logger.Loggers; import org.apache.ignite.internal.sql.SqlCommon; import org.apache.ignite.internal.systemview.api.SystemView; import org.apache.ignite.internal.systemview.api.SystemViewProvider; -import org.apache.ignite.internal.systemview.api.SystemViews; import org.apache.ignite.internal.util.ExceptionUtils; import org.apache.ignite.internal.util.IgniteSpinBusyLock; import org.apache.ignite.internal.util.PendingComparableValuesTracker; -import org.apache.ignite.internal.util.SubscriptionUtils; -import org.apache.ignite.internal.util.TransformingIterator; import org.apache.ignite.lang.ErrorGroups.Common; import org.jetbrains.annotations.Nullable; @@ -96,8 +81,6 @@ public class CatalogManagerImpl extends AbstractEventProducer<CatalogEvent, Cata private static final int MAX_RETRY_COUNT = 10; - private static final int SYSTEM_VIEW_STRING_COLUMN_LENGTH = Short.MAX_VALUE; - /** Safe time to wait before new Catalog version activation. */ static final int DEFAULT_DELAY_DURATION = 0; @@ -134,6 +117,8 @@ public class CatalogManagerImpl extends AbstractEventProducer<CatalogEvent, Cata private final LongSupplier partitionIdleSafeTimePropagationPeriodMsSupplier; + private final CatalogSystemViewRegistry catalogSystemViewProvider; + /** Busy lock to stop synchronously. */ private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock(); @@ -157,6 +142,7 @@ public class CatalogManagerImpl extends AbstractEventProducer<CatalogEvent, Cata this.clockService = clockService; this.delayDurationMsSupplier = delayDurationMsSupplier; this.partitionIdleSafeTimePropagationPeriodMsSupplier = partitionIdleSafeTimePropagationPeriodMsSupplier; + this.catalogSystemViewProvider = new CatalogSystemViewRegistry(() -> catalogAt(clockService.nowLong())); } @Override @@ -522,12 +508,7 @@ public class CatalogManagerImpl extends AbstractEventProducer<CatalogEvent, Cata @Override public List<SystemView<?>> systemViews() { - return List.of( - createSystemViewsView(), - createSystemViewColumnsView(), - createZonesView(), - createIndexesView() - ); + return catalogSystemViewProvider.systemViews(); } class OnUpdateHandlerImpl implements OnUpdateHandler { @@ -635,172 +616,4 @@ public class CatalogManagerImpl extends AbstractEventProducer<CatalogEvent, Cata return bulkUpdateEntries; } } - - private SystemView<?> createSystemViewsView() { - Iterable<SchemaAwareDescriptor<CatalogSystemViewDescriptor>> viewData = () -> { - Catalog catalog = catalogAt(clockService.nowLong()); - - return catalog.schemas().stream() - .flatMap(schema -> Arrays.stream(schema.systemViews()) - .map(viewDescriptor -> new SchemaAwareDescriptor<>(viewDescriptor, schema.name())) - ) - .iterator(); - }; - - Publisher<SchemaAwareDescriptor<CatalogSystemViewDescriptor>> viewDataPublisher = SubscriptionUtils.fromIterable(viewData); - - return SystemViews.<SchemaAwareDescriptor<CatalogSystemViewDescriptor>>clusterViewBuilder() - .name("SYSTEM_VIEWS") - .addColumn("ID", INT32, entry -> entry.descriptor.id()) - .addColumn("SCHEMA", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), - entry -> entry.schema) - .addColumn("NAME", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), - entry -> entry.descriptor.name()) - .addColumn("TYPE", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), - entry -> entry.descriptor.systemViewType().name()) - .dataProvider(viewDataPublisher) - .build(); - } - - private SystemView<?> createSystemViewColumnsView() { - Iterable<ParentIdAwareDescriptor<CatalogTableColumnDescriptor>> viewData = () -> { - Catalog catalog = catalogAt(clockService.nowLong()); - - return catalog.schemas().stream() - .flatMap(schema -> Arrays.stream(schema.systemViews())) - .flatMap(viewDescriptor -> viewDescriptor.columns().stream() - .map(columnDescriptor -> new ParentIdAwareDescriptor<>(columnDescriptor, viewDescriptor.id())) - ) - .iterator(); - }; - - Publisher<ParentIdAwareDescriptor<CatalogTableColumnDescriptor>> viewDataPublisher = SubscriptionUtils.fromIterable(viewData); - - return SystemViews.<ParentIdAwareDescriptor<CatalogTableColumnDescriptor>>clusterViewBuilder() - .name("SYSTEM_VIEW_COLUMNS") - .addColumn("VIEW_ID", INT32, entry -> entry.id) - .addColumn("NAME", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.descriptor.name()) - .addColumn("TYPE", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.descriptor.type().name()) - .addColumn("NULLABLE", BOOLEAN, entry -> entry.descriptor.nullable()) - .addColumn("PRECISION", INT32, entry -> entry.descriptor.precision()) - .addColumn("SCALE", INT32, entry -> entry.descriptor.scale()) - .addColumn("LENGTH", INT32, entry -> entry.descriptor.length()) - .dataProvider(viewDataPublisher) - .build(); - } - - private SystemView<?> createZonesView() { - return SystemViews.<ZoneWithDefaultMarker>clusterViewBuilder() - .name("ZONES") - .<String>addColumn("NAME", STRING, wrapper -> wrapper.zone.name()) - .<Integer>addColumn("PARTITIONS", INT32, wrapper -> wrapper.zone.partitions()) - .<Integer>addColumn("REPLICAS", INT32, wrapper -> wrapper.zone.replicas()) - .<Integer>addColumn("DATA_NODES_AUTO_ADJUST_SCALE_UP", INT32, wrapper -> wrapper.zone.dataNodesAutoAdjustScaleUp()) - .<Integer>addColumn("DATA_NODES_AUTO_ADJUST_SCALE_DOWN", INT32, wrapper -> wrapper.zone.dataNodesAutoAdjustScaleDown()) - .<String>addColumn("DATA_NODES_FILTER", STRING, wrapper -> wrapper.zone.filter()) - .<Boolean>addColumn("IS_DEFAULT_ZONE", BOOLEAN, wrapper -> wrapper.isDefault) - .dataProvider(SubscriptionUtils.fromIterable(() -> { - Catalog catalog = catalogAt(clockService.nowLong()); - CatalogZoneDescriptor defaultZone = catalog.defaultZone(); - return new TransformingIterator<>(catalog.zones().iterator(), - (zone) -> new ZoneWithDefaultMarker(zone, defaultZone != null && defaultZone.id() == zone.id())); - } - )) - .build(); - } - - private SystemView<?> createIndexesView() { - Iterable<CatalogAwareDescriptor<CatalogIndexDescriptor>> viewData = () -> { - Catalog catalog = catalogAt(clockService.nowLong()); - - return catalog.indexes().stream() - .filter(index -> index.status().isAlive()) - .map(index -> new CatalogAwareDescriptor<>(index, catalog)) - .iterator(); - }; - - return SystemViews.<CatalogAwareDescriptor<CatalogIndexDescriptor>>clusterViewBuilder() - .name("INDEXES") - .addColumn("INDEX_ID", INT32, entry -> entry.descriptor.id()) - .addColumn("INDEX_NAME", STRING, entry -> entry.descriptor.name()) - .addColumn("TABLE_ID", INT32, entry -> entry.descriptor.tableId()) - .addColumn("TABLE_NAME", STRING, entry -> getTableDescriptor(entry).name()) - .addColumn("SCHEMA_ID", INT32, CatalogManagerImpl::getSchemaId) - .addColumn("SCHEMA_NAME", STRING, entry -> entry.catalog.schema(getSchemaId(entry)).name()) - .addColumn("TYPE", STRING, entry -> entry.descriptor.indexType().name()) - .addColumn("IS_UNIQUE", BOOLEAN, entry -> entry.descriptor.unique()) - .addColumn("COLUMNS", STRING, CatalogManagerImpl::getColumnsString) - .addColumn("STATUS", STRING, entry -> entry.descriptor.status().name()) - .dataProvider(SubscriptionUtils.fromIterable(viewData)) - .build(); - } - - private static int getSchemaId(CatalogAwareDescriptor<CatalogIndexDescriptor> entry) { - return getTableDescriptor(entry).schemaId(); - } - - private static CatalogTableDescriptor getTableDescriptor(CatalogAwareDescriptor<CatalogIndexDescriptor> entry) { - return entry.catalog.table(entry.descriptor.tableId()); - } - - private static String getColumnsString(CatalogAwareDescriptor<CatalogIndexDescriptor> entry) { - return entry.descriptor.indexType() == HASH - ? String.join(", ", ((CatalogHashIndexDescriptor) entry.descriptor).columns()) - : ((CatalogSortedIndexDescriptor) entry.descriptor) - .columns() - .stream() - .map(column -> column.name() + (column.collation().asc() ? " ASC" : " DESC")) - .collect(joining(", ")); - } - - /** Wraps a CatalogZoneDescriptor and a flag indicating whether this zone is the default zone. */ - static class ZoneWithDefaultMarker { - private final CatalogZoneDescriptor zone; - private final boolean isDefault; - - ZoneWithDefaultMarker(CatalogZoneDescriptor zone, boolean isDefault) { - this.zone = zone; - this.isDefault = isDefault; - } - } - - /** - * A container that keeps given descriptor along with name of the schema this - * descriptor belongs to. - */ - private static class SchemaAwareDescriptor<T> { - private final T descriptor; - private final String schema; - - SchemaAwareDescriptor(T descriptor, String schema) { - this.descriptor = descriptor; - this.schema = schema; - } - } - - /** - * A container that keeps given descriptor along with its parent's id. - */ - private static class ParentIdAwareDescriptor<T> { - private final T descriptor; - private final int id; - - ParentIdAwareDescriptor(T descriptor, int id) { - this.descriptor = descriptor; - this.id = id; - } - } - - /** - * A container that keeps given descriptor along with the catalog it belongs to. - */ - private static class CatalogAwareDescriptor<T> { - private final T descriptor; - private final Catalog catalog; - - CatalogAwareDescriptor(T descriptor, Catalog catalog) { - this.descriptor = descriptor; - this.catalog = catalog; - } - } } diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogSystemViewProvider.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogSystemViewProvider.java new file mode 100644 index 0000000000..e59a99c461 --- /dev/null +++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogSystemViewProvider.java @@ -0,0 +1,34 @@ +/* + * 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.catalog; + +import java.util.List; +import java.util.function.Supplier; +import org.apache.ignite.internal.systemview.api.SystemView; + +/** Returns a list of available {@link SystemView} that expose catalog information. */ +public interface CatalogSystemViewProvider { + + /** + * Returns a list of available systems views. + * + * @param catalogSupplier Supplier that returns the current catalog. + * @return List of system views. + */ + List<SystemView<?>> getView(Supplier<Catalog> catalogSupplier); +} diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogSystemViewRegistry.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogSystemViewRegistry.java new file mode 100644 index 0000000000..a9b27f1ff4 --- /dev/null +++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/CatalogSystemViewRegistry.java @@ -0,0 +1,52 @@ +/* + * 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.catalog; + +import java.util.List; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import org.apache.ignite.internal.catalog.systemviews.IndexSystemViewProvider; +import org.apache.ignite.internal.catalog.systemviews.SystemViewViewProvider; +import org.apache.ignite.internal.catalog.systemviews.ZonesSystemViewProvider; +import org.apache.ignite.internal.systemview.api.SystemView; +import org.apache.ignite.internal.systemview.api.SystemViewProvider; + +/** Implementation of {@link SystemViewProvider} for the catalog. */ +public class CatalogSystemViewRegistry implements SystemViewProvider { + + private final List<CatalogSystemViewProvider> providers; + + private final Supplier<Catalog> catalogSupplier; + + /** Constructor. */ + public CatalogSystemViewRegistry(Supplier<Catalog> catalogSupplier) { + this.catalogSupplier = catalogSupplier; + + providers = List.of( + new SystemViewViewProvider(), + new IndexSystemViewProvider(), + new ZonesSystemViewProvider() + ); + } + + /** {@inheritDoc} */ + @Override + public List<SystemView<?>> systemViews() { + return providers.stream().flatMap(p -> p.getView(catalogSupplier).stream()).collect(Collectors.toList()); + } +} diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/IndexSystemViewProvider.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/IndexSystemViewProvider.java new file mode 100644 index 0000000000..bd16577ffa --- /dev/null +++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/IndexSystemViewProvider.java @@ -0,0 +1,104 @@ +/* + * 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.catalog.systemviews; + +import static java.util.stream.Collectors.joining; +import static org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor.CatalogIndexDescriptorType.HASH; +import static org.apache.ignite.internal.type.NativeTypes.BOOLEAN; +import static org.apache.ignite.internal.type.NativeTypes.INT32; +import static org.apache.ignite.internal.type.NativeTypes.STRING; + +import java.util.List; +import java.util.function.Supplier; +import org.apache.ignite.internal.catalog.Catalog; +import org.apache.ignite.internal.catalog.CatalogSystemViewProvider; +import org.apache.ignite.internal.catalog.descriptors.CatalogHashIndexDescriptor; +import org.apache.ignite.internal.catalog.descriptors.CatalogIndexDescriptor; +import org.apache.ignite.internal.catalog.descriptors.CatalogSortedIndexDescriptor; +import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor; +import org.apache.ignite.internal.systemview.api.SystemView; +import org.apache.ignite.internal.systemview.api.SystemViews; +import org.apache.ignite.internal.util.SubscriptionUtils; + +/** + * Exposes information on index objects. + * + * <ul> + * <li>INDEXES system view</li> + * </ul> + */ +public class IndexSystemViewProvider implements CatalogSystemViewProvider { + + /** {@inheritDoc} */ + @Override + public List<SystemView<?>> getView(Supplier<Catalog> catalogSupplier) { + Iterable<IndexWithCatalog> viewData = () -> { + Catalog catalog = catalogSupplier.get(); + + return catalog.indexes().stream() + .filter(index -> index.status().isAlive()) + .map(index -> new IndexWithCatalog(index, catalog)) + .iterator(); + }; + + SystemView<?> indexView = SystemViews.<IndexWithCatalog>clusterViewBuilder() + .name("INDEXES") + .addColumn("INDEX_ID", INT32, entry -> entry.descriptor.id()) + .addColumn("INDEX_NAME", STRING, entry -> entry.descriptor.name()) + .addColumn("TABLE_ID", INT32, entry -> entry.descriptor.tableId()) + .addColumn("TABLE_NAME", STRING, entry -> getTableDescriptor(entry).name()) + .addColumn("SCHEMA_ID", INT32, IndexSystemViewProvider::getSchemaId) + .addColumn("SCHEMA_NAME", STRING, entry -> entry.catalog.schema(getSchemaId(entry)).name()) + .addColumn("TYPE", STRING, entry -> entry.descriptor.indexType().name()) + .addColumn("IS_UNIQUE", BOOLEAN, entry -> entry.descriptor.unique()) + .addColumn("COLUMNS", STRING, IndexSystemViewProvider::getColumnsString) + .addColumn("STATUS", STRING, entry -> entry.descriptor.status().name()) + .dataProvider(SubscriptionUtils.fromIterable(viewData)) + .build(); + + return List.of(indexView); + } + + private static CatalogTableDescriptor getTableDescriptor(IndexWithCatalog entry) { + return entry.catalog.table(entry.descriptor.tableId()); + } + + private static String getColumnsString(IndexWithCatalog entry) { + return entry.descriptor.indexType() == HASH + ? String.join(", ", ((CatalogHashIndexDescriptor) entry.descriptor).columns()) + : ((CatalogSortedIndexDescriptor) entry.descriptor) + .columns() + .stream() + .map(column -> column.name() + (column.collation().asc() ? " ASC" : " DESC")) + .collect(joining(", ")); + } + + private static int getSchemaId(IndexWithCatalog entry) { + return getTableDescriptor(entry).schemaId(); + } + + private static class IndexWithCatalog { + final CatalogIndexDescriptor descriptor; + final Catalog catalog; + + IndexWithCatalog(CatalogIndexDescriptor descriptor, Catalog catalog) { + this.descriptor = descriptor; + this.catalog = catalog; + } + } +} diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/SystemViewViewProvider.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/SystemViewViewProvider.java new file mode 100644 index 0000000000..9390d0c4ef --- /dev/null +++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/SystemViewViewProvider.java @@ -0,0 +1,126 @@ +/* + * 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.catalog.systemviews; + +import static org.apache.ignite.internal.type.NativeTypes.BOOLEAN; +import static org.apache.ignite.internal.type.NativeTypes.INT32; +import static org.apache.ignite.internal.type.NativeTypes.stringOf; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.Flow.Publisher; +import java.util.function.Supplier; +import org.apache.ignite.internal.catalog.Catalog; +import org.apache.ignite.internal.catalog.CatalogSystemViewProvider; +import org.apache.ignite.internal.catalog.descriptors.CatalogSystemViewDescriptor; +import org.apache.ignite.internal.catalog.descriptors.CatalogTableColumnDescriptor; +import org.apache.ignite.internal.systemview.api.SystemView; +import org.apache.ignite.internal.systemview.api.SystemViews; +import org.apache.ignite.internal.util.SubscriptionUtils; + +/** + * Exposes information on system views. + * + * <ul> + * <li>SYSTEM_VIEWS - available system views</li> + * <li>SYSTEM_VIEW_COLUMNS - columns of available system view columns.</li> + * </ul> + */ +public class SystemViewViewProvider implements CatalogSystemViewProvider { + + private static final int SYSTEM_VIEW_STRING_COLUMN_LENGTH = Short.MAX_VALUE; + + /** {@inheritDoc} */ + @Override + public List<SystemView<?>> getView(Supplier<Catalog> catalogSupplier) { + SystemView<?> systemViewsView = getSystemViewView(catalogSupplier); + SystemView<?> systemViewColumnsView = getSystemViewColumnsView(catalogSupplier); + + return List.of(systemViewsView, systemViewColumnsView); + } + + private static SystemView<?> getSystemViewView(Supplier<Catalog> catalogSupplier) { + Iterable<ViewWithSchema> viewData = () -> { + Catalog catalog = catalogSupplier.get(); + + return catalog.schemas().stream() + .flatMap(schema -> Arrays.stream(schema.systemViews()) + .map(viewDescriptor -> new ViewWithSchema(viewDescriptor, schema.name())) + ) + .iterator(); + }; + + Publisher<ViewWithSchema> viewDataPublisher = SubscriptionUtils.fromIterable(viewData); + + return SystemViews.<ViewWithSchema>clusterViewBuilder() + .name("SYSTEM_VIEWS") + .addColumn("ID", INT32, entry -> entry.descriptor.id()) + .addColumn("SCHEMA", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.schema) + .addColumn("NAME", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.descriptor.name()) + .addColumn("TYPE", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.descriptor.systemViewType().name()) + .dataProvider(viewDataPublisher) + .build(); + } + + private static SystemView<?> getSystemViewColumnsView(Supplier<Catalog> catalogSupplier) { + Iterable<ColumnWithTableId> viewData = () -> { + Catalog catalog = catalogSupplier.get(); + + return catalog.schemas().stream() + .flatMap(schema -> Arrays.stream(schema.systemViews())) + .flatMap(viewDescriptor -> viewDescriptor.columns().stream() + .map(columnDescriptor -> new ColumnWithTableId(columnDescriptor, viewDescriptor.id())) + ) + .iterator(); + }; + + Publisher<ColumnWithTableId> viewDataPublisher = SubscriptionUtils.fromIterable(viewData); + + return SystemViews.<ColumnWithTableId>clusterViewBuilder() + .name("SYSTEM_VIEW_COLUMNS") + .addColumn("VIEW_ID", INT32, entry -> entry.id) + .addColumn("NAME", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.descriptor.name()) + .addColumn("TYPE", stringOf(SYSTEM_VIEW_STRING_COLUMN_LENGTH), entry -> entry.descriptor.type().name()) + .addColumn("NULLABLE", BOOLEAN, entry -> entry.descriptor.nullable()) + .addColumn("PRECISION", INT32, entry -> entry.descriptor.precision()) + .addColumn("SCALE", INT32, entry -> entry.descriptor.scale()) + .addColumn("LENGTH", INT32, entry -> entry.descriptor.length()) + .dataProvider(viewDataPublisher) + .build(); + } + + private static class ColumnWithTableId { + final CatalogTableColumnDescriptor descriptor; + final int id; + + ColumnWithTableId(CatalogTableColumnDescriptor descriptor, int id) { + this.descriptor = descriptor; + this.id = id; + } + } + + private static class ViewWithSchema { + final CatalogSystemViewDescriptor descriptor; + final String schema; + + ViewWithSchema(CatalogSystemViewDescriptor descriptor, String schema) { + this.descriptor = descriptor; + this.schema = schema; + } + } +} diff --git a/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/ZonesSystemViewProvider.java b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/ZonesSystemViewProvider.java new file mode 100644 index 0000000000..13be1e872e --- /dev/null +++ b/modules/catalog/src/main/java/org/apache/ignite/internal/catalog/systemviews/ZonesSystemViewProvider.java @@ -0,0 +1,77 @@ +/* + * 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.catalog.systemviews; + +import static org.apache.ignite.internal.type.NativeTypes.BOOLEAN; +import static org.apache.ignite.internal.type.NativeTypes.INT32; +import static org.apache.ignite.internal.type.NativeTypes.STRING; + +import java.util.List; +import java.util.function.Supplier; +import org.apache.ignite.internal.catalog.Catalog; +import org.apache.ignite.internal.catalog.CatalogSystemViewProvider; +import org.apache.ignite.internal.catalog.descriptors.CatalogZoneDescriptor; +import org.apache.ignite.internal.systemview.api.SystemView; +import org.apache.ignite.internal.systemview.api.SystemViews; +import org.apache.ignite.internal.util.SubscriptionUtils; +import org.apache.ignite.internal.util.TransformingIterator; + +/** + * Exposes information on zones. + * + * <ul> + * <li>ZONES system view</li> + * </ul> + */ +public final class ZonesSystemViewProvider implements CatalogSystemViewProvider { + + /** {@inheritDoc} */ + @Override + public List<SystemView<?>> getView(Supplier<Catalog> catalogSupplier) { + SystemView<?> zoneSystemView = SystemViews.<ZoneWithDefaultMarker>clusterViewBuilder() + .name("ZONES") + .addColumn("NAME", STRING, wrapper -> wrapper.zone.name()) + .addColumn("PARTITIONS", INT32, wrapper -> wrapper.zone.partitions()) + .addColumn("REPLICAS", INT32, wrapper -> wrapper.zone.replicas()) + .addColumn("DATA_NODES_AUTO_ADJUST_SCALE_UP", INT32, wrapper -> wrapper.zone.dataNodesAutoAdjustScaleUp()) + .addColumn("DATA_NODES_AUTO_ADJUST_SCALE_DOWN", INT32, wrapper -> wrapper.zone.dataNodesAutoAdjustScaleDown()) + .addColumn("DATA_NODES_FILTER", STRING, wrapper -> wrapper.zone.filter()) + .addColumn("IS_DEFAULT_ZONE", BOOLEAN, wrapper -> wrapper.isDefault) + .dataProvider(SubscriptionUtils.fromIterable(() -> { + Catalog catalog = catalogSupplier.get(); + CatalogZoneDescriptor defaultZone = catalog.defaultZone(); + return new TransformingIterator<>(catalog.zones().iterator(), + (zone) -> new ZoneWithDefaultMarker(zone, defaultZone != null && defaultZone.id() == zone.id())); + } + )) + .build(); + + return List.of(zoneSystemView); + } + + /** Wraps a CatalogZoneDescriptor and a flag indicating whether this zone is the default zone. */ + static class ZoneWithDefaultMarker { + private final CatalogZoneDescriptor zone; + private final boolean isDefault; + + ZoneWithDefaultMarker(CatalogZoneDescriptor zone, boolean isDefault) { + this.zone = zone; + this.isDefault = isDefault; + } + } +}