AMashenkov commented on a change in pull request #518:
URL: https://github.com/apache/ignite-3/pull/518#discussion_r798534043



##########
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()));
+
+        createIndexLocally(tbl, (SortedIndexView) ctx.newValue());
+    }
+
+    @Override
+    public void stop() throws Exception {
+        if (!stopGuard.compareAndSet(false, true)) {
+            return;
+        }
+
+        busyLock.block();
+    }
+
+    @Override
+    public List<InternalSortedIndex> indexes(UUID tblId) {
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public InternalSortedIndex createIndex(
+            String idxCanonicalName,
+            String tblCanonicalName,
+            Consumer<TableIndexChange> idxChange
+    ) {
+        return join(createIndexAsync(idxCanonicalName, tblCanonicalName, 
idxChange));
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void dropIndex(
+            String idxCanonicalName
+    ) {
+        join(dropIndexAsync(idxCanonicalName));
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<Void> dropIndexAsync(String idxCanonicalName) {
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(new NodeStoppingException());
+        }
+        try {
+            CompletableFuture<Void> idxFut = new CompletableFuture<>();
+
+            indexAsyncInternal(idxCanonicalName).thenAccept((idx) -> {
+                if (idx == null) {
+                    idxFut.completeExceptionally(new 
IndexNotFoundException(idxCanonicalName));
+                    return;
+                }
+
+                try {
+                    CompletableFuture<TableImpl> tblFut = 
tblMgr.tableAsync(idx.tableId());
+
+                    tblFut.thenAccept(tbl -> {
+                        tablesCfg.tables().change(ch -> ch.createOrUpdate(
+                                        tbl.name(),
+                                        tblCh -> tblCh.changeIndices(idxes -> 
idxes.delete(idxCanonicalName))
+                                ))
+                                .whenComplete((res, t) -> {
+                                    if (t != null) {
+                                        Throwable ex = getRootCause(t);
+
+                                        if (!(ex instanceof 
IndexNotFoundException)) {
+                                            LOG.error("Index wasn't dropped 
[name={}]", ex, idxCanonicalName);
+                                        }
+
+                                        idxFut.completeExceptionally(ex);
+                                    } else {
+                                        idxFut.complete(null);
+                                    }
+                                });
+                    });
+                } catch (NodeStoppingException ex) {
+                    idxFut.completeExceptionally(ex);
+                }
+            });
+
+            return idxFut;
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<InternalSortedIndex> createIndexAsync(
+            String idxCanonicalName,
+            String tblCanonicalName,
+            Consumer<TableIndexChange> idxChange
+    ) {
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(new NodeStoppingException());
+        }
+        try {
+            CompletableFuture<InternalSortedIndex> idxFut = new 
CompletableFuture<>();
+
+            tblMgr.tableAsync(tblCanonicalName).thenAccept((tbl) -> {
+                if (tbl == null) {
+                    idxFut.completeExceptionally(new 
TableNotFoundException(tblCanonicalName));
+                }
+
+                indexAsyncInternal(idxCanonicalName).thenAccept((idx) -> {
+                    if (idx != null) {
+                        idxFut.completeExceptionally(new 
IndexAlreadyExistsException(idxCanonicalName));
+                    }
+
+                    tblMgr.alterTableAsync(tblCanonicalName, chng -> 
chng.changeIndices(idxes -> {
+                                if (idxes.get(idxCanonicalName) != null) {
+                                    idxFut.completeExceptionally(new 
IndexAlreadyExistsException(idxCanonicalName));
+                                }
+
+                                idxes.create(idxCanonicalName, 
idxChange::accept);
+                            }
+                    )).whenComplete((res, t) -> {
+                        if (t != null) {
+                            Throwable ex = getRootCause(t);
+
+                            if (!(ex instanceof IndexAlreadyExistsException)) {
+                                LOG.error("Index wasn't created [name={}]", 
ex, idxCanonicalName);
+                            }
+
+                            idxFut.completeExceptionally(ex);
+                        } else {
+                            idxFut.complete(idxsByName.get(idxCanonicalName));
+                        }
+                    });
+                });
+            });
+
+            return idxFut;
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public InternalSortedIndex index(String idxCanonicalName) {
+        return join(indexAsync(idxCanonicalName));
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CompletableFuture<InternalSortedIndex> indexAsync(String 
idxCanonicalName) {
+        if (!busyLock.enterBusy()) {
+            throw new IgniteException(new NodeStoppingException());
+        }
+        try {
+            return indexAsyncInternal(idxCanonicalName);
+        } finally {
+            busyLock.leaveBusy();
+        }
+    }
+
+    private CompletableFuture<InternalSortedIndex> indexAsyncInternal(String 
idxCanonicalName) {
+        var idx = idxsByName.get(idxCanonicalName);
+
+        if (idx != null) {
+            return CompletableFuture.completedFuture(idx);
+        }
+
+        CompletableFuture<InternalSortedIndex> getIdxFut = new 
CompletableFuture<>();
+
+        EventListener<IndexEventParameters> clo = new EventListener<>() {
+            @Override
+            public boolean notify(@NotNull IndexEventParameters parameters, 
@Nullable Throwable e) {
+                if (!idxCanonicalName.equals(parameters.indexName())) {
+                    return false;
+                }
+
+                if (e == null) {
+                    getIdxFut.complete(parameters.index());
+                } else {
+                    getIdxFut.completeExceptionally(e);
+                }
+
+                return true;
+            }
+
+            @Override
+            public void remove(@NotNull Throwable e) {
+                getIdxFut.completeExceptionally(e);
+            }
+        };
+
+        listen(IndexEvent.CREATE, clo);
+
+        idx = idxsByName.get(idxCanonicalName);
+
+        if (idx != null && getIdxFut.complete(idx) || 
!isIndexConfigured(idxCanonicalName) && getIdxFut.complete(null)) {
+            removeListener(IndexEvent.CREATE, clo, null);
+        } else {
+            getIdxFut.thenRun(() -> removeListener(IndexEvent.CREATE, clo, 
null));
+        }
+
+        return getIdxFut;
+    }
+
+    /**
+     * Checks that the index is configured with specific name.
+     *
+     * @param idxName Index name.
+     * @return True when the table is configured into cluster, false otherwise.
+     */
+    private boolean isIndexConfigured(String idxName) {
+        NamedListView<TableView> directTablesCfg = 
directProxy(tablesCfg.tables()).value();
+
+        // TODO: IGNITE-15721 Need to review this approach after the ticket 
would be fixed.
+        // Probably, it won't be required getting configuration of all tables 
from Metastore.
+        for (String name : directTablesCfg.namedListKeys()) {
+            TableView tableView = directTablesCfg.get(name);
+
+            if (tableView != null) {
+                if (tableView.indices().get(idxName) != null) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+
+    /**
+     * Waits for future result and return, or unwraps {@link 
CompletionException} to {@link IgniteException} if failed.
+     *
+     * @param future Completable future.
+     * @return Future result.
+     */
+    private <T> T join(CompletableFuture<T> future) {
+        if (!busyLock.enterBusy()) {

Review comment:
       This will prevent node from stopping is the one called sync operation.
   However, the behavior will be different when waiting on future that were got 
from async method.




-- 
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]


Reply via email to