AMashenkov commented on a change in pull request #518: URL: https://github.com/apache/ignite-3/pull/518#discussion_r798530020
########## File path: modules/index/src/main/java/org/apache/ignite/internal/idx/IndexManagerImpl.java ########## @@ -0,0 +1,515 @@ +/* + * 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.idx; + +import static org.apache.ignite.internal.configuration.util.ConfigurationUtil.directProxy; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.function.Consumer; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.ignite.configuration.ConfigurationChangeException; +import org.apache.ignite.configuration.NamedListView; +import org.apache.ignite.configuration.notifications.ConfigurationNamedListListener; +import org.apache.ignite.configuration.notifications.ConfigurationNotificationEvent; +import org.apache.ignite.configuration.schemas.table.IndexColumnView; +import org.apache.ignite.configuration.schemas.table.SortedIndexView; +import org.apache.ignite.configuration.schemas.table.TableConfiguration; +import org.apache.ignite.configuration.schemas.table.TableIndexChange; +import org.apache.ignite.configuration.schemas.table.TableIndexView; +import org.apache.ignite.configuration.schemas.table.TableView; +import org.apache.ignite.configuration.schemas.table.TablesConfiguration; +import org.apache.ignite.internal.configuration.schema.ExtendedTableConfiguration; +import org.apache.ignite.internal.idx.event.IndexEvent; +import org.apache.ignite.internal.idx.event.IndexEventParameters; +import org.apache.ignite.internal.manager.AbstractProducer; +import org.apache.ignite.internal.manager.EventListener; +import org.apache.ignite.internal.manager.IgniteComponent; +import org.apache.ignite.internal.schema.Column; +import org.apache.ignite.internal.schema.SchemaDescriptor; +import org.apache.ignite.internal.table.TableImpl; +import org.apache.ignite.internal.table.distributed.TableManager; +import org.apache.ignite.internal.util.IgniteSpinBusyLock; +import org.apache.ignite.lang.IgniteException; +import org.apache.ignite.lang.IgniteLogger; +import org.apache.ignite.lang.IgniteUuid; +import org.apache.ignite.lang.IndexAlreadyExistsException; +import org.apache.ignite.lang.IndexNotFoundException; +import org.apache.ignite.lang.NodeStoppingException; +import org.apache.ignite.lang.TableNotFoundException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * Internal index manager facade provides low-level methods for indexes operations. + */ +public class IndexManagerImpl extends AbstractProducer<IndexEvent, IndexEventParameters> implements IndexManager, IgniteComponent { + private static final IgniteLogger LOG = IgniteLogger.forClass(IndexManagerImpl.class); + + private final TablesConfiguration tablesCfg; + + private final TableManager tblMgr; + + /** Indexes by canonical name. */ + private final Map<String, InternalSortedIndex> idxsByName = new ConcurrentHashMap<>(); + + /** Busy lock to stop synchronously. */ + private final IgniteSpinBusyLock busyLock = new IgniteSpinBusyLock(); + + /** Prevents double stopping the component. */ + private final AtomicBoolean stopGuard = new AtomicBoolean(); + + /** + * Constructor. + * + * @param tablesCfg Tables configuration. + */ + public IndexManagerImpl( + TableManager tblMgr, + TablesConfiguration tablesCfg + ) { + this.tblMgr = tblMgr; + this.tablesCfg = tablesCfg; + } + + @Override + public void start() { + tablesCfg.tables().any().indices().listenElements( + new ConfigurationNamedListListener<>() { + @Override + public @NotNull CompletableFuture<?> onCreate(@NotNull ConfigurationNotificationEvent<TableIndexView> ctx) { + TableConfiguration tbl = ctx.config(TableConfiguration.class); + String tblName = tbl.name().value(); + String idxName = ctx.newValue().name(); + + if (!busyLock.enterBusy()) { + fireEvent(IndexEvent.CREATE, + new IndexEventParameters( + idxName, + tblName), + new NodeStoppingException()); + + return CompletableFuture.completedFuture(new NodeStoppingException()); + } + try { + onIndexCreate(ctx); + } catch (Exception e) { + fireEvent(IndexEvent.CREATE, new IndexEventParameters(idxName, tblName), e); + + LOG.error("Internal error, index creation failed [name={}, table={}]", e, idxName, tblName); + + return CompletableFuture.completedFuture(e); + } finally { + busyLock.leaveBusy(); + } + + return CompletableFuture.completedFuture(null); + } + + @Override + public @NotNull CompletableFuture<?> onRename(@NotNull String oldName, @NotNull String newName, + @NotNull ConfigurationNotificationEvent<TableIndexView> ctx) { + // TODO: IGNITE-16196 Supports index rename + return ConfigurationNamedListListener.super.onRename(oldName, newName, ctx); + } + + @Override + public @NotNull CompletableFuture<?> onDelete(@NotNull ConfigurationNotificationEvent<TableIndexView> ctx) { + TableConfiguration tbl = ctx.config(TableConfiguration.class); + String tblName = tbl.name().value(); + String idxName = ctx.oldValue().name(); + + if (!busyLock.enterBusy()) { + fireEvent(IndexEvent.DROP, + new IndexEventParameters( + idxName, + tblName + ), + new NodeStoppingException()); + + return CompletableFuture.completedFuture(new NodeStoppingException()); + } + try { + onIndexDrop(ctx); + } finally { + busyLock.leaveBusy(); + } + + return CompletableFuture.completedFuture(null); + } + + @Override + public @NotNull CompletableFuture<?> onUpdate(@NotNull ConfigurationNotificationEvent<TableIndexView> ctx) { + assert false : "Index cannot be updated [ctx=" + ctx + ']'; + + return CompletableFuture.completedFuture(null); + } + }); + } + + private void onIndexDrop(ConfigurationNotificationEvent<TableIndexView> ctx) { + TableConfiguration tbl = ctx.config(TableConfiguration.class); + String tblName = tbl.name().value(); + + InternalSortedIndex idx = idxsByName.remove(ctx.oldValue().name()); + + idx.drop(); + + fireEvent(IndexEvent.DROP, new IndexEventParameters( + idx.name(), + tblName + ), null); + } + + private void onIndexCreate(ConfigurationNotificationEvent<TableIndexView> ctx) throws NodeStoppingException { + // TODO: https://issues.apache.org/jira/browse/IGNITE-15916 + assert ctx.newValue() instanceof SortedIndexView : "Unsupported index type: " + ctx.newValue(); + + ExtendedTableConfiguration tblCfg = ctx.config(TableConfiguration.class); + + TableImpl tbl = tblMgr.table(IgniteUuid.fromString(tblCfg.id().value())); Review comment: ```suggestion TableImpl tbl = tblMgr.table(UUID.fromString(tblCfg.id().value())); ``` -- 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]
