Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HDDS-10425. Increase OM transaction index for non-Ratis based on existing Ratis transactionInfoTable #6281

Merged
merged 2 commits into from
Feb 28, 2024
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 @@ -24,6 +24,8 @@
import org.apache.hadoop.hdds.client.ReplicationFactor;
import org.apache.hadoop.hdds.client.ReplicationType;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.utils.TransactionInfo;
import org.apache.hadoop.hdds.utils.db.Table;
import org.apache.hadoop.ozone.MiniOzoneCluster;
import org.apache.hadoop.ozone.OmUtils;
import org.apache.hadoop.ozone.client.ObjectStore;
Expand All @@ -42,11 +44,13 @@
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import static org.apache.hadoop.ozone.OzoneConsts.TRANSACTION_INFO_KEY;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.hadoop.ozone.OmUtils.EPOCH_ID_SHIFT;
import static org.apache.hadoop.ozone.OmUtils.EPOCH_WHEN_RATIS_NOT_ENABLED;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_RATIS_ENABLE_KEY;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Tests OM epoch generation for when Ratis is not enabled.
Expand Down Expand Up @@ -145,6 +149,48 @@ public void testUniqueTrxnIndexOnOMRestart() throws Exception {
assertEquals(4, om.getLastTrxnIndexForNonRatis());
}

@Test
public void testIncreaseTrxnIndexBasedOnExistingDB() throws Exception {
// Set transactionInfo.getTerm() not -1 to mock the DB migrated from ratis cluster.
// When OM is first started from the existing ratis DB, the transaction index for
// requests should not start from 0. It should incrementally increase from the last
// transaction index which was stored in DB transactionInfoTable before started.

String volumeName = "volume" + RandomStringUtils.randomNumeric(5);
String bucketName = "bucket" + RandomStringUtils.randomNumeric(5);
String keyName = "key" + RandomStringUtils.randomNumeric(5);

OzoneManager om = cluster.getOzoneManager();
ObjectStore objectStore = client.getObjectStore();

objectStore.createVolume(volumeName);
OzoneVolume ozoneVolume = objectStore.getVolume(volumeName);
ozoneVolume.createBucket(bucketName);

Table<String, TransactionInfo> transactionInfoTable = om.getMetadataManager().getTransactionInfoTable();
long initIndex = transactionInfoTable.get(TRANSACTION_INFO_KEY).getTransactionIndex();
// Set transactionInfo.getTerm() = 1 to mock the DB migrated from ratis cluster
transactionInfoTable.put(TRANSACTION_INFO_KEY, TransactionInfo.valueOf(1, initIndex));
TransactionInfo transactionInfo = transactionInfoTable.get(TRANSACTION_INFO_KEY);
// Verify transaction term != -1 and index > 1
assertEquals(1, transactionInfo.getTerm());
assertTrue(initIndex > 1);

// Restart the OM and create new object
cluster.restartOzoneManager();

String data = "random data";
OzoneOutputStream ozoneOutputStream = ozoneVolume.getBucket(bucketName).createKey(keyName, data.length());
ozoneOutputStream.write(data.getBytes(UTF_8), 0, data.length());
ozoneOutputStream.close();

// Transaction index after OM restart is incremented by 2 (create and commit op) from the last
// transaction index before OM restart rather than from 0.
// So, the transactionIndex should be (initIndex + 2) rather than (0 + 2)
assertEquals(initIndex + 2,
om.getMetadataManager().getTransactionInfoTable().get(TRANSACTION_INFO_KEY).getTransactionIndex());
}

@Test
public void testEpochIntegrationInObjectID() throws Exception {
// Create a volume and check the objectID has the epoch as
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2153,15 +2153,16 @@ public long getObjectIdFromTxId(long trxnId) {
long getLastTrxnIndexForNonRatis() throws IOException {
TransactionInfo transactionInfo =
TransactionInfo.readTransactionInfo(metadataManager);
// If the OMTransactionInfo does not exist in DB or if the term is not -1
// (corresponding to non-Ratis cluster), return 0 so that new incoming
// If the OMTransactionInfo does not exist in DB, return 0 so that new incoming
// requests can have transaction index starting from 1.
if (transactionInfo == null || transactionInfo.getTerm() != -1) {
if (transactionInfo == null) {
return 0;
}
// If there exists a last transaction index in DB, the new incoming
// requests in non-Ratis cluster must have transaction index
// incrementally increasing from the stored transaction index onwards.
// If there exists a last transaction index in DB, including two cases:
// 1. transactionInfo.getTerm() == -1 corresponds to a non-Ratis cluster
// 2. transactionInfo.getTerm() != -1 indicates that the DB may be migrated from Ratis cluster
// For both cases above, the new incoming requests in non-Ratis cluster must have
// transaction index incrementally increasing from the stored transaction index onwards.
return transactionInfo.getTransactionIndex();
}

Expand Down