Explorar o código

优化Pcap文件生成,提升范围查询速度

wenjie hai 1 ano
pai
achega
d054965992

+ 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);
+    }
+}