use of com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest 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.CheckAndSetRequest in project atlasdb by palantir.
the class SchemaMetadataServiceImpl method putSchemaMetadata.
@Override
public void putSchemaMetadata(String schemaName, SchemaMetadata schemaMetadata) {
byte[] serializedMetadata = schemaMetadata.persistToBytes();
while (!Thread.currentThread().isInterrupted()) {
Optional<byte[]> existingMetadata = loadMetadataCellFromKeyValueService(schemaName);
CheckAndSetRequest request = existingMetadata.map(existingData -> CheckAndSetRequest.singleCell(AtlasDbConstants.DEFAULT_SCHEMA_METADATA_TABLE, createCellForGivenSchemaName(schemaName), existingData, serializedMetadata)).orElse(CheckAndSetRequest.newCell(AtlasDbConstants.DEFAULT_SCHEMA_METADATA_TABLE, createCellForGivenSchemaName(schemaName), serializedMetadata));
try {
keyValueService.checkAndSet(request);
log.info("Successfully updated the schema metadata to {}.", UnsafeArg.of("committedUpdate", request));
return;
} catch (CheckAndSetException e) {
log.info("Failed to update the schema metadata: {}. Retrying.", UnsafeArg.of("attemptedUpdate", request));
}
}
}
use of com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest in project atlasdb by palantir.
the class TableRemappingKeyValueService method checkAndSet.
@Override
public void checkAndSet(CheckAndSetRequest checkAndSetRequest) {
try {
CheckAndSetRequest request = ImmutableCheckAndSetRequest.builder().from(checkAndSetRequest).table(tableMapper.getMappedTableName(checkAndSetRequest.table())).build();
delegate().checkAndSet(request);
} catch (TableMappingNotFoundException e) {
throw new IllegalArgumentException(e);
}
}
use of com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest in project atlasdb by palantir.
the class AbstractKeyValueServiceTest method testCheckAndSetFromWrongValue.
@Test(expected = CheckAndSetException.class)
public void testCheckAndSetFromWrongValue() {
Assume.assumeTrue(checkAndSetSupported());
CheckAndSetRequest request = CheckAndSetRequest.newCell(TEST_TABLE, TEST_CELL, value00);
keyValueService.checkAndSet(request);
try {
CheckAndSetRequest secondRequest = CheckAndSetRequest.singleCell(TEST_TABLE, TEST_CELL, value01, value00);
keyValueService.checkAndSet(secondRequest);
} catch (CheckAndSetException ex) {
assertThat(ex.getActualValues(), contains(value00));
throw ex;
}
}
use of com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest in project atlasdb by palantir.
the class AbstractKeyValueServiceTest method testCheckAndSetFromValueWhenNoValue.
@Test(expected = CheckAndSetException.class)
public void testCheckAndSetFromValueWhenNoValue() {
Assume.assumeTrue(checkAndSetSupported());
CheckAndSetRequest request = CheckAndSetRequest.singleCell(TEST_TABLE, TEST_CELL, value00, value01);
keyValueService.checkAndSet(request);
}
Aggregations