Skip to content

Commit

Permalink
Formatting with Spotless
Browse files Browse the repository at this point in the history
  • Loading branch information
pXius committed Nov 6, 2023
1 parent 1802983 commit 25d9d03
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 105 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package net.javacrumbs.shedlock.provider.spanner;

import static java.util.Objects.requireNonNull;

import com.google.cloud.spanner.DatabaseClient;
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
import net.javacrumbs.shedlock.support.Utils;
import net.javacrumbs.shedlock.support.annotation.NonNull;

import static java.util.Objects.requireNonNull;

/**
* A lock provider for Google Cloud Spanner.
* This provider uses Spanner as the backend storage for the locks.
Expand All @@ -19,10 +19,8 @@ public class SpannerLockProvider extends StorageBasedLockProvider {
* @param databaseClient the client for interacting with Google Cloud Spanner.
*/
public SpannerLockProvider(@NonNull DatabaseClient databaseClient) {
this(new SpannerStorageAccessor(Configuration.builder()
.withDatabaseClient(databaseClient)
.build()
));
this(new SpannerStorageAccessor(
Configuration.builder().withDatabaseClient(databaseClient).build()));
}

/**
Expand Down Expand Up @@ -100,15 +98,14 @@ public static final class Builder {

// Default table configuration if not specified by the user of the builder.
private TableConfiguration tableConfiguration = TableConfiguration.builder()
.withTableName("shedlock")
.withLockName("name")
.withLockedBy("locked_by")
.withLockedAt("locked_at")
.withLockUntil("lock_until")
.build();

private Builder() {
}
.withTableName("shedlock")
.withLockName("name")
.withLockedBy("locked_by")
.withLockedAt("locked_at")
.withLockUntil("lock_until")
.build();

private Builder() {}

public Builder withDatabaseClient(DatabaseClient databaseClient) {
this.databaseClient = databaseClient;
Expand Down Expand Up @@ -186,7 +183,6 @@ public String getLockedBy() {
return lockedBy;
}


/**
* Builder for creating {@code TableConfiguration} instances.
*/
Expand All @@ -197,8 +193,7 @@ public static final class Builder {
private String lockedAt;
private String lockedBy;

private Builder() {
}
private Builder() {}

public Builder withTableName(String tableName) {
this.tableName = tableName;
Expand Down Expand Up @@ -233,7 +228,6 @@ public Builder withLockedBy(String lockedByColumn) {
public TableConfiguration build() {
return new TableConfiguration(this);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@
import com.google.cloud.spanner.Statement;
import com.google.cloud.spanner.Struct;
import com.google.cloud.spanner.TransactionContext;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.support.AbstractStorageAccessor;
import net.javacrumbs.shedlock.support.annotation.NonNull;

import java.time.Instant;
import java.util.List;
import java.util.Optional;
import net.javacrumbs.shedlock.core.LockConfiguration;
import net.javacrumbs.shedlock.support.AbstractStorageAccessor;
import net.javacrumbs.shedlock.support.annotation.NonNull;

/**
* Accessor for managing lock records within a Google Spanner database.
Expand Down Expand Up @@ -54,22 +53,24 @@ public SpannerStorageAccessor(SpannerLockProvider.Configuration configuration) {
*/
@Override
public boolean insertRecord(@NonNull LockConfiguration lockConfiguration) {
return Boolean.TRUE.equals(databaseClient.readWriteTransaction().run(transaction ->
findLock(transaction, lockConfiguration.getName())
return Boolean.TRUE.equals(databaseClient.readWriteTransaction().run(transaction -> findLock(
transaction, lockConfiguration.getName())
.map(lock -> false) // Lock already exists, so we return false.
.orElseGet(() -> {
transaction.buffer(Mutation.newInsertBuilder(table)
.set(name).to(lockConfiguration.getName())
.set(lockUntil).to(toTimestamp(lockConfiguration.getLockAtMostUntil()))
.set(lockedAt).to(Timestamp.now())
.set(lockedBy).to(hostname)
.build());
.set(name)
.to(lockConfiguration.getName())
.set(lockUntil)
.to(toTimestamp(lockConfiguration.getLockAtMostUntil()))
.set(lockedAt)
.to(Timestamp.now())
.set(lockedBy)
.to(hostname)
.build());
return true;
})
));
})));
}


/**
* Attempts to update an existing lock record in the Spanner table.
*
Expand All @@ -78,19 +79,23 @@ public boolean insertRecord(@NonNull LockConfiguration lockConfiguration) {
*/
@Override
public boolean updateRecord(@NonNull LockConfiguration lockConfiguration) {
return Boolean.TRUE.equals(databaseClient.readWriteTransaction().run(transaction ->
findLock(transaction, lockConfiguration.getName())
return Boolean.TRUE.equals(databaseClient.readWriteTransaction().run(transaction -> findLock(
transaction, lockConfiguration.getName())
.filter(lock -> lock.getLockedUntil().compareTo(Timestamp.now()) <= 0)
.map(lock -> {
transaction.buffer(Mutation.newUpdateBuilder(table)
.set(name).to(lockConfiguration.getName())
.set(lockUntil).to(toTimestamp(lockConfiguration.getLockAtMostUntil()))
.set(lockedAt).to(Timestamp.now())
.set(lockedBy).to(hostname)
.build());
.set(name)
.to(lockConfiguration.getName())
.set(lockUntil)
.to(toTimestamp(lockConfiguration.getLockAtMostUntil()))
.set(lockedAt)
.to(Timestamp.now())
.set(lockedBy)
.to(hostname)
.build());
return true;
}).orElse(false)
));
})
.orElse(false)));
}

/**
Expand All @@ -101,18 +106,20 @@ public boolean updateRecord(@NonNull LockConfiguration lockConfiguration) {
*/
@Override
public boolean extend(@NonNull LockConfiguration lockConfiguration) {
return Boolean.TRUE.equals(databaseClient.readWriteTransaction().run(transaction ->
findLock(transaction, lockConfiguration.getName())
return Boolean.TRUE.equals(databaseClient.readWriteTransaction().run(transaction -> findLock(
transaction, lockConfiguration.getName())
.filter(lock -> hostname.equals(lock.getLockedBy()))
.filter(lock -> lock.getLockedUntil().compareTo(Timestamp.now()) > 0)
.map(lock -> {
transaction.buffer(
Mutation.newUpdateBuilder(table)
.set(name).to(lockConfiguration.getName())
.set(lockUntil).to(toTimestamp(lockConfiguration.getLockAtMostUntil()))
transaction.buffer(Mutation.newUpdateBuilder(table)
.set(name)
.to(lockConfiguration.getName())
.set(lockUntil)
.to(toTimestamp(lockConfiguration.getLockAtMostUntil()))
.build());
return true;
}).orElse(false)));
})
.orElse(false)));
}

/**
Expand All @@ -124,12 +131,12 @@ public boolean extend(@NonNull LockConfiguration lockConfiguration) {
public void unlock(@NonNull LockConfiguration lockConfiguration) {
databaseClient.readWriteTransaction().run(transaction -> {
findLock(transaction, lockConfiguration.getName())
.filter(lock -> hostname.equals(lock.getLockedBy()))
.ifPresent(lock ->
transaction.buffer(
Mutation.newUpdateBuilder(table)
.set(name).to(lockConfiguration.getName())
.set(lockUntil).to(toTimestamp(lockConfiguration.getUnlockTime()))
.filter(lock -> hostname.equals(lock.getLockedBy()))
.ifPresent(lock -> transaction.buffer(Mutation.newUpdateBuilder(table)
.set(name)
.to(lockConfiguration.getName())
.set(lockUntil)
.to(toTimestamp(lockConfiguration.getUnlockTime()))
.build()));
return null; // need a return to commit the transaction
});
Expand All @@ -143,20 +150,21 @@ public void unlock(@NonNull LockConfiguration lockConfiguration) {
* @return An {@code Optional<Lock>} containing the lock if found, otherwise empty.
*/
protected Optional<Lock> findLock(TransactionContext transaction, String lockName) {
return Optional.ofNullable(transaction.readRow(
table,
Key.of(lockName),
List.of(name, lockUntil, lockedBy, lockedAt)))
.map(Lock::new);
return Optional.ofNullable(
transaction.readRow(table, Key.of(lockName), List.of(name, lockUntil, lockedBy, lockedAt)))
.map(Lock::new);
}

protected Optional<Lock> nonTransactionFindLock(String lockName) {
return Optional.ofNullable(databaseClient.singleUse().executeQuery(Statement.newBuilder("SELECT * FROM " + table + " WHERE " + name + " = @name")
.bind(name).to(lockName)
.build()))
.filter(ResultSet::next)
.map(ResultSet::getCurrentRowAsStruct)
.map(Lock::new);
return Optional.ofNullable(databaseClient
.singleUse()
.executeQuery(Statement.newBuilder("SELECT * FROM " + table + " WHERE " + name + " = @name")
.bind(name)
.to(lockName)
.build()))
.filter(ResultSet::next)
.map(ResultSet::getCurrentRowAsStruct)
.map(Lock::new);
}

/**
Expand Down Expand Up @@ -206,5 +214,4 @@ protected Timestamp getLockedUntil() {
return lockedUntil;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@
import com.google.cloud.spanner.InstanceInfo;
import com.google.cloud.spanner.Spanner;
import com.google.cloud.spanner.SpannerOptions;
import java.util.List;
import java.util.concurrent.ExecutionException;
import net.javacrumbs.shedlock.test.support.AbstractStorageBasedLockProviderIntegrationTest;
import org.junit.jupiter.api.BeforeAll;
import org.testcontainers.containers.SpannerEmulatorContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Testcontainers
public abstract class AbstractSpannerStorageBasedLockProviderIntegrationTest extends AbstractStorageBasedLockProviderIntegrationTest {
public abstract class AbstractSpannerStorageBasedLockProviderIntegrationTest
extends AbstractStorageBasedLockProviderIntegrationTest {

private static final String SPANNER_EMULATOR_IMAGE = "gcr.io/cloud-spanner-emulator/emulator:latest";
private static final String PROJECT_NAME = "test-project";
Expand All @@ -34,8 +34,7 @@ public abstract class AbstractSpannerStorageBasedLockProviderIntegrationTest ext

@Container
public static final SpannerEmulatorContainer emulator =
new SpannerEmulatorContainer(DockerImageName.parse(SPANNER_EMULATOR_IMAGE));

new SpannerEmulatorContainer(DockerImageName.parse(SPANNER_EMULATOR_IMAGE));

@BeforeAll
public static void setUpSpanner() {
Expand All @@ -50,12 +49,11 @@ static DatabaseClient getDatabaseClient() {
}

private static Spanner createSpannerService() {
SpannerOptions options = SpannerOptions
.newBuilder()
.setEmulatorHost(emulator.getEmulatorGrpcEndpoint())
.setCredentials(NoCredentials.getInstance())
.setProjectId(PROJECT_NAME)
.build();
SpannerOptions options = SpannerOptions.newBuilder()
.setEmulatorHost(emulator.getEmulatorGrpcEndpoint())
.setCredentials(NoCredentials.getInstance())
.setProjectId(PROJECT_NAME)
.build();

return options.getService();
}
Expand All @@ -66,15 +64,12 @@ private static InstanceId createInstance(Spanner spanner) {
InstanceAdminClient insAdminClient = spanner.getInstanceAdminClient();
try {
Instance instance = insAdminClient
.createInstance(
InstanceInfo
.newBuilder(instanceId)
.setNodeCount(1)
.setDisplayName("Test instance")
.setInstanceConfigId(instanceConfig)
.build()
)
.get();
.createInstance(InstanceInfo.newBuilder(instanceId)
.setNodeCount(1)
.setDisplayName("Test instance")
.setInstanceConfigId(instanceConfig)
.build())
.get();
} catch (ExecutionException | InterruptedException e) {
throw new RuntimeException("Failed creating Spanner instance.", e);
}
Expand All @@ -85,21 +80,16 @@ private static DatabaseId createDatabase(Spanner spanner) {
DatabaseAdminClient dbAdminClient = spanner.getDatabaseAdminClient();
try {
Database database = dbAdminClient
.createDatabase(
INSTANCE_NAME,
DATABASE_NAME,
List.of(getShedlockDdl())
)
.get();
.createDatabase(INSTANCE_NAME, DATABASE_NAME, List.of(getShedlockDdl()))
.get();
return database.getId();
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException("Failed creating Spanner database.", e);
}
}

private static String getShedlockDdl() {
return
"""
return """
CREATE TABLE shedlock (
name STRING(64) NOT NULL,
lock_until TIMESTAMP NOT NULL,
Expand All @@ -108,5 +98,4 @@ locked_by STRING(255) NOT NULL
) PRIMARY KEY (name)
""";
}

}
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package net.javacrumbs.shedlock.provider.spanner;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.cloud.Timestamp;
import com.google.cloud.spanner.KeySet;
import com.google.cloud.spanner.Mutation;
import java.time.Instant;
import java.util.List;
import net.javacrumbs.shedlock.core.ClockProvider;
import net.javacrumbs.shedlock.support.StorageBasedLockProvider;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;

import java.time.Instant;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

class SpannerLockProviderIntegrationTest extends AbstractSpannerStorageBasedLockProviderIntegrationTest {

private static SpannerStorageAccessor accessor;

@BeforeAll
static void setUp() {
SpannerLockProvider.Configuration configuration = SpannerLockProvider.Configuration.builder()
.withDatabaseClient(getDatabaseClient())
.build();
.withDatabaseClient(getDatabaseClient())
.build();
accessor = new SpannerStorageAccessor(configuration);
}

Expand Down Expand Up @@ -65,5 +64,4 @@ private void cleanLockTable() {
private Instant toInstant(Timestamp timestamp) {
return Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
}

}

0 comments on commit 25d9d03

Please sign in to comment.