ignite-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [ignite] alex-plekhanov commented on a change in pull request #6845: IGNITE-12145: Monitoring list engine.
Date Mon, 09 Sep 2019 13:32:47 GMT
alex-plekhanov commented on a change in pull request #6845: IGNITE-12145: Monitoring list engine.
URL: https://github.com/apache/ignite/pull/6845#discussion_r321754188
 
 

 ##########
 File path: modules/codegen/src/main/java/org/apache/ignite/codegen/MonitoringRowAttributeWalkerGenerator.java
 ##########
 @@ -0,0 +1,283 @@
+/*
+ * 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.codegen;
+
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.function.ObjIntConsumer;
+import org.apache.ignite.internal.processors.metric.list.walker.Order;
+import org.apache.ignite.spi.metric.jmx.MonitoringListMBean;
+import org.apache.ignite.spi.metric.list.MonitoringList;
+import org.apache.ignite.spi.metric.list.MonitoringRow;
+import org.apache.ignite.spi.metric.list.MonitoringRowAttributeWalker;
+import org.apache.ignite.spi.metric.list.view.CacheGroupView;
+import org.apache.ignite.spi.metric.list.view.CacheView;
+import org.apache.ignite.spi.metric.list.view.ComputeTaskView;
+import org.apache.ignite.spi.metric.list.view.ServiceView;
+
+import static org.apache.ignite.codegen.MessageCodeGenerator.DFLT_SRC_DIR;
+import static org.apache.ignite.codegen.MessageCodeGenerator.TAB;
+
+/**
+ * Application for code generation of {@link MonitoringRowAttributeWalker}.
+ * Usage: simply run main method from Ignite source folder(using IDE or other way).
+ * Generated code used in {@link MonitoringList}.
+ *
+ * @see MonitoringListMBean
+ * @see MonitoringListLocalSystemView
+ */
+public class MonitoringRowAttributeWalkerGenerator {
+    /** Methods that should be excluded from specific {@link MonitoringRowAttributeWalker}.
*/
+    private static final Set<String> SYS_METHODS = new HashSet<>(Arrays.asList("equals",
"hashCode", "toString",
+        "getClass", "monitoringRowId"));
+
+    /** Package for {@link MonitoringRowAttributeWalker} implementations. */
+    public static final String WALKER_PACKAGE = "org.apache.ignite.internal.processors.metric.list.walker";
+
+    /**
+     * @throws Exception If generation failed.
+     */
+    public static void main(String[] args) throws Exception {
+        MonitoringRowAttributeWalkerGenerator gen = new MonitoringRowAttributeWalkerGenerator();
+
+        gen.generateAndWrite(CacheGroupView.class, DFLT_SRC_DIR);
+        gen.generateAndWrite(CacheView.class, DFLT_SRC_DIR);
+        gen.generateAndWrite(ServiceView.class, DFLT_SRC_DIR);
+        gen.generateAndWrite(ComputeTaskView.class, DFLT_SRC_DIR);
+    }
+
+    /**
+     * Generates {@link MonitoringRowAttributeWalker} implementation and write it to the
file.
+     *
+     * @param clazz Class to geneare {@link MonitoringRowAttributeWalker} for.
+     * @param srcRoot Source root folder.
+     * @param <T> type of the {@link MonitoringRow}.
+     * @throws IOException If generation failed.
+     */
+    private <T extends MonitoringRow<?>> void generateAndWrite(Class<T>
clazz, String srcRoot) throws IOException {
+        File walkerClass = new File(srcRoot + '/' + WALKER_PACKAGE.replaceAll("\\.", "/")
+ '/' +
+            clazz.getSimpleName() + "Walker.java");
+
+        Collection<String> code = generate(clazz);
+
+        walkerClass.createNewFile();
+
+        try (FileWriter writer = new FileWriter(walkerClass)) {
+            for (String line : code) {
+                writer.write(line);
+                writer.write('\n');
+            }
+        }
+    }
+
+    /**
+     * Generates {@link MonitoringRowAttributeWalker} implementation.
+     *
+     * @param clazz Class to geneare {@link MonitoringRowAttributeWalker} for.
+     * @param <T> type of the {@link MonitoringRow}.
+     * @return Java source code of the {@link MonitoringRowAttributeWalker} implementation.
+     */
+    private <T extends MonitoringRow<?>> Collection<String> generate(Class<T>
clazz) {
+        final List<String> code = new ArrayList<>();
+        final Set<String> imports = new TreeSet<>();
+
+        imports.add("import " + MonitoringRowAttributeWalker.class.getName() + ';');
+        imports.add("import " + clazz.getName() + ';');
+
+        String simpleName = clazz.getSimpleName();
+
+        code.add("package " + WALKER_PACKAGE + ";");
+        code.add("");
+        code.add("/**");
+        code.add(" * Generated by {@code " + MonitoringRowAttributeWalkerGenerator.class.getName()
+ "}.");
+        code.add(" * {@link " + simpleName + "} attributes walker.");
+        code.add(" * ");
+        code.add(" * @see " + simpleName);
+        code.add(" */");
+        code.add("public class " + simpleName + "Walker implements MonitoringRowAttributeWalker<"
+ simpleName + "> {");
+        code.add(TAB + "/** {@inheritDoc} */");
+        code.add(TAB + "@Override public void visitAll(AttributeVisitor v) {");
+
+        forEachMethod(clazz, (m, i) -> {
+            String name = m.getName();
+
+            Class<?> retClazz = m.getReturnType();
+
+            String line = TAB + TAB;
+
+            if (!retClazz.isPrimitive()) {
+                if (!retClazz.getName().startsWith("java.lang"))
+                    imports.add("import " + retClazz.getName() + ';');
+
+                line += "v.accept(" + i + ", \"" + name + "\", " + retClazz.getSimpleName()
+ ".class);";
+            } else if (retClazz == boolean.class)
 
 Review comment:
   NL

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message