Transaction.prepare should be public (#10412)

Summary:
The absence of a public modifier appears to be an omission. prepare() is necessary for the TM to participate as a peer in a distributed transaction.

Also add basic “yes it does work in java” tests.

Resolves https://github.com/facebook/rocksdb/issues/10283

Pull Request resolved: https://github.com/facebook/rocksdb/pull/10412

Reviewed By: ajkr

Differential Revision: D38135513

Pulled By: riversand963

fbshipit-source-id: ff52b96bc7218bc3bf12845dee49f5d8edf0e297
main
Alan Paxton 2 years ago committed by Facebook GitHub Bot
parent 3134471457
commit 8db8b98f98
  1. 2
      java/src/main/java/org/rocksdb/Transaction.java
  2. 32
      java/src/test/java/org/rocksdb/OptimisticTransactionTest.java
  3. 114
      java/src/test/java/org/rocksdb/TransactionTest.java

@ -178,7 +178,7 @@ public class Transaction extends RocksObject {
/** /**
* Prepare the current transaction for 2PC * Prepare the current transaction for 2PC
*/ */
void prepare() throws RocksDBException { public void prepare() throws RocksDBException {
//TODO(AR) consider a Java'ish version of this function, which returns an AutoCloseable (commit) //TODO(AR) consider a Java'ish version of this function, which returns an AutoCloseable (commit)
assert(isOwningHandle()); assert(isOwningHandle());
prepare(nativeHandle_); prepare(nativeHandle_);

@ -5,17 +5,39 @@
package org.rocksdb; package org.rocksdb;
import org.junit.Test; import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import org.junit.Test;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
public class OptimisticTransactionTest extends AbstractTransactionTest { public class OptimisticTransactionTest extends AbstractTransactionTest {
@Test
public void prepare_commit() throws RocksDBException {
final byte[] k1 = "key1".getBytes(UTF_8);
final byte[] v1 = "value1".getBytes(UTF_8);
final byte[] v12 = "value12".getBytes(UTF_8);
try (final DBContainer dbContainer = startDb();
final ReadOptions readOptions = new ReadOptions()) {
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.put(k1, v1);
txn.commit();
}
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.put(k1, v12);
txn.prepare();
failBecauseExceptionWasNotThrown(RocksDBException.class);
} catch (final RocksDBException e) {
assertThat(e.getMessage())
.contains("Two phase commit not supported for optimistic transactions");
}
}
}
@Test @Test
public void getForUpdate_cf_conflict() throws RocksDBException { public void getForUpdate_cf_conflict() throws RocksDBException {

@ -51,6 +51,120 @@ public class TransactionTest extends AbstractTransactionTest {
} }
} }
@Test
public void prepare_commit() throws RocksDBException {
final byte[] k1 = "key1".getBytes(UTF_8);
final byte[] v1 = "value1".getBytes(UTF_8);
final byte[] v12 = "value12".getBytes(UTF_8);
try (final DBContainer dbContainer = startDb();
final ReadOptions readOptions = new ReadOptions()) {
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.put(k1, v1);
txn.commit();
}
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.setName("txnPrepare1");
txn.put(k1, v12);
txn.prepare();
txn.commit();
}
try (final Transaction txn = dbContainer.beginTransaction()) {
assertThat(txn.get(readOptions, k1)).isEqualTo(v12);
}
}
}
@Test
public void prepare_rollback() throws RocksDBException {
final byte[] k1 = "key1".getBytes(UTF_8);
final byte[] v1 = "value1".getBytes(UTF_8);
final byte[] v12 = "value12".getBytes(UTF_8);
try (final DBContainer dbContainer = startDb();
final ReadOptions readOptions = new ReadOptions()) {
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.put(k1, v1);
txn.commit();
}
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.setName("txnPrepare1");
txn.put(k1, v12);
txn.prepare();
txn.rollback();
}
try (final Transaction txn = dbContainer.beginTransaction()) {
assertThat(txn.get(readOptions, k1)).isEqualTo(v1);
}
}
}
@Test
public void prepare_read_prepared_commit() throws RocksDBException {
final byte[] k1 = "key1".getBytes(UTF_8);
final byte[] v1 = "value1".getBytes(UTF_8);
final byte[] v12 = "value12".getBytes(UTF_8);
try (final DBContainer dbContainer = startDb();
final ReadOptions readOptions = new ReadOptions()) {
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.put(k1, v1);
txn.commit();
}
Transaction txnPrepare;
txnPrepare = dbContainer.beginTransaction();
txnPrepare.setName("txnPrepare1");
txnPrepare.put(k1, v12);
txnPrepare.prepare();
try (final Transaction txn = dbContainer.beginTransaction()) {
assertThat(txn.get(readOptions, k1)).isEqualTo(v1);
}
txnPrepare.commit();
try (final Transaction txn = dbContainer.beginTransaction()) {
assertThat(txn.get(readOptions, k1)).isEqualTo(v12);
}
}
}
@Test
public void prepare_read_prepared_rollback() throws RocksDBException {
final byte[] k1 = "key1".getBytes(UTF_8);
final byte[] v1 = "value1".getBytes(UTF_8);
final byte[] v12 = "value12".getBytes(UTF_8);
try (final DBContainer dbContainer = startDb();
final ReadOptions readOptions = new ReadOptions()) {
try (final Transaction txn = dbContainer.beginTransaction()) {
txn.put(k1, v1);
txn.commit();
}
Transaction txnPrepare;
txnPrepare = dbContainer.beginTransaction();
txnPrepare.setName("txnPrepare1");
txnPrepare.put(k1, v12);
txnPrepare.prepare();
try (final Transaction txn = dbContainer.beginTransaction()) {
assertThat(txn.get(readOptions, k1)).isEqualTo(v1);
}
txnPrepare.rollback();
try (final Transaction txn = dbContainer.beginTransaction()) {
assertThat(txn.get(readOptions, k1)).isEqualTo(v1);
}
}
}
@Test @Test
public void getForUpdate_conflict() throws RocksDBException { public void getForUpdate_conflict() throws RocksDBException {
final byte[] k1 = "key1".getBytes(UTF_8); final byte[] k1 = "key1".getBytes(UTF_8);

Loading…
Cancel
Save