Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#7584] improvement: Add rollback logic to the in-memory test store (#7687)

Reuben George0540c3321aef
diff --git a/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java b/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java
index 7c1cbb5a41..4bf70297e8 100644
--- a/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java
+++ b/core/src/test/java/org/apache/gravitino/storage/memory/TestMemoryEntityStore.java
@@ -154,9 +154,17 @@ public class TestMemoryEntityStore {
@Override
public <R, E extends Exception> R executeInTransaction(Executable<R, E> executable)
throws E, IOException {
+ Map<NameIdentifier, Entity> snapshot = createSnapshot();
+ lock.lock();
try {
- lock.lock();
return executable.execute();
+ } catch (Exception e) {
+ if (snapshot != null) {
+ // restore the entityMap in case of failed transactions
+ entityMap.clear();
+ entityMap.putAll(snapshot);
+ }
+ throw e;
} finally {
lock.unlock();
}
@@ -166,6 +174,20 @@ public class TestMemoryEntityStore {
public void close() throws IOException {
entityMap.clear();
}
+
+ public Map<NameIdentifier, Entity> createSnapshot() {
+ lock.lock();
+ try {
+ return entityMap.entrySet().stream()
+ .collect(
+ Collectors.toMap(
+ Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, Maps::newHashMap));
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to create snapshot of entity store", e);
+ } finally {
+ lock.unlock();
+ }
+ }
}
@Test