Search in sources :

Example 6 with CheckAndSetRequest

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);
    }
}
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 CheckAndSetRequest

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));
        }
    }
}
Also used : Iterables(com.google.common.collect.Iterables) Logger(org.slf4j.Logger) Value(com.palantir.atlasdb.keyvalue.api.Value) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) LoggerFactory(org.slf4j.LoggerFactory) CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) AutoDelegate(com.palantir.processors.AutoDelegate) Collectors(java.util.stream.Collectors) AsyncInitializer(com.palantir.async.initializer.AsyncInitializer) PtBytes(com.palantir.atlasdb.encoding.PtBytes) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) UnsafeArg(com.palantir.logsafe.UnsafeArg) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) Map(java.util.Map) Optional(java.util.Optional) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) SchemaMetadata(com.palantir.atlasdb.schema.SchemaMetadata) CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException)

Example 8 with CheckAndSetRequest

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

Example 9 with CheckAndSetRequest

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;
    }
}
Also used : CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) Test(org.junit.Test)

Example 10 with CheckAndSetRequest

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);
}
Also used : CheckAndSetRequest(com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest) Test(org.junit.Test)

Aggregations

CheckAndSetRequest (com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest)12 Test (org.junit.Test)7 CheckAndSetException (com.palantir.atlasdb.keyvalue.api.CheckAndSetException)3 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 Iterables (com.google.common.collect.Iterables)1 AsyncInitializer (com.palantir.async.initializer.AsyncInitializer)1 AtlasDbConstants (com.palantir.atlasdb.AtlasDbConstants)1 PtBytes (com.palantir.atlasdb.encoding.PtBytes)1 ImmutableCheckAndSetRequest (com.palantir.atlasdb.keyvalue.api.ImmutableCheckAndSetRequest)1 InsufficientConsistencyException (com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException)1 KeyAlreadyExistsException (com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)1 KeyValueService (com.palantir.atlasdb.keyvalue.api.KeyValueService)1 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)1 Value (com.palantir.atlasdb.keyvalue.api.Value)1 SchemaMetadata (com.palantir.atlasdb.schema.SchemaMetadata)1 FunctionCheckedException (com.palantir.common.base.FunctionCheckedException)1 PalantirRuntimeException (com.palantir.common.exception.PalantirRuntimeException)1