From ec6501b0ae91ac3c8f1f3fdc599a629774fd6ae7 Mon Sep 17 00:00:00 2001 From: "zhang.xin" Date: Wed, 9 Mar 2016 19:40:02 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../analysis/chainbuild/ChainBuildMapper.java | 1 + .../chainbuild/ChainBuildReducer.java | 1 + .../analysis/mapper/CallChainMapperTest.java | 97 +++++++++++++++---- 3 files changed, 81 insertions(+), 18 deletions(-) diff --git a/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildMapper.java b/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildMapper.java index 694b5df6a904..fdcaa2f722e7 100644 --- a/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildMapper.java +++ b/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildMapper.java @@ -58,6 +58,7 @@ protected void map(ImmutableBytesWritable key, Result value, Context context) context.write( new Text(chainInfo.getEntranceNodeToken()), new Text(new Gson().toJson(chainInfo))); } catch (Exception e) { + e.printStackTrace(); logger.error("Failed to mapper call chain[" + key.toString() + "]", e); } diff --git a/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildReducer.java b/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildReducer.java index d326ba783108..aa9cc6208909 100644 --- a/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildReducer.java +++ b/skywalking-analysis/src/main/java/com/ai/cloud/skywalking/analysis/chainbuild/ChainBuildReducer.java @@ -42,6 +42,7 @@ public void doReduceAction(String key, Iterator chainInfoIterator) container.addMergedChainIfNotContain(chainInfo); chainTree.summary(chainInfo); } catch (Exception e) { + e.printStackTrace(); logger.error( "Failed to summary call chain, maybe illegal data:" + callChainData, e); diff --git a/skywalking-analysis/src/test/java/com/ai/cloud/skywalking/analysis/mapper/CallChainMapperTest.java b/skywalking-analysis/src/test/java/com/ai/cloud/skywalking/analysis/mapper/CallChainMapperTest.java index 5a4e19cbda41..2055a398a476 100644 --- a/skywalking-analysis/src/test/java/com/ai/cloud/skywalking/analysis/mapper/CallChainMapperTest.java +++ b/skywalking-analysis/src/test/java/com/ai/cloud/skywalking/analysis/mapper/CallChainMapperTest.java @@ -1,40 +1,38 @@ package com.ai.cloud.skywalking.analysis.mapper; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - +import com.ai.cloud.skywalking.analysis.chainbuild.ChainBuildMapper; +import com.ai.cloud.skywalking.analysis.chainbuild.ChainBuildReducer; +import com.ai.cloud.skywalking.analysis.chainbuild.po.ChainInfo; +import com.ai.cloud.skywalking.analysis.chainbuild.po.ChainNode; +import com.ai.cloud.skywalking.analysis.config.ConfigInitializer; +import com.ai.cloud.skywalking.analysis.config.HBaseTableMetaData; +import com.ai.cloud.skywalking.protocol.Span; +import com.google.gson.Gson; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.Cell; import org.apache.hadoop.hbase.HBaseConfiguration; import org.apache.hadoop.hbase.TableName; -import org.apache.hadoop.hbase.client.Connection; -import org.apache.hadoop.hbase.client.ConnectionFactory; -import org.apache.hadoop.hbase.client.Get; -import org.apache.hadoop.hbase.client.Result; -import org.apache.hadoop.hbase.client.Table; +import org.apache.hadoop.hbase.client.*; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.io.Text; import org.junit.Before; import org.junit.Test; -import com.ai.cloud.skywalking.analysis.chainbuild.ChainBuildMapper; -import com.ai.cloud.skywalking.analysis.chainbuild.ChainBuildReducer; -import com.ai.cloud.skywalking.analysis.chainbuild.po.ChainInfo; -import com.ai.cloud.skywalking.analysis.config.ConfigInitializer; -import com.ai.cloud.skywalking.analysis.config.HBaseTableMetaData; -import com.ai.cloud.skywalking.protocol.Span; -import com.google.gson.Gson; +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.*; /** * Created by astraea on 2016/1/15. */ public class CallChainMapperTest { - private static String ZK_QUORUM = "10.1.235.197,10.1.235.198,10.1.235.199"; + //private static String ZK_QUORUM = "10.1.235.197,10.1.235.198,10.1.235.199"; + private static String ZK_QUORUM = "10.1.241.18,10.1.241.19,10.1.241.20"; private static String ZK_CLIENT_PORT = "29181"; - private static String chain_Id = "1.0a2.1455875996366.eb2055d.30051.88.29"; + private static String chain_Id = "1.0a2.1457436000002.860568c.21818.49.82"; // private static String chain_Id = "1.0a2.1453429608422.2701d43.6468.56.1"; private static Configuration configuration = null; @@ -64,6 +62,67 @@ public static List selectByTraceId(String traceId) throws IOException { return entries; } + @Test + public void testDataIsCorrect() throws IOException, ParseException { + List chainInfos = selectSpans(); + Iterator chainInfoIterator = chainInfos.iterator(); + while (chainInfoIterator.hasNext()) { + ChainInfo chainInfo = chainInfoIterator.next(); + if (!"http://m.aisse.asiainfo.com/aisseWorkPage/workPay".equals(chainInfo.getCallEntrance())) { + chainInfoIterator.remove(); + } + } + + Map> result = new HashMap<>(); + for (int i = 0; i < chainInfos.size(); i++) { + for (ChainNode chainNode :chainInfos.get(i).getNodes()){ + if ("0".equals(chainNode.getTraceLevelId())){ + Calendar calendar = Calendar.getInstance(); + calendar.setTime(new Date(chainNode.getStartDate())); + List chainNodes = result.get(getKey(calendar)); + if (chainNodes == null){ + chainNodes = new ArrayList<>(); + } + chainNodes.add(chainNode); + result.put(getKey(calendar), chainNodes); + } + } + } + + System.out.println(result.size()); + } + + private String getKey(Calendar calendar) { + return calendar.get(Calendar.YEAR) + "-" + (calendar.get(Calendar.MONTH)) + "-" + + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR) + ":00:00"; + } + + public static List selectSpans() throws IOException, ParseException { + List chainInfos = new ArrayList(); + Table table = connection.getTable(TableName.valueOf(HBaseTableMetaData.TABLE_CALL_CHAIN.TABLE_NAME)); + Scan scan = new Scan(); + scan.setTimeRange(new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss").parse("2015-12-09/18:44:48").getTime(), new SimpleDateFormat("yyyy-MM-dd/HH:mm:ss").parse("2016-03-09/18:44:48").getTime()); + ResultScanner resultScanner = table.getScanner(scan); + Iterator resultIterator = resultScanner.iterator(); + while (resultIterator.hasNext()) { + Result result = resultIterator.next(); + List entries = new ArrayList(); + for (Cell cell : result.rawCells()) { + if (cell.getValueArray().length > 0) + entries.add(new Span(Bytes.toString(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()))); + } + ChainInfo chainInfo = null; + try { + chainInfo = ChainBuildMapper.spanToChainInfo(Bytes.toString(result.getRow()), entries); + } catch (Exception e) { + continue; + } + chainInfos.add(chainInfo); + } + + return chainInfos; + } + @Before public void initHBaseClient() throws IOException { @@ -73,5 +132,7 @@ public void initHBaseClient() throws IOException { configuration.set("hbase.zookeeper.property.clientPort", ZK_CLIENT_PORT); connection = ConnectionFactory.createConnection(configuration); } + + ConfigInitializer.initialize(); } } \ No newline at end of file