Search in sources :

Example 1 with CheckAndSetException

use of com.palantir.atlasdb.keyvalue.api.CheckAndSetException in project atlasdb by palantir.

the class UpdateExecutor method update.

public void update(Cell cell, long ts, byte[] oldValue, byte[] newValue) {
    Object[] args = new Object[] { cell.getRowName(), cell.getColumnName(), ts, newValue, cell.getRowName(), cell.getColumnName(), ts, oldValue };
    String prefixedTableName = prefixedTableNames.get(tableRef, conns);
    String sqlString = "/* UPDATE (" + prefixedTableName + ") */" + " UPDATE " + prefixedTableName + "" + " SET row_name = ?, col_name = ?, ts = ?, val = ?" + " WHERE row_name = ?" + " AND col_name = ?" + " AND ts = ?" + " AND val = ?";
    PalantirSqlConnection connection = (PalantirSqlConnection) conns.get();
    while (true) {
        int updated = connection.updateCountRowsUnregisteredQuery(sqlString, args);
        if (updated != 0) {
            return;
        }
        List<byte[]> currentValue = getCurrentValue(connection, cell, ts, prefixedTableName);
        byte[] onlyValue = Iterables.getOnlyElement(currentValue, null);
        if (!Arrays.equals(onlyValue, oldValue)) {
            throw new CheckAndSetException(cell, tableRef, oldValue, currentValue);
        }
    }
}
Also used : PalantirSqlConnection(com.palantir.nexus.db.sql.PalantirSqlConnection) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException)

Example 2 with CheckAndSetException

use of com.palantir.atlasdb.keyvalue.api.CheckAndSetException in project atlasdb by palantir.

the class PersistentLockManagerTest method doesNotDeadlockOnShutdownIfLockCannotBeAcquired.

@Test(timeout = 10_000)
// for acquirePersistentLockWithRetry
@SuppressWarnings("FutureReturnValueIgnored")
public void doesNotDeadlockOnShutdownIfLockCannotBeAcquired() throws InterruptedException {
    CountDownLatch acquireStarted = new CountDownLatch(1);
    when(mockPls.acquireBackupLock(any())).then(inv -> {
        acquireStarted.countDown();
        throw new CheckAndSetException("foo");
    });
    executor.submit(manager::acquirePersistentLockWithRetry);
    acquireStarted.await();
    manager.shutdown();
}
Also used : CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with CheckAndSetException

use of com.palantir.atlasdb.keyvalue.api.CheckAndSetException 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 4 with CheckAndSetException

use of com.palantir.atlasdb.keyvalue.api.CheckAndSetException 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 5 with CheckAndSetException

use of com.palantir.atlasdb.keyvalue.api.CheckAndSetException in project atlasdb by palantir.

the class LockStoreTest method noErrorIfLockOpenedWhileCreatingTable.

@Test
public void noErrorIfLockOpenedWhileCreatingTable() {
    doThrow(new CheckAndSetException("foo", null, null, ImmutableList.of())).when(kvs).checkAndSet(anyObject());
    // should not throw
    new LockStoreImpl.LockStorePopulator(kvs).populate();
}
Also used : CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) Test(org.junit.Test)

Aggregations

CheckAndSetException (com.palantir.atlasdb.keyvalue.api.CheckAndSetException)8 Test (org.junit.Test)4 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 CheckAndSetRequest (com.palantir.atlasdb.keyvalue.api.CheckAndSetRequest)2 ImmutableLockEntry (com.palantir.atlasdb.persistentlock.ImmutableLockEntry)2 LockEntry (com.palantir.atlasdb.persistentlock.LockEntry)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 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 UnsafeArg (com.palantir.logsafe.UnsafeArg)1 PalantirSqlConnection (com.palantir.nexus.db.sql.PalantirSqlConnection)1