World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#805] improvement(core): Separate key space betwen Gravitino system inforamtion and entities data (#816)
Qi Yu30ee246f9832
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvNameMappingService.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvNameMappingService.javaindex cc4210920b..7b0f784dd4 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/KvNameMappingService.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/KvNameMappingService.java@@ -27,15 +27,20 @@ public class KvNameMappingService implements NameMappingService {@VisibleForTesting final ReentrantReadWriteLock lock;@VisibleForTesting final IdGenerator idGenerator = new RandomIdGenerator();- // name prefix of name in name to id mapping,+ // To separate it from user keys, we will add three control flag 0x1D, 0x00, 0x00 as the prefix.+ private static final byte[] GENERAL_NAME_MAPPING_PREFIX = new byte[] {0x1D, 0x00, 0x00};++ // Name prefix of name in name to id mapping,// e.g., name_metalake1 -> 1// name_metalake2 -> 2- private static final byte[] NAME_PREFIX = "name_".getBytes(StandardCharsets.UTF_8);+ private static final byte[] NAME_PREFIX =+ Bytes.concat(GENERAL_NAME_MAPPING_PREFIX, "name_".getBytes(StandardCharsets.UTF_8));- // id prefix of id in name to id mapping,+ // Id prefix of id in name to id mapping,// e.g., id_1 -> metalake1// id_2 -> metalake2- private static final byte[] ID_PREFIX = "id_".getBytes(StandardCharsets.UTF_8);+ private static final byte[] ID_PREFIX =+ Bytes.concat(GENERAL_NAME_MAPPING_PREFIX, "id_".getBytes(StandardCharsets.UTF_8));@VisibleForTesting final TransactionalKvBackend transactionalKvBackend;@@ -48,12 +53,11 @@ public class KvNameMappingService implements NameMappingService {@Overridepublic Long getIdByName(String name) throws IOException {+ byte[] nameByte = getNameKey(name);return FunctionUtils.executeWithReadLock(() ->FunctionUtils.executeInTransaction(() -> {- byte[] nameByte =- Bytes.concat(NAME_PREFIX, name.getBytes(StandardCharsets.UTF_8));byte[] idByte = transactionalKvBackend.get(nameByte);return idByte == null ? null : ByteUtils.byteToLong(idByte);},@@ -62,15 +66,14 @@ public class KvNameMappingService implements NameMappingService {}private long bindNameAndId(String name) throws IOException {- byte[] nameByte = Bytes.concat(NAME_PREFIX, name.getBytes(StandardCharsets.UTF_8));+ byte[] nameByte = getNameKey(name);long id = idGenerator.nextId();-+ byte[] idByte = getIdKey(id);return FunctionUtils.executeWithWriteLock(() ->FunctionUtils.executeInTransaction(() -> {transactionalKvBackend.put(nameByte, ByteUtils.longToByte(id), false);- byte[] idByte = Bytes.concat(ID_PREFIX, ByteUtils.longToByte(id));transactionalKvBackend.put(idByte, name.getBytes(StandardCharsets.UTF_8), false);return id;},@@ -84,25 +87,19 @@ public class KvNameMappingService implements NameMappingService {() ->FunctionUtils.executeInTransaction(() -> {- byte[] nameByte =- Bytes.concat(NAME_PREFIX, oldName.getBytes(StandardCharsets.UTF_8));+ byte[] nameByte = getNameKey(oldName);byte[] oldIdValue = transactionalKvBackend.get(nameByte);-// Old mapping has been deleted, no need to do it;if (oldIdValue == null) {return false;}+// Delete old name --> id mappingtransactionalKvBackend.delete(nameByte);+ transactionalKvBackend.put(getNameKey(newName), oldIdValue, false);transactionalKvBackend.put(- Bytes.concat(NAME_PREFIX, newName.getBytes(StandardCharsets.UTF_8)),- oldIdValue,- false);- transactionalKvBackend.put(- Bytes.concat(ID_PREFIX, oldIdValue),- newName.getBytes(StandardCharsets.UTF_8),- true);+ oldIdValue, newName.getBytes(StandardCharsets.UTF_8), true);return true;},transactionalKvBackend),@@ -144,4 +141,14 @@ public class KvNameMappingService implements NameMappingService {return id;}++ /** Generate key to store id to name mapping. */+ private static byte[] getIdKey(long id) {+ return Bytes.concat(ID_PREFIX, ByteUtils.longToByte(id));+ }++ /** Generate key to store name to id mapping. */+ private static byte[] getNameKey(String name) {+ return Bytes.concat(NAME_PREFIX, name.getBytes(StandardCharsets.UTF_8));+ }}diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.javaindex 545f09c0f2..689a091b4e 100644--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java@@ -9,6 +9,7 @@ import com.datastrato.gravitino.Config;import com.datastrato.gravitino.Configs;import com.datastrato.gravitino.storage.TransactionIdGenerator;import com.datastrato.gravitino.utils.ByteUtils;+import com.datastrato.gravitino.utils.Bytes;import com.google.common.annotations.VisibleForTesting;import com.google.common.util.concurrent.ThreadFactoryBuilder;import java.io.IOException;@@ -23,7 +24,14 @@ public class TransactionIdGeneratorImpl implements TransactionIdGenerator {private static final Logger LOGGER = LoggerFactory.getLogger(TransactionIdGeneratorImpl.class);private final KvBackend kvBackend;- @VisibleForTesting static final String LAST_TIMESTAMP = "last_timestamp";++ // We use three control characters 0x1D, 0x00, 0x01 to separate it from other keys.+ private static final byte[] ID_GENERATOR_PREFIX = new byte[] {0x1D, 0x00, 0x01};++ @VisibleForTesting+ static final byte[] LAST_TIMESTAMP =+ Bytes.concat(ID_GENERATOR_PREFIX, "last_timestamp".getBytes(StandardCharsets.UTF_8));+private volatile long incrementId = 0L;private volatile long lastTransactionId = 0L;private final Config config;@@ -57,10 +65,7 @@ public class TransactionIdGeneratorImpl implements TransactionIdGenerator {int i = 0;while (i++ < 3) {try {- kvBackend.put(- LAST_TIMESTAMP.getBytes(StandardCharsets.UTF_8),- ByteUtils.longToByte(System.currentTimeMillis()),- true);+ kvBackend.put(LAST_TIMESTAMP, ByteUtils.longToByte(System.currentTimeMillis()), true);return;} catch (IOException e) {LOGGER.warn("Failed to save current timestamp to storage layer, retrying...", e);@@ -104,7 +109,7 @@ public class TransactionIdGeneratorImpl implements TransactionIdGenerator {* storage layer every 2(default) seconds.*/private long getSavedTs() throws IOException {- byte[] oldIdBytes = kvBackend.get(LAST_TIMESTAMP.getBytes(StandardCharsets.UTF_8));+ byte[] oldIdBytes = kvBackend.get(LAST_TIMESTAMP);return oldIdBytes == null ? 0 : ByteUtils.byteToLong(oldIdBytes);}diff --git a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.javaindex 6bbb5f0e6f..0fa5edcb3b 100644--- a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java+++ b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java@@ -15,7 +15,6 @@ import com.google.common.io.Files;import com.google.common.util.concurrent.ThreadFactoryBuilder;import java.io.File;import java.io.IOException;-import java.nio.charset.StandardCharsets;import java.util.concurrent.LinkedBlockingQueue;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;@@ -59,8 +58,7 @@ public class TestTransactionIdGenerator {transactionIdGenerator.start();// Make sure the scheduler has schedule onceThread.sleep(2000 + 500);- Assertions.assertNotNull(- kvBackend.get(TransactionIdGeneratorImpl.LAST_TIMESTAMP.getBytes(StandardCharsets.UTF_8)));+ Assertions.assertNotNull(kvBackend.get(TransactionIdGeneratorImpl.LAST_TIMESTAMP));}@ParameterizedTestdiff --git a/rfc/rfc-3/Transaction-implementation-on-kv.md b/rfc/rfc-3/Transaction-implementation-on-kv.mdindex 4cb4a3b5a6..821b46ce8b 100644--- a/rfc/rfc-3/Transaction-implementation-on-kv.md+++ b/rfc/rfc-3/Transaction-implementation-on-kv.md@@ -116,4 +116,11 @@ KEY: 0x6B657931 1F FFFFFFFFFFFFFFFE ---> VALUE: 0x01000000 00000000 76616C75653```### Scan and range query process-Scan and range query are almost the same as that of read process, for more detailed information, please see related implementation `TransactionalKvBackendImpl`.\ No newline at end of file+Scan and range query are almost the same as that of read process, for more detailed information, please see related implementation `TransactionalKvBackendImpl`.++## Key format after this modification++- Key starts with 0x'1D0000' store the contents of id-name mapping. for more please refer to class `KvNameMappingService`.+- Key starts with 0x'1D0001' stores the data of current timestamp which is used for generating transaction id, for more please refer to class `TransactionIdGeneratorImpl`.+- Key starts with 0x'1E' stores transaction marks.+- Other key spaces are used to store gravitino entities like `metalakes`,`catalogs`, `scheams`, `tables` and so on.\ No newline at end of file