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 2020/04/02 09:42:35 UTC

[GitHub] [ignite] Vladsz83 commented on a change in pull request #7446: IGNITE-12464 : Service metrics

Vladsz83 commented on a change in pull request #7446: IGNITE-12464 : Service metrics
URL: https://github.com/apache/ignite/pull/7446#discussion_r401141419
 
 

 ##########
 File path: modules/core/src/main/java/org/apache/ignite/internal/processors/service/IgniteServiceProcessor.java
 ##########
 @@ -1801,4 +1842,178 @@ private boolean enterBusy() {
     private void leaveBusy() {
         opsLock.readLock().unlock();
     }
+
+    /**
+     * Registers metrics to measure durations of service methods.
+     *
+     * @param srvc Service for invocations measurement.
+     * @param srvcName Name of {@code srvc}.
+     */
+    private void registerMetrics(Service srvc, String srvcName) {
+        getInterfaces(srvc.getClass()).stream().map(Class::getMethods).flatMap(Arrays::stream)
+            .filter(mtd -> !isMetricIgnoredFor(mtd.getDeclaringClass()))
+            .forEach(mtd -> {
+                // All metrics for current service.
+                Map<String, MethodHistogramHolder> srvcHistograms =
+                    invocationHistograms.computeIfAbsent(srvcName, name -> new HashMap<>(1));
+
+                // Histograms for this method name.
+                MethodHistogramHolder mtdHistograms =
+                    srvcHistograms.computeIfAbsent(mtd.getName(), mdtName -> new MethodHistogramHolder());
+
+                mtdHistograms.addIfAbsent(mtd, () -> createHistogram(srvcName, mtd));
+            });
+    }
+
+    /**
+     * Removes metrics for service {@code srvcName}.
+     *
+     * @param srvcName Service name.
+     */
+    private void unregisterMetrics(String srvcName) {
+        ctx.metric().remove(serviceMetricRegistryName(srvcName));
+
+        invocationHistograms.remove(srvcName);
+    }
+
+    /**
+     * Creates histogram for service method. If exist,s considers one or several argument types has same name but
+     * different package and tries to extend metric name with abbreviation of java package name.
+     *
+     * @param srvcName Service name.
+     * @param method Method to measure.
+     * @return Histogram of service method timings.
+     */
+    HistogramMetricImpl createHistogram(String srvcName, Method method) {
+        MetricRegistry metricRegistry = ctx.metric().registry(serviceMetricRegistryName(srvcName));
+
+        HistogramMetricImpl histogram = null;
+
+        // Find/create histogram.
+        for (int i = 0; i <= MAX_ABBREVIATE_NAME_LVL; ++i) {
+            String methodMetricName = methodMetricName(method, i);
+
+            synchronized (metricRegistry) {
+                // If the metric exists skip and try extending metric name in next cycle.
+                if (metricRegistry.findMetric(methodMetricName) == null) {
+                    histogram = metricRegistry.histogram(methodMetricName, DEFAULT_INVOCATION_BOUNDS,
+                        DESCRIPTION_OF_INVOCATION_METRIC);
+
+                    break;
+                }
+            }
+        }
+
+        assert histogram != null;
+
+        return histogram;
+    }
+
+    /**
+     * @param method Method for the invocation timings.
+     * @param pkgNameDepth Level of package name abbreviation. See {@link MetricUtils#abbreviateName(Class, int)}.
+     * @return Metric name for {@code method}.
+     */
+    static String methodMetricName(Method method, int pkgNameDepth) {
+        StringBuilder sb = new StringBuilder();
+
+        sb.append(abbreviateName(method.getReturnType(), pkgNameDepth));
+        sb.append(" ");
+        sb.append(method.getName());
+        sb.append("(");
+        sb.append(Stream.of(method.getParameterTypes()).map(t -> abbreviateName(t, pkgNameDepth))
+            .collect(Collectors.joining(", ")));
+        sb.append(")");
+
+        return sb.toString();
+    }
+
+    /** @return {@code True} if metrics should not be created for this class or interface. */
+    private static boolean isMetricIgnoredFor(Class<?> cls){
+        return Object.class.equals(cls) || Service.class.equals(cls) || Externalizable.class.equals(cls);
+    }
+
+    /**
+     * Searches histogram for service method.
+     *
+     * @param srvcName Service name.
+     * @param mtd Service method.
+     * @return Histogram for {@code srvcName} and {@code mtd} or {@code null} if not found.
+     */
+    HistogramMetricImpl histogram(String srvcName, Method mtd) {
 
 Review comment:
   @ivandasch Yep, just realized we can lose value on parallel puts. Doesn’t matter. I finally remembered the reason why staying with simple HashMap without sync blocks or any other concurrency protections. Service deploying/un deploying are guided by discovery messages which are serial. Actually, all we do here we do in single thread. WDYT?

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