use of com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException in project atlasdb by palantir.
the class CassandraKeyValueServiceImpl method putUnlessExists.
/**
* Puts values into the key-value store. This call <i>does not</i> guarantee
* atomicity across cells. On failure, it is possible that some of the requests will
* have succeeded (without having been rolled back). Similarly, concurrent batched requests may
* interleave. However, concurrent writes to the same Cell will not both report success.
* One of them will throw {@link KeyAlreadyExistsException}.
* <p>
* Does not require all Cassandra nodes to be up and available, works as long as quorum is achieved.
*
* @param tableRef the name of the table to put values into.
* @param values map containing the key-value entries to put.
*
* @throws KeyAlreadyExistsException If you are putting a Cell with the same timestamp as one that already exists.
*/
@Override
public void putUnlessExists(final TableReference tableRef, final Map<Cell, byte[]> values) throws KeyAlreadyExistsException {
try {
clientPool.runWithRetry(client -> {
for (Entry<Cell, byte[]> e : values.entrySet()) {
CheckAndSetRequest request = CheckAndSetRequest.newCell(tableRef, e.getKey(), e.getValue());
CASResult casResult = executeCheckAndSet(client, request);
if (!casResult.isSuccess()) {
throw new KeyAlreadyExistsException(String.format("The row in table %s already exists.", tableRef.getQualifiedName()), ImmutableList.of(e.getKey()));
}
}
clientPool.markWritesForTable(values, tableRef);
return null;
});
} catch (Exception e) {
throw QosAwareThrowables.unwrapAndThrowRateLimitExceededOrAtlasDbDependencyException(e);
}
}
use of com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException in project atlasdb by palantir.
the class AbstractDbWriteTable method put.
private void put(List<Object[]> args) {
try {
String prefixedTableName = prefixedTableNames.get(tableRef, conns);
conns.get().insertManyUnregisteredQuery("/* INSERT_ONE (" + prefixedTableName + ") */" + " INSERT INTO " + prefixedTableName + " (row_name, col_name, ts, val) " + " VALUES (?, ?, ?, ?) ", args);
} catch (PalantirSqlException e) {
if (ExceptionCheck.isUniqueConstraintViolation(e)) {
throw new KeyAlreadyExistsException("primary key violation", e);
}
throw e;
}
}
use of com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException in project atlasdb by palantir.
the class DbKvs method executePutUnlessExists.
private void executePutUnlessExists(CheckAndSetRequest checkAndSetRequest) {
try {
Map<Cell, byte[]> value = ImmutableMap.of(checkAndSetRequest.cell(), checkAndSetRequest.newValue());
putUnlessExists(checkAndSetRequest.table(), value);
} catch (KeyAlreadyExistsException e) {
throw new CheckAndSetException("Value unexpectedly present when running check and set", e);
}
}
use of com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException in project atlasdb by palantir.
the class DbKvs method putIfNotUpdate.
private void putIfNotUpdate(DbReadTable readTable, DbWriteTable writeTable, TableReference tableRef, List<Entry<Cell, Value>> batch, KeyAlreadyExistsException ex) {
Map<Cell, Long> timestampByCell = Maps.newHashMap();
for (Entry<Cell, Value> entry : batch) {
timestampByCell.put(entry.getKey(), entry.getValue().getTimestamp() + 1);
}
Map<Cell, Value> results = extractResults(readTable, tableRef, readTable.getLatestCells(timestampByCell, true));
ListIterator<Entry<Cell, Value>> iter = batch.listIterator();
while (iter.hasNext()) {
Entry<Cell, Value> entry = iter.next();
Cell key = entry.getKey();
Value value = entry.getValue();
if (results.containsKey(key)) {
if (results.get(key).equals(value)) {
iter.remove();
} else {
throw new KeyAlreadyExistsException("primary key violation for key " + key + " with value " + value, ex);
}
}
}
writeTable.put(batch);
}
use of com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException in project atlasdb by palantir.
the class CommitTsLoaderTest method loadShouldContinueIfKeyAlreadyExistsIsThrown.
@Test
public void loadShouldContinueIfKeyAlreadyExistsIsThrown() throws Exception {
AtomicLong answerCount = new AtomicLong();
doAnswer((invocation) -> answerCount.get() > 0 ? VALID_COMMIT_TIMESTAMP : NO_TIMESTAMP).when(mockTransactionService).get(VALID_START_TIMESTAMP);
doAnswer((invocation) -> {
answerCount.set(1);
throw new KeyAlreadyExistsException("Already exists");
}).when(mockTransactionService).putUnlessExists(VALID_START_TIMESTAMP, ROLLBACK_TIMESTAMP);
assertThat(loader.load(VALID_START_TIMESTAMP)).isEqualTo(VALID_COMMIT_TIMESTAMP);
verify(mockTransactionService).putUnlessExists(VALID_START_TIMESTAMP, ROLLBACK_TIMESTAMP);
}
Aggregations