Skip to content

Commit

Permalink
tuning performance
Browse files Browse the repository at this point in the history
  • Loading branch information
gc-garcol committed Dec 12, 2024
1 parent 1070fff commit 8903c3c
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
Binary file modified benchmark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

group = "io.github.gc-garcol"
version = "0.3.0"
version = "0.4.0"

java {
withJavadocJar()
Expand Down
4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Cafe WAL

Append over 180,000 records per second.

[![Maven Central Version](https://img.shields.io/maven-central/v/io.github.gc-garcol/cafe-wal?logo=sonatype&logoColor=red&style=flat&label=Maven%20Central)](https://central.sonatype.com/artifact/io.github.gc-garcol/cafe-wal?)
[![javadoc](https://javadoc.io/badge2/io.github.gc-garcol/cafe-wal/javadoc.svg?)](https://javadoc.io/doc/io.github.gc-garcol/cafe-wal?)

## Benchmark

140k records per second (not batching)
180k records per second (not batching)

![benchmark.png](benchmark.png)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
*/
@State(Scope.Thread)
@BenchmarkMode({ Mode.Throughput, Mode.AverageTime })
@OutputTimeUnit(TimeUnit.SECONDS)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Fork(1)
public class LogRepositoryAppendBenchmark
{
Expand Down
2 changes: 1 addition & 1 deletion wal-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group = "io.github.gc-garcol"
version = "0.3.0"
version = "0.4.0"

java {
toolchain {
Expand Down
51 changes: 21 additions & 30 deletions wal-core/src/main/java/gc/garcol/walcore/LogRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
* @author thaivc
* @since 2024
*/
public class LogRepository
{
public class LogRepository {
/**
* The first segment number.
*/
Expand All @@ -38,13 +37,15 @@ public class LogRepository
FileChannel indexChannel;
FileChannel logChannel;

long logOffset;
long indexOffset;

/**
* Constructs a `LogRepository` with the specified base log directory.
*
* @param baseLogDir the base directory for log files
*/
public LogRepository(String baseLogDir)
{
public LogRepository(String baseLogDir) {
LogUtil.createDirectoryNX(baseLogDir);
this.baseLogDir = baseLogDir;
}
Expand All @@ -59,21 +60,21 @@ public LogRepository(String baseLogDir)
* @return the segment number of the latest log file, or 0 if no log files are found
* @throws IOException if an I/O error occurs while accessing the log directory
*/
public long getLatestSegment() throws IOException
{
public long getLatestSegment() throws IOException {
Optional<Path> lastFile = Files.list(Paths.get(baseLogDir))
.filter(Files::isRegularFile)
.max(Comparator.comparing(Path::getFileName));
return lastFile.map(path -> LogUtil.segment(path.getFileName().toString())).orElse(-1L);
}

private void generateFiles(long segment) throws IOException
{
private void generateFiles(long segment) throws IOException {
indexFile = new RandomAccessFile(indexPath(segment), "rw");
logFile = new RandomAccessFile(logPath(segment), "rw");
currentIndex = totalRecords(currentSegment);
indexChannel = indexFile.getChannel();
logChannel = logFile.getChannel();
logOffset = logFile.length();
indexOffset = indexFile.length();
}

/**
Expand All @@ -86,11 +87,9 @@ private void generateFiles(long segment) throws IOException
* @throws IOException if an I/O error occurs during the file operations
* @throws IllegalArgumentException if the specified segment is older than the current segment
*/
public void switchToSegment(long segment) throws IOException
{
public void switchToSegment(long segment) throws IOException {
long latestSegment = getLatestSegment();
if (segment < latestSegment)
{
if (segment < latestSegment) {
throw new IllegalArgumentException("Cannot switch to an older segment");
}
currentSegment = segment;
Expand All @@ -106,8 +105,7 @@ public void switchToSegment(long segment) throws IOException
* @param readerBuffer the buffer to read the log entry into
* @throws IOException if an I/O error occurs
*/
public void read(long segment, long index, ByteBuffer readerBuffer) throws IOException
{
public void read(long segment, long index, ByteBuffer readerBuffer) throws IOException {
RandomAccessFile indexFileRead = indexFile != null && segment == currentSegment ? indexFile : new RandomAccessFile(indexPath(segment), "r");
RandomAccessFile logFileRead = logFile != null && segment == currentSegment ? logFile : new RandomAccessFile(logPath(segment), "r");

Expand All @@ -134,17 +132,16 @@ public void read(long segment, long index, ByteBuffer readerBuffer) throws IOExc
* @param logs the buffer containing the log entries to append
* @throws IOException if an I/O error occurs during the write operation
*/
public void append(ByteBuffer logs) throws IOException
{
public void append(ByteBuffer logs) throws IOException {
indexBufferWriter.clear();
indexBufferWriter.putLong(logFile.length());
indexBufferWriter.putInt(logs.limit());
indexBufferWriter.flip();

var logOffset = logFile.length();
var indexOffset = indexFile.length();
logChannel.write(logs, logOffset);
indexChannel.write(indexBufferWriter, indexOffset);
logOffset += logs.limit();
indexOffset += LogIndex.SIZE;
currentIndex++;
}

Expand All @@ -157,19 +154,16 @@ public void append(ByteBuffer logs) throws IOException
* @param fromIndex the index from which to truncate
* @throws IOException if an I/O error occurs
*/
public void truncate(long segment, long fromIndex) throws IOException
{
public void truncate(long segment, long fromIndex) throws IOException {
File indexFileSys = new File(indexPath(segment));
if (!indexFileSys.exists())
{
if (!indexFileSys.exists()) {
return;
}

try (
RandomAccessFile indexFile = new RandomAccessFile(indexPath(segment), "rw");
RandomAccessFile logFile = new RandomAccessFile(logPath(segment), "rw")
)
{
) {
indexBufferReader.clear();
var indexChannel = indexFile.getChannel();
indexChannel.read(indexBufferReader, fromIndex * LogIndex.SIZE);
Expand All @@ -187,19 +181,16 @@ public void truncate(long segment, long fromIndex) throws IOException
* @return the total number of records
* @throws IOException if an I/O error occurs
*/
public long totalRecords(long segment) throws IOException
{
public long totalRecords(long segment) throws IOException {
RandomAccessFile indexFileRead = segment == currentSegment ? indexFile : new RandomAccessFile(indexPath(segment), "r");
return indexFileRead.length() / LogIndex.SIZE;
}

private String indexPath(long segment)
{
private String indexPath(long segment) {
return baseLogDir + "/" + LogUtil.indexName(segment);
}

private String logPath(long segment)
{
private String logPath(long segment) {
return baseLogDir + "/" + LogUtil.logName(segment);
}
}

0 comments on commit 8903c3c

Please sign in to comment.