From 2ddff503940f39b97579485ff4414a2c63fffa23 Mon Sep 17 00:00:00 2001 From: Woonduk Kang Date: Wed, 28 Nov 2012 05:03:39 +0000 Subject: [PATCH] =?UTF-8?q?[=EA=B0=95=EC=9A=B4=EB=8D=95]=20[LUCYSUS-1744]?= =?UTF-8?q?=20span=EC=9D=98=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=EA=B8=B8?= =?UTF-8?q?=EC=9D=B4=EB=A5=BC=20255=EC=9E=90=20=EC=9D=B4=EB=82=B4=EB=A1=9C?= =?UTF-8?q?=20=EC=A0=9C=ED=95=9C=ED=95=98=EA=B3=A0=201byte=20unsigned=20ch?= =?UTF-8?q?unk=EB=A1=9C=20=EB=A7=88=ED=81=AC=ED=95=98=EB=8A=94=EA=B2=83?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EB=B3=80=EA=B2=BD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-commons/trunk@944 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../java/com/profiler/common/bo/SpanBo.java | 14 ++++----- .../java/com/profiler/common/util/Buffer.java | 12 +++++++ .../com/profiler/common/bo/SpanBoTest.java | 31 ++++++++++++++++++- .../com/profiler/common/util/BufferTest.java | 18 +++++++---- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/profiler/common/bo/SpanBo.java b/src/main/java/com/profiler/common/bo/SpanBo.java index 9c425a041..cf8e06688 100644 --- a/src/main/java/com/profiler/common/bo/SpanBo.java +++ b/src/main/java/com/profiler/common/bo/SpanBo.java @@ -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(); diff --git a/src/main/java/com/profiler/common/util/Buffer.java b/src/main/java/com/profiler/common/util/Buffer.java index eb8bf2b93..576bbcff5 100644 --- a/src/main/java/com/profiler/common/util/Buffer.java +++ b/src/main/java/com/profiler/common/util/Buffer.java @@ -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) { diff --git a/src/test/java/com/profiler/common/bo/SpanBoTest.java b/src/test/java/com/profiler/common/bo/SpanBoTest.java index bfd203193..6305d05d0 100644 --- a/src/test/java/com/profiler/common/bo/SpanBoTest.java +++ b/src/test/java/com/profiler/common/bo/SpanBoTest.java @@ -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(); + } + } diff --git a/src/test/java/com/profiler/common/util/BufferTest.java b/src/test/java/com/profiler/common/util/BufferTest.java index 11572d473..ad6544443 100644 --- a/src/test/java/com/profiler/common/util/BufferTest.java +++ b/src/test/java/com/profiler/common/util/BufferTest.java @@ -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); }