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


##########
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:
   This is an offer.



-- 
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-unsubscr...@ignite.apache.org

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

Reply via email to