You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2022/05/30 07:54:11 UTC

[GitHub] [ignite] tkalkirill commented on a diff in pull request #10042: IGNITE-17002 Control.sh command to schedule index rebuild in Maintenance Mode

tkalkirill commented on code in PR #10042:
URL: https://github.com/apache/ignite/pull/10042#discussion_r884475350


##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheCommandList.java:
##########
@@ -87,7 +87,12 @@ public enum CacheCommandList {
     /**
      * Index force rebuild.
      */
-    INDEX_FORCE_REBUILD("indexes_force_rebuild", new CacheIndexesForceRebuild());
+    INDEX_FORCE_REBUILD("indexes_force_rebuild", new CacheIndexesForceRebuild()),
+
+    /**
+     * Schedule index rebuild.
+     */
+    INDEX_REBUILD("indexes_rebuild", new CacheIndexesRebuild());

Review Comment:
   **indexes_rebuild** doesn't sound to me like scheduling an index rebuild, but how to perform an index rebuild, I suggest calling it something else: **schedule_indexes_rebuild**



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheIndexesRebuild.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.commandline.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.commandline.argument.CommandArgUtils;
+import org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskArg;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskRes;
+
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.NODE_ID;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.TARGET;
+
+/**
+ * Cache subcommand that schedules indexes rebuild via the maintenance mode.
+ */
+public class CacheIndexesRebuild extends AbstractCommand<CacheIndexesRebuild.Arguments> {

Review Comment:
   Maybe: **CacheScheduleIndexesRebuild** ?



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildJobRes.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildJob.
+ */
+public class IndexRebuildJobRes extends IgniteDataTransferObject {
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/control-utility/src/test/java/org/apache/ignite/internal/commandline/CommandHandlerParsingTest.java:
##########
@@ -934,6 +934,26 @@ public void testIndexForceRebuildWrongArgs() {
         );
     }
 
+    /** */
+    @Test
+    public void testIndexRebuildWrongArgs() {

Review Comment:
   Missing check --node-id twice



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildJobRes.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildJob.
+ */
+public class IndexRebuildJobRes extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Map cache names -> indexes scheduled for the rebuild. */
+    private Map<String, Set<String>> cacheToIndexes;
+
+    /** Names of caches that were not found. */
+    private Set<String> notFoundCacheNames;
+
+    /** Names of cache indexes that were not found (cache -> set of indexes). */
+    private Map<String, Set<String>> notFoundIndexes;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildJobRes() {
+        // No-op.
+    }
+
+    /** */
+    public IndexRebuildJobRes(
+        Map<String, Set<String>> cacheToIndexes,
+        Map<String, Set<String>> notFoundIndexes,
+        Set<String> notFoundCacheNames
+    ) {
+        this.cacheToIndexes = cacheToIndexes;
+        this.notFoundIndexes = notFoundIndexes;
+        this.notFoundCacheNames = notFoundCacheNames;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void writeExternalData(ObjectOutput out) throws IOException {
+        U.writeMap(out, cacheToIndexes);
+        U.writeMap(out, notFoundIndexes);
+        U.writeCollection(out, notFoundCacheNames);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException {
+        cacheToIndexes = U.readMap(in);
+        notFoundIndexes = U.readMap(in);
+        notFoundCacheNames = U.readSet(in);
+    }
+
+    /** */
+    public Map<String, Set<String>> cacheToIndexes() {
+        return cacheToIndexes;
+    }
+
+    /** */
+    public Set<String> notFoundCacheNames() {
+        return notFoundCacheNames;
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildJobRes.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildJob.
+ */
+public class IndexRebuildJobRes extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Map cache names -> indexes scheduled for the rebuild. */
+    private Map<String, Set<String>> cacheToIndexes;
+
+    /** Names of caches that were not found. */
+    private Set<String> notFoundCacheNames;
+
+    /** Names of cache indexes that were not found (cache -> set of indexes). */
+    private Map<String, Set<String>> notFoundIndexes;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildJobRes() {
+        // No-op.
+    }
+
+    /** */
+    public IndexRebuildJobRes(
+        Map<String, Set<String>> cacheToIndexes,
+        Map<String, Set<String>> notFoundIndexes,
+        Set<String> notFoundCacheNames
+    ) {
+        this.cacheToIndexes = cacheToIndexes;
+        this.notFoundIndexes = notFoundIndexes;
+        this.notFoundCacheNames = notFoundCacheNames;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void writeExternalData(ObjectOutput out) throws IOException {
+        U.writeMap(out, cacheToIndexes);
+        U.writeMap(out, notFoundIndexes);
+        U.writeCollection(out, notFoundCacheNames);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException {
+        cacheToIndexes = U.readMap(in);
+        notFoundIndexes = U.readMap(in);
+        notFoundCacheNames = U.readSet(in);
+    }
+
+    /** */
+    public Map<String, Set<String>> cacheToIndexes() {
+        return cacheToIndexes;
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskRes.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildTask.
+ */
+public class IndexRebuildTaskRes extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Map node id -> rebuild command result. */
+    private Map<UUID, IndexRebuildJobRes> results;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildTaskRes() {
+        // No-op.
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/indexing/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTask.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.visor.cache.index;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor;
+import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing;
+import org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorMultiNodeTask;
+import org.apache.ignite.maintenance.MaintenanceRegistry;
+import org.apache.ignite.maintenance.MaintenanceTask;
+import org.h2.index.Index;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.mergeTasks;
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.toMaintenanceTask;
+
+/**
+ * Task that schedules indexes rebuild for specified caches via the maintenance mode.
+ */
+@GridInternal
+public class IndexRebuildTask extends VisorMultiNodeTask<IndexRebuildTaskArg, IndexRebuildTaskRes, IndexRebuildJobRes> {
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheIndexesRebuild.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.commandline.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.commandline.argument.CommandArgUtils;
+import org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskArg;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskRes;
+
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.NODE_ID;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.TARGET;
+
+/**
+ * Cache subcommand that schedules indexes rebuild via the maintenance mode.
+ */
+public class CacheIndexesRebuild extends AbstractCommand<CacheIndexesRebuild.Arguments> {
+    /** Command's parsed arguments. */
+    private Arguments args;
+
+    /** {@inheritDoc} */
+    @Override public void printUsage(Logger logger) {
+        String desc = "Schedules rebuild of the indexes for specified caches.";
+
+        Map<String, String> map = U.newLinkedHashMap(16);
+
+        map.put(NODE_ID.argName(), "(Optional) Specify node for indexes rebuild.");

Review Comment:
   ```suggestion
           map.put(NODE_ID.argName(), "(Optional) Specify node for indexes rebuild.");
           
   ```



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheIndexesRebuild.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.commandline.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.commandline.argument.CommandArgUtils;
+import org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskArg;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskRes;
+
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.NODE_ID;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.TARGET;
+
+/**
+ * Cache subcommand that schedules indexes rebuild via the maintenance mode.
+ */
+public class CacheIndexesRebuild extends AbstractCommand<CacheIndexesRebuild.Arguments> {
+    /** Command's parsed arguments. */
+    private Arguments args;
+
+    /** {@inheritDoc} */
+    @Override public void printUsage(Logger logger) {
+        String desc = "Schedules rebuild of the indexes for specified caches.";
+
+        Map<String, String> map = U.newLinkedHashMap(16);

Review Comment:
   Why not just **new LinkedHashMap<>**?



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheSubcommands.java:
##########
@@ -88,6 +89,11 @@ public enum CacheSubcommands {
      */
     INDEX_FORCE_REBUILD("indexes_force_rebuild", IndexForceRebuildCommandArg.class, new CacheIndexesForceRebuild()),
 
+    /**
+     * Index rebuild via the maintenance mode.
+     */
+    INDEX_REBUILD("indexes_rebuild", IndexRebuildCommandArg.class, new CacheIndexesRebuild()),

Review Comment:
   **indexes_rebuild** doesn't sound to me like scheduling an index rebuild, but how to perform an index rebuild, I suggest calling it something else: **schedule_indexes_rebuild**



##########
modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerIndexRebuildTest.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.ListeningTestLogger;
+import org.junit.Test;
+
+import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK;
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.util.IgniteUtils.max;
+import static org.apache.ignite.testframework.GridTestUtils.assertContains;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.breakSqlIndex;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.complexIndexEntity;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.createAndFillCache;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.createAndFillThreeFieldsEntryCache;
+
+/**
+ * Tests for --cache indexes_rebuild command. Uses single cluster per suite.
+ */
+public class GridCommandHandlerIndexRebuildTest extends GridCommandHandlerAbstractTest {
+    /** */
+    private static final String CACHE_NAME_1_1 = "cache_1_1";
+
+    /** */
+    private static final String CACHE_NAME_1_2 = "cache_1_2";
+
+    /** */
+    private static final String CACHE_NAME_2_1 = "cache_2_1";
+
+    /** */
+    private static final String CACHE_NAME_NO_GRP = "cache_no_group";
+
+    /** */
+    private static final String CACHE_NAME_NON_EXISTING = "non_existing_cache";
+
+    /** */
+    private static final String GRP_NAME_1 = "group_1";
+
+    /** */
+    private static final String GRP_NAME_2 = "group_2";
+
+    /** */
+    private static final int GRIDS_NUM = 3;
+
+    /** */
+    private static final int LAST_NODE_NUM = GRIDS_NUM - 1;
+
+    /** */
+    private static final int REBUILD_TIMEOUT = 30_000;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.setGridLogger(new ListeningTestLogger(log));
+
+        cfg.setBuildIndexThreadPoolSize(max(2, cfg.getBuildIndexThreadPoolSize()));
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        cleanPersistenceDir();
+
+        startupTestCluster();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        stopAllGrids();
+
+        cleanPersistenceDir();
+
+        super.afterTestsStopped();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        G.allGrids().forEach(ignite -> assertFalse(((IgniteEx)ignite).context().maintenanceRegistry().isMaintenanceMode()));
+    }
+
+    /** */
+    private void startupTestCluster() throws Exception {
+        for (int i = 0; i < GRIDS_NUM; i++ )
+            startGrid(i);
+
+        IgniteEx ignite = grid(0);
+
+        ignite.cluster().state(ClusterState.ACTIVE);
+
+        createAndFillCache(ignite, CACHE_NAME_1_1, GRP_NAME_1);
+        createAndFillCache(ignite, CACHE_NAME_1_2, GRP_NAME_1);
+        createAndFillCache(ignite, CACHE_NAME_2_1, GRP_NAME_2);
+
+        createAndFillThreeFieldsEntryCache(ignite, CACHE_NAME_NO_GRP, null, Collections.singletonList(complexIndexEntity()));
+
+        assertTrue(grid(LAST_NODE_NUM).context().config().getBuildIndexThreadPoolSize() > 1);
+    }
+
+    /**
+     * Checks error messages when trying to rebuild indexes for non-existent cache or when trying
+     * to rebuild non-existent indexes.
+     */
+    @Test
+    public void testErrors() {
+        injectTestSystemOut();
+
+        IgniteEx lastNode = grid(LAST_NODE_NUM);
+
+        assertEquals(EXIT_CODE_OK, execute("--cache", "indexes_rebuild",
+            "--node-id", lastNode.localNode().id().toString(),
+            "--target", CACHE_NAME_NON_EXISTING));
+
+        String notExistingCacheOutputStr = testOut.toString();
+
+        assertTrue(notExistingCacheOutputStr.contains("WARNING: Indexes rebuild was not scheduled for any cache. Check command input."));
+
+        testOut.reset();
+
+        assertEquals(EXIT_CODE_OK, execute("--cache", "indexes_rebuild",
+            "--node-id", grid(LAST_NODE_NUM).localNode().id().toString(),
+            "--target", CACHE_NAME_1_1 + "=non-existing-index"));
+
+        String notExistingIndexOutputStr = testOut.toString();
+
+        assertTrue(notExistingIndexOutputStr.contains("WARNING: Indexes rebuild was not scheduled for any cache. Check command input."));
+
+        assertTrue(notExistingIndexOutputStr.contains(
+            "WARNING: These indexes were not found:" + System.lineSeparator()
+            + INDENT + CACHE_NAME_1_1 + ":" + System.lineSeparator()
+            + INDENT + INDENT + "non-existing-index")
+        );
+    }
+
+    /**
+     * Checks that index is rebuilt correctly.
+     */
+    @Test
+    public void testRebuild() throws Exception {
+        injectTestSystemOut();
+
+        IgniteEx node = grid(LAST_NODE_NUM);
+
+        assertEquals(EXIT_CODE_OK, execute("--cache", "indexes_rebuild",
+            "--node-id", node.localNode().id().toString(),
+            "--target", CACHE_NAME_NO_GRP));
+
+        node.close();

Review Comment:
   Please add a check that an index rebuild is scheduled for the given cache and index.



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheIndexesRebuild.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.commandline.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.commandline.argument.CommandArgUtils;
+import org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskArg;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskRes;
+
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.NODE_ID;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.TARGET;
+
+/**
+ * Cache subcommand that schedules indexes rebuild via the maintenance mode.
+ */
+public class CacheIndexesRebuild extends AbstractCommand<CacheIndexesRebuild.Arguments> {
+    /** Command's parsed arguments. */
+    private Arguments args;
+
+    /** {@inheritDoc} */
+    @Override public void printUsage(Logger logger) {
+        String desc = "Schedules rebuild of the indexes for specified caches.";
+
+        Map<String, String> map = U.newLinkedHashMap(16);
+
+        map.put(NODE_ID.argName(), "(Optional) Specify node for indexes rebuild.");
+        map.put(
+            TARGET.argName(),
+            "Cache name with optionally specified indexes. If indexes are not specified then all indexes of the cache will be scheduled "
+            + "for the rebuild operation."
+        );
+
+        usageCache(
+            logger,
+            CacheSubcommands.INDEX_REBUILD,
+            desc,
+            map,
+            NODE_ID.argName() + " nodeId",
+            TARGET + " cacheName1=index1,...indexN"
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object execute(GridClientConfiguration clientCfg, Logger logger) throws Exception {
+        IndexRebuildTaskRes taskRes;
+
+        try (GridClient client = Command.startClient(clientCfg)) {
+            UUID nodeId = args.nodeId;
+
+            if (nodeId == null)
+                nodeId = TaskExecutor.BROADCAST_UUID;
+
+            taskRes = TaskExecutor.executeTaskByNameOnNode(
+                client,
+                "org.apache.ignite.internal.visor.cache.index.IndexRebuildTask",
+                new IndexRebuildTaskArg(args.cacheToIndexes),
+                nodeId,
+                clientCfg
+            );
+        }
+
+        printResult(taskRes, logger);
+
+        return taskRes;
+    }
+
+    /**
+     * @param taskRes Rebuild task result.
+     * @param logger Logger to print to.
+     */
+    private void printResult(IndexRebuildTaskRes taskRes, Logger logger) {
+        taskRes.results().forEach((nodeId, res) -> {
+            if (!F.isEmpty(res.notFoundCacheNames())) {
+                String warning = "WARNING: These caches were not found:";
+
+                logger.info(warning);
+
+                res.notFoundCacheNames()
+                    .stream()
+                    .sorted()
+                    .forEach(name -> logger.info(INDENT + name));
+
+                logger.info("");
+            }
+
+            if (!F.isEmpty(res.notFoundIndexes()) && hasIndexes(res.notFoundIndexes())) {
+                String warning = "WARNING: These indexes were not found:";
+
+                logger.info(warning);
+
+                printCachesAndIndexes(res.notFoundIndexes(), logger);
+            }
+
+            if (!F.isEmpty(res.cacheToIndexes()) && hasIndexes(res.cacheToIndexes())) {
+                logger.info("Indexes rebuild was scheduled for these caches:");
+
+                printCachesAndIndexes(res.cacheToIndexes(), logger);
+            }
+            else
+                logger.info("WARNING: Indexes rebuild was not scheduled for any cache. Check command input.");
+
+            logger.info("");
+        });
+    }
+
+    /** */
+    private static void printCachesAndIndexes(Map<String, Set<String>> cachesToIndexes, Logger logger) {
+        cachesToIndexes.forEach((cacheName, indexes) -> {
+            logger.info(INDENT + cacheName + ":");
+            indexes.forEach(index -> logger.info(INDENT + INDENT + index));
+        });
+    }
+
+    /**
+     * @param cacheToIndexes Cache name -> indexes map.
+     * @return {@code true} if has indexes, {@code false} otherwise.
+     */
+    private static boolean hasIndexes(Map<String, Set<String>> cacheToIndexes) {

Review Comment:
   Incomprehensible method semantics.



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheIndexesRebuild.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.commandline.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.commandline.argument.CommandArgUtils;
+import org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskArg;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskRes;
+
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.NODE_ID;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.TARGET;
+
+/**
+ * Cache subcommand that schedules indexes rebuild via the maintenance mode.
+ */
+public class CacheIndexesRebuild extends AbstractCommand<CacheIndexesRebuild.Arguments> {
+    /** Command's parsed arguments. */
+    private Arguments args;
+
+    /** {@inheritDoc} */
+    @Override public void printUsage(Logger logger) {
+        String desc = "Schedules rebuild of the indexes for specified caches.";
+
+        Map<String, String> map = U.newLinkedHashMap(16);
+
+        map.put(NODE_ID.argName(), "(Optional) Specify node for indexes rebuild.");
+        map.put(
+            TARGET.argName(),
+            "Cache name with optionally specified indexes. If indexes are not specified then all indexes of the cache will be scheduled "
+            + "for the rebuild operation."
+        );
+
+        usageCache(
+            logger,
+            CacheSubcommands.INDEX_REBUILD,
+            desc,
+            map,
+            NODE_ID.argName() + " nodeId",
+            TARGET + " cacheName1=index1,...indexN"
+        );
+    }
+
+    /** {@inheritDoc} */
+    @Override public Object execute(GridClientConfiguration clientCfg, Logger logger) throws Exception {
+        IndexRebuildTaskRes taskRes;
+
+        try (GridClient client = Command.startClient(clientCfg)) {
+            UUID nodeId = args.nodeId;
+
+            if (nodeId == null)
+                nodeId = TaskExecutor.BROADCAST_UUID;
+
+            taskRes = TaskExecutor.executeTaskByNameOnNode(
+                client,
+                "org.apache.ignite.internal.visor.cache.index.IndexRebuildTask",
+                new IndexRebuildTaskArg(args.cacheToIndexes),
+                nodeId,
+                clientCfg
+            );
+        }
+
+        printResult(taskRes, logger);
+
+        return taskRes;
+    }
+
+    /**
+     * @param taskRes Rebuild task result.
+     * @param logger Logger to print to.
+     */
+    private void printResult(IndexRebuildTaskRes taskRes, Logger logger) {
+        taskRes.results().forEach((nodeId, res) -> {
+            if (!F.isEmpty(res.notFoundCacheNames())) {
+                String warning = "WARNING: These caches were not found:";
+
+                logger.info(warning);
+
+                res.notFoundCacheNames()
+                    .stream()
+                    .sorted()
+                    .forEach(name -> logger.info(INDENT + name));
+
+                logger.info("");
+            }
+
+            if (!F.isEmpty(res.notFoundIndexes()) && hasIndexes(res.notFoundIndexes())) {
+                String warning = "WARNING: These indexes were not found:";
+
+                logger.info(warning);
+
+                printCachesAndIndexes(res.notFoundIndexes(), logger);
+            }
+
+            if (!F.isEmpty(res.cacheToIndexes()) && hasIndexes(res.cacheToIndexes())) {
+                logger.info("Indexes rebuild was scheduled for these caches:");
+
+                printCachesAndIndexes(res.cacheToIndexes(), logger);
+            }
+            else
+                logger.info("WARNING: Indexes rebuild was not scheduled for any cache. Check command input.");
+
+            logger.info("");
+        });
+    }
+
+    /** */
+    private static void printCachesAndIndexes(Map<String, Set<String>> cachesToIndexes, Logger logger) {
+        cachesToIndexes.forEach((cacheName, indexes) -> {
+            logger.info(INDENT + cacheName + ":");
+            indexes.forEach(index -> logger.info(INDENT + INDENT + index));
+        });
+    }
+
+    /**
+     * @param cacheToIndexes Cache name -> indexes map.
+     * @return {@code true} if has indexes, {@code false} otherwise.
+     */
+    private static boolean hasIndexes(Map<String, Set<String>> cacheToIndexes) {
+        return cacheToIndexes.values().stream()
+            .anyMatch(indexes -> !indexes.isEmpty());
+    }
+
+    /** {@inheritDoc} */
+    @Override public Arguments arg() {
+        return args;
+    }
+
+    /** {@inheritDoc} */
+    @Override public String name() {
+        return CacheSubcommands.INDEX_REBUILD.text().toUpperCase();
+    }
+
+    /**
+     * Container for command arguments.
+     */
+    static class Arguments {
+        /** Node id. */
+        private final UUID nodeId;
+
+        /** Cache name -> indexes. */
+        private final Map<String, Set<String>> cacheToIndexes;
+
+        /** */
+        private Arguments(UUID nodeId, Map<String, Set<String>> cacheToIndexes) {
+            this.nodeId = nodeId;
+            this.cacheToIndexes = cacheToIndexes;
+        }
+
+        /** {@inheritDoc} */
+        @Override public String toString() {
+            return S.toString(Arguments.class, this);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override public void parseArguments(CommandArgIterator argIterator) {
+        UUID nodeId = null;
+        Map<String, Set<String>> cacheToIndexes = new HashMap<>();

Review Comment:
   ```suggestion
           Map<String, Set<String>> cacheToIndexes = new HashMap<>();
           
   ```



##########
modules/control-utility/src/main/java/org/apache/ignite/internal/commandline/cache/CacheIndexesRebuild.java:
##########
@@ -0,0 +1,239 @@
+/*
+ * 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.commandline.cache;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.util.logging.Logger;
+import java.util.stream.Collectors;
+import org.apache.ignite.internal.client.GridClient;
+import org.apache.ignite.internal.client.GridClientConfiguration;
+import org.apache.ignite.internal.commandline.AbstractCommand;
+import org.apache.ignite.internal.commandline.Command;
+import org.apache.ignite.internal.commandline.CommandArgIterator;
+import org.apache.ignite.internal.commandline.TaskExecutor;
+import org.apache.ignite.internal.commandline.argument.CommandArgUtils;
+import org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.internal.util.typedef.internal.S;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskArg;
+import org.apache.ignite.internal.visor.cache.index.IndexRebuildTaskRes;
+
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.NODE_ID;
+import static org.apache.ignite.internal.commandline.cache.argument.IndexRebuildCommandArg.TARGET;
+
+/**
+ * Cache subcommand that schedules indexes rebuild via the maintenance mode.
+ */
+public class CacheIndexesRebuild extends AbstractCommand<CacheIndexesRebuild.Arguments> {
+    /** Command's parsed arguments. */
+    private Arguments args;
+
+    /** {@inheritDoc} */
+    @Override public void printUsage(Logger logger) {
+        String desc = "Schedules rebuild of the indexes for specified caches.";
+
+        Map<String, String> map = U.newLinkedHashMap(16);
+
+        map.put(NODE_ID.argName(), "(Optional) Specify node for indexes rebuild.");
+        map.put(
+            TARGET.argName(),
+            "Cache name with optionally specified indexes. If indexes are not specified then all indexes of the cache will be scheduled "
+            + "for the rebuild operation."
+        );
+
+        usageCache(
+            logger,
+            CacheSubcommands.INDEX_REBUILD,
+            desc,
+            map,
+            NODE_ID.argName() + " nodeId",
+            TARGET + " cacheName1=index1,...indexN"

Review Comment:
   I think it would be better to have two arguments and have them in the following options:
   - --cache-names=cache0,cache1[idx0,idx2]
   - --group-names=grp0,grp1[idx0,idx2]



##########
modules/control-utility/src/test/java/org/apache/ignite/util/GridCommandHandlerIndexRebuildTest.java:
##########
@@ -0,0 +1,369 @@
+/*
+ * 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.util;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.apache.ignite.cluster.ClusterState;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.ListeningTestLogger;
+import org.junit.Test;
+
+import static org.apache.ignite.internal.commandline.CommandHandler.EXIT_CODE_OK;
+import static org.apache.ignite.internal.commandline.CommandLogger.INDENT;
+import static org.apache.ignite.internal.util.IgniteUtils.max;
+import static org.apache.ignite.testframework.GridTestUtils.assertContains;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.breakSqlIndex;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.complexIndexEntity;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.createAndFillCache;
+import static org.apache.ignite.util.GridCommandHandlerIndexingUtils.createAndFillThreeFieldsEntryCache;
+
+/**
+ * Tests for --cache indexes_rebuild command. Uses single cluster per suite.
+ */
+public class GridCommandHandlerIndexRebuildTest extends GridCommandHandlerAbstractTest {
+    /** */
+    private static final String CACHE_NAME_1_1 = "cache_1_1";
+
+    /** */
+    private static final String CACHE_NAME_1_2 = "cache_1_2";
+
+    /** */
+    private static final String CACHE_NAME_2_1 = "cache_2_1";
+
+    /** */
+    private static final String CACHE_NAME_NO_GRP = "cache_no_group";
+
+    /** */
+    private static final String CACHE_NAME_NON_EXISTING = "non_existing_cache";
+
+    /** */
+    private static final String GRP_NAME_1 = "group_1";
+
+    /** */
+    private static final String GRP_NAME_2 = "group_2";
+
+    /** */
+    private static final int GRIDS_NUM = 3;
+
+    /** */
+    private static final int LAST_NODE_NUM = GRIDS_NUM - 1;
+
+    /** */
+    private static final int REBUILD_TIMEOUT = 30_000;
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        cfg.setGridLogger(new ListeningTestLogger(log));
+
+        cfg.setBuildIndexThreadPoolSize(max(2, cfg.getBuildIndexThreadPoolSize()));
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        cleanPersistenceDir();

Review Comment:
   ```suggestion
           stopAllGrids();
           
           cleanPersistenceDir();
   ```



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildJobRes.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildJob.
+ */
+public class IndexRebuildJobRes extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Map cache names -> indexes scheduled for the rebuild. */
+    private Map<String, Set<String>> cacheToIndexes;
+
+    /** Names of caches that were not found. */
+    private Set<String> notFoundCacheNames;
+
+    /** Names of cache indexes that were not found (cache -> set of indexes). */
+    private Map<String, Set<String>> notFoundIndexes;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildJobRes() {
+        // No-op.
+    }
+
+    /** */
+    public IndexRebuildJobRes(

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskArg.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Argument for the IndexRebuildTask.
+ */
+public class IndexRebuildTaskArg extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Cache name. */
+    private Map<String, Set<String>> cacheToIndexes;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildTaskArg() {
+        // No-op.
+    }
+
+    /** */
+    public IndexRebuildTaskArg(Map<String, Set<String>> cacheToIndexes) {
+        this.cacheToIndexes = cacheToIndexes;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void writeExternalData(ObjectOutput out) throws IOException {
+        U.writeMap(out, cacheToIndexes);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException {
+        cacheToIndexes = U.readMap(in);
+    }
+
+    /**
+     * @return Cache group name.

Review Comment:
   Wrong javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildJobRes.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildJob.

Review Comment:
   Missing link.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildJobRes.java:
##########
@@ -0,0 +1,90 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildJob.
+ */
+public class IndexRebuildJobRes extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Map cache names -> indexes scheduled for the rebuild. */
+    private Map<String, Set<String>> cacheToIndexes;
+
+    /** Names of caches that were not found. */
+    private Set<String> notFoundCacheNames;
+
+    /** Names of cache indexes that were not found (cache -> set of indexes). */
+    private Map<String, Set<String>> notFoundIndexes;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildJobRes() {
+        // No-op.
+    }
+
+    /** */
+    public IndexRebuildJobRes(
+        Map<String, Set<String>> cacheToIndexes,
+        Map<String, Set<String>> notFoundIndexes,
+        Set<String> notFoundCacheNames
+    ) {
+        this.cacheToIndexes = cacheToIndexes;
+        this.notFoundIndexes = notFoundIndexes;
+        this.notFoundCacheNames = notFoundCacheNames;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void writeExternalData(ObjectOutput out) throws IOException {
+        U.writeMap(out, cacheToIndexes);
+        U.writeMap(out, notFoundIndexes);
+        U.writeCollection(out, notFoundCacheNames);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException {
+        cacheToIndexes = U.readMap(in);
+        notFoundIndexes = U.readMap(in);
+        notFoundCacheNames = U.readSet(in);
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskRes.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildTask.

Review Comment:
   Missing link.



##########
modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/maintenance/MaintenanceRebuildIndexUtils.java:
##########
@@ -96,6 +99,30 @@ public static MaintenanceTask toMaintenanceTask(int cacheId, String idxName) {
         );
     }
 
+    /**
+     * Constructs an index rebuild maintenance task based on a map cacheId -> indexes.
+     *

Review Comment:
   Please add example of parameters.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskRes.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildTask.
+ */
+public class IndexRebuildTaskRes extends IgniteDataTransferObject {
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskRes.java:
##########
@@ -0,0 +1,64 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.UUID;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Result of the IndexRebuildTask.
+ */
+public class IndexRebuildTaskRes extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Map node id -> rebuild command result. */
+    private Map<UUID, IndexRebuildJobRes> results;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildTaskRes() {
+        // No-op.
+    }
+
+    /** */
+    public IndexRebuildTaskRes(Map<UUID, IndexRebuildJobRes> results) {
+        this.results = results;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void writeExternalData(ObjectOutput out) throws IOException {
+        U.writeMap(out, results);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void readExternalData(byte protoVer, ObjectInput in) throws IOException, ClassNotFoundException {
+        results = U.readMap(in);
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/indexing/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTask.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.visor.cache.index;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor;
+import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing;
+import org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorMultiNodeTask;
+import org.apache.ignite.maintenance.MaintenanceRegistry;
+import org.apache.ignite.maintenance.MaintenanceTask;
+import org.h2.index.Index;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.mergeTasks;
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.toMaintenanceTask;
+
+/**
+ * Task that schedules indexes rebuild for specified caches via the maintenance mode.
+ */
+@GridInternal
+public class IndexRebuildTask extends VisorMultiNodeTask<IndexRebuildTaskArg, IndexRebuildTaskRes, IndexRebuildJobRes> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected IndexRebuildJob job(IndexRebuildTaskArg arg) {
+        return new IndexRebuildJob(arg, debug);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected @Nullable IndexRebuildTaskRes reduce0(List<ComputeJobResult> results) throws IgniteException {
+        Map<UUID, IndexRebuildJobRes> taskResultMap = results.stream()
+            .collect(Collectors.toMap(res -> res.getNode().id(), ComputeJobResult::getData));
+
+        return new IndexRebuildTaskRes(taskResultMap);
+    }
+
+    /** */
+    private static class IndexRebuildJob extends VisorJob<IndexRebuildTaskArg, IndexRebuildJobRes> {
+        /** */

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskArg.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Argument for the IndexRebuildTask.
+ */
+public class IndexRebuildTaskArg extends IgniteDataTransferObject {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** Cache name. */
+    private Map<String, Set<String>> cacheToIndexes;
+
+    /**
+     * Empty constructor required for Serializable.
+     */
+    public IndexRebuildTaskArg() {
+        // No-op.
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/indexing/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTask.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.visor.cache.index;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor;
+import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing;
+import org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorMultiNodeTask;
+import org.apache.ignite.maintenance.MaintenanceRegistry;
+import org.apache.ignite.maintenance.MaintenanceTask;
+import org.h2.index.Index;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.mergeTasks;
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.toMaintenanceTask;
+
+/**
+ * Task that schedules indexes rebuild for specified caches via the maintenance mode.
+ */
+@GridInternal
+public class IndexRebuildTask extends VisorMultiNodeTask<IndexRebuildTaskArg, IndexRebuildTaskRes, IndexRebuildJobRes> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected IndexRebuildJob job(IndexRebuildTaskArg arg) {
+        return new IndexRebuildJob(arg, debug);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected @Nullable IndexRebuildTaskRes reduce0(List<ComputeJobResult> results) throws IgniteException {
+        Map<UUID, IndexRebuildJobRes> taskResultMap = results.stream()
+            .collect(Collectors.toMap(res -> res.getNode().id(), ComputeJobResult::getData));
+
+        return new IndexRebuildTaskRes(taskResultMap);
+    }
+
+    /** */
+    private static class IndexRebuildJob extends VisorJob<IndexRebuildTaskArg, IndexRebuildJobRes> {
+        /** */
+        private static final long serialVersionUID = 0L;
+
+        /**
+         * Create job with specified argument.
+         *
+         * @param arg Job argument.
+         * @param debug Flag indicating whether debug information should be printed into node log.
+         */
+        protected IndexRebuildJob(@Nullable IndexRebuildTaskArg arg, boolean debug) {
+            super(arg, debug);
+        }
+
+        /** {@inheritDoc} */
+        @Override protected IndexRebuildJobRes run(@Nullable IndexRebuildTaskArg arg) throws IgniteException {
+            assert arg.cacheToIndexes() != null && !arg.cacheToIndexes().isEmpty() : "Cache to indexes map must be specified.";
+
+            Set<String> notFound = new HashSet<>();
+
+            GridCacheProcessor cacheProcessor = ignite.context().cache();
+
+            Map<String, Set<String>> cacheToIndexes = new HashMap<>();
+            Map<String, Set<String>> cacheToMissedIndexes = new HashMap<>();
+
+            for (Entry<String, Set<String>> indexesByCache : arg.cacheToIndexes().entrySet()) {
+                String cache = indexesByCache.getKey();
+                Set<String> indexesArg = indexesByCache.getValue();
+                int cacheId = CU.cacheId(cache);
+
+                GridCacheContext<?, ?> cacheCtx = cacheProcessor.context().cacheContext(cacheId);
+
+                if (cacheCtx == null) {
+                    notFound.add(cache);
+                    continue;
+                }
+
+                Set<String> existingIndexes = indexes(cache);
+                Set<String> indexesToRebuild = cacheToIndexes.computeIfAbsent(cache, s -> new HashSet<>());
+                Set<String> missedIndexes = cacheToMissedIndexes.computeIfAbsent(cache, s -> new HashSet<>());
+
+                if (indexesArg.isEmpty())
+                    indexesToRebuild.addAll(existingIndexes);
+                else {
+                    indexesArg.forEach(index -> {
+                        if (!existingIndexes.contains(index)) {
+                            missedIndexes.add(index);
+                            return;
+                        }
+
+                        indexesToRebuild.add(index);
+                    });
+                }
+            }
+
+            if (hasIndexes(cacheToIndexes)) {

Review Comment:
   Why not **!cacheToIndexes.isEmpty()**?



##########
modules/indexing/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTask.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.visor.cache.index;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.UUID;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.compute.ComputeJobResult;
+import org.apache.ignite.internal.processors.cache.GridCacheContext;
+import org.apache.ignite.internal.processors.cache.GridCacheProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryProcessor;
+import org.apache.ignite.internal.processors.query.GridQueryTypeDescriptor;
+import org.apache.ignite.internal.processors.query.h2.IgniteH2Indexing;
+import org.apache.ignite.internal.processors.query.h2.database.H2TreeIndexBase;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2Table;
+import org.apache.ignite.internal.processors.task.GridInternal;
+import org.apache.ignite.internal.util.typedef.internal.CU;
+import org.apache.ignite.internal.visor.VisorJob;
+import org.apache.ignite.internal.visor.VisorMultiNodeTask;
+import org.apache.ignite.maintenance.MaintenanceRegistry;
+import org.apache.ignite.maintenance.MaintenanceTask;
+import org.h2.index.Index;
+import org.jetbrains.annotations.Nullable;
+
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.mergeTasks;
+import static org.apache.ignite.internal.cache.query.index.sorted.maintenance.MaintenanceRebuildIndexUtils.toMaintenanceTask;
+
+/**
+ * Task that schedules indexes rebuild for specified caches via the maintenance mode.
+ */
+@GridInternal
+public class IndexRebuildTask extends VisorMultiNodeTask<IndexRebuildTaskArg, IndexRebuildTaskRes, IndexRebuildJobRes> {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** {@inheritDoc} */
+    @Override protected IndexRebuildJob job(IndexRebuildTaskArg arg) {
+        return new IndexRebuildJob(arg, debug);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected @Nullable IndexRebuildTaskRes reduce0(List<ComputeJobResult> results) throws IgniteException {
+        Map<UUID, IndexRebuildJobRes> taskResultMap = results.stream()
+            .collect(Collectors.toMap(res -> res.getNode().id(), ComputeJobResult::getData));
+
+        return new IndexRebuildTaskRes(taskResultMap);
+    }
+
+    /** */

Review Comment:
   Missing javadoc.



##########
modules/core/src/main/java/org/apache/ignite/internal/visor/cache/index/IndexRebuildTaskArg.java:
##########
@@ -0,0 +1,66 @@
+/*
+ * 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.visor.cache.index;
+
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.util.Map;
+import java.util.Set;
+import org.apache.ignite.internal.dto.IgniteDataTransferObject;
+import org.apache.ignite.internal.util.typedef.internal.U;
+
+/**
+ * Argument for the IndexRebuildTask.

Review Comment:
   Missing link.



##########
modules/indexing/src/test/java/org/apache/ignite/internal/processors/query/h2/maintenance/MaintenanceRebuildIndexUtilsSelfTest.java:
##########
@@ -117,4 +125,27 @@ public void testIndexNameWithSeparatorCharacter() {
         assertEquals(cacheId, target.cacheId());
         assertEquals(idxName, target.idxName());
     }
+
+    /**
+     * Tests that maintenance task can be constructed from a map.
+     */
+    @Test
+    public void testConstructFromMap() {
+        Map<Integer, Set<String>> cacheToIndexes = new HashMap<>();
+        cacheToIndexes.put(1, new HashSet<>(Arrays.asList("foo", "bar")));
+        cacheToIndexes.put(2, new HashSet<>(Arrays.asList("foo1", "bar1")));
+
+        MaintenanceTask task = toMaintenanceTask(cacheToIndexes);
+
+        List<MaintenanceRebuildIndexTarget> targets = parseMaintenanceTaskParameters(task.parameters());
+
+        assertEquals(4, targets.size());
+
+        Map<Integer, Set<String>> result = targets.stream().collect(groupingBy(
+            MaintenanceRebuildIndexTarget::cacheId,
+            mapping(MaintenanceRebuildIndexTarget::idxName, toSet())
+        ));
+
+        assertEquals(cacheToIndexes, result);

Review Comment:
   Please use **GridCommonAbstractTest#assertEqualsMaps**



-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org