diff --git a/src/main/java/com/nhn/pinpoint/common/buffer/AutomaticBuffer.java b/src/main/java/com/nhn/pinpoint/common/buffer/AutomaticBuffer.java index f5188dc53..5b9081758 100644 --- a/src/main/java/com/nhn/pinpoint/common/buffer/AutomaticBuffer.java +++ b/src/main/java/com/nhn/pinpoint/common/buffer/AutomaticBuffer.java @@ -55,7 +55,7 @@ public class AutomaticBuffer extends FixedBuffer { public void putPrefixedBytes(final byte[] bytes) { if (bytes == null) { checkExpend(1); - super.putSVar(-1); + super.putSVar(NULL); } else { checkExpend(bytes.length + BytesUtils.VINT_MAX_SIZE); super.putSVar(bytes.length); @@ -63,6 +63,33 @@ public class AutomaticBuffer extends FixedBuffer { } } + @Override + public void put2PrefixedBytes(final byte[] bytes) { + if (bytes == null) { + checkExpend(BytesUtils.SHORT_BYTE_LENGTH); + super.put((short)NULL); + } else { + if (bytes.length > Short.MAX_VALUE) { + throw new IllegalArgumentException("too large bytes length:" + bytes.length); + } + checkExpend(bytes.length + BytesUtils.SHORT_BYTE_LENGTH); + super.put((short)bytes.length); + super.put(bytes); + } + } + + @Override + public void put4PrefixedBytes(final byte[] bytes) { + if (bytes == null) { + checkExpend(BytesUtils.INT_BYTE_LENGTH); + super.put(NULL); + } else { + checkExpend(bytes.length + BytesUtils.INT_BYTE_LENGTH); + super.put(bytes.length); + super.put(bytes); + } + } + @Override public void putPrefixedString(final String string) { diff --git a/src/main/java/com/nhn/pinpoint/common/buffer/Buffer.java b/src/main/java/com/nhn/pinpoint/common/buffer/Buffer.java index 182c7e127..7b3f18baf 100644 --- a/src/main/java/com/nhn/pinpoint/common/buffer/Buffer.java +++ b/src/main/java/com/nhn/pinpoint/common/buffer/Buffer.java @@ -14,8 +14,16 @@ public interface Buffer { void putPrefixedBytes(byte[] bytes); + void put2PrefixedBytes(byte[] bytes); + + void put4PrefixedBytes(byte[] bytes); + void putPrefixedString(String string); + void put2PrefixedString(String string); + + void put4PrefixedString(String string); + void put(byte v); void put(boolean v); @@ -88,8 +96,14 @@ public interface Buffer { byte[] readPrefixedBytes(); + byte[] read2PrefixedBytes(); + + byte[] read4PrefixedBytes(); + String readPrefixedString(); + String read2PrefixedString(); + String read4PrefixedString(); byte[] getBuffer(); diff --git a/src/main/java/com/nhn/pinpoint/common/buffer/FixedBuffer.java b/src/main/java/com/nhn/pinpoint/common/buffer/FixedBuffer.java index e95fa744d..6f8568a3f 100644 --- a/src/main/java/com/nhn/pinpoint/common/buffer/FixedBuffer.java +++ b/src/main/java/com/nhn/pinpoint/common/buffer/FixedBuffer.java @@ -9,6 +9,7 @@ import java.io.UnsupportedEncodingException; */ public class FixedBuffer implements Buffer { + protected static final int NULL = -1; protected byte[] buffer; protected int offset; @@ -34,21 +35,67 @@ public class FixedBuffer implements Buffer { @Override - public void putPrefixedBytes(final byte[] bytes) { + public void putPrefixedBytes(final byte[] bytes) { if (bytes == null) { - putSVar(-1); + putSVar(NULL); } else { putSVar(bytes.length); put(bytes); } } + @Override + public void put2PrefixedBytes(final byte[] bytes) { + if (bytes == null) { + put((short)NULL); + } else { + if (bytes.length > Short.MAX_VALUE) { + throw new IllegalArgumentException("too large bytes length:" + bytes.length); + } + put((short)bytes.length); + put(bytes); + } + } + + @Override + public void put4PrefixedBytes(final byte[] bytes) { + if (bytes == null) { + put(NULL); + } else { + put(bytes.length); + put(bytes); + } + } + @Override public void putPrefixedString(final String string) { final byte[] bytes = BytesUtils.toBytes(string); putPrefixedBytes(bytes); } + @Override + public void put2PrefixedString(final String string) { + final byte[] bytes = BytesUtils.toBytes(string); + if (string == null) { + put((short)NULL); + return; + } + if (bytes.length > Short.MAX_VALUE) { + throw new IllegalArgumentException("too large String size:" + bytes.length); + } + put2PrefixedBytes(bytes); + } + + @Override + public void put4PrefixedString(final String string) { + final byte[] bytes = BytesUtils.toBytes(string); + if (string == null) { + put(NULL); + return; + } + put4PrefixedBytes(bytes); + } + @Override public void put(final byte v) { this.buffer[offset++] = v; @@ -221,7 +268,31 @@ public class FixedBuffer implements Buffer { @Override public byte[] readPrefixedBytes() { final int size = readSVarInt(); - if (size == -1) { + if (size == NULL) { + return null; + } + if (size == 0) { + return EMPTY; + } + return readBytes(size); + } + + @Override + public byte[] read2PrefixedBytes() { + final int size = readShort(); + if (size == NULL) { + return null; + } + if (size == 0) { + return EMPTY; + } + return readBytes(size); + } + + @Override + public byte[] read4PrefixedBytes() { + final int size = readInt(); + if (size == NULL) { return null; } if (size == 0) { @@ -241,7 +312,19 @@ public class FixedBuffer implements Buffer { @Override public String readPrefixedString() { final int size = readSVarInt(); - if (size == -1) { + if (size == NULL) { + return null; + } + if (size == 0) { + return ""; + } + return readString(size); + } + + @Override + public String read2PrefixedString() { + final int size = readShort(); + if (size == NULL) { return null; } if (size == 0) { @@ -253,7 +336,7 @@ public class FixedBuffer implements Buffer { @Override public String read4PrefixedString() { final int size = readInt(); - if (size == -1) { + if (size == NULL) { return null; } if (size == 0) { diff --git a/src/main/java/com/nhn/pinpoint/common/util/ApplicationMapStatisticsUtils.java b/src/main/java/com/nhn/pinpoint/common/util/ApplicationMapStatisticsUtils.java index a5f665255..b6fccc230 100644 --- a/src/main/java/com/nhn/pinpoint/common/util/ApplicationMapStatisticsUtils.java +++ b/src/main/java/com/nhn/pinpoint/common/util/ApplicationMapStatisticsUtils.java @@ -112,12 +112,16 @@ public class ApplicationMapStatisticsUtils { return BytesUtils.concat(applicationnameBytesLength, applicationnameBytes, applicationtypeBytes, slot); } - public static String getApplicationNameFromRowKey(byte[] bytes) { + public static String getApplicationNameFromRowKey(byte[] bytes, int offset) { if (bytes == null) { throw new NullPointerException("bytes must not be null"); } - short applicationNameLength = BytesUtils.bytesToShort(bytes, 0); - return BytesUtils.toString(bytes, 2, applicationNameLength); //.trim(); + short applicationNameLength = BytesUtils.bytesToShort(bytes, offset); + return BytesUtils.toString(bytes, offset + 2, applicationNameLength); //.trim(); + } + + public static String getApplicationNameFromRowKey(byte[] bytes) { + return getApplicationNameFromRowKey(bytes, 0); } public static short getApplicationTypeFromRowKey(byte[] bytes) { diff --git a/src/test/java/com/nhn/pinpoint/common/buffer/AutomaticBufferTest.java b/src/test/java/com/nhn/pinpoint/common/buffer/AutomaticBufferTest.java index e48e3441c..47a7dda8f 100644 --- a/src/test/java/com/nhn/pinpoint/common/buffer/AutomaticBufferTest.java +++ b/src/test/java/com/nhn/pinpoint/common/buffer/AutomaticBufferTest.java @@ -1,7 +1,8 @@ package com.nhn.pinpoint.common.buffer; import com.nhn.pinpoint.common.util.BytesUtils; -import junit.framework.Assert; +import org.junit.Assert; +import org.junit.Ignore; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,6 +24,55 @@ public class AutomaticBufferTest { Assert.assertEquals(1, BytesUtils.bytesToInt(buf, 0)); } + @Test + public void testPut2PrefixedBytes() throws Exception { + byte[] bytes1 = new byte[2]; + checkPut2PrefixedBytes(bytes1); + + byte[] bytes2 = new byte[0]; + checkPut2PrefixedBytes(bytes2); + + byte[] bytes3 = new byte[Short.MAX_VALUE]; + checkPut2PrefixedBytes(bytes3); + + checkPut2PrefixedBytes(null); + + try { + byte[] bytes4 = new byte[Short.MAX_VALUE+1]; + checkPut2PrefixedBytes(bytes4); + Assert.fail("too large bytes"); + } catch (Exception e) { + } + } + + private void checkPut2PrefixedBytes(byte[] bytes) { + Buffer buffer = new AutomaticBuffer(0); + buffer.put2PrefixedBytes(bytes); + + Buffer copy = new FixedBuffer(buffer.getBuffer()); + Assert.assertArrayEquals(bytes, copy.read2PrefixedBytes()); + } + + @Test + public void testPut4PrefixedBytes() throws Exception { + byte[] bytes1 = new byte[2]; + checkPut4PrefixedBytes(bytes1); + + byte[] bytes2 = new byte[0]; + checkPut4PrefixedBytes(bytes2); + + checkPut4PrefixedBytes(null); + + } + + private void checkPut4PrefixedBytes(byte[] bytes) { + Buffer buffer = new AutomaticBuffer(0); + buffer.put4PrefixedBytes(bytes); + + Buffer copy = new FixedBuffer(buffer.getBuffer()); + Assert.assertArrayEquals(bytes, copy.read4PrefixedBytes()); + } + @Test public void testPutPrefixedBytesCheckRange() throws Exception { Buffer buffer = new AutomaticBuffer(1); @@ -40,7 +90,7 @@ public class AutomaticBufferTest { long l = System.currentTimeMillis(); buffer.putSVar(l); - logger.info("currentTime size:{}", buffer.getOffset()); + logger.trace("currentTime size:{}", buffer.getOffset()); buffer.setOffset(0); Assert.assertEquals(buffer.readSVarLong(), l); @@ -126,6 +176,7 @@ public class AutomaticBufferTest { } + @Ignore @Test public void testUdp() throws Exception { // Signature:Header{signature=85, version=100, type=28704} diff --git a/src/test/java/com/nhn/pinpoint/common/buffer/FixedBufferTest.java b/src/test/java/com/nhn/pinpoint/common/buffer/FixedBufferTest.java index 5e49d93ef..0f0e74b9e 100644 --- a/src/test/java/com/nhn/pinpoint/common/buffer/FixedBufferTest.java +++ b/src/test/java/com/nhn/pinpoint/common/buffer/FixedBufferTest.java @@ -1,25 +1,39 @@ package com.nhn.pinpoint.common.buffer; +import com.nhn.pinpoint.common.util.BytesUtils; import junit.framework.Assert; import org.apache.hadoop.hbase.util.Bytes; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.UnsupportedEncodingException; +import java.util.Random; + /** * @author emeroad */ public class FixedBufferTest { private Logger logger = LoggerFactory.getLogger(this.getClass()); + private Random random = new Random(); @Test public void testPutPrefixedBytes() throws Exception { String test = "test"; - int expected = 3333; + int endExpected = 3333; + testPutPrefixedBytes(test, endExpected); + testPutPrefixedBytes(null, endExpected); + testPutPrefixedBytes("", endExpected); + } + private void testPutPrefixedBytes(String test, int expected) throws UnsupportedEncodingException { Buffer buffer = new FixedBuffer(1024); - buffer.putPrefixedBytes(test.getBytes("UTF-8")); + if (test != null) { + buffer.putPrefixedBytes(test.getBytes("UTF-8")); + } else { + buffer.putPrefixedString(null); + } buffer.put(expected); byte[] buffer1 = buffer.getBuffer(); @@ -30,8 +44,80 @@ public class FixedBufferTest { int i = actual.readInt(); Assert.assertEquals(expected, i); + } + @Test + public void testPut2PrefixedBytes() throws Exception { + String test = "test"; + int endExpected = 3333; + checkPut2PrefixedBytes(test, endExpected); + checkPut2PrefixedBytes(null, endExpected); + checkPut2PrefixedBytes("", endExpected); + + byte[] bytes = new byte[Short.MAX_VALUE]; + checkPut2PrefixedBytes(BytesUtils.toString(bytes), endExpected, Short.MAX_VALUE * 2); + + try { + byte[] bytes2 = new byte[Short.MAX_VALUE + 1]; + checkPut2PrefixedBytes(BytesUtils.toString(bytes2), endExpected, Short.MAX_VALUE * 2); + Assert.fail("too large bytes"); + } catch (Exception e) { + } + + } + + private void checkPut2PrefixedBytes(String test, int expected) throws UnsupportedEncodingException { + checkPut2PrefixedBytes(test, expected, 1024); + } + + private void checkPut2PrefixedBytes(String test, int expected, int bufferSize) throws UnsupportedEncodingException { + Buffer buffer = new FixedBuffer(bufferSize); + if (test != null) { + buffer.put2PrefixedBytes(test.getBytes("UTF-8")); + } else { + buffer.put2PrefixedBytes(null); + } + + buffer.put(expected); + byte[] buffer1 = buffer.getBuffer(); + + Buffer actual = new FixedBuffer(buffer1); + String s = actual.read2PrefixedString(); + Assert.assertEquals(test, s); + + int i = actual.readInt(); + Assert.assertEquals(expected, i); + } + + @Test + public void testPut4PrefixedBytes() throws Exception { + String test = "test"; + int endExpected = 3333; + + checkPut4PrefixedBytes(test, endExpected); + checkPut4PrefixedBytes(null, endExpected); + checkPut4PrefixedBytes("", endExpected); + + } + + private void checkPut4PrefixedBytes(String test, int expected) throws UnsupportedEncodingException { + Buffer buffer = new FixedBuffer(1024); + if (test != null) { + buffer.put4PrefixedBytes(test.getBytes("UTF-8")); + } else { + buffer.put4PrefixedBytes(null); + } + + buffer.put(expected); + byte[] buffer1 = buffer.getBuffer(); + + Buffer actual = new FixedBuffer(buffer1); + String s = actual.read4PrefixedString(); + Assert.assertEquals(test, s); + + int i = actual.readInt(); + Assert.assertEquals(expected, i); } @Test