Skip to content

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

[#252] improvement(core):Remove nested RocksDB transaction logic and code block (#258)

Qi Yu570eacb12ab9
diff --git a/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java b/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java
index d0b770d656..88283acbeb 100644
--- a/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java
+++ b/core/src/main/java/com/datastrato/graviton/storage/kv/KvEntityStore.java
@@ -94,22 +94,18 @@ public class KvEntityStore implements EntityStore {
@Override
public <E extends Entity & HasIdentifier> void put(E e, boolean overwritten)
throws IOException, EntityAlreadyExistsException {
- executeInTransaction(
- () -> {
- byte[] key = entityKeyEncoder.encode(e.nameIdentifier(), e.type());
- byte[] value = serDe.serialize(e);
- backend.put(key, value, overwritten);
- return null;
- });
+ byte[] key = entityKeyEncoder.encode(e.nameIdentifier(), e.type());
+ byte[] value = serDe.serialize(e);
+ backend.put(key, value, overwritten);
}
@Override
public <E extends Entity & HasIdentifier> E update(
NameIdentifier ident, Class<E> type, EntityType entityType, Function<E, E> updater)
throws IOException, NoSuchEntityException {
+ byte[] key = entityKeyEncoder.encode(ident, entityType);
return executeInTransaction(
() -> {
- byte[] key = entityKeyEncoder.encode(ident, entityType);
byte[] value = backend.get(key);
if (value == null) {
throw new NoSuchEntityException(ident.toString());
diff --git a/core/src/main/java/com/datastrato/graviton/storage/kv/RocksDBKvBackend.java b/core/src/main/java/com/datastrato/graviton/storage/kv/RocksDBKvBackend.java
index 393a4373e2..bf507609b2 100644
--- a/core/src/main/java/com/datastrato/graviton/storage/kv/RocksDBKvBackend.java
+++ b/core/src/main/java/com/datastrato/graviton/storage/kv/RocksDBKvBackend.java
@@ -205,13 +205,8 @@ public class RocksDBKvBackend implements KvBackend {
@Override
public <R, E extends Exception> R executeInTransaction(Executable<R, E> executable)
throws E, IOException {
- // Already in tx, directly execute it without creating a new tx
- if (TX_LOCAL.get() != null) {
- return executable.execute();
- }
-
Transaction tx = db.beginTransaction(new WriteOptions());
- LOGGER.info("Starting transaction: {}", tx);
+ LOGGER.info("Starting transaction: id: '{}'", tx.getID());
TX_LOCAL.set(tx);
try {
R r = executable.execute();
@@ -224,15 +219,16 @@ public class RocksDBKvBackend implements KvBackend {
rollback(tx, e);
throw e;
} finally {
+ LOGGER.info("Transaction close, tx id: '{}', tx state: '{}'", tx.getID(), tx.getState());
tx.close();
- LOGGER.info("Transaction close: {}", tx);
TX_LOCAL.remove();
}
}
private void rollback(Transaction tx, Exception e) {
LOGGER.error(
- "Error executing transaction, exception: {}, message: {}, stackTrace: \n{}",
+ "Error executing transaction, tx id: '{}', exception: {}, message: {}, stackTrace: \n{}",
+ tx.getID(),
e.getCause(),
e.getMessage(),
Throwables.getStackTraceAsString(e));
@@ -241,7 +237,8 @@ public class RocksDBKvBackend implements KvBackend {
tx.rollback();
} catch (Exception e1) {
LOGGER.error(
- "Error rolling back transaction, exception: {}, message: {}, stackTrace: \n{}",
+ "Error rolling back transaction, tx id: '{}', exception: {}, message: {}, stackTrace: \n{}",
+ tx.getID(),
e1.getCause(),
e1.getMessage(),
Throwables.getStackTraceAsString(e));