Skip to content
Merged
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 @@ -98,13 +98,40 @@ public synchronized void cache(final MetaData data) {
/**
* Remove auth data.
*
* <p>Deletion events may carry only half of the identity: node sync sends
* an id without a path, path sync sends a path without an id (#7790).
* The partial identity is resolved against the cached canonical record so
* both the id map and the derived path results are invalidated, and the
* removal stays idempotent instead of throwing.</p>
*
* @param data the data
*/
public synchronized void remove(final MetaData data) {
META_DATA_MAP.remove(data.getId());
final String id = data.getId();
final String path = data.getPath();
final MetaData cached = Objects.nonNull(id) ? META_DATA_MAP.get(id) : findByPath(path);
if (Objects.nonNull(id)) {
META_DATA_MAP.remove(id);
} else if (Objects.nonNull(cached)) {
META_DATA_MAP.remove(cached.getId());
}
indexDirty = true;
negativeCacheDirty = true;
clean(data.getPath());
if (Objects.nonNull(path)) {
clean(path);
} else if (Objects.nonNull(cached) && Objects.nonNull(cached.getPath())) {
clean(cached.getPath());
}
}

private MetaData findByPath(final String path) {
if (Objects.isNull(path)) {
return null;
}
return META_DATA_MAP.values().stream()
.filter(metaData -> path.equals(metaData.getPath()))
.findFirst()
.orElse(null);
}

private void rebuildPathIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;

class MetaDataCacheTest {
Expand All @@ -45,6 +47,54 @@ void shouldPreferMostSpecificMatchingPattern() {
assertSame(specific, cache.obtain("/api/users/42"));
}

@Test
void shouldRemoveByIdOnlyRecordWithoutPath() {
MetaData full = createMetaData("id-1", "/rpc/dubbo/save");
cache.cache(full);
assertSame(full, cache.obtain("/rpc/dubbo/save"));

MetaData idOnly = new MetaData();
idOnly.setId("id-1");

assertDoesNotThrow(() -> cache.remove(idOnly));
assertNull(cache.obtain("/rpc/dubbo/save"));
}

@Test
void shouldRemoveByPathOnlyRecordWithoutId() {
MetaData full = createMetaData("id-2", "/rpc/sofa/find");
cache.cache(full);
assertSame(full, cache.obtain("/rpc/sofa/find"));

MetaData pathOnly = new MetaData();
pathOnly.setPath("/rpc/sofa/find");

assertDoesNotThrow(() -> cache.remove(pathOnly));
assertNull(cache.obtain("/rpc/sofa/find"));
}

@Test
void shouldKeepRemovingFullRecord() {
MetaData full = createMetaData("id-3", "/rpc/http/get");
cache.cache(full);

cache.remove(createMetaData("id-3", "/rpc/http/get"));

assertNull(cache.obtain("/rpc/http/get"));
}

@Test
void shouldBeIdempotentForPartialRecords() {
MetaData idOnly = new MetaData();
idOnly.setId("missing-id");
MetaData pathOnly = new MetaData();
pathOnly.setPath("/rpc/never/cached");

assertDoesNotThrow(() -> cache.remove(idOnly));
assertDoesNotThrow(() -> cache.remove(pathOnly));
assertDoesNotThrow(() -> cache.remove(idOnly));
}

private MetaData createMetaData(final String id, final String path) {
MetaData metaData = new MetaData();
metaData.setId(id);
Expand Down
Loading