Skip to content

Commit

Permalink
Use operation attributes for implicit pre-read and insert mode of Put
Browse files Browse the repository at this point in the history
  • Loading branch information
brfrn169 committed Nov 18, 2024
1 parent 3f20979 commit 517ba82
Show file tree
Hide file tree
Showing 12 changed files with 367 additions and 161 deletions.
18 changes: 12 additions & 6 deletions core/src/main/java/com/scalar/db/api/OperationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,24 @@ public interface ClearValues<T> {

public interface ImplicitPreReadEnabled<T> {
/**
* Disables implicit pre-read for this put operation.
* Disables implicit pre-read for this put operation. This is a utility method for Consensus
* Commit.
*
* @return the operation builder
*/
T disableImplicitPreRead();

/**
* Enables implicit pre-read for this put operation.
* Enables implicit pre-read for this put operation. This is a utility method for Consensus
* Commit.
*
* @return the operation builder
*/
T enableImplicitPreRead();

/**
* Sets whether implicit pre-read is enabled or not for this put operation.
* Sets whether implicit pre-read is enabled or not for this put operation. This is a utility
* method for Consensus Commit.
*
* @param implicitPreReadEnabled whether implicit pre-read is enabled or not
* @return the operation builder
Expand All @@ -309,21 +312,24 @@ public interface ImplicitPreReadEnabled<T> {

public interface InsertModeEnabled<T> {
/**
* Disables the insert mode for this put operation.
* Disables the insert mode for this put operation. This is a utility method for Consensus
* Commit.
*
* @return the operation builder
*/
T disableInsertMode();

/**
* Enables the insert mode for this put operation.
* Enables the insert mode for this put operation. This is a utility method for Consensus
* Commit.
*
* @return the operation builder
*/
T enableInsertMode();

/**
* Sets whether the insert mode is enabled or not for this put operation.
* Sets whether the insert mode is enabled or not for this put operation. This is a utility
* method for Consensus Commit.
*
* @param insertModeEnabled whether the insert mode is enabled or not
* @return the operation builder
Expand Down
59 changes: 18 additions & 41 deletions core/src/main/java/com/scalar/db/api/Put.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.scalar.db.io.Key;
import com.scalar.db.io.TextColumn;
import com.scalar.db.io.Value;
import com.scalar.db.transaction.consensuscommit.ConsensusCommitOperationAttribute;
import com.scalar.db.util.ScalarDbUtils;
import java.nio.ByteBuffer;
import java.util.Collection;
Expand All @@ -42,10 +43,6 @@ public class Put extends Mutation {

private final Map<String, Column<?>> columns;

private boolean implicitPreReadEnabled;

private boolean insertModeEnabled;

Put(
@Nullable String namespace,
String tableName,
Expand All @@ -54,13 +51,9 @@ public class Put extends Mutation {
@Nullable Consistency consistency,
ImmutableMap<String, String> attributes,
@Nullable MutationCondition condition,
Map<String, Column<?>> columns,
boolean implicitPreReadEnabled,
boolean insertModeEnabled) {
Map<String, Column<?>> columns) {
super(namespace, tableName, partitionKey, clusteringKey, consistency, attributes, condition);
this.columns = columns;
this.implicitPreReadEnabled = implicitPreReadEnabled;
this.insertModeEnabled = insertModeEnabled;
}

/**
Expand Down Expand Up @@ -104,8 +97,6 @@ public Put(Key partitionKey, Key clusteringKey) {
public Put(Put put) {
super(put);
columns = new LinkedHashMap<>(put.columns);
implicitPreReadEnabled = put.implicitPreReadEnabled;
insertModeEnabled = put.insertModeEnabled;
}

/**
Expand Down Expand Up @@ -779,41 +770,31 @@ public Optional<MutationCondition> getCondition() {
}

/**
* Returns whether implicit pre-read is enabled for this Put.
* Returns whether implicit pre-read is enabled for this Put. This is a utility method for
* Consensus Commit.
*
* @return whether implicit pre-read is enabled for this Put
* @deprecated As of release 3.15.0. Will be removed in release 5.0.0. Use {@link
* ConsensusCommitOperationAttribute#isImplicitPreReadEnabled(Put)} instead
*/
@SuppressWarnings("InlineMeSuggester")
@Deprecated
public boolean isImplicitPreReadEnabled() {
return implicitPreReadEnabled;
}

/**
* Sets whether implicit pre-read is enabled for this Put.
*
* @param implicitPreReadEnabled whether the implicit pre-read is enabled for this Put
*/
Put setImplicitPreReadEnabled(boolean implicitPreReadEnabled) {
this.implicitPreReadEnabled = implicitPreReadEnabled;
return this;
return ConsensusCommitOperationAttribute.isImplicitPreReadEnabled(this);
}

/**
* Returns whether the insert mode is enabled for this Put.
* Returns whether the insert mode is enabled for this Put. This is a utility method for Consensus
* Commit.
*
* @return whether the insert mode is enabled for this Put
* @deprecated As of release 3.15.0. Will be removed in release 5.0.0. Use {@link
* ConsensusCommitOperationAttribute#isInsertModeEnabled(Put)} instead
*/
@SuppressWarnings("InlineMeSuggester")
@Deprecated
public boolean isInsertModeEnabled() {
return insertModeEnabled;
}

/**
* Sets whether the insert mode is enabled for this Put.
*
* @param insertModeEnabled whether the insert mode is enabled for this Put
*/
Put setInsertModeEnabled(boolean insertModeEnabled) {
this.insertModeEnabled = insertModeEnabled;
return this;
return ConsensusCommitOperationAttribute.isInsertModeEnabled(this);
}

@Override
Expand Down Expand Up @@ -846,14 +827,12 @@ public boolean equals(Object o) {
return false;
}
Put other = (Put) o;
return columns.equals(other.columns)
&& implicitPreReadEnabled == other.implicitPreReadEnabled
&& insertModeEnabled == other.insertModeEnabled;
return columns.equals(other.columns);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), columns, implicitPreReadEnabled, insertModeEnabled);
return Objects.hash(super.hashCode(), columns);
}

@Override
Expand All @@ -867,8 +846,6 @@ public String toString() {
.add("attributes", getAttributes())
.add("condition", getCondition())
.add("columns", getColumns())
.add("implicitPreReadEnabled", isImplicitPreReadEnabled())
.add("insertModeEnabled", isInsertModeEnabled())
.toString();
}
}
29 changes: 16 additions & 13 deletions core/src/main/java/com/scalar/db/api/PutBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.scalar.db.io.IntColumn;
import com.scalar.db.io.Key;
import com.scalar.db.io.TextColumn;
import com.scalar.db.transaction.consensuscommit.ConsensusCommitOperationAttribute;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -91,8 +92,6 @@ public static class Buildable extends OperationBuilder.Buildable<Put>
@Nullable com.scalar.db.api.Consistency consistency;
final Map<String, String> attributes = new HashMap<>();
@Nullable MutationCondition condition;
boolean implicitPreReadEnabled;
boolean insertModeEnabled;

private Buildable(@Nullable String namespace, String table, Key partitionKey) {
super(namespace, table, partitionKey);
Expand Down Expand Up @@ -235,37 +234,45 @@ public Buildable value(Column<?> column) {

@Override
public Buildable disableImplicitPreRead() {
implicitPreReadEnabled = false;
ConsensusCommitOperationAttribute.disableImplicitPreRead(attributes);
return this;
}

@Override
public Buildable enableImplicitPreRead() {
implicitPreReadEnabled = true;
ConsensusCommitOperationAttribute.enableImplicitPreRead(attributes);
return this;
}

@Override
public Buildable implicitPreReadEnabled(boolean implicitPreReadEnabled) {
this.implicitPreReadEnabled = implicitPreReadEnabled;
if (implicitPreReadEnabled) {
ConsensusCommitOperationAttribute.enableImplicitPreRead(attributes);
} else {
ConsensusCommitOperationAttribute.disableImplicitPreRead(attributes);
}
return this;
}

@Override
public Buildable disableInsertMode() {
insertModeEnabled = false;
ConsensusCommitOperationAttribute.disableInsertMode(attributes);
return this;
}

@Override
public Buildable enableInsertMode() {
insertModeEnabled = true;
ConsensusCommitOperationAttribute.enableInsertMode(attributes);
return this;
}

@Override
public Buildable insertModeEnabled(boolean insertModeEnabled) {
this.insertModeEnabled = insertModeEnabled;
if (insertModeEnabled) {
ConsensusCommitOperationAttribute.enableInsertMode(attributes);
} else {
ConsensusCommitOperationAttribute.disableInsertMode(attributes);
}
return this;
}

Expand All @@ -279,9 +286,7 @@ public Put build() {
consistency,
ImmutableMap.copyOf(attributes),
condition,
columns,
implicitPreReadEnabled,
insertModeEnabled);
columns);
}
}

Expand All @@ -302,8 +307,6 @@ public static class BuildableFromExisting extends Buildable
this.consistency = put.getConsistency();
this.condition = put.getCondition().orElse(null);
this.attributes.putAll(put.getAttributes());
this.implicitPreReadEnabled = put.isImplicitPreReadEnabled();
this.insertModeEnabled = put.isInsertModeEnabled();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.scalar.db.transaction.consensuscommit;

import com.scalar.db.api.Put;
import java.util.Map;
import java.util.Optional;

/** A class to manage the operations attributes for Consensus Commit. */
public final class ConsensusCommitOperationAttribute {

private static final String OPERATION_ATTRIBUTE_PREFIX = "cc-";
public static final String IMPLICIT_PRE_READ_ENABLED =
OPERATION_ATTRIBUTE_PREFIX + "implicit-pre-read-enabled";
public static final String INSERT_MODE_ENABLED =
OPERATION_ATTRIBUTE_PREFIX + "insert-mode-enabled";

private ConsensusCommitOperationAttribute() {}

public static Put enableImplicitPreRead(Put put) {
return Put.newBuilder(put).attribute(IMPLICIT_PRE_READ_ENABLED, "true").build();
}

public static void enableImplicitPreRead(Map<String, String> attributes) {
attributes.put(IMPLICIT_PRE_READ_ENABLED, "true");
}

public static Put disableImplicitPreRead(Put put) {
return Put.newBuilder(put).clearAttribute(IMPLICIT_PRE_READ_ENABLED).build();
}

public static void disableImplicitPreRead(Map<String, String> attributes) {
attributes.remove(IMPLICIT_PRE_READ_ENABLED);
}

public static Put enableInsertMode(Put put) {
return Put.newBuilder(put).attribute(INSERT_MODE_ENABLED, "true").build();
}

public static void enableInsertMode(Map<String, String> attributes) {
attributes.put(INSERT_MODE_ENABLED, "true");
}

public static Put disableInsertMode(Put put) {
return Put.newBuilder(put).clearAttribute(INSERT_MODE_ENABLED).build();
}

public static void disableInsertMode(Map<String, String> attributes) {
attributes.remove(INSERT_MODE_ENABLED);
}

public static boolean isImplicitPreReadEnabled(Put put) {
Optional<String> attribute = put.getAttribute(IMPLICIT_PRE_READ_ENABLED);
return attribute.isPresent() && "true".equalsIgnoreCase(attribute.get());
}

public static boolean isInsertModeEnabled(Put put) {
Optional<String> attribute = put.getAttribute(INSERT_MODE_ENABLED);
return attribute.isPresent() && "true".equalsIgnoreCase(attribute.get());
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.scalar.db.transaction.consensuscommit;

import static com.google.common.base.Preconditions.checkNotNull;
import static com.scalar.db.transaction.consensuscommit.ConsensusCommitOperationAttribute.isImplicitPreReadEnabled;

import com.google.common.annotations.VisibleForTesting;
import com.scalar.db.api.Consistency;
Expand Down Expand Up @@ -195,15 +196,15 @@ public void put(Put put) throws CrudException {
Snapshot.Key key = new Snapshot.Key(put);

if (put.getCondition().isPresent()
&& (!put.isImplicitPreReadEnabled() && !snapshot.containsKeyInReadSet(key))) {
&& (!isImplicitPreReadEnabled(put) && !snapshot.containsKeyInReadSet(key))) {
throw new IllegalArgumentException(
CoreError
.CONSENSUS_COMMIT_PUT_CANNOT_HAVE_CONDITION_WHEN_TARGET_RECORD_UNREAD_AND_IMPLICIT_PRE_READ_DISABLED
.buildMessage(put));
}

if (put.getCondition().isPresent()) {
if (put.isImplicitPreReadEnabled() && !snapshot.containsKeyInReadSet(key)) {
if (isImplicitPreReadEnabled(put) && !snapshot.containsKeyInReadSet(key)) {
read(key, createGet(key));
}
mutationConditionsValidator.checkIfConditionIsSatisfied(
Expand Down Expand Up @@ -233,7 +234,7 @@ public void readIfImplicitPreReadEnabled() throws CrudException {
// For each put in the write set, if implicit pre-read is enabled and the record is not read
// yet, read the record
for (Put put : snapshot.getPutsInWriteSet()) {
if (put.isImplicitPreReadEnabled()) {
if (isImplicitPreReadEnabled(put)) {
Snapshot.Key key = new Snapshot.Key(put);
if (!snapshot.containsKeyInReadSet(key)) {
tasks.add(() -> read(key, createGet(key)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static com.scalar.db.transaction.consensuscommit.Attribute.ID;
import static com.scalar.db.transaction.consensuscommit.Attribute.VERSION;
import static com.scalar.db.transaction.consensuscommit.ConsensusCommitOperationAttribute.isInsertModeEnabled;
import static com.scalar.db.transaction.consensuscommit.ConsensusCommitUtils.getNextTxVersion;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -64,7 +65,7 @@ private void add(Put base, @Nullable TransactionResult result) throws ExecutionE
putBuilder.intValue(Attribute.STATE, TransactionState.PREPARED.get());
putBuilder.bigIntValue(Attribute.PREPARED_AT, current);

if (!base.isInsertModeEnabled() && result != null) { // overwrite existing record
if (!isInsertModeEnabled(base) && result != null) { // overwrite existing record
createBeforeColumns(base, result).forEach(putBuilder::value);
int version = result.getVersion();
putBuilder.intValue(Attribute.VERSION, getNextTxVersion(version));
Expand Down
Loading

0 comments on commit 517ba82

Please sign in to comment.