ibessonov commented on a change in pull request #9207:
URL: https://github.com/apache/ignite/pull/9207#discussion_r677231318



##########
File path: 
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/DurableBackgroundCleanupIndexTreeTaskV2.java
##########
@@ -0,0 +1,502 @@
+/*
+ * 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.cache.query.index.sorted;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.IgniteInternalFuture;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexKeyType;
+import 
org.apache.ignite.internal.cache.query.index.sorted.inline.InlineIndexTree;
+import org.apache.ignite.internal.cache.query.index.sorted.keys.IndexKey;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.pagemem.FullPageId;
+import org.apache.ignite.internal.pagemem.wal.IgniteWriteAheadLogManager;
+import org.apache.ignite.internal.pagemem.wal.record.IndexRenameRootPageRecord;
+import org.apache.ignite.internal.processors.cache.CacheGroupContext;
+import org.apache.ignite.internal.processors.cache.persistence.CacheDataRow;
+import org.apache.ignite.internal.processors.cache.persistence.RootPage;
+import 
org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTask;
+import 
org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTaskResult;
+import 
org.apache.ignite.internal.processors.cache.persistence.tree.io.PageIoResolver;
+import org.apache.ignite.internal.util.future.GridFinishedFuture;
+import org.apache.ignite.internal.util.future.GridFutureAdapter;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.util.worker.GridWorker;
+import org.apache.ignite.thread.IgniteThread;
+import org.jetbrains.annotations.Nullable;
+
+import static java.util.Collections.emptyList;
+import static java.util.Collections.emptyMap;
+import static java.util.Collections.singleton;
+
+/**
+ * Task for background cleaning of index trees.
+ */
+public class DurableBackgroundCleanupIndexTreeTaskV2 extends 
IgniteDataTransferObject implements
+    DurableBackgroundTask<Long> {
+    /** Serial version uid. */
+    private static final long serialVersionUID = 0L;
+
+    /**
+     * Index tree index factory.
+     * NOTE: Change only in tests, to control the creation of trees in the 
task.
+     */
+    public static InlineIndexTreeFactory idxTreeFactory = new 
InlineIndexTreeFactory();
+
+    /** Logger. */
+    @Nullable private transient volatile IgniteLogger log;
+
+    /** Unique id. */
+    private String uid;
+
+    /** Cache group name. */
+    @Nullable private String grpName;
+
+    /** Cache name. */
+    private String cacheName;
+
+    /** Index name. */
+    private String idxName;
+
+    /** Old name of underlying index tree name. */
+    private String oldTreeName;
+
+    /** New name of underlying index tree name. */
+    private String newTreeName;
+
+    /** Number of segments. */
+    private int segments;
+
+    /** Need to rename index root pages. */
+    private transient volatile boolean needToRen;
+
+    /** Index root pages. Mapping: segment number -> index root page. */
+    private final transient Map<Integer, RootPage> rootPages = new 
ConcurrentHashMap<>();
+
+    /** Worker cleaning index trees. */
+    @Nullable private transient volatile GridWorker worker;
+
+    /** Total number of pages recycled from index trees. */
+    private final transient AtomicLong pageCnt = new AtomicLong();
+
+    /**
+     * Constructor.
+     *
+     * @param grpName Cache group name.
+     * @param cacheName Cache name.
+     * @param idxName Index name.
+     * @param oldTreeName Old name of underlying index tree name.
+     * @param newTreeName New name of underlying index tree name.
+     * @param segments Number of segments.
+     * @param trees Index trees.
+     */
+    public DurableBackgroundCleanupIndexTreeTaskV2(
+        @Nullable String grpName,
+        String cacheName,
+        String idxName,
+        String oldTreeName,
+        String newTreeName,
+        int segments,
+        @Nullable InlineIndexTree[] trees
+    ) {
+        uid = UUID.randomUUID().toString();
+        this.grpName = grpName;
+        this.cacheName = cacheName;
+        this.idxName = idxName;
+        this.oldTreeName = oldTreeName;
+        this.newTreeName = newTreeName;
+        this.segments = segments;
+
+        if (trees != null) {
+            assert trees.length == segments :
+                "Invalid number of index trees [trees=" + trees.length + ", 
segments=" + segments + ']';
+
+            this.rootPages.putAll(toRootPages(trees));
+        }
+
+        needToRen = true;
+    }
+
+    /**
+     * Default constructor for {@link Externalizable}.
+     */
+    public DurableBackgroundCleanupIndexTreeTaskV2() {
+        // No-op.
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void writeExternalData(ObjectOutput out) throws 
IOException {
+        U.writeLongString(out, uid);
+        U.writeLongString(out, grpName);
+        U.writeLongString(out, cacheName);
+        U.writeLongString(out, idxName);
+        U.writeLongString(out, oldTreeName);
+        U.writeLongString(out, newTreeName);
+        out.writeInt(segments);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void readExternalData(
+        byte protoVer,
+        ObjectInput in
+    ) throws IOException, ClassNotFoundException {
+        uid = U.readLongString(in);
+        grpName = U.readLongString(in);
+        cacheName = U.readLongString(in);
+        idxName = U.readLongString(in);
+        oldTreeName = U.readLongString(in);
+        newTreeName = U.readLongString(in);
+        segments = in.readInt();
+    }
+
+    /** {@inheritDoc} */
+    @Override public String name() {
+        return "drop-sql-index-" + cacheName + "-" + idxName + "-" + uid;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void cancel() {
+        rootPages.clear();
+
+        GridWorker w = worker;
+
+        if (w != null) {
+            worker = null;
+
+            U.awaitForWorkersStop(singleton(w), true, log);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void onDeactivationCluster() {
+        rootPages.clear();

Review comment:
       Why don't you wait for worker task completion, or cancel it? Are we 
relying on the fact that task will just fail?




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