Skip to content

Commit

Permalink
feat(wal):reduce concurrent conflicts between block write operations …
Browse files Browse the repository at this point in the history
…and poll operations (AutoMQ#1550)
  • Loading branch information
CLFutureX committed Jul 11, 2024
1 parent 1ccbeff commit b46383f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class BlockBatch {
private final Collection<Block> blocks;
private final long startOffset;
private final long endOffset;
private final long blockBatchSize;

public BlockBatch(Collection<Block> blocks) {
assert !blocks.isEmpty();
Expand All @@ -33,6 +34,9 @@ public BlockBatch(Collection<Block> blocks) {
.map(b -> b.startOffset() + b.size())
.max(Long::compareTo)
.orElseThrow();
this.blockBatchSize = blocks.stream()
.mapToLong(Block::size)
.sum();
}

public long startOffset() {
Expand All @@ -47,6 +51,10 @@ public Collection<Block> blocks() {
return Collections.unmodifiableCollection(blocks);
}

public long blockBatchSize(){
return blockBatchSize;
}

public Iterator<CompletableFuture<AppendResult.CallbackResult>> futures() {
return new Iterator<>() {
private final Iterator<Block> blockIterator = blocks.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import com.automq.stream.utils.Threads;
import java.util.Collection;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -71,7 +71,7 @@ public class SlidingWindowService {
/**
* Blocks that are being written.
*/
private final Queue<Long> writingBlocks = new PriorityQueue<>();
private final Queue<Long> writingBlocks = new PriorityBlockingQueue<>();
/**
* Whether the service is initialized.
* After the service is initialized, data in {@link #windowCoreData} is valid.
Expand Down Expand Up @@ -331,23 +331,10 @@ private BlockBatch pollBlocksLocked() {
* Finish the given block batch, and return the start offset of the first block which has not been flushed yet.
*/
private long wroteBlocks(BlockBatch wroteBlocks) {
blockLock.lock();
try {
return wroteBlocksLocked(wroteBlocks);
} finally {
blockLock.unlock();
}
}

/**
* Finish the given block batch, and return the start offset of the first block which has not been flushed yet.
* Note: this method is NOT thread safe, and it should be called with {@link #blockLock} locked.
*/
private long wroteBlocksLocked(BlockBatch wroteBlocks) {
boolean removed = writingBlocks.remove(wroteBlocks.startOffset());
assert removed;
if (writingBlocks.isEmpty()) {
return getCurrentBlockLocked().startOffset();
return wroteBlocks.startOffset() + WALUtil.alignLargeByBlockSize(wroteBlocks.blockBatchSize());
}
return writingBlocks.peek();
}
Expand Down

0 comments on commit b46383f

Please sign in to comment.