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:
   As far as I know, concurrent put/get/remove are not atomic but are safe 
meaning do not fail or throw an exception. Iteration is a problem here. You can 
only get null when a value exist, or a value when it is already removed.
   
   Please, take a look to this simple test. Forks for me. Gives null 
periodically, and, I believe, inconsistent values. But no any lock/crash 
scenario. What else should we check?
   
   ```
   @Test
       public void testMapConcurrency() throws InterruptedException {
           final Map<Integer, Integer> map = new HashMap<>(1);
           final Random rnd = new Random();
           final int valueCnt = 100000;
           final Integer[] holder = new Integer[1];
   
           for(int i=0; i<5; ++i) {
               new Thread(() -> {
                   while (true) {
                       for (int v = 0; v < valueCnt; ++v)
                           map.put(v, rnd.nextInt(valueCnt));
   
                       System.err.println("Filled");
   
                       try {
                           Thread.sleep(rnd.nextInt(5000));
                       }
                       catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                   }
               }, "putter_"+i).start();
           }
   
           for(int i=0; i<30; ++i) {
               new Thread(() -> {
                   while (true) {
   
                       try {
                           holder[0] = map.get(rnd.nextInt(valueCnt));
   
                           Thread.sleep(rnd.nextInt(50));
                       }
                       catch (InterruptedException e) {
                           e.printStackTrace();
                       }
                       catch (Exception e) {
                           e.printStackTrace();
                       }
                   }
               }, "getter_"+i).start();
           }
   
           for(int i=0; i<5; ++i) {
               new Thread(() -> {
                   while (true) {
                       try {
                           Thread.sleep(rnd.nextInt(5000));
                       }
                       catch (InterruptedException e) {
                           e.printStackTrace();
                       }
   
                       System.err.println("Clearing");
   
                       map.clear();
   
                       System.err.println("Cleared");
                   }
               }, "clearer_"+i).start();
           }
   
           Thread.sleep(5*60000);
       }
   ```
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to