mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
perf(sqs): stop copying the whole queue on send and receive (#7478)
GuardedMessageQueue copied the whole message list three times on every change and once more on every receive, so SendMessage and ReceiveMessage cost more the deeper the queue was. The queue now keeps its messages in MessageList, an immutable list whose appends share one array. A send or a receive copies nothing, and a removal copies the list once. The store still gets a list that never changes afterwards, and a failed save still leaves the queue as it was.
This commit is contained in:
1 parent
196b672563
commit
ca54394bf0
4 files changed
+302
-28
No files matched your search
@@ -23,7 +23,7 @@ import java.util.stream.Collectors;
|
||||
class GuardedMessageQueue {
|
||||
|
||||
private final ReentrantLock lock = new ReentrantLock();
|
||||
private final List<Message> messages;
|
||||
private MessageList messages;
|
||||
private final StorageBackend<String, List<Message>> messageStore;
|
||||
private final String storageKey;
|
||||
private final String receiptHandleSecret;
|
||||
@@ -55,7 +55,7 @@ class GuardedMessageQueue {
|
||||
this.detachedSnapshots = messageStore != null
|
||||
&& !(messageStore instanceof InMemoryStorage<?, ?>
|
||||
|| messageStore instanceof AccountAwareStorageBackend<?> aware && aware.isInMemory());
|
||||
this.messages = snapshot(initial);
|
||||
this.messages = MessageList.copyOf(snapshot(initial));
|
||||
this.messageStore = messageStore;
|
||||
this.storageKey = storageKey;
|
||||
this.receiptHandleSecret = receiptHandleSecret;
|
||||
@@ -66,17 +66,13 @@ class GuardedMessageQueue {
|
||||
|
||||
void addMessage(Message message) {
|
||||
try (Guard _ = hold()) {
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
candidate.add(message);
|
||||
commit(candidate);
|
||||
commit(messages.plus(message));
|
||||
}
|
||||
}
|
||||
|
||||
void addAll(List<Message> toAdd) {
|
||||
try (Guard _ = hold()) {
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
candidate.addAll(toAdd);
|
||||
commit(candidate);
|
||||
commit(messages.plusAll(toAdd));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,9 +93,7 @@ class GuardedMessageQueue {
|
||||
claimed, dlqCandidates, previous);
|
||||
}
|
||||
if (!dlqCandidates.isEmpty()) {
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
candidate.removeAll(dlqCandidates);
|
||||
commit(candidate);
|
||||
commit(messages.withoutAll(dlqCandidates::contains));
|
||||
} else if (!claimed.isEmpty()) {
|
||||
persist(messages);
|
||||
}
|
||||
@@ -196,9 +190,7 @@ class GuardedMessageQueue {
|
||||
if (result != HandleResult.APPLIED) {
|
||||
return new Removal(result, null);
|
||||
}
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
candidate.remove(msg);
|
||||
commit(candidate);
|
||||
commit(messages.without(msg));
|
||||
return new Removal(HandleResult.APPLIED, msg);
|
||||
}
|
||||
}
|
||||
@@ -246,8 +238,8 @@ class GuardedMessageQueue {
|
||||
|
||||
void removeExpired(Instant cutoff) {
|
||||
try (Guard _ = hold()) {
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
if (candidate.removeIf(msg -> msg.isExpired(cutoff))) {
|
||||
MessageList candidate = messages.withoutAll(msg -> msg.isExpired(cutoff));
|
||||
if (candidate != messages) {
|
||||
commit(candidate);
|
||||
}
|
||||
}
|
||||
@@ -255,14 +247,14 @@ class GuardedMessageQueue {
|
||||
|
||||
void purge() {
|
||||
try (Guard _ = hold()) {
|
||||
commit(List.of());
|
||||
commit(MessageList.EMPTY);
|
||||
}
|
||||
}
|
||||
|
||||
List<Message> drainAll() {
|
||||
try (Guard _ = hold()) {
|
||||
List<Message> drained = new ArrayList<>(messages);
|
||||
commit(List.of());
|
||||
commit(MessageList.EMPTY);
|
||||
return drained;
|
||||
}
|
||||
}
|
||||
@@ -276,9 +268,8 @@ class GuardedMessageQueue {
|
||||
if (messages.isEmpty() || !eligible.test(messages.getFirst())) {
|
||||
return null;
|
||||
}
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
Message head = candidate.removeFirst();
|
||||
commit(candidate);
|
||||
Message head = messages.getFirst();
|
||||
commit(messages.withoutFirst());
|
||||
return head;
|
||||
}
|
||||
}
|
||||
@@ -286,9 +277,7 @@ class GuardedMessageQueue {
|
||||
/** Put a message that could not be delivered back at the head, preserving queue order. */
|
||||
void restoreFirst(Message message) {
|
||||
try (Guard _ = hold()) {
|
||||
List<Message> candidate = new ArrayList<>(messages);
|
||||
candidate.addFirst(message);
|
||||
commit(candidate);
|
||||
commit(messages.withFirst(message));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -387,15 +376,14 @@ class GuardedMessageQueue {
|
||||
closed = true;
|
||||
}
|
||||
|
||||
private void commit(List<Message> candidate) {
|
||||
private void commit(MessageList candidate) {
|
||||
persist(candidate);
|
||||
messages.clear();
|
||||
messages.addAll(candidate);
|
||||
messages = candidate;
|
||||
}
|
||||
|
||||
private List<Message> snapshot(List<Message> source) {
|
||||
if (!detachedSnapshots) {
|
||||
return new ArrayList<>(source);
|
||||
return source;
|
||||
}
|
||||
return source.stream().map(Message::copy).collect(Collectors.toCollection(ArrayList::new));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
|
||||
import java.util.AbstractList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.RandomAccess;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/** Immutable message list whose appends share one array, so a queue can grow without copying. */
|
||||
final class MessageList extends AbstractList<Message> implements RandomAccess {
|
||||
|
||||
static final MessageList EMPTY = new MessageList(new Slots(new Message[0], 0), 0);
|
||||
|
||||
private static final class Slots {
|
||||
final Message[] items;
|
||||
// Slots any list has written. A list appends in place only when it is the one that wrote last.
|
||||
int filled;
|
||||
|
||||
Slots(Message[] items, int filled) {
|
||||
this.items = items;
|
||||
this.filled = filled;
|
||||
}
|
||||
}
|
||||
|
||||
private final Slots slots;
|
||||
private final int size;
|
||||
|
||||
private MessageList(Slots slots, int size) {
|
||||
this.slots = slots;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
static MessageList copyOf(List<Message> source) {
|
||||
Message[] items = source.toArray(new Message[0]);
|
||||
return new MessageList(new Slots(items, items.length), items.length);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Message get(int index) {
|
||||
Objects.checkIndex(index, size);
|
||||
return slots.items[index];
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
MessageList plus(Message message) {
|
||||
return plusAll(List.of(message));
|
||||
}
|
||||
|
||||
MessageList plusAll(List<Message> added) {
|
||||
if (added.isEmpty()) {
|
||||
return this;
|
||||
}
|
||||
int newSize = size + added.size();
|
||||
if (size == slots.filled && newSize <= slots.items.length) {
|
||||
int index = size;
|
||||
for (Message message : added) {
|
||||
slots.items[index++] = message;
|
||||
}
|
||||
slots.filled = newSize;
|
||||
return new MessageList(slots, newSize);
|
||||
}
|
||||
Message[] items = grown(newSize);
|
||||
int index = size;
|
||||
for (Message message : added) {
|
||||
items[index++] = message;
|
||||
}
|
||||
return new MessageList(new Slots(items, newSize), newSize);
|
||||
}
|
||||
|
||||
MessageList withFirst(Message message) {
|
||||
Message[] items = new Message[size + 1];
|
||||
items[0] = message;
|
||||
System.arraycopy(slots.items, 0, items, 1, size);
|
||||
return new MessageList(new Slots(items, items.length), items.length);
|
||||
}
|
||||
|
||||
MessageList withoutFirst() {
|
||||
Message[] items = new Message[size - 1];
|
||||
System.arraycopy(slots.items, 1, items, 0, items.length);
|
||||
return new MessageList(new Slots(items, items.length), items.length);
|
||||
}
|
||||
|
||||
MessageList without(Message message) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (slots.items[i] == message) {
|
||||
Message[] items = new Message[size - 1];
|
||||
System.arraycopy(slots.items, 0, items, 0, i);
|
||||
System.arraycopy(slots.items, i + 1, items, i, size - i - 1);
|
||||
return new MessageList(new Slots(items, items.length), items.length);
|
||||
}
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
MessageList withoutAll(Predicate<Message> drop) {
|
||||
int first = 0;
|
||||
while (first < size && !drop.test(slots.items[first])) {
|
||||
first++;
|
||||
}
|
||||
if (first == size) {
|
||||
return this;
|
||||
}
|
||||
Message[] kept = new Message[size - 1];
|
||||
System.arraycopy(slots.items, 0, kept, 0, first);
|
||||
int count = first;
|
||||
for (int i = first + 1; i < size; i++) {
|
||||
Message message = slots.items[i];
|
||||
if (!drop.test(message)) {
|
||||
kept[count++] = message;
|
||||
}
|
||||
}
|
||||
Message[] items = new Message[count];
|
||||
System.arraycopy(kept, 0, items, 0, count);
|
||||
return new MessageList(new Slots(items, count), count);
|
||||
}
|
||||
|
||||
private Message[] grown(int newSize) {
|
||||
Message[] items = new Message[Math.max(newSize, size + (size >> 1) + 10)];
|
||||
System.arraycopy(slots.items, 0, items, 0, size);
|
||||
return items;
|
||||
}
|
||||
}
|
||||
@@ -837,6 +837,54 @@ class GuardedMessageQueueTest {
|
||||
assertEquals(0, added.copies);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void memoryBackendsKeepEarlierListsUnchangedWhenMessagesAreAdded(boolean accountAware) {
|
||||
InMemoryStorage<String, List<Message>> raw = new InMemoryStorage<>();
|
||||
StorageBackend<String, List<Message>> store = accountAware
|
||||
? new AccountAwareStorageBackend<>(raw, null, "000000000000") : raw;
|
||||
String rawKey = accountAware ? "000000000000/" + KEY : KEY;
|
||||
GuardedMessageQueue queue = new GuardedMessageQueue(store, KEY);
|
||||
Message first = new Message("first");
|
||||
Message second = new Message("second");
|
||||
queue.addMessage(first);
|
||||
List<Message> earlier = raw.get(rawKey).orElseThrow();
|
||||
queue.addMessage(second);
|
||||
|
||||
assertEquals(1, earlier.size());
|
||||
assertSame(first, earlier.getFirst());
|
||||
List<Message> current = raw.get(rawKey).orElseThrow();
|
||||
assertEquals(2, current.size());
|
||||
assertSame(first, current.get(0));
|
||||
assertSame(second, current.get(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
void memoryBackendFailureDoesNotLeakIntoLaterAdds() {
|
||||
MemoryFailingStore raw = new MemoryFailingStore();
|
||||
GuardedMessageQueue queue = new GuardedMessageQueue(raw, KEY);
|
||||
Message m1 = new Message("m1");
|
||||
Message m2 = new Message("m2");
|
||||
Message m3 = new Message("m3");
|
||||
queue.addMessage(m1);
|
||||
List<Message> beforeFailure = raw.get(KEY).orElseThrow();
|
||||
raw.fail = true;
|
||||
assertThrows(IllegalStateException.class, () -> queue.addMessage(m2));
|
||||
raw.fail = false;
|
||||
queue.addMessage(m3);
|
||||
|
||||
List<Message> stored = raw.get(KEY).orElseThrow();
|
||||
assertEquals(2, stored.size());
|
||||
assertSame(m1, stored.get(0));
|
||||
assertSame(m3, stored.get(1));
|
||||
List<Message> live = queue.peekAll();
|
||||
assertEquals(2, live.size());
|
||||
assertSame(m1, live.get(0));
|
||||
assertSame(m3, live.get(1));
|
||||
assertEquals(1, beforeFailure.size());
|
||||
assertSame(m1, beforeFailure.getFirst());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void memoryBackendFailuresStillRestoreDeliveryAndListState(boolean accountAware) {
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
package io.github.hectorvent.floci.services.sqs;
|
||||
|
||||
import io.github.hectorvent.floci.services.sqs.model.Message;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class MessageListTest {
|
||||
|
||||
private final Message a = new Message("a");
|
||||
private final Message b = new Message("b");
|
||||
private final Message c = new Message("c");
|
||||
|
||||
private static void assertIdentical(MessageList actual, Message... expected) {
|
||||
assertEquals(expected.length, actual.size());
|
||||
for (int i = 0; i < expected.length; i++) {
|
||||
assertSame(expected[i], actual.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void plusFromSharedBaseKeepsBothBranchesIntact() {
|
||||
MessageList base = MessageList.EMPTY.plus(a);
|
||||
MessageList left = base.plus(b);
|
||||
MessageList right = base.plus(c);
|
||||
|
||||
assertIdentical(base, a);
|
||||
assertIdentical(left, a, b);
|
||||
assertIdentical(right, a, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
void plusAllFromSharedBaseKeepsBothBranchesIntact() {
|
||||
MessageList base = MessageList.EMPTY.plus(a);
|
||||
MessageList left = base.plusAll(List.of(b));
|
||||
MessageList right = base.plusAll(List.of(c, b));
|
||||
|
||||
assertIdentical(left, a, b);
|
||||
assertIdentical(right, a, c, b);
|
||||
assertSame(base, base.plusAll(List.of()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void plusKeepsEarlierListsAcrossGrowth() {
|
||||
MessageList list = MessageList.EMPTY;
|
||||
MessageList[] versions = new MessageList[50];
|
||||
Message[] added = new Message[50];
|
||||
for (int i = 0; i < 50; i++) {
|
||||
added[i] = new Message("m" + i);
|
||||
list = list.plus(added[i]);
|
||||
versions[i] = list;
|
||||
}
|
||||
for (int i = 0; i < 50; i++) {
|
||||
assertEquals(i + 1, versions[i].size());
|
||||
for (int j = 0; j <= i; j++) {
|
||||
assertSame(added[j], versions[i].get(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutRemovesFirstMiddleAndLast() {
|
||||
MessageList list = MessageList.copyOf(List.of(a, b, c));
|
||||
|
||||
assertIdentical(list.without(a), b, c);
|
||||
assertIdentical(list.without(b), a, c);
|
||||
assertIdentical(list.without(c), a, b);
|
||||
assertIdentical(list, a, b, c);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutReturnsSameInstanceWhenAbsent() {
|
||||
MessageList list = MessageList.copyOf(List.of(a, b));
|
||||
|
||||
assertSame(list, list.without(c));
|
||||
}
|
||||
|
||||
@Test
|
||||
void withoutAllKeepsTheOtherMessagesInOrder() {
|
||||
MessageList list = MessageList.copyOf(List.of(a, b, c));
|
||||
|
||||
assertIdentical(list.withoutAll(message -> message == b), a, c);
|
||||
assertTrue(list.withoutAll(message -> true).isEmpty());
|
||||
assertSame(list, list.withoutAll(message -> false));
|
||||
}
|
||||
|
||||
@Test
|
||||
void withFirstAndWithoutFirstKeepOrder() {
|
||||
MessageList list = MessageList.copyOf(List.of(a, b));
|
||||
|
||||
assertIdentical(list.withFirst(c), c, a, b);
|
||||
assertIdentical(list.withoutFirst(), b);
|
||||
assertIdentical(list, a, b);
|
||||
}
|
||||
|
||||
@Test
|
||||
void listCannotBeChangedFromOutside() {
|
||||
MessageList list = MessageList.copyOf(List.of(a));
|
||||
|
||||
assertThrows(UnsupportedOperationException.class, () -> list.add(b));
|
||||
assertThrows(UnsupportedOperationException.class, () -> list.remove(0));
|
||||
assertThrows(UnsupportedOperationException.class, () -> list.set(0, b));
|
||||
assertThrows(IndexOutOfBoundsException.class, () -> list.get(1));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user