Skip to content

Commit

Permalink
refactor: add internal data structures for transactional connection s…
Browse files Browse the repository at this point in the history
…tate (#3321)

* refactor: add internal data structures for transactional connection state

This change adds internal data structures that can be used for transactional
connection state. These data structures also reduces the amount of code that
is needed for each connection property that is added. Connection properties
are currently represented as actual variables in the ConnectionImpl class.
These new data structures removes the need for that.

Only the connection property retryAbortsInternally is refactored to use
the new data structure. All other connection properties will be refactored
in a following change, in order to keep each change as small as possible.

The data structure supports both transactional and non-transactional
connection state. Transactional state is disabled in the current version
in order to be consistent with the current behavior. It will be enabled
in a later change when all connection properties have been refactored
to use the new data structure.

* refactor: replace individual variables with ConnectionProperty (#3322)

* refactor: add internal data structures for transactional connection state

This change adds internal data structures that can be used for transactional
connection state. These data structures also reduces the amount of code that
is needed for each connection property that is added. Connection properties
are currently represented as actual variables in the ConnectionImpl class.
These new data structures removes the need for that.

Only the connection property retryAbortsInternally is refactored to use
the new data structure. All other connection properties will be refactored
in a following change, in order to keep each change as small as possible.

The data structure supports both transactional and non-transactional
connection state. Transactional state is disabled in the current version
in order to be consistent with the current behavior. It will be enabled
in a later change when all connection properties have been refactored
to use the new data structure.

* refactor: replace individual variables with ConnectionProperty

Replace individual variables in ConnectionOptions and ConnectionImpl with
references to ConnectionProperties. This reduces the amount of code bloat,
especially in ConnectionOptions, as all connection property parsing is
now handled by the ConnectionState class in a generic way.

This setup also reduces the amount of code that is needed to add a new
connection property, as there is only one source of truth: the list of
properties in the ConnectionProperties class.

Following steps that will reduce the amount of code bloat further are:
1. Replace all the regular expressions for SET and SHOW statements with
   a simple token-based parser.
2. Cleaning up ConnectionOptions further by removing the duplicate list
   of ConnectionProperties there. These can be removed once the JDBC
   driver has been updated to use the new list of properties.

* feat: enable transactional connection state as an opt-in (#3326)

* refactor: add internal data structures for transactional connection state

This change adds internal data structures that can be used for transactional
connection state. These data structures also reduces the amount of code that
is needed for each connection property that is added. Connection properties
are currently represented as actual variables in the ConnectionImpl class.
These new data structures removes the need for that.

Only the connection property retryAbortsInternally is refactored to use
the new data structure. All other connection properties will be refactored
in a following change, in order to keep each change as small as possible.

The data structure supports both transactional and non-transactional
connection state. Transactional state is disabled in the current version
in order to be consistent with the current behavior. It will be enabled
in a later change when all connection properties have been refactored
to use the new data structure.

* refactor: replace individual variables with ConnectionProperty

Replace individual variables in ConnectionOptions and ConnectionImpl with
references to ConnectionProperties. This reduces the amount of code bloat,
especially in ConnectionOptions, as all connection property parsing is
now handled by the ConnectionState class in a generic way.

This setup also reduces the amount of code that is needed to add a new
connection property, as there is only one source of truth: the list of
properties in the ConnectionProperties class.

Following steps that will reduce the amount of code bloat further are:
1. Replace all the regular expressions for SET and SHOW statements with
   a simple token-based parser.
2. Cleaning up ConnectionOptions further by removing the duplicate list
   of ConnectionProperties there. These can be removed once the JDBC
   driver has been updated to use the new list of properties.

* refactor: add internal data structures for transactional connection state

This change adds internal data structures that can be used for transactional
connection state. These data structures also reduces the amount of code that
is needed for each connection property that is added. Connection properties
are currently represented as actual variables in the ConnectionImpl class.
These new data structures removes the need for that.

Only the connection property retryAbortsInternally is refactored to use
the new data structure. All other connection properties will be refactored
in a following change, in order to keep each change as small as possible.

The data structure supports both transactional and non-transactional
connection state. Transactional state is disabled in the current version
in order to be consistent with the current behavior. It will be enabled
in a later change when all connection properties have been refactored
to use the new data structure.

* refactor: replace individual variables with ConnectionProperty

Replace individual variables in ConnectionOptions and ConnectionImpl with
references to ConnectionProperties. This reduces the amount of code bloat,
especially in ConnectionOptions, as all connection property parsing is
now handled by the ConnectionState class in a generic way.

This setup also reduces the amount of code that is needed to add a new
connection property, as there is only one source of truth: the list of
properties in the ConnectionProperties class.

Following steps that will reduce the amount of code bloat further are:
1. Replace all the regular expressions for SET and SHOW statements with
   a simple token-based parser.
2. Cleaning up ConnectionOptions further by removing the duplicate list
   of ConnectionProperties there. These can be removed once the JDBC
   driver has been updated to use the new list of properties.

* feat: enable transactional connection state as an opt-in

This change enables transactional connection state for the Connection API.
It can be enabled by default for PostgreSQL-dialect databases using a system
property, and is always an opt-in for GoogleSQL-dialect databases.

Transactional connection state can be enabled for any database connection
with the `connection_state_type` connection STARTUP property.

* chore: remove misleading comment

* chore: generate libraries at Tue Oct  8 12:50:40 UTC 2024

---------

Co-authored-by: cloud-java-bot <[email protected]>
  • Loading branch information
olavloite and cloud-java-bot authored Oct 11, 2024
1 parent 139a715 commit 1e40bcc
Show file tree
Hide file tree
Showing 27 changed files with 2,809 additions and 786 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.parseTimeUnit;
import static com.google.cloud.spanner.connection.ReadOnlyStalenessUtil.toChronoUnit;

import com.google.api.gax.core.CredentialsProvider;
import com.google.cloud.spanner.Dialect;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.Options.RpcPriority;
import com.google.cloud.spanner.SpannerException;
Expand All @@ -31,6 +33,8 @@
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.spanner.v1.DirectedReadOptions;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
Expand Down Expand Up @@ -73,7 +77,11 @@ private E get(String value) {

/** Converter from string to {@link Boolean} */
static class BooleanConverter implements ClientSideStatementValueConverter<Boolean> {
static final BooleanConverter INSTANCE = new BooleanConverter();

private BooleanConverter() {}

/** Constructor that is needed for reflection. */
public BooleanConverter(String allowedValues) {}

@Override
Expand Down Expand Up @@ -142,7 +150,11 @@ public Boolean convert(String value) {

/** Converter from string to a non-negative integer. */
static class NonNegativeIntegerConverter implements ClientSideStatementValueConverter<Integer> {
static final NonNegativeIntegerConverter INSTANCE = new NonNegativeIntegerConverter();

private NonNegativeIntegerConverter() {}

/** Constructor needed for reflection. */
public NonNegativeIntegerConverter(String allowedValues) {}

@Override
Expand All @@ -167,6 +179,9 @@ public Integer convert(String value) {

/** Converter from string to {@link Duration}. */
static class DurationConverter implements ClientSideStatementValueConverter<Duration> {
static final DurationConverter INSTANCE =
new DurationConverter("('(\\d{1,19})(s|ms|us|ns)'|\\d{1,19}|NULL)");

private final String resetValue;

private final Pattern allowedValues;
Expand Down Expand Up @@ -227,6 +242,10 @@ public PgDurationConverter(String allowedValues) {
/** Converter from string to possible values for read only staleness ({@link TimestampBound}). */
static class ReadOnlyStalenessConverter
implements ClientSideStatementValueConverter<TimestampBound> {
static final ReadOnlyStalenessConverter INSTANCE =
new ReadOnlyStalenessConverter(
"'((STRONG)|(MIN_READ_TIMESTAMP)[\\t ]+((\\d{4})-(\\d{2})-(\\d{2})([Tt](\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(READ_TIMESTAMP)[\\t ]+((\\d{4})-(\\d{2})-(\\d{2})([Tt](\\d{2}):(\\d{2}):(\\d{2})(\\.\\d{1,9})?)([Zz]|([+-])(\\d{2}):(\\d{2})))|(MAX_STALENESS)[\\t ]+((\\d{1,19})(s|ms|us|ns))|(EXACT_STALENESS)[\\t ]+((\\d{1,19})(s|ms|us|ns)))'");

private final Pattern allowedValues;
private final CaseInsensitiveEnumMap<Mode> values = new CaseInsensitiveEnumMap<>(Mode.class);

Expand Down Expand Up @@ -337,9 +356,14 @@ public DirectedReadOptions convert(String value) {
/** Converter for converting strings to {@link AutocommitDmlMode} values. */
static class AutocommitDmlModeConverter
implements ClientSideStatementValueConverter<AutocommitDmlMode> {
static final AutocommitDmlModeConverter INSTANCE = new AutocommitDmlModeConverter();

private final CaseInsensitiveEnumMap<AutocommitDmlMode> values =
new CaseInsensitiveEnumMap<>(AutocommitDmlMode.class);

private AutocommitDmlModeConverter() {}

/** Constructor needed for reflection. */
public AutocommitDmlModeConverter(String allowedValues) {}

@Override
Expand All @@ -353,7 +377,35 @@ public AutocommitDmlMode convert(String value) {
}
}

static class ConnectionStateTypeConverter
implements ClientSideStatementValueConverter<ConnectionState.Type> {
static final ConnectionStateTypeConverter INSTANCE = new ConnectionStateTypeConverter();

private final CaseInsensitiveEnumMap<ConnectionState.Type> values =
new CaseInsensitiveEnumMap<>(ConnectionState.Type.class);

private ConnectionStateTypeConverter() {}

/** Constructor that is needed for reflection. */
public ConnectionStateTypeConverter(String allowedValues) {}

@Override
public Class<ConnectionState.Type> getParameterClass() {
return ConnectionState.Type.class;
}

@Override
public ConnectionState.Type convert(String value) {
return values.get(value);
}
}

static class StringValueConverter implements ClientSideStatementValueConverter<String> {
static final StringValueConverter INSTANCE = new StringValueConverter();

private StringValueConverter() {}

/** Constructor needed for reflection. */
public StringValueConverter(String allowedValues) {}

@Override
Expand Down Expand Up @@ -481,6 +533,8 @@ public PgTransactionMode convert(String value) {

/** Converter for converting strings to {@link RpcPriority} values. */
static class RpcPriorityConverter implements ClientSideStatementValueConverter<RpcPriority> {
static final RpcPriorityConverter INSTANCE = new RpcPriorityConverter("(HIGH|MEDIUM|LOW|NULL)");

private final CaseInsensitiveEnumMap<RpcPriority> values =
new CaseInsensitiveEnumMap<>(RpcPriority.class);
private final Pattern allowedValues;
Expand Down Expand Up @@ -512,9 +566,14 @@ public RpcPriority convert(String value) {
/** Converter for converting strings to {@link SavepointSupport} values. */
static class SavepointSupportConverter
implements ClientSideStatementValueConverter<SavepointSupport> {
static final SavepointSupportConverter INSTANCE = new SavepointSupportConverter();

private final CaseInsensitiveEnumMap<SavepointSupport> values =
new CaseInsensitiveEnumMap<>(SavepointSupport.class);

private SavepointSupportConverter() {}

/** Constructor needed for reflection. */
public SavepointSupportConverter(String allowedValues) {}

@Override
Expand All @@ -528,6 +587,30 @@ public SavepointSupport convert(String value) {
}
}

/** Converter for converting strings to {@link DdlInTransactionMode} values. */
static class DdlInTransactionModeConverter
implements ClientSideStatementValueConverter<DdlInTransactionMode> {
static final DdlInTransactionModeConverter INSTANCE = new DdlInTransactionModeConverter();

private final CaseInsensitiveEnumMap<DdlInTransactionMode> values =
new CaseInsensitiveEnumMap<>(DdlInTransactionMode.class);

private DdlInTransactionModeConverter() {}

/** Constructor needed for reflection. */
public DdlInTransactionModeConverter(String allowedValues) {}

@Override
public Class<DdlInTransactionMode> getParameterClass() {
return DdlInTransactionMode.class;
}

@Override
public DdlInTransactionMode convert(String value) {
return values.get(value);
}
}

static class ExplainCommandConverter implements ClientSideStatementValueConverter<String> {
@Override
public Class<String> getParameterClass() {
Expand Down Expand Up @@ -588,4 +671,71 @@ public String convert(String filePath) {
return filePath;
}
}

static class CredentialsProviderConverter
implements ClientSideStatementValueConverter<CredentialsProvider> {
static final CredentialsProviderConverter INSTANCE = new CredentialsProviderConverter();

private CredentialsProviderConverter() {}

@Override
public Class<CredentialsProvider> getParameterClass() {
return CredentialsProvider.class;
}

@Override
public CredentialsProvider convert(String credentialsProviderName) {
if (!Strings.isNullOrEmpty(credentialsProviderName)) {
try {
Class<? extends CredentialsProvider> clazz =
(Class<? extends CredentialsProvider>) Class.forName(credentialsProviderName);
Constructor<? extends CredentialsProvider> constructor = clazz.getDeclaredConstructor();
return constructor.newInstance();
} catch (ClassNotFoundException classNotFoundException) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Unknown or invalid CredentialsProvider class name: " + credentialsProviderName,
classNotFoundException);
} catch (NoSuchMethodException noSuchMethodException) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Credentials provider "
+ credentialsProviderName
+ " does not have a public no-arg constructor.",
noSuchMethodException);
} catch (InvocationTargetException
| InstantiationException
| IllegalAccessException exception) {
throw SpannerExceptionFactory.newSpannerException(
ErrorCode.INVALID_ARGUMENT,
"Failed to create an instance of "
+ credentialsProviderName
+ ": "
+ exception.getMessage(),
exception);
}
}
return null;
}
}

/** Converter for converting strings to {@link Dialect} values. */
static class DialectConverter implements ClientSideStatementValueConverter<Dialect> {
static final DialectConverter INSTANCE = new DialectConverter();

private final CaseInsensitiveEnumMap<Dialect> values =
new CaseInsensitiveEnumMap<>(Dialect.class);

private DialectConverter() {}

@Override
public Class<Dialect> getParameterClass() {
return Dialect.class;
}

@Override
public Dialect convert(String value) {
return values.get(value);
}
}
}
Loading

0 comments on commit 1e40bcc

Please sign in to comment.