2020-06-28 18:20:38 +08:00
|
|
|
|
package cn.ac.iie.dao;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.ac.iie.config.ApplicationConfig;
|
2020-07-20 19:36:32 +08:00
|
|
|
|
import cn.ac.iie.service.ingestion.ReadHistoryArangoData;
|
2020-06-28 18:20:38 +08:00
|
|
|
|
import cn.ac.iie.utils.ArangoDBConnect;
|
|
|
|
|
|
import cn.ac.iie.utils.ExecutorThreadPool;
|
|
|
|
|
|
import com.arangodb.ArangoCursor;
|
|
|
|
|
|
import com.arangodb.entity.BaseDocument;
|
|
|
|
|
|
import com.arangodb.entity.BaseEdgeDocument;
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2020-07-20 19:36:32 +08:00
|
|
|
|
import java.util.concurrent.CountDownLatch;
|
2020-06-28 18:20:38 +08:00
|
|
|
|
|
2020-07-13 20:00:59 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 获取arangoDB历史数据
|
|
|
|
|
|
*/
|
2020-06-28 18:20:38 +08:00
|
|
|
|
public class BaseArangoData {
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(BaseArangoData.class);
|
|
|
|
|
|
|
2020-07-20 19:36:32 +08:00
|
|
|
|
public static ConcurrentHashMap<String, BaseDocument> historyVertexFqdnMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
public static ConcurrentHashMap<String, BaseDocument> historyVertexIpMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
public static ConcurrentHashMap<String, BaseDocument> historyVertexSubscriberMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
public static ConcurrentHashMap<String, BaseEdgeDocument> historyRelationFqdnAddressIpMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
public static ConcurrentHashMap<String, BaseEdgeDocument> historyRelationIpVisitFqdnMap = new ConcurrentHashMap<>();
|
|
|
|
|
|
public static ConcurrentHashMap<String, BaseEdgeDocument> historyRelationSubsciberLocateIpMap = new ConcurrentHashMap<>();
|
2020-06-28 18:20:38 +08:00
|
|
|
|
|
2020-07-17 19:28:03 +08:00
|
|
|
|
private static ArangoDBConnect arangoDBConnect = ArangoDBConnect.getInstance();
|
2020-06-28 18:20:38 +08:00
|
|
|
|
|
2020-07-20 19:36:32 +08:00
|
|
|
|
private ExecutorThreadPool threadPool = ExecutorThreadPool.getInstance();
|
2020-06-28 18:20:38 +08:00
|
|
|
|
|
2020-07-20 19:36:32 +08:00
|
|
|
|
<T extends BaseDocument> void readHistoryData(String table, ConcurrentHashMap<String, T> map, Class<T> type){
|
2020-07-08 19:44:46 +08:00
|
|
|
|
try {
|
2020-07-20 19:36:32 +08:00
|
|
|
|
long start = System.currentTimeMillis();
|
|
|
|
|
|
CountDownLatch countDownLatch = new CountDownLatch(ApplicationConfig.THREAD_POOL_NUMBER);
|
|
|
|
|
|
long[] timeRange = getTimeRange(table);
|
2020-07-08 19:44:46 +08:00
|
|
|
|
for (int i = 0; i < ApplicationConfig.THREAD_POOL_NUMBER; i++) {
|
2020-07-20 19:36:32 +08:00
|
|
|
|
String sql = getQuerySql(timeRange, i, table);
|
|
|
|
|
|
ReadHistoryArangoData<T> readHistoryArangoData = new ReadHistoryArangoData<>(arangoDBConnect, sql, map,type,table,countDownLatch);
|
2020-07-08 19:44:46 +08:00
|
|
|
|
threadPool.executor(readHistoryArangoData);
|
|
|
|
|
|
}
|
2020-07-20 19:36:32 +08:00
|
|
|
|
countDownLatch.await();
|
|
|
|
|
|
long last = System.currentTimeMillis();
|
|
|
|
|
|
LOG.info("读取"+table+" arangoDB 共耗时:"+(last-start));
|
|
|
|
|
|
LOG.info(table+" history Map大小为:"+map.size());
|
2020-07-08 19:44:46 +08:00
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
e.printStackTrace();
|
2020-06-28 18:20:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-08 19:44:46 +08:00
|
|
|
|
private long[] getTimeRange(String table){
|
2020-06-28 18:20:38 +08:00
|
|
|
|
long minTime = 0L;
|
|
|
|
|
|
long maxTime = 0L;
|
|
|
|
|
|
long startTime = System.currentTimeMillis();
|
2020-07-08 19:44:46 +08:00
|
|
|
|
String sql = "LET doc = (FOR doc IN "+table+" RETURN doc) return {max_time:MAX(doc[*].FIRST_FOUND_TIME),min_time:MIN(doc[*].FIRST_FOUND_TIME)}";
|
2020-06-28 18:20:38 +08:00
|
|
|
|
ArangoCursor<BaseDocument> timeDoc = arangoDBConnect.executorQuery(sql, BaseDocument.class);
|
|
|
|
|
|
try {
|
|
|
|
|
|
if (timeDoc != null){
|
|
|
|
|
|
while (timeDoc.hasNext()) {
|
|
|
|
|
|
BaseDocument doc = timeDoc.next();
|
|
|
|
|
|
maxTime = Long.parseLong(doc.getAttribute("max_time").toString()) + ApplicationConfig.THREAD_POOL_NUMBER;
|
|
|
|
|
|
minTime = Long.parseLong(doc.getAttribute("min_time").toString());
|
|
|
|
|
|
}
|
|
|
|
|
|
long lastTime = System.currentTimeMillis();
|
2020-06-29 19:06:23 +08:00
|
|
|
|
LOG.info(sql+"\n查询最大最小时间用时:" + (lastTime - startTime));
|
2020-06-28 18:20:38 +08:00
|
|
|
|
}else {
|
|
|
|
|
|
LOG.warn("获取ArangoDb时间范围为空");
|
|
|
|
|
|
}
|
|
|
|
|
|
}catch (Exception e){
|
2020-07-08 19:44:46 +08:00
|
|
|
|
e.printStackTrace();
|
2020-06-28 18:20:38 +08:00
|
|
|
|
}
|
2020-07-08 19:44:46 +08:00
|
|
|
|
return new long[]{minTime, maxTime};
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String getQuerySql(long[] timeRange,int threadNumber,String table){
|
|
|
|
|
|
long minTime = timeRange[0];
|
|
|
|
|
|
long maxTime = timeRange[1];
|
|
|
|
|
|
long diffTime = (maxTime - minTime) / ApplicationConfig.THREAD_POOL_NUMBER;
|
|
|
|
|
|
long maxThreadTime = minTime + (threadNumber + 1)* diffTime;
|
|
|
|
|
|
long minThreadTime = minTime + threadNumber * diffTime;
|
|
|
|
|
|
return "FOR doc IN "+table+" filter doc.FIRST_FOUND_TIME >= "+minThreadTime+" and doc.FIRST_FOUND_TIME <= "+maxThreadTime+" RETURN doc";
|
2020-06-28 18:20:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|