mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
@@ -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