luchunliang commented on code in PR #9905:
URL: https://github.com/apache/inlong/pull/9905#discussion_r1544285276


##########
inlong-audit/audit-sdk/src/main/java/org/apache/inlong/audit/AuditReporterImpl.java:
##########
@@ -198,50 +219,201 @@ public void add(int auditID, String auditTag, String 
inlongGroupID, String inlon
         keyJoiner.add(String.valueOf(auditID));
         keyJoiner.add(auditTag);
         keyJoiner.add(String.valueOf(auditVersion));
-        addByKey(keyJoiner.toString(), count, size, delayTime);
+        addByKey(DEFAULT_ISOLATE_KEY, keyJoiner.toString(), count, size, 
delayTime);
+    }
+
+    public void add(AuditDimensions dimensions, AuditValues values) {
+        StringJoiner keyJoiner = new StringJoiner(FIELD_SEPARATORS);
+        keyJoiner.add(String.valueOf(dimensions.getLogTime() / PERIOD));
+        keyJoiner.add(dimensions.getInlongGroupID());
+        keyJoiner.add(dimensions.getInlongStreamID());
+        keyJoiner.add(String.valueOf(dimensions.getAuditID()));
+        keyJoiner.add(dimensions.getAuditTag());
+        keyJoiner.add(String.valueOf(dimensions.getAuditVersion()));
+        addByKey(dimensions.getIsolateKey(), keyJoiner.toString(), 
values.getCount(),
+                values.getSize(), values.getDelayTime());
     }
 
     /**
      * Add audit info by key.
      */
-    private void addByKey(String key, long count, long size, long delayTime) {
-        if (countMap.get(key) == null) {
-            countMap.put(key, new StatInfo(0L, 0L, 0L));
+    private void addByKey(long isolateKey, String statKey, long count, long 
size, long delayTime) {
+        if (null == this.preStatMap.get(isolateKey)) {
+            this.preStatMap.putIfAbsent(isolateKey, new ConcurrentHashMap<>());
+        }
+        ConcurrentHashMap<String, StatInfo> statMap = 
this.preStatMap.get(isolateKey);
+        if (null == statMap.get(statKey)) {
+            statMap.putIfAbsent(statKey, new StatInfo(0L, 0L, 0L));
         }
-        countMap.get(key).count.addAndGet(count);
-        countMap.get(key).size.addAndGet(size);
-        countMap.get(key).delay.addAndGet(delayTime);
+        StatInfo stat = statMap.get(statKey);
+        stat.count.addAndGet(count);
+        stat.size.addAndGet(size);
+        stat.delay.addAndGet(delayTime);
+    }
+
+    /**
+     * Flush audit data by default audit version
+     */
+    public synchronized void flush() {
+        flush(DEFAULT_AUDIT_VERSION);
     }
 
     /**
-     * Send audit data
+     * Flush audit data
      */
-    public synchronized void send() {
+    public synchronized void flush(long isolateKey) {
+        if (flushTime.putIfAbsent(isolateKey, 
Calendar.getInstance().getTimeInMillis()) != null
+                || flushStat.addAndGet(1) > flushStatThreshold) {
+            return;
+        }
+        LOGGER.info("Audit flush isolate key {} ", isolateKey);
         manager.clearBuffer();
         resetStat();
-        // Retrieve statistics from the list of objects without statistics to 
be eliminated
-        for (Map.Entry<String, StatInfo> entry : 
this.deleteCountMap.entrySet()) {
-            this.sumThreadGroup(entry.getKey(), entry.getValue());
+        LOGGER.info("pre stat map size {} {} {} {}", this.preStatMap.size(), 
this.expiredStatMap.size(),
+                this.summaryStatMap.size(), this.expiredKeyList.size());
+
+        summaryExpiredStatMap(isolateKey);
+
+        Iterator<Map.Entry<Long, ConcurrentHashMap<String, StatInfo>>> 
iterator = this.preStatMap.entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry<Long, ConcurrentHashMap<String, StatInfo>> entry = 
iterator.next();
+            if (entry.getValue().isEmpty()) {
+                LOGGER.info("Remove the key of pre stat map: {},isolate key: 
{} ", entry.getKey(), isolateKey);
+                iterator.remove();
+                continue;
+            }
+            if (entry.getKey() > isolateKey) {
+                continue;
+            }
+            summaryPreStatMap(entry.getKey(), entry.getValue());
+            send(entry.getKey());
+
+        }
+
+        clearExpiredKey(isolateKey);
+
+        LOGGER.info("Finish report audit data");
+    }
+
+    /**
+     * Send base command
+     */
+    private void sendByBaseCommand(AuditApi.AuditRequest auditRequest) {
+        AuditApi.BaseCommand.Builder baseCommand = 
AuditApi.BaseCommand.newBuilder();
+        
baseCommand.setType(AUDIT_REQUEST).setAuditRequest(auditRequest).build();
+        manager.send(baseCommand.build(), auditRequest);
+    }
+
+    /**
+     * Summary
+     */
+    private void sumThreadGroup(long isolateKey, String key, StatInfo 
statInfo) {
+        long count = statInfo.count.getAndSet(0);
+        if (0 == count) {
+            return;
+        }
+        ConcurrentHashMap<String, StatInfo> sumMap =
+                this.summaryStatMap.computeIfAbsent(isolateKey, k -> new 
ConcurrentHashMap<>());
+        StatInfo stat = sumMap.computeIfAbsent(key, k -> new StatInfo(0L, 0L, 
0L));
+        stat.count.addAndGet(count);
+        stat.size.addAndGet(statInfo.size.getAndSet(0));
+        stat.delay.addAndGet(statInfo.delay.getAndSet(0));
+    }
+
+    /**
+     * Reset statistics
+     */
+    private void resetStat() {
+        dataId = 0;
+        packageId = 1;
+    }
+
+    /**
+     * Summary expired stat map
+     */
+    private void summaryExpiredStatMap(long isolateKey) {
+        Iterator<Map.Entry<Long, ConcurrentHashMap<String, StatInfo>>> 
iterator =
+                this.expiredStatMap.entrySet().iterator();
+        while (iterator.hasNext()) {
+            Map.Entry<Long, ConcurrentHashMap<String, StatInfo>> entry = 
iterator.next();
+            if (entry.getValue().isEmpty()) {
+                LOGGER.info("Remove the key of expired stat map: {},isolate 
key: {} ", entry.getKey(), isolateKey);
+                iterator.remove();

Review Comment:
   It has synchronization issues between "entry.getValue().isEmpty()" and 
"iterator.remove()".



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to