|
| 1 | +package net.javacrumbs.shedlock.provider.cassandra; |
| 2 | + |
| 3 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 4 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 5 | +import com.datastax.oss.driver.api.core.cql.Row; |
| 6 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 7 | +import com.datastax.oss.driver.api.querybuilder.QueryBuilder; |
| 8 | +import net.javacrumbs.shedlock.core.LockConfiguration; |
| 9 | +import net.javacrumbs.shedlock.support.AbstractStorageAccessor; |
| 10 | +import net.javacrumbs.shedlock.support.Utils; |
| 11 | +import org.jetbrains.annotations.NotNull; |
| 12 | + |
| 13 | +import java.net.InetSocketAddress; |
| 14 | +import java.time.Instant; |
| 15 | +import java.util.Optional; |
| 16 | + |
| 17 | +import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.literal; |
| 18 | + |
| 19 | +public class CassandraStorageAccessor extends AbstractStorageAccessor { |
| 20 | + |
| 21 | + private static final String LOCK_NAME = "name"; |
| 22 | + private static final String LOCK_UNTIL = "lockUntil"; |
| 23 | + private static final String LOCKED_AT = "lockedAt"; |
| 24 | + private static final String LOCKED_BY = "lockedBy"; |
| 25 | + |
| 26 | + private final String hostname; |
| 27 | + private final String table; |
| 28 | + private final CqlSession cqlSession; |
| 29 | + |
| 30 | + public CassandraStorageAccessor(@NotNull String contactPoint, @NotNull int port, @NotNull String datacenter, @NotNull String keyspace, @NotNull String table) { |
| 31 | + this.hostname = Utils.getHostname(); |
| 32 | + this.table = table; |
| 33 | + cqlSession = CqlSession.builder() |
| 34 | + .addContactPoint(new InetSocketAddress(contactPoint, port)) |
| 35 | + .withLocalDatacenter(datacenter) |
| 36 | + .withKeyspace(keyspace) |
| 37 | + .build(); |
| 38 | + } |
| 39 | + |
| 40 | + public CassandraStorageAccessor(@NotNull CqlSession cqlSession, @NotNull String table) { |
| 41 | + this.hostname = Utils.getHostname(); |
| 42 | + this.table = table; |
| 43 | + this.cqlSession = cqlSession; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public boolean insertRecord(@NotNull LockConfiguration lockConfiguration) { |
| 48 | + if (find(lockConfiguration.getName()).isPresent()) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + return insert(lockConfiguration.getName(), lockConfiguration.getLockAtMostUntil()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public boolean updateRecord(@NotNull LockConfiguration lockConfiguration) { |
| 57 | + Optional<Lock> lock = find(lockConfiguration.getName()); |
| 58 | + if (!lock.isPresent() || lock.get().getLockUntil().isAfter(Instant.now())) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + return update(lockConfiguration.getName(), lockConfiguration.getLockAtMostUntil()); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void unlock(@NotNull LockConfiguration lockConfiguration) { |
| 67 | + updateUntil(lockConfiguration.getName(), lockConfiguration.getUnlockTime()); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public boolean extend(@NotNull LockConfiguration lockConfiguration) { |
| 72 | + Optional<Lock> lock = find(lockConfiguration.getName()); |
| 73 | + if (!lock.isPresent() || lock.get().getLockUntil().isBefore(Instant.now()) || !lock.get().getLockedBy().equals(hostname)) { |
| 74 | + logger.trace("extend false"); |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + return updateUntil(lockConfiguration.getName(), lockConfiguration.getLockAtMostUntil()); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Find existing row by primary key lock.name |
| 83 | + * |
| 84 | + * @param name lock name |
| 85 | + * @return optional lock row or empty |
| 86 | + */ |
| 87 | + Optional<Lock> find(String name) { |
| 88 | + SimpleStatement selectStatement = QueryBuilder.selectFrom(table) |
| 89 | + .column(LOCK_NAME) |
| 90 | + .column(LOCK_UNTIL) |
| 91 | + .column(LOCKED_AT) |
| 92 | + .column(LOCKED_BY) |
| 93 | + .whereColumn(LOCK_NAME).isEqualTo(literal(name)) |
| 94 | + .build(); |
| 95 | + |
| 96 | + ResultSet resultSet = cqlSession.execute(selectStatement); |
| 97 | + Row row = resultSet.one(); |
| 98 | + return row != null ? |
| 99 | + Optional.of(new Lock(row.getString(LOCK_NAME), row.getInstant(LOCK_UNTIL), row.getInstant(LOCKED_AT), row.getString(LOCKED_BY))) : |
| 100 | + Optional.empty(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Insert new lock row |
| 105 | + * |
| 106 | + * @param name lock name |
| 107 | + * @param until new until instant value |
| 108 | + */ |
| 109 | + boolean insert(String name, Instant until) { |
| 110 | + SimpleStatement insertStatement = QueryBuilder.insertInto(table) |
| 111 | + .value(LOCK_NAME, literal(name)) |
| 112 | + .value(LOCK_UNTIL, literal(until)) |
| 113 | + .value(LOCKED_AT, literal(Instant.now())) |
| 114 | + .value(LOCKED_BY, literal(hostname)) |
| 115 | + .ifNotExists() |
| 116 | + .build(); |
| 117 | + |
| 118 | + ResultSet resultSet = cqlSession.execute(insertStatement); |
| 119 | + if (resultSet == null) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + return resultSet.wasApplied(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Update existing lock row |
| 127 | + * |
| 128 | + * @param name lock name |
| 129 | + * @param until new until instant value |
| 130 | + */ |
| 131 | + boolean update(String name, Instant until) { |
| 132 | + SimpleStatement updateStatement = QueryBuilder.update(table) |
| 133 | + .setColumn(LOCK_UNTIL, literal(until)) |
| 134 | + .setColumn(LOCKED_AT, literal(Instant.now())) |
| 135 | + .setColumn(LOCKED_BY, literal(hostname)) |
| 136 | + .whereColumn(LOCK_NAME).isEqualTo(literal(name)) |
| 137 | + .ifColumn(LOCK_UNTIL).isLessThan(literal(Instant.now())) |
| 138 | + .build(); |
| 139 | + |
| 140 | + ResultSet resultSet = cqlSession.execute(updateStatement); |
| 141 | + if (resultSet == null) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + return resultSet.wasApplied(); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Updates lock.until field where lockConfiguration.name |
| 149 | + * |
| 150 | + * @param name lock name |
| 151 | + * @param until new until instant value |
| 152 | + */ |
| 153 | + boolean updateUntil(String name, Instant until) { |
| 154 | + SimpleStatement updateStatement = QueryBuilder.update(table) |
| 155 | + .setColumn(LOCK_UNTIL, literal(until)) |
| 156 | + .whereColumn(LOCK_NAME).isEqualTo(literal(name)) |
| 157 | + .ifColumn(LOCK_UNTIL).isGreaterThanOrEqualTo(literal(Instant.now())) |
| 158 | + .ifColumn(LOCKED_BY).isEqualTo(literal(hostname)) |
| 159 | + .build(); |
| 160 | + |
| 161 | + ResultSet resultSet = cqlSession.execute(updateStatement); |
| 162 | + if (resultSet == null) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + return resultSet.wasApplied(); |
| 166 | + } |
| 167 | +} |
0 commit comments