-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from mvallim/feature/add-ring-buffer-bocking-q…
…ueue Feature/add ring buffer bocking queue
- Loading branch information
Showing
11 changed files
with
306 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
200 changes: 200 additions & 0 deletions
200
...mplate/src/main/java/com/amazon/sns/messaging/lib/concurrent/RingBufferBlockingQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.amazon.sns.messaging.lib.concurrent; | ||
|
||
import java.util.AbstractQueue; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.concurrent.BlockingQueue; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.concurrent.locks.Condition; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.SneakyThrows; | ||
|
||
@SuppressWarnings({ "java:S2274", "unchecked" }) | ||
public class RingBufferBlockingQueue<E> extends AbstractQueue<E> implements BlockingQueue<E> { | ||
|
||
private static final int DEFAULT_CAPACITY = 2048; | ||
|
||
private final Entry<E>[] buffer; | ||
|
||
private final int capacity; | ||
|
||
private final AtomicInteger writeSequence = new AtomicInteger(-1); | ||
|
||
private final AtomicInteger readSequence = new AtomicInteger(0); | ||
|
||
private final ReentrantLock reentrantLock; | ||
|
||
private final Condition notEmpty; | ||
|
||
private final Condition notFull; | ||
|
||
public RingBufferBlockingQueue(final int capacity) { | ||
this.capacity = capacity; | ||
this.buffer = new Entry[capacity]; | ||
Arrays.setAll(buffer, p -> new Entry<>()); | ||
reentrantLock = new ReentrantLock(true); | ||
notEmpty = reentrantLock.newCondition(); | ||
notFull = reentrantLock.newCondition(); | ||
} | ||
|
||
public RingBufferBlockingQueue() { | ||
this(DEFAULT_CAPACITY); | ||
} | ||
|
||
@SneakyThrows | ||
private void enqueue(final E element) { | ||
while (isFull()) { | ||
notFull.await(); | ||
} | ||
|
||
final int nextWriteSeq = writeSequence.get() + 1; | ||
buffer[wrap(nextWriteSeq)].setValue(element); | ||
writeSequence.incrementAndGet(); | ||
notEmpty.signal(); | ||
} | ||
|
||
@SneakyThrows | ||
private E dequeue() { | ||
while (isEmpty()) { | ||
notEmpty.await(); | ||
} | ||
|
||
final E nextValue = buffer[wrap(readSequence.get())].getValue(); | ||
readSequence.incrementAndGet(); | ||
notFull.signal(); | ||
return nextValue; | ||
} | ||
|
||
private int wrap(final int sequence) { | ||
return sequence % capacity; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return (writeSequence.get() - readSequence.get()) + 1; | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return writeSequence.get() < readSequence.get(); | ||
} | ||
|
||
public boolean isFull() { | ||
return size() >= capacity; | ||
} | ||
|
||
public int writeSequence() { | ||
return writeSequence.get(); | ||
} | ||
|
||
public int readSequence() { | ||
return readSequence.get(); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public E peek() { | ||
if (isEmpty()) { | ||
return null; | ||
} | ||
|
||
return buffer[wrap(readSequence.get())].getValue(); | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public void put(final E element) { | ||
try { | ||
reentrantLock.lock(); | ||
enqueue(element); | ||
} finally { | ||
reentrantLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
@SneakyThrows | ||
public E take() { | ||
try { | ||
reentrantLock.lock(); | ||
return dequeue(); | ||
} finally { | ||
reentrantLock.unlock(); | ||
} | ||
} | ||
|
||
@Getter | ||
@Setter | ||
static class Entry<E> { | ||
|
||
private E value; | ||
|
||
} | ||
|
||
@Override | ||
public boolean offer(final E e) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public E poll() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public Iterator<E> iterator() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean add(final E e) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public boolean offer(final E e, final long timeout, final TimeUnit unit) throws InterruptedException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public E poll(final long timeout, final TimeUnit unit) throws InterruptedException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int remainingCapacity() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int drainTo(final Collection<? super E> c) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public int drainTo(final Collection<? super E> c, final int maxElements) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...te/src/test/java/com/amazon/sns/messaging/lib/concurrent/RingBufferBlockingQueueTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.amazon.sns.messaging.lib.concurrent; | ||
|
||
import static org.awaitility.Awaitility.await; | ||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.hasSize; | ||
|
||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import com.amazon.sns.messaging.lib.model.RequestEntry; | ||
|
||
class RingBufferBlockingQueueTest { | ||
|
||
private final ExecutorService producer = Executors.newSingleThreadExecutor(); | ||
|
||
private final ScheduledExecutorService consumer = Executors.newSingleThreadScheduledExecutor(); | ||
|
||
@Test | ||
void testSuccess() throws InterruptedException { | ||
final List<RequestEntry<Integer>> requestEntriesOut = new LinkedList<>(); | ||
|
||
final RingBufferBlockingQueue<RequestEntry<Integer>> ringBlockingQueue = new RingBufferBlockingQueue<>(5120); | ||
|
||
producer.submit(() -> { | ||
IntStream.range(0, 100_000).forEach(value -> { | ||
ringBlockingQueue.put(RequestEntry.<Integer>builder().withValue(value).build()); | ||
}); | ||
}); | ||
|
||
consumer.scheduleAtFixedRate(() -> { | ||
while (!ringBlockingQueue.isEmpty()) { | ||
final List<RequestEntry<Integer>> requestEntries = new LinkedList<>(); | ||
|
||
while ((requestEntries.size() < 10) && Objects.nonNull(ringBlockingQueue.peek())) { | ||
requestEntries.add(ringBlockingQueue.take()); | ||
} | ||
|
||
requestEntriesOut.addAll(requestEntries); | ||
} | ||
}, 0, 100L, TimeUnit.MILLISECONDS); | ||
|
||
await().atMost(1, TimeUnit.MINUTES).until(() -> ringBlockingQueue.writeSequence() == 99_999); | ||
producer.shutdownNow(); | ||
|
||
await().atMost(1, TimeUnit.MINUTES).until(() -> ringBlockingQueue.readSequence() == 100_000); | ||
consumer.shutdownNow(); | ||
|
||
assertThat(ringBlockingQueue.isEmpty(), is(true)); | ||
|
||
assertThat(requestEntriesOut, hasSize(100_000)); | ||
requestEntriesOut.sort((a, b) -> a.getValue() - b.getValue()); | ||
|
||
for (int i = 0; i < 100_000; i++) { | ||
assertThat(requestEntriesOut.get(i).getValue(), is(i)); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.