[강운덕] [LUCYSUS-1744] span의 문자열 길이를 255자 이내로 제한하고 1byte unsigned chunk로 마크하는것으로 변경.

git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-commons/trunk@944 84d0f5b1-2673-498c-a247-62c4ff18d310
This commit is contained in:
Woonduk Kang
2012-11-28 05:03:39 +00:00
parent a5df4b5b22
commit 2ddff50394
4 changed files with 61 additions and 14 deletions
@@ -226,7 +226,7 @@ public class SpanBo {
private int getBufferLength(int a, int b, int c, int d) {
int size = a + b + c + d;
size += 1 + 2 + 2 + 2 + VERSION_SIZE; // chunk size chunk
size += 1 + 1 + 1 + 1 + VERSION_SIZE; // chunk size chunk
// size = size + TIMESTAMP + MOSTTRACEID + LEASTTRACEID + SPANID +
// PARENTSPANID + FLAG + TERMINAL;
size += PARENTSPANID + FLAG + SERVICETYPE;
@@ -259,10 +259,10 @@ public class SpanBo {
buffer.put(startTime);
buffer.put(elapsed);
buffer.put2PrefixedBytes(rpcBytes);
buffer.put2PrefixedBytes(serviceNameBytes);
buffer.put1PrefixedBytes(rpcBytes);
buffer.put1PrefixedBytes(serviceNameBytes);
buffer.put(serviceType.getCode());
buffer.put2PrefixedBytes(endPointBytes);
buffer.put1PrefixedBytes(endPointBytes);
buffer.put(flag);
return buffer.getBuffer();
@@ -284,10 +284,10 @@ public class SpanBo {
this.startTime = buffer.readLong();
this.elapsed = buffer.readInt();
this.rpc = buffer.read2PrefixedString();
this.serviceName = buffer.read2PrefixedString();
this.rpc = buffer.read1UnsignedPrefixedString();
this.serviceName = buffer.read1UnsignedPrefixedString();
this.serviceType = ServiceType.parse(buffer.readShort());
this.endPoint = buffer.read2PrefixedString();
this.endPoint = buffer.read1UnsignedPrefixedString();
this.flag = buffer.readShort();
return buffer.getOffset();
@@ -77,6 +77,10 @@ public class Buffer {
return this.buffer[offset++];
}
public int readUnsignedByte() {
return readByte() & 0xff;
}
public boolean readBoolean() {
byte b = readByte();
if (b == BOOLEAN_FALSE) {
@@ -151,6 +155,14 @@ public class Buffer {
return readString(size);
}
public String read1UnsignedPrefixedString() {
int size = readUnsignedByte();
if (size == 0) {
return "";
}
return readString(size);
}
public String read2PrefixedString() {
int size = readShort();
if (size == 0) {
@@ -52,6 +52,35 @@ public class SpanBoTest {
int i = newSpanBo.readValue(bytes, 0);
logger.info("length:{}", i);
Assert.assertEquals(bytes.length, i);
}
@Test
public void serialize2() {
SpanBo spanBo = new SpanBo();
spanBo.setAgentId("agent");
String endPoint = createString(127);
spanBo.setEndPoint(endPoint);
String rpc = createString(255);
spanBo.setRpc(rpc);
String service = createString(5);
spanBo.setServiceName(service);
spanBo.setServiceType(ServiceType.BLOC);
byte[] bytes = spanBo.writeValue();
logger.info("length:{}", bytes.length);
SpanBo newSpanBo = new SpanBo();
int i = newSpanBo.readValue(bytes, 0);
logger.info("length:{}", i);
Assert.assertEquals(bytes.length, i);
}
private String createString(int size) {
StringBuilder sb = new StringBuilder(size);
for (int i = 0; i < size; i++) {
sb.append('a');
}
return sb.toString();
}
}
@@ -1,16 +1,10 @@
package com.profiler.common.util;
import junit.framework.Assert;
import org.apache.avro.io.BinaryEncoder;
import org.apache.avro.io.DirectBinaryEncoder;
import org.apache.avro.io.EncoderFactory;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
/**
*
*/
@@ -77,7 +71,19 @@ public class BufferTest {
@Test
public void testPut() throws Exception {
checkUnsignedByte(255);
checkUnsignedByte(0);
}
private void checkUnsignedByte(int value) {
Buffer buffer = new Buffer(1024);
buffer.put((byte) value);
byte[] buffer1 = buffer.getBuffer();
Buffer reader = new Buffer(buffer1);
int i = reader.readUnsignedByte();
Assert.assertEquals(value, i);
}