Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private synchronized void addToCacheInMemory(final ByteArray holder) {
logger.trace("address = {} adding {}", address, describeID(holder.bytes));
}

cache.put(holder, cachedBoxedInts.apply(pos));
final Integer slot = cachedBoxedInts.apply(pos);

if (pos < ids.size()) {
// Need fast array style access here -hence ArrayList typing
Expand All @@ -202,7 +202,8 @@ private synchronized void addToCacheInMemory(final ByteArray holder) {
logger.trace("address = {} removing excess duplicateDetection {}", address, describeID(id.bytes));
}

cache.remove(id);
// The same id may be held in a newer slot too: then it stays
cache.remove(id, slot);
}

if (logger.isTraceEnabled()) {
Expand All @@ -217,6 +218,8 @@ private synchronized void addToCacheInMemory(final ByteArray holder) {
ids.add(holder);
}

cache.put(holder, slot);

if (pos++ == cacheSize - 1) {
pos = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private synchronized void addToCacheInMemory(final ByteArray holder, final long
logger.trace("address = {} adding {}", address, describeID(holder.bytes, recordID));
}

cache.put(holder, cachedBoxedInts.apply(pos));
final Integer slot = cachedBoxedInts.apply(pos);

ObjLongPair<ByteArray> id;

Expand All @@ -296,7 +296,8 @@ private synchronized void addToCacheInMemory(final ByteArray holder, final long
logger.trace("address = {} removing excess duplicateDetection {}", address, describeID(id.getA().bytes, id.getB()));
}

cache.remove(id.getA());
// The same id may be held in a newer slot too: then it stays
cache.remove(id.getA(), slot);

assert id.getB() != NIL;
try {
Expand Down Expand Up @@ -325,6 +326,8 @@ private synchronized void addToCacheInMemory(final ByteArray holder, final long

}

cache.put(holder, slot);

if (pos++ == cacheSize - 1) {
pos = 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@
*/
package org.apache.activemq.artemis.tests.unit.core.postoffice.impl;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.IntFunction;

import org.apache.activemq.artemis.api.config.ActiveMQDefaultConfiguration;
import org.apache.activemq.artemis.api.core.Pair;
import org.apache.activemq.artemis.api.core.SimpleString;
import org.apache.activemq.artemis.core.config.Configuration;
import org.apache.activemq.artemis.core.persistence.impl.journal.JournalStorageManager;
import org.apache.activemq.artemis.core.persistence.impl.nullpm.NullStorageManager;
import org.apache.activemq.artemis.core.postoffice.DuplicateIDCache;
import org.apache.activemq.artemis.core.postoffice.PostOffice;
import org.apache.activemq.artemis.core.postoffice.impl.DuplicateIDCaches;
Expand All @@ -47,6 +50,8 @@
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class DuplicateDetectionUnitTest extends ActiveMQTestBase {

Expand Down Expand Up @@ -153,4 +158,38 @@ public void testReloadDuplication() throws Exception {
}

}

@Test
public void testIdHeldTwiceInMemory() throws Exception {
testIdHeldTwice(size -> DuplicateIDCaches.inMemory(SimpleString.of("address"), size));
}

@Test
public void testIdHeldTwicePersistent() throws Exception {
testIdHeldTwice(size -> DuplicateIDCaches.persistent(SimpleString.of("address"), size, new NullStorageManager()));
}

/*
* An id the cache holds in two slots stays a duplicate until its newer slot is overwritten
*/
private void testIdHeldTwice(IntFunction<DuplicateIDCache> caches) throws Exception {
final byte[] same = "same".getBytes(StandardCharsets.UTF_8);

DuplicateIDCache cache = caches.apply(3);
cache.addToCache(same);
cache.addToCache(same);
cache.addToCache("other".getBytes(StandardCharsets.UTF_8));
// Overwrites the older slot holding "same"
cache.addToCache("new".getBytes(StandardCharsets.UTF_8));
assertTrue(cache.contains(same));
// Overwrites the newer one
cache.addToCache("newer".getBytes(StandardCharsets.UTF_8));
assertFalse(cache.contains(same));

// The same id overwriting itself
cache = caches.apply(1);
cache.addToCache(same);
cache.addToCache(same);
assertTrue(cache.contains(same));
}
}
Loading