Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
Use bitmasking instead of ByteBuffer for id gen
Browse files Browse the repository at this point in the history
Tests change to use AssertJ.
  • Loading branch information
waldeinburg committed Sep 5, 2019
1 parent b4e8628 commit 5eae7eb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
14 changes: 6 additions & 8 deletions src/main/java/biz/paluch/logging/gelf/intern/GelfMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,14 +378,14 @@ public ByteBuffer toTCPBuffer(ByteBuffer buffer) {
protected ByteBuffer[] sliceDatagrams(ByteBuffer source, int datagrams, ByteBuffer target) {
int messageLength = source.limit();

byte[] msgId = generateMsgId();
long msgId = generateMsgId();

// Reuse length of datagrams array since this is supposed to be the correct number of datagrams
ByteBuffer[] slices = new ByteBuffer[datagrams];
for (int idx = 0; idx < datagrams; idx++) {

int start = target.position();
target.put(GELF_CHUNKED_ID).put(msgId).put((byte) idx).put((byte) datagrams);
target.put(GELF_CHUNKED_ID).putLong(msgId).put((byte) idx).put((byte) datagrams);

int from = idx * maximumMessageSize;
int to = from + maximumMessageSize;
Expand All @@ -403,7 +403,7 @@ protected ByteBuffer[] sliceDatagrams(ByteBuffer source, int datagrams, ByteBuff
return slices;
}

byte[] generateMsgId() {
long generateMsgId() {
// Considerations about generating the message ID: The GELF documentation suggests to
// "[g]enerate [the id] from millisecond timestamp + hostname for example":
// https://docs.graylog.org/en/3.1/pages/gelf.html#chunking
Expand All @@ -428,17 +428,15 @@ byte[] generateMsgId() {
// Thus, we just need six seconds which will require two bytes. Then we can spend six bytes
// on a random number.

return ByteBuffer.allocate(8).putLong(getRandomLong())
// Overwrite the last two bytes with the timestamp.
.putShort(6, getCurrentTimeMillis()).array();
return (getCurrentTimeMillis() & 0xFFFF000000000000L) | (getRandomLong() & 0x0000FFFFFFFFFFFFL);
}

long getRandomLong() {
return rand.nextLong();
}

short getCurrentTimeMillis() {
return (short) System.currentTimeMillis();
long getCurrentTimeMillis() {
return System.currentTimeMillis();
}

private byte[] gzipMessage(byte[] message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import static biz.paluch.logging.gelf.GelfMessageBuilder.newInstance;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -189,15 +187,12 @@ long getRandomLong() {
}

@Override
short getCurrentTimeMillis() {
return 0x0603;
long getCurrentTimeMillis() {
return 0x90C06030090C0603L;
}
};

byte[] expectedBytes = { (byte) 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x06, 0x03 };
byte[] msgId = gelfMessage.generateMsgId();
assertEquals(8, msgId.length); // Just to be explicit.
assertArrayEquals(expectedBytes, msgId);
assertThat(gelfMessage.generateMsgId()).isEqualTo(0x90C0201008040201L);
}

String toString(ByteBuffer allocate) {
Expand Down Expand Up @@ -271,8 +266,8 @@ private GelfMessage createGelfMessage() {

GelfMessage gelfMessage = new GelfMessage() {
@Override
byte[] generateMsgId() {
return new byte[] { (byte) 128, 64, 32, 16, 8, 4, 2, 1 };
long generateMsgId() {
return 0x80406030090C0603L;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ private GelfMessage createGelfMessage() {

GelfMessage gelfMessage = new GelfMessage() {
@Override
byte[] generateMsgId() {
return new byte[] { (byte) 128, 64, 32, 16, 8, 4, 2, 1 };
long generateMsgId() {
return 0x80406030090C0603L;
}
};

Expand All @@ -135,8 +135,8 @@ private PoolingGelfMessage createPooledGelfMessage() {

PoolingGelfMessage gelfMessage = new PoolingGelfMessage(PoolHolder.threadLocal()) {
@Override
byte[] generateMsgId() {
return new byte[] { (byte) 128, 64, 32, 16, 8, 4, 2, 1 };
long generateMsgId() {
return 0x80406030090C0603L;
}
};

Expand Down

0 comments on commit 5eae7eb

Please sign in to comment.