提交 43eb0f42 编写于 作者: P pengys5

Fixed bug #203.

The nextString method in JsonReader class return a non quote value, some special character e.g. ‘, ” ,\ , / , it will be trigger an exception when Segment entity build a json string to insert into elasticsearch.
上级 30b79503
...@@ -6,8 +6,8 @@ import com.a.eye.skywalking.collector.actor.LocalAsyncWorkerRef; ...@@ -6,8 +6,8 @@ import com.a.eye.skywalking.collector.actor.LocalAsyncWorkerRef;
import com.a.eye.skywalking.collector.actor.LocalWorkerContext; import com.a.eye.skywalking.collector.actor.LocalWorkerContext;
import com.a.eye.skywalking.collector.actor.Role; import com.a.eye.skywalking.collector.actor.Role;
import com.a.eye.skywalking.collector.worker.segment.entity.Segment; import com.a.eye.skywalking.collector.worker.segment.entity.Segment;
import com.a.eye.skywalking.collector.worker.segment.entity.SegmentJsonReader;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.stream.JsonReader;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletException; import javax.servlet.ServletException;
...@@ -57,12 +57,12 @@ public abstract class AbstractPost extends AbstractLocalAsyncWorker { ...@@ -57,12 +57,12 @@ public abstract class AbstractPost extends AbstractLocalAsyncWorker {
} }
private void streamReader(BufferedReader bufferedReader) throws Exception { private void streamReader(BufferedReader bufferedReader) throws Exception {
try (JsonReader reader = new JsonReader(bufferedReader)) { try (SegmentJsonReader reader = new SegmentJsonReader(bufferedReader)) {
readSegmentArray(reader); readSegmentArray(reader);
} }
} }
private void readSegmentArray(JsonReader reader) throws Exception { private void readSegmentArray(SegmentJsonReader reader) throws Exception {
reader.beginArray(); reader.beginArray();
while (reader.hasNext()) { while (reader.hasNext()) {
Segment segment = new Segment(); Segment segment = new Segment();
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
/** /**
...@@ -14,8 +12,8 @@ public class GlobalTraceId extends DeserializeObject { ...@@ -14,8 +12,8 @@ public class GlobalTraceId extends DeserializeObject {
return globalTraceId; return globalTraceId;
} }
public GlobalTraceId deserialize(JsonReader reader) throws IOException { public GlobalTraceId deserialize(SegmentJsonReader reader) throws IOException {
this.globalTraceId = reader.nextString(); this.globalTraceId = reader.nextString().getNonQuoteValue();
this.setJsonStr("\"" + globalTraceId + "\""); this.setJsonStr("\"" + globalTraceId + "\"");
return this; return this;
} }
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -21,7 +19,7 @@ public class LogData extends DeserializeObject { ...@@ -21,7 +19,7 @@ public class LogData extends DeserializeObject {
return fields; return fields;
} }
public LogData deserialize(JsonReader reader) throws IOException { public LogData deserialize(SegmentJsonReader reader) throws IOException {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{"); stringBuilder.append("{");
...@@ -40,7 +38,7 @@ public class LogData extends DeserializeObject { ...@@ -40,7 +38,7 @@ public class LogData extends DeserializeObject {
while (reader.hasNext()) { while (reader.hasNext()) {
String key = reader.nextName(); String key = reader.nextName();
String value = reader.nextString(); String value = reader.nextString().getQuoteValue();
fields.put(key, value); fields.put(key, value);
} }
reader.endObject(); reader.endObject();
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -46,7 +44,7 @@ public class Segment extends DeserializeObject { ...@@ -46,7 +44,7 @@ public class Segment extends DeserializeObject {
return relatedGlobalTraces; return relatedGlobalTraces;
} }
public Segment deserialize(JsonReader reader) throws IOException { public Segment deserialize(SegmentJsonReader reader) throws IOException {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{"); stringBuilder.append("{");
...@@ -55,14 +53,14 @@ public class Segment extends DeserializeObject { ...@@ -55,14 +53,14 @@ public class Segment extends DeserializeObject {
while (reader.hasNext()) { while (reader.hasNext()) {
switch (reader.nextName()) { switch (reader.nextName()) {
case "ts": case "ts":
String ts = reader.nextString(); SegmentJsonReader.StringValue ts = reader.nextString();
this.traceSegmentId = ts; this.traceSegmentId = ts.getNonQuoteValue();
JsonBuilder.INSTANCE.append(stringBuilder, "ts", ts, first); JsonBuilder.INSTANCE.append(stringBuilder, "ts", ts.getQuoteValue(), first);
break; break;
case "ac": case "ac":
String ac = reader.nextString(); SegmentJsonReader.StringValue ac = reader.nextString();
this.applicationCode = ac; this.applicationCode = ac.getNonQuoteValue();
JsonBuilder.INSTANCE.append(stringBuilder, "ac", ac, first); JsonBuilder.INSTANCE.append(stringBuilder, "ac", ac.getQuoteValue(), first);
break; break;
case "st": case "st":
long st = reader.nextLong(); long st = reader.nextLong();
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
...@@ -15,7 +13,7 @@ public enum SegmentDeserialize { ...@@ -15,7 +13,7 @@ public enum SegmentDeserialize {
INSTANCE; INSTANCE;
public Segment deserializeSingle(String singleSegmentJsonStr) throws IOException { public Segment deserializeSingle(String singleSegmentJsonStr) throws IOException {
JsonReader reader = new JsonReader(new StringReader(singleSegmentJsonStr)); SegmentJsonReader reader = new SegmentJsonReader(new StringReader(singleSegmentJsonStr));
Segment segment = new Segment(); Segment segment = new Segment();
segment.deserialize(reader); segment.deserialize(reader);
return segment; return segment;
...@@ -28,12 +26,12 @@ public enum SegmentDeserialize { ...@@ -28,12 +26,12 @@ public enum SegmentDeserialize {
} }
private void streamReader(List<Segment> segmentList, FileReader fileReader) throws Exception { private void streamReader(List<Segment> segmentList, FileReader fileReader) throws Exception {
try (JsonReader reader = new JsonReader(fileReader)) { try (SegmentJsonReader reader = new SegmentJsonReader(fileReader)) {
readSegmentArray(segmentList, reader); readSegmentArray(segmentList, reader);
} }
} }
private void readSegmentArray(List<Segment> segmentList, JsonReader reader) throws Exception { private void readSegmentArray(List<Segment> segmentList, SegmentJsonReader reader) throws Exception {
reader.beginArray(); reader.beginArray();
while (reader.hasNext()) { while (reader.hasNext()) {
Segment segment = new Segment(); Segment segment = new Segment();
......
package com.a.eye.skywalking.collector.worker.segment.entity;
/**
* Copy from {@link com.google.gson.stream.JsonScope}, this class is invisible, {@link SegmentJsonReader} can not use it
* because of the package is different
*
* @author pengys5
*/
public class SegmentJsonScope {
/**
* An array with no elements requires no separators or newlines before
* it is closed.
*/
static final int EMPTY_ARRAY = 1;
/**
* A array with at least one value requires a comma and newline before
* the next element.
*/
static final int NONEMPTY_ARRAY = 2;
/**
* An object with no name/value pairs requires no separators or newlines
* before it is closed.
*/
static final int EMPTY_OBJECT = 3;
/**
* An object whose most recent element is a key. The next element must
* be a value.
*/
static final int DANGLING_NAME = 4;
/**
* An object with at least one name/value pair requires a comma and
* newline before the next element.
*/
static final int NONEMPTY_OBJECT = 5;
/**
* No object or array has been started.
*/
static final int EMPTY_DOCUMENT = 6;
/**
* A document with at an array or object.
*/
static final int NONEMPTY_DOCUMENT = 7;
/**
* A document that's been closed and cannot be accessed.
*/
static final int CLOSED = 8;
}
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
...@@ -58,7 +56,7 @@ public class Span extends DeserializeObject { ...@@ -58,7 +56,7 @@ public class Span extends DeserializeObject {
return logs; return logs;
} }
public Span deserialize(JsonReader reader) throws IOException { public Span deserialize(SegmentJsonReader reader) throws IOException {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{"); stringBuilder.append("{");
...@@ -87,7 +85,7 @@ public class Span extends DeserializeObject { ...@@ -87,7 +85,7 @@ public class Span extends DeserializeObject {
JsonBuilder.INSTANCE.append(stringBuilder, "et", et, first); JsonBuilder.INSTANCE.append(stringBuilder, "et", et, first);
break; break;
case "on": case "on":
String on = reader.nextString(); String on = reader.nextString().getNonQuoteValue();
this.operationName = on; this.operationName = on;
JsonBuilder.INSTANCE.append(stringBuilder, "on", on, first); JsonBuilder.INSTANCE.append(stringBuilder, "on", on, first);
break; break;
...@@ -97,7 +95,7 @@ public class Span extends DeserializeObject { ...@@ -97,7 +95,7 @@ public class Span extends DeserializeObject {
while (reader.hasNext()) { while (reader.hasNext()) {
String key = reader.nextName(); String key = reader.nextName();
String value = reader.nextString(); String value = reader.nextString().getQuoteValue();
tagsWithStr.put(key, value); tagsWithStr.put(key, value);
} }
reader.endObject(); reader.endObject();
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
/** /**
...@@ -32,7 +31,7 @@ public class TraceSegmentRef extends DeserializeObject { ...@@ -32,7 +31,7 @@ public class TraceSegmentRef extends DeserializeObject {
return peerHost; return peerHost;
} }
public TraceSegmentRef deserialize(JsonReader reader) throws IOException { public TraceSegmentRef deserialize(SegmentJsonReader reader) throws IOException {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{"); stringBuilder.append("{");
...@@ -41,7 +40,7 @@ public class TraceSegmentRef extends DeserializeObject { ...@@ -41,7 +40,7 @@ public class TraceSegmentRef extends DeserializeObject {
while (reader.hasNext()) { while (reader.hasNext()) {
switch (reader.nextName()) { switch (reader.nextName()) {
case "ts": case "ts":
String ts = reader.nextString(); String ts = reader.nextString().getNonQuoteValue();
this.traceSegmentId = ts; this.traceSegmentId = ts;
JsonBuilder.INSTANCE.append(stringBuilder, "ts", ts, first); JsonBuilder.INSTANCE.append(stringBuilder, "ts", ts, first);
break; break;
...@@ -51,12 +50,12 @@ public class TraceSegmentRef extends DeserializeObject { ...@@ -51,12 +50,12 @@ public class TraceSegmentRef extends DeserializeObject {
JsonBuilder.INSTANCE.append(stringBuilder, "si", si, first); JsonBuilder.INSTANCE.append(stringBuilder, "si", si, first);
break; break;
case "ac": case "ac":
String ac = reader.nextString(); String ac = reader.nextString().getNonQuoteValue();
this.applicationCode = ac; this.applicationCode = ac;
JsonBuilder.INSTANCE.append(stringBuilder, "ac", ac, first); JsonBuilder.INSTANCE.append(stringBuilder, "ac", ac, first);
break; break;
case "ph": case "ph":
String ph = reader.nextString(); String ph = reader.nextString().getNonQuoteValue();
this.peerHost = ph; this.peerHost = ph;
JsonBuilder.INSTANCE.append(stringBuilder, "ph", ph, first); JsonBuilder.INSTANCE.append(stringBuilder, "ph", ph, first);
break; break;
......
...@@ -42,7 +42,7 @@ public enum PersistenceTimer { ...@@ -42,7 +42,7 @@ public enum PersistenceTimer {
List<AbstractLocalSyncWorker> workers = PersistenceWorkerListener.INSTANCE.getWorkers(); List<AbstractLocalSyncWorker> workers = PersistenceWorkerListener.INSTANCE.getWorkers();
for (AbstractLocalSyncWorker worker : workers) { for (AbstractLocalSyncWorker worker : workers) {
logger.info("worker role name: %s", worker.getRole().roleName()); logger.debug("worker role name: %s", worker.getRole().roleName());
try { try {
worker.allocateJob(new FlushAndSwitch(), dataList); worker.allocateJob(new FlushAndSwitch(), dataList);
} catch (Exception e) { } catch (Exception e) {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
</RollingFile> </RollingFile>
</Appenders> </Appenders>
<Loggers> <Loggers>
<logger name="com.a.eye.skywalking.collector" level="debug"> <logger name="com.a.eye.skywalking.collector" level="INFO">
<AppenderRef ref="RollingFile"/> <AppenderRef ref="RollingFile"/>
</logger> </logger>
<Root level="INFO"> <Root level="INFO">
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import java.util.Map; import java.util.Map;
...@@ -16,7 +15,7 @@ public class LogDataTestCase { ...@@ -16,7 +15,7 @@ public class LogDataTestCase {
public void deserialize() throws IOException { public void deserialize() throws IOException {
LogData logData = new LogData(); LogData logData = new LogData();
JsonReader reader = new JsonReader(new StringReader("{\"tm\":1, \"fi\": {\"test1\":\"test1\",\"test2\":\"test2\"}, \"skip\":\"skip\"}")); SegmentJsonReader reader = new SegmentJsonReader(new StringReader("{\"tm\":1, \"fi\": {\"test1\":\"test1\",\"test2\":\"test2\"}, \"skip\":\"skip\"}"));
logData.deserialize(reader); logData.deserialize(reader);
Assert.assertEquals(1L, logData.getTime()); Assert.assertEquals(1L, logData.getTime());
......
package com.a.eye.skywalking.collector.worker.segment.entity; package com.a.eye.skywalking.collector.worker.segment.entity;
import com.google.gson.stream.JsonReader;
import java.io.IOException; import java.io.IOException;
import java.io.StringReader; import java.io.StringReader;
import org.junit.Assert; import org.junit.Assert;
...@@ -14,7 +13,7 @@ public class TraceSegmentRefTestCase { ...@@ -14,7 +13,7 @@ public class TraceSegmentRefTestCase {
@Test @Test
public void deserialize() throws IOException { public void deserialize() throws IOException {
TraceSegmentRef traceSegmentRef = new TraceSegmentRef(); TraceSegmentRef traceSegmentRef = new TraceSegmentRef();
JsonReader reader = new JsonReader(new StringReader("{\"ts\" :\"ts\",\"si\":0,\"ac\":\"ac\",\"ph\":\"ph\", \"skip\":\"skip\"}")); SegmentJsonReader reader = new SegmentJsonReader(new StringReader("{\"ts\" :\"ts\",\"si\":0,\"ac\":\"ac\",\"ph\":\"ph\", \"skip\":\"skip\"}"));
traceSegmentRef.deserialize(reader); traceSegmentRef.deserialize(reader);
Assert.assertEquals("ts", traceSegmentRef.getTraceSegmentId()); Assert.assertEquals("ts", traceSegmentRef.getTraceSegmentId());
......
...@@ -357,11 +357,19 @@ ...@@ -357,11 +357,19 @@
"ts": { "ts": {
"span.layer": "rpc", "span.layer": "rpc",
"component": "Motan", "component": "Motan",
"span.kind": "server" "span.kind": "server",
"sql": "select * from table where column=\"value\""
}, },
"tb": {}, "tb": {},
"ti": {}, "ti": {},
"lo": [] "lo": [
{
"tm": 1490923010332,
"fi": {
"special.character": "\'\" \\ /"
}
}
]
} }
], ],
"ac": "cache-service", "ac": "cache-service",
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册