Skip to content

Commit

Permalink
pinpoint-apm#1819 trace format v2
Browse files Browse the repository at this point in the history
  - refactoring annotationBO
  • Loading branch information
emeroad committed Aug 5, 2016
1 parent 538aa30 commit 2c2908c
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,19 @@

package com.navercorp.pinpoint.common.server.bo;

import com.navercorp.pinpoint.common.util.AnnotationTranscoder;

/**
* @author emeroad
*/
public class AnnotationBo {

@Deprecated
private static final AnnotationTranscoder transcoder = new AnnotationTranscoder();

private byte version = 0;

private int key;

private byte valueType;
private byte[] byteValue;
private Object value;

private boolean isAuthorized = true;

public AnnotationBo() {
}

public int getVersion() {
return version & 0xFF;
}

public byte getRawVersion() {
return version;
}

public void setVersion(int version) {
if (version < 0 || version > 255) {
throw new IllegalArgumentException("out of range (0~255) " + version);
}
// check range
this.version = (byte) (version & 0xFF);
}

public int getKey() {
return key;
}
Expand All @@ -64,26 +39,6 @@ public void setKey(int key) {
}


public int getValueType() {
return valueType;
}

public byte getRawValueType() {
return valueType;
}

public void setValueType(byte valueType) {
this.valueType = valueType;
}

public byte[] getByteValue() {
return byteValue;
}

public void setByteValue(byte[] byteValue) {
this.byteValue = byteValue;
}

public Object getValue() {
return value;
}
Expand All @@ -103,10 +58,10 @@ public void setAuthorized(boolean isAuthorized) {

@Override
public String toString() {
if (value == null) {
return "AnnotationBo{" + "version=" + version + ", key='" + key + '\'' + ", valueType=" + valueType + '}';
}
return "AnnotationBo{" + "version=" + version + ", key='" + key + '\'' + ", value=" + value + '}';
return "AnnotationBo{" +
"key=" + key +
", value=" + value +
", isAuthorized=" + isAuthorized +
'}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -279,12 +279,7 @@ private AnnotationBo newAnnotationBo(TAnnotation tAnnotation) {
annotationBo.setKey(tAnnotation.getKey());

Object value = transcoder.getMappingValue(tAnnotation);
byte typeCode = transcoder.getTypeCode(value);
byte[] encodeObject = transcoder.encode(value, typeCode);

annotationBo.setValue(value);
annotationBo.setValueType(typeCode);
annotationBo.setByteValue(encodeObject);

return annotationBo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
public class AnnotationBoDecoder {

private final AnnotationTranscoder transcoder = new AnnotationTranscoder();
private static final byte VERSION = AnnotationSerializer.VERSION;


public List<AnnotationBo> decode(Buffer qualifier, Buffer valueBuffer, SpanDecodingContext decodingContext) {
Expand Down Expand Up @@ -48,14 +49,14 @@ private AnnotationBo decodeAnnotation(Buffer buffer) {

final AnnotationBo annotation = new AnnotationBo();

annotation.setVersion(buffer.readByte());
final byte version = buffer.readByte();
if (version != VERSION) {
throw new IllegalStateException("unknown version:" + version);
}
annotation.setKey(buffer.readSVInt());

byte valueType = buffer.readByte();
annotation.setValueType(valueType);

byte[] byteValue = buffer.readPrefixedBytes();
annotation.setByteValue(byteValue);

Object decodeObject = transcoder.decode(valueType, byteValue);
annotation.setValue(decodeObject);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.navercorp.pinpoint.common.server.bo.SpanBo;
import com.navercorp.pinpoint.common.server.bo.serializer.HbaseSerializer;
import com.navercorp.pinpoint.common.server.bo.serializer.SerializationContext;
import com.navercorp.pinpoint.common.util.AnnotationTranscoder;
import org.apache.commons.collections.CollectionUtils;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.util.Bytes;
Expand All @@ -23,6 +24,8 @@
@Component
public class AnnotationSerializer implements HbaseSerializer<SpanBo, Put> {

private static final AnnotationTranscoder transcoder = new AnnotationTranscoder();
public static final byte VERSION = 0;

@Override
public void serialize(SpanBo spanBo, Put put, SerializationContext context) {
Expand Down Expand Up @@ -63,12 +66,16 @@ public ByteBuffer writeAnnotationList(List<AnnotationBo> annotationList, Buffer

// for test
public void writeAnnotation(AnnotationBo annotationBo, Buffer puffer) {
// int key; // required 4
// int valueTypeCode; // required 4
// ByteBuffer value; // optional 4 + buf.length
puffer.putByte(annotationBo.getRawVersion());

puffer.putByte(VERSION);
puffer.putSVInt(annotationBo.getKey());
puffer.putByte(annotationBo.getRawValueType());
puffer.putPrefixedBytes(annotationBo.getByteValue());

Object value = annotationBo.getValue();

byte typeCode = transcoder.getTypeCode(value);
byte[] bytes = transcoder.encode(value, typeCode);

puffer.putByte(typeCode);
puffer.putPrefixedBytes(bytes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@ private AnnotationBo readFirstAnnotationBo(Buffer buffer) {
byte[] valueBytes = buffer.readPrefixedBytes();
Object value = transcoder.decode(valueType, valueBytes);

current.setValueType(valueType);
current.setValue(value);
return current;
}
Expand All @@ -373,7 +372,7 @@ private AnnotationBo readDeltaAnnotationBo(Buffer buffer, AnnotationBo prev) {
byte valueType = buffer.readByte();
byte[] valueBytes = buffer.readPrefixedBytes();
Object value = transcoder.decode(valueType, valueBytes);
annotation.setValueType(valueType);

annotation.setValue(value);
return annotation;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.navercorp.pinpoint.common.server.bo.serializer.trace.v2.bitfield.SpanBitFiled;
import com.navercorp.pinpoint.common.server.bo.serializer.trace.v2.bitfield.SpanEventBitField;
import com.navercorp.pinpoint.common.server.bo.serializer.trace.v2.bitfield.SpanEventQualifierBitField;
import com.navercorp.pinpoint.common.util.AnnotationTranscoder;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

Expand All @@ -21,6 +22,8 @@
@Component
public class SpanEncoderV0 implements SpanEncoder {

private static final AnnotationTranscoder transcoder = new AnnotationTranscoder();

@Override
public ByteBuffer encodeSpanQualifier(SpanEncodingContext<SpanBo> encodingContext) {
final SpanBo spanBo = encodingContext.getValue();
Expand Down Expand Up @@ -353,8 +356,13 @@ private void writeAnnotationList(Buffer buffer, List<AnnotationBo> annotationBoL

// first annotation
buffer.putSVInt(current.getKey());
buffer.putByte(current.getRawValueType());
buffer.putPrefixedBytes(current.getByteValue());

Object value = current.getValue();
byte valueTypeCode = transcoder.getTypeCode(value);
byte[] valueBytes = transcoder.encode(value, valueTypeCode);

buffer.putByte(valueTypeCode);
buffer.putPrefixedBytes(valueBytes);
// else {
// writeDeltaAnnotationBo(buffer, prev, current);
// }
Expand All @@ -374,8 +382,13 @@ private void writeDeltaAnnotationBo(Buffer buffer, AnnotationBo prev, Annotation
final int prevKey = prev.getKey();
final int currentKey = current.getKey();
buffer.putSVInt(currentKey - prevKey);
buffer.putByte(current.getRawValueType());
buffer.putPrefixedBytes(current.getByteValue());

Object value = current.getValue();
byte valueTypeCode = transcoder.getTypeCode(value);
byte[] valueBytes = transcoder.encode(value, valueTypeCode);

buffer.putByte(valueTypeCode);
buffer.putPrefixedBytes(valueBytes);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
*/
public class AnnotationBoDecoderTest {

private static final Charset UTF_8 = Charset.forName("UTF-8");

private AnnotationSerializer serializer = new AnnotationSerializer();

private AnnotationBoDecoder annotationBoDecoder = new AnnotationBoDecoder();
Expand All @@ -52,7 +50,7 @@ public void testWriteValue() throws Exception {
annotation.setKey(AnnotationKey.API.getCode());

final String value = RandomStringUtils.random(RandomUtils.nextInt(20));
annotation.setByteValue(value.getBytes(UTF_8));
annotation.setValue(value);

final Buffer buffer = new AutomaticBuffer(128);
this.serializer.writeAnnotationList(Lists.newArrayList(annotation), buffer);
Expand All @@ -62,8 +60,7 @@ public void testWriteValue() throws Exception {
Assert.assertEquals(decode.size(), 1);
AnnotationBo decodedAnnotation = decode.get(0);
Assert.assertEquals(annotation.getKey(), decodedAnnotation.getKey());
Assert.assertEquals(annotation.getValueType(), decodedAnnotation.getValueType());
Assert.assertArrayEquals(annotation.getByteValue(), decodedAnnotation.getByteValue());
Assert.assertEquals(annotation.getValue(), decodedAnnotation.getValue());

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public void encodeSpanColumnValue() throws Exception {

logger.debug("span dump \noriginal spanBo:{} \ndecode spanBo:{} ", spanBo, decode);

// TODO check annotationBoList
List<String> excludeField = Lists.newArrayList("parentApplicationId", "parentApplicationServiceType", "annotationBoList");
Assert.assertTrue(EqualsBuilder.reflectionEquals(decode, spanBo, excludeField));

logger.debug("{} {}", spanBo.getAnnotationBoList(), decode.getAnnotationBoList());
Assert.assertTrue("annotation", EqualsBuilder.reflectionEquals(spanBo.getAnnotationBoList(), decode.getAnnotationBoList()));

}

private long getCollectorAcceptTime() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
public class SpanMapperV2Test {
private final Logger logger = LoggerFactory.getLogger(this.getClass());

private final AnnotationTranscoder transcoder = new AnnotationTranscoder();
private final SpanDecoderV0 decoder = new SpanDecoderV0();

@Test
Expand Down Expand Up @@ -96,11 +95,7 @@ public void test() {
private AnnotationBo newAnnotation(int key, Object value) {
AnnotationBo annotationBo = new AnnotationBo();
annotationBo.setKey(key);

byte typeCode = transcoder.getTypeCode(value);
byte[] encode = transcoder.encode(value, typeCode);
annotationBo.setValue(value);
annotationBo.setByteValue(encode);
return annotationBo;
}

Expand Down

0 comments on commit 2c2908c

Please sign in to comment.