Skip to content

Commit

Permalink
Re #134: Seting endianness when accessing buffers.
Browse files Browse the repository at this point in the history
  • Loading branch information
tkowalcz committed Dec 20, 2024
1 parent 893e2f9 commit aefa292
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 38 deletions.
10 changes: 5 additions & 5 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,31 +82,31 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.14.3</version>
<version>1.20.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.14.3</version>
<version>1.20.1</version>
<scope>test</scope>
</dependency>

Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/pl/tkowalcz/tjahzi/LabelSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import org.agrona.DirectBuffer;
import org.agrona.ExpandableArrayBuffer;

import java.nio.ByteOrder;

public class LabelSerializer {

private final ExpandableArrayBuffer buffer = new ExpandableArrayBuffer();
Expand Down Expand Up @@ -48,7 +50,9 @@ public LabelSerializer appendPartialLabelValue(CharSequence value) {
public LabelSerializer finishAppendingLabelValue() {
buffer.putInt(
lastSizePosition,
cursor - lastSizePosition - Integer.BYTES);
cursor - lastSizePosition - Integer.BYTES,
ByteOrder.LITTLE_ENDIAN
);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.agrona.concurrent.AtomicBuffer;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

public class LogBufferSerializer {

Expand Down Expand Up @@ -51,13 +52,13 @@ private int writeHeader(
long nanoOfMillisecond,
LabelSerializer serializedLabels
) {
buffer.putLong(cursor, epochMillisecond);
buffer.putLong(cursor, epochMillisecond, ByteOrder.LITTLE_ENDIAN);
cursor += Long.BYTES;

buffer.putLong(cursor, nanoOfMillisecond);
buffer.putLong(cursor, nanoOfMillisecond, ByteOrder.LITTLE_ENDIAN);
cursor += Long.BYTES;

buffer.putInt(cursor, serializedLabels.getLabelsCount());
buffer.putInt(cursor, serializedLabels.getLabelsCount(), ByteOrder.LITTLE_ENDIAN);
cursor += Integer.BYTES;
return cursor;
}
Expand All @@ -70,7 +71,7 @@ private int writeLabels(int cursor, LabelSerializer serializedLabels) {
}

private void writeLogLine(int cursor, ByteBuffer line) {
buffer.putInt(cursor, line.remaining());
buffer.putInt(cursor, line.remaining(), ByteOrder.LITTLE_ENDIAN);
cursor += Integer.BYTES;

buffer.putBytes(
Expand Down
11 changes: 6 additions & 5 deletions core/src/main/java/pl/tkowalcz/tjahzi/LogBufferTranscoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pl.tkowalcz.tjahzi.utils.TextBuilders;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;

public class LogBufferTranscoder {
Expand All @@ -30,10 +31,10 @@ public LogBufferTranscoder(Map<String, String> staticLabels, AtomicBuffer buffer
}

public void deserializeIntoByteBuf(DirectBuffer buffer, int index, OutputBuffer outputBuffer) {
long epochMillisecond = buffer.getLong(index);
long epochMillisecond = buffer.getLong(index, ByteOrder.LITTLE_ENDIAN);
index += Long.BYTES;

long nanoOfMillisecond = buffer.getLong(index);
long nanoOfMillisecond = buffer.getLong(index, ByteOrder.LITTLE_ENDIAN);
index += Long.BYTES;

TextBuilder labelsBuilder = TextBuilders.threadLocal();
Expand Down Expand Up @@ -65,13 +66,13 @@ private int readLabels(
int index,
TextBuilder labelsBuilder
) {
int labelsCount = buffer.getInt(index);
int labelsCount = buffer.getInt(index, ByteOrder.LITTLE_ENDIAN);
index += Integer.BYTES;

for (int i = 0; i < labelsCount; i++) {
index += buffer.getStringAscii(index, labelsBuilder) + Integer.BYTES;
index += buffer.getStringAscii(index, labelsBuilder, ByteOrder.LITTLE_ENDIAN) + Integer.BYTES;
labelsBuilder.append("=").append("\"");
index += buffer.getStringAscii(index, labelsBuilder) + Integer.BYTES;
index += buffer.getStringAscii(index, labelsBuilder, ByteOrder.LITTLE_ENDIAN) + Integer.BYTES;
labelsBuilder.append("\",");
}

Expand Down
7 changes: 5 additions & 2 deletions core/src/main/java/pl/tkowalcz/tjahzi/TjahziInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import pl.tkowalcz.tjahzi.stats.MonitoringModule;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Map;

public class TjahziInitializer {
Expand Down Expand Up @@ -98,9 +99,11 @@ private ByteBuffer allocateJavaBuffer(
int totalSize = bufferSize + RingBufferDescriptor.TRAILER_LENGTH;

if (offHeap) {
return ByteBuffer.allocateDirect(totalSize);
return ByteBuffer.allocateDirect(totalSize)
.order(ByteOrder.LITTLE_ENDIAN);
}

return ByteBuffer.allocate(totalSize);
return ByteBuffer.allocate(totalSize)
.order(ByteOrder.LITTLE_ENDIAN);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void serialize(
ByteBuf target
) {
int messageStartIndex = target.writerIndex();
target.writeInt(0);
target.writeIntLE(0);

target.writeByte(TIMESTAMP_FIELD_NUMBER << 3 | LENGTH_DELIMITED_TYPE);
TimestampSerializer.serialize(epochMillisecond, nanoOfMillisecond, target);
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/pl/tkowalcz/tjahzi/protobuf/Protobuf.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Protobuf {

public static void writeSize(ByteBuf target, int messageStartIndex) {
int messageSize = target.writerIndex() - messageStartIndex - Integer.BYTES;
target.setInt(
target.setIntLE(
messageStartIndex,
getFixed32Varint(messageSize)
);
Expand All @@ -21,7 +21,7 @@ public static int getFixed32Varint(int value) {
int byte3 = ((value >>> 14) & 0x07F) | 0x80;
int byte4 = ((value >>> 21) & 0x07F);

return intFromBytes(
return intFromBytesLE(
(byte) byte1,
(byte) byte2,
(byte) byte3,
Expand All @@ -41,10 +41,10 @@ public static void writeUnsignedVarint(long value, ByteBuf target) {
}
}

public static int intFromBytes(byte byte1, byte byte2, byte byte3, byte byte4) {
return (byte1 & 0xFF) << 24
| (byte2 & 0xFF) << 16
| (byte3 & 0xFF) << 8
| (byte4 & 0xFF);
public static int intFromBytesLE(byte byte1, byte byte2, byte byte3, byte byte4) {
return (byte1 & 0xFF)
| (byte2 & 0xFF) << 8
| (byte3 & 0xFF) << 16
| (byte4 & 0xFF) << 24;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void serialize(
ByteBuf target
) {
int messageStartIndex = target.writerIndex();
target.writeInt(0);
target.writeIntLE(0);

target.writeByte(LABELS_FIELD_NUMBER << 3 | LENGTH_DELIMITED_TYPE);
StringSerializer.serialize(labels, target);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void serialize(

int messageStartIndex = target.writerIndex();

target.writeInt(0);
target.writeIntLE(0);
target.writeByte(1 << 3 | VARINT_TYPE);
writeUnsignedVarint(timestampSeconds, target);
target.writeByte(2 << 3 | VARINT_TYPE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.ssl.SslContext;
import org.agrona.collections.MutableLong;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.org.apache.commons.lang.mutable.MutableLong;
import pl.tkowalcz.tjahzi.stats.SettableClock;
import pl.tkowalcz.tjahzi.stats.StandardMonitoringModule;

Expand Down Expand Up @@ -132,7 +132,7 @@ public Clock getClock() {

@Override
public void recordResponseTime(long time) {
requestRTT.setValue(time);
requestRTT.set(time);
}
};

Expand Down Expand Up @@ -193,7 +193,7 @@ public Clock getClock() {

@Override
public void recordResponseTime(long time) {
requestRTT.setValue(time);
requestRTT.set(time);
}
};

Expand Down
14 changes: 7 additions & 7 deletions logback-appender/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,44 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.14.3</version>
<version>1.20.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.14.3</version>
<version>1.20.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>nginx</artifactId>
<version>1.14.3</version>
<version>1.20.1</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>5.6.2</version>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

Expand Down

0 comments on commit aefa292

Please sign in to comment.