3 Commits 71d41daaf0 ... 4a76116685

Author SHA1 Message Date
  FutureYu 4a76116685 Merge branch 'changeToLongAdder' into optimize-netty 1 year ago
  FutureYu 81efbd5604 改用longAdder改进性能 1 year ago
  wenjie d054965992 优化Pcap文件生成,提升范围查询速度 1 year ago

+ 20 - 19
insert-app/src/main/java/db/entity/Statistic.java

@@ -6,6 +6,7 @@ import lombok.Data;
 
 import java.text.ParseException;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.concurrent.atomic.LongAdder;
 
 /**
  * 功能描述
@@ -16,51 +17,51 @@ import java.util.concurrent.atomic.AtomicLong;
 @Data
 public class Statistic {
     long beginTime;
-    AtomicLong dataCount = new AtomicLong();
-    AtomicLong dataCountTsdbSuccess = new AtomicLong();
-    AtomicLong dataCountDiskSuccess = new AtomicLong();
+    LongAdder dataCount = new LongAdder();
+    LongAdder dataCountTsdbSuccess = new LongAdder();
+    LongAdder dataCountDiskSuccess = new LongAdder();
 
     public Statistic() {
         beginTime = System.currentTimeMillis();
     }
 
-    public long incrDataCount() {
-        return this.dataCount.incrementAndGet();
+    public void incrDataCount() {
+        this.dataCount.increment();
     }
 
-    public long increDataCountTsdbSuccess() {
-        return this.dataCountTsdbSuccess.incrementAndGet();
+    public void increDataCountTsdbSuccess() {
+        this.dataCountTsdbSuccess.increment();
     }
 
-    public long increDataCountTsdbSuccess(int count) {
-        return this.dataCountTsdbSuccess.addAndGet(count);
+    public void increDataCountTsdbSuccess(int count) {
+        this.dataCountTsdbSuccess.add(count);
     }
 
-    public long increDataCountDiskSuccess() {
-        return this.dataCountDiskSuccess.incrementAndGet();
+    public void increDataCountDiskSuccess() {
+        this.dataCountDiskSuccess.increment();
     }
 
-    public long increDataCountDiskSuccess(int count) {
-        return this.dataCountDiskSuccess.addAndGet(count);
+    public void increDataCountDiskSuccess(int count) {
+        this.dataCountDiskSuccess.add(count);
     }
 
     public double dataRate() {
         long time = System.currentTimeMillis() - this.beginTime;
-        return dataCount.get() * 1000.0 / time;
+        return dataCount.longValue() * 1000.0 / time;
     }
 
     public StatisticDto toDto() {
-        StatisticDto statisticDto = new StatisticDto(this.beginTime, this.dataCount.get(),
-                this.dataCountTsdbSuccess.get(), this.dataCountDiskSuccess.get());
+        StatisticDto statisticDto = new StatisticDto(this.beginTime, this.dataCount.longValue(),
+                this.dataCountTsdbSuccess.longValue(), this.dataCountDiskSuccess.longValue());
         return statisticDto;
     }
 
     @Override
     public String toString() {
         long time = System.currentTimeMillis() - this.beginTime;
-        double dataRate = dataCount.get() * 1000.0 / time;
-        double dataCountTsdbSuccessRate = dataCountTsdbSuccess.get() * 1000.0 / time;
-        double dataCountDiskSuccessRate = dataCountDiskSuccess.get() * 1000.0 / time;
+        double dataRate = dataCount.longValue() * 1000.0 / time;
+        double dataCountTsdbSuccessRate = dataCountTsdbSuccess.longValue() * 1000.0 / time;
+        double dataCountDiskSuccessRate = dataCountDiskSuccess.longValue() * 1000.0 / time;
 
         String beginTimeString = "";
         try {

+ 10 - 10
insert-app/src/main/java/db/service/StatisticService.java

@@ -15,24 +15,24 @@ public class StatisticService {
 
     Statistic statistic = new Statistic();
 
-    public long incrDataCount() {
-        return this.statistic.incrDataCount();
+    public void incrDataCount() {
+        this.statistic.incrDataCount();
     }
 
-    public long increDataCountTsdbSuccess() {
-        return this.statistic.increDataCountTsdbSuccess();
+    public void increDataCountTsdbSuccess() {
+        this.statistic.increDataCountTsdbSuccess();
     }
 
-    public long increDataCountTsdbSuccess(int count) {
-        return this.statistic.increDataCountTsdbSuccess(count);
+    public void increDataCountTsdbSuccess(int count) {
+        this.statistic.increDataCountTsdbSuccess(count);
     }
 
-    public long increDataCountDiskSuccess() {
-        return this.statistic.increDataCountDiskSuccess();
+    public void increDataCountDiskSuccess() {
+        this.statistic.increDataCountDiskSuccess();
     }
 
-    public long increDataCountDiskSuccess(int count) {
-        return this.statistic.increDataCountDiskSuccess(count);
+    public void increDataCountDiskSuccess(int count) {
+        this.statistic.increDataCountDiskSuccess(count);
     }
 
     public Statistic getStatistic() {

+ 0 - 6
query-app/pom.xml

@@ -45,12 +45,6 @@
             <version>1.8.2</version>
             <scope>test</scope>
         </dependency>
-        <dependency>
-            <groupId>org.pcap4j</groupId>
-            <artifactId>pcap4j-core</artifactId>
-            <version>1.8.2</version>
-            <scope>compile</scope>
-        </dependency>
     </dependencies>
 
     <build>

+ 16 - 9
query-app/src/main/java/db/service/QueryService.java

@@ -8,6 +8,7 @@ import db.mapper.LogFileMetaMapper;
 import db.util.ConvertFormat;
 import db.util.SpentTimeCalculator;
 import db.util.Util;
+import db.utils.PcapHelper;
 import lombok.extern.slf4j.Slf4j;
 import org.pcap4j.core.*;
 import org.pcap4j.packet.UnknownPacket;
@@ -16,6 +17,7 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.PostConstruct;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.text.ParseException;
 import java.util.ArrayList;
@@ -84,7 +86,7 @@ public class QueryService {
         spentTimeCalculator.begin();
         HashMap<Long, ArrayList<Integer>> map = new ConvertFormat().convertToLongAndGetIndex(lists);
         spentTimeCalculator.end();
-        log.info("记录分片耗时:{}",spentTimeCalculator.getSpendTime());
+        log.info("记录分片耗时:{}", spentTimeCalculator.getSpendTime());
 
         ArrayList<byte[]> logItems = new ArrayList<>();
         for (Long key : map.keySet()) {
@@ -115,7 +117,7 @@ public class QueryService {
         return String.valueOf(returnLink);
     }
 
-    public String queryByTime(String start, String end) throws ParseException, IOException, PcapNativeException, NotOpenException {
+    public String queryByTime(String start, String end) throws ParseException, IOException {
 
         long startTime = Util.dateStringToUTCMilliSeconds(start);
         long endTime = Util.dateStringToUTCMilliSeconds(end);
@@ -130,25 +132,29 @@ public class QueryService {
         spentTimeCalculator.end();
         log.debug("queryByTime,查询sqlite,查询条目:{},查询时间:{}", logFileMetas.size(), spentTimeCalculator.getSpendTime());
 
-        PcapHandle handle = Pcaps.openOffline(pcapTemplate);
         String pcapName = new StringBuilder().append(System.currentTimeMillis())
                 .append(startTime).append(endTime).toString();
-        PcapDumper dumper = handle.dumpOpen(pcapPrefix + pcapName + pcapSuffix);
 
         spentTimeCalculator.begin();
+        PcapHelper pcapHelper = new PcapHelper();
         for (LogFileMeta logFileMeta : logFileMetas) {
             List<byte[]> bytes = logFileService.queryLogBetweenTimeRange(logFileMeta, startTime, endTime);
             for (byte[] item : bytes) {
-                UnknownPacket packet = UnknownPacket.newPacket(item, 0, item.length);
-                dumper.dump(packet);
+                pcapHelper.addItem(item.length, item);
             }
         }
+
+        try (FileOutputStream fileOutputStream = new FileOutputStream(pcapPrefix + pcapName + pcapSuffix)) {
+            byte[] bytes = pcapHelper.getBytes();
+            fileOutputStream.write(bytes, 0, bytes.length);
+            fileOutputStream.flush();
+        } catch (IOException e) {
+            log.info("生成pcap文件失败:{}",e);
+        }
+
         spentTimeCalculator.end();
         log.info("queryByTime,查询磁盘并生成pcap总时间:{}", spentTimeCalculator.getSpendTime());
 
-        dumper.close();
-        handle.close();
-
         StringBuilder returnLink = new StringBuilder();
         returnLink.append("http://").append(hostIP).append(":")
                 .append(serverPort).append("/download?pcapName=").append(pcapName);
@@ -156,4 +162,5 @@ public class QueryService {
         log.info("返回链接:{}", returnLink);
         return String.valueOf(returnLink);
     }
+
 }

+ 13 - 0
query-app/src/main/java/db/util/ConvertFormat.java

@@ -42,4 +42,17 @@ public class ConvertFormat {
 
         return map;
     }
+    /**
+     * 将int转为低字节在前,高字节在后的byte数组(小端)
+     * @param n int
+     * @return byte[]
+     */
+    public static byte[] intToByteLittle(int n) {
+        byte[] b = new byte[4];
+        b[0] = (byte) (n & 0xff);
+        b[1] = (byte) (n >> 8 & 0xff);
+        b[2] = (byte) (n >> 16 & 0xff);
+        b[3] = (byte) (n >> 24 & 0xff);
+        return b;
+    }
 }

+ 34 - 0
query-app/src/main/java/db/utils/PcapHelper.java

@@ -0,0 +1,34 @@
+package db.utils;
+
+import db.util.ConvertFormat;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.ByteBufUtil;
+import io.netty.buffer.Unpooled;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.stereotype.Component;
+
+@Component
+@Slf4j
+public class PcapHelper {
+    ByteBuf buf = Unpooled.buffer();
+    public PcapHelper(){
+        buf.writeInt(0xd4c3b2a1);
+        buf.writeShort(0x0200);
+        buf.writeShort(0x0400);
+        buf.writeInt(0x0);
+        buf.writeInt(0x0);
+        buf.writeInt(0x00000400);
+        buf.writeInt(0x01000000);
+    }
+    public void addItem(int len,byte[] bytes){
+        buf.writeInt(0x0);
+        buf.writeInt(0x0);
+        buf.writeBytes(ConvertFormat.intToByteLittle(len));
+        buf.writeBytes(ConvertFormat.intToByteLittle(len));
+        buf.writeBytes(bytes);
+
+    }
+    public byte[] getBytes(){
+        return ByteBufUtil.getBytes(buf);
+    }
+}