Search in sources :

Example 6 with KeyAlreadyExistsException

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);
    }
}
Also used : CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) Cell(com.palantir.atlasdb.keyvalue.api.Cell) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) FunctionCheckedException(com.palantir.common.base.FunctionCheckedException) TException(org.apache.thrift.TException) UnavailableException(org.apache.cassandra.thrift.UnavailableException) PalantirRuntimeException(com.palantir.common.exception.PalantirRuntimeException) CASResult(org.apache.cassandra.thrift.CASResult)

Example 7 with KeyAlreadyExistsException

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;
    }
}
Also used : PalantirSqlException(com.palantir.exception.PalantirSqlException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 8 with KeyAlreadyExistsException

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);
    }
}
Also used : CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) Cell(com.palantir.atlasdb.keyvalue.api.Cell) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 9 with KeyAlreadyExistsException

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);
}
Also used : Entry(java.util.Map.Entry) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 10 with KeyAlreadyExistsException

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);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Test(org.junit.Test)

Aggregations

KeyAlreadyExistsException (com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)11 Cell (com.palantir.atlasdb.keyvalue.api.Cell)5 CheckAndSetException (com.palantir.atlasdb.keyvalue.api.CheckAndSetException)2 PalantirSqlException (com.palantir.exception.PalantirSqlException)2 Entry (java.util.Map.Entry)2 ResultSet (com.datastax.driver.core.ResultSet)1 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)1 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)1 CheckAndSetRequest (com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest)1 InsufficientConsistencyException (com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException)1 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)1 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)1 Value (com.palantir.atlasdb.keyvalue.api.Value)1 MultiTimestampPutBatch (com.palantir.atlasdb.keyvalue.jdbc.impl.MultiTimestampPutBatch)1 PutBatch (com.palantir.atlasdb.keyvalue.jdbc.impl.PutBatch)1 SingleTimestampPutBatch (com.palantir.atlasdb.keyvalue.jdbc.impl.SingleTimestampPutBatch)1 TransactionFailedRetriableException (com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException)1 FunctionCheckedException (com.palantir.common.base.FunctionCheckedException)1 PalantirRuntimeException (com.palantir.common.exception.PalantirRuntimeException)1 ServiceNotAvailableException (com.palantir.common.remoting.ServiceNotAvailableException)1