Search in sources :

Example 1 with KeyAlreadyExistsException

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

the class Scrubber method getCommitTimestampRollBackIfNecessary.

private long getCommitTimestampRollBackIfNecessary(long startTimestamp, Multimap<TableReference, Cell> tableNameToCell) {
    Long commitTimestamp = transactionService.get(startTimestamp);
    if (commitTimestamp == null) {
        // can never cause correctness issues, only liveness issues)
        try {
            transactionService.putUnlessExists(startTimestamp, TransactionConstants.FAILED_COMMIT_TS);
        } catch (KeyAlreadyExistsException e) {
            String msg = "Could not roll back transaction with start timestamp " + startTimestamp + "; either" + " it was already rolled back (by a different transaction), or it committed successfully" + " before we could roll it back.";
            log.error("This isn't a bug but it should be very infrequent. {}", msg, new TransactionFailedRetriableException(msg, e));
        }
        commitTimestamp = transactionService.get(startTimestamp);
    }
    if (commitTimestamp == null) {
        throw new RuntimeException("expected commit timestamp to be non-null for startTs: " + startTimestamp);
    }
    if (commitTimestamp == TransactionConstants.FAILED_COMMIT_TS) {
        for (TableReference table : tableNameToCell.keySet()) {
            Map<Cell, Long> toDelete = Maps2.createConstantValueMap(tableNameToCell.get(table), startTimestamp);
            keyValueService.delete(table, Multimaps.forMap(toDelete));
        }
    }
    return commitTimestamp;
}
Also used : TransactionFailedRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) Cell(com.palantir.atlasdb.keyvalue.api.Cell) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 2 with KeyAlreadyExistsException

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

the class KvTableMappingService method addTable.

@Override
public TableReference addTable(TableReference tableRef) {
    if (tableRef.getNamespace().isEmptyNamespace()) {
        return tableRef;
    }
    if (tableMap.get().containsKey(tableRef)) {
        return tableMap.get().get(tableRef);
    }
    Cell key = Cell.create(getBytesForTableRef(tableRef), AtlasDbConstants.NAMESPACE_SHORT_COLUMN_BYTES);
    String shortName = AtlasDbConstants.NAMESPACE_PREFIX + Preconditions.checkNotNull(uniqueLongSupplier.get(), "uniqueLongSupplier returned null");
    byte[] value = PtBytes.toBytes(shortName);
    try {
        kv.putUnlessExists(AtlasDbConstants.NAMESPACE_TABLE, ImmutableMap.of(key, value));
    } catch (KeyAlreadyExistsException e) {
        return getAlreadyExistingMappedTableName(tableRef);
    }
    return TableReference.createWithEmptyNamespace(shortName);
}
Also used : Cell(com.palantir.atlasdb.keyvalue.api.Cell) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)

Example 3 with KeyAlreadyExistsException

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

the class OracleOverflowWriteTable method put.

private void put(List<Object[]> args, List<Object[]> overflowArgs) {
    if (!overflowArgs.isEmpty()) {
        if (config.overflowMigrationState() == OverflowMigrationState.UNSTARTED) {
            conns.get().insertManyUnregisteredQuery("/* INSERT_OVERFLOW */" + " INSERT INTO " + config.singleOverflowTable() + " (id, val) VALUES (?, ?) ", overflowArgs);
        } else {
            String shortOverflowTableName = getShortOverflowTableName();
            conns.get().insertManyUnregisteredQuery("/* INSERT_OVERFLOW (" + shortOverflowTableName + ") */" + " INSERT INTO " + shortOverflowTableName + " (id, val) VALUES (?, ?) ", overflowArgs);
        }
    }
    try {
        String shortTableName = oraclePrefixedTableNames.get(tableRef, conns);
        conns.get().insertManyUnregisteredQuery("/* INSERT_ONE (" + shortTableName + ") */" + " INSERT INTO " + shortTableName + " (row_name, col_name, ts, val, overflow) " + " 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 4 with KeyAlreadyExistsException

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

the class CqlKeyValueService method putInternal.

protected void putInternal(TableReference tableRef, Iterable<Map.Entry<Cell, Value>> values, TransactionType transactionType, int ttl, boolean recursive) throws Exception {
    List<ResultSetFuture> resultSetFutures = Lists.newArrayList();
    int mutationBatchCount = config.mutationBatchCount();
    long mutationBatchSizeBytes = limitBatchSizesToServerDefaults ? CqlKeyValueServices.UNCONFIGURED_DEFAULT_BATCH_SIZE_BYTES : config.mutationBatchSizeBytes();
    for (List<Entry<Cell, Value>> partition : IterablePartitioner.partitionByCountAndBytes(values, mutationBatchCount, mutationBatchSizeBytes, tableRef, CqlKeyValueServices.PUT_ENTRY_SIZING_FUNCTION)) {
        resultSetFutures.add(getPutPartitionResultSetFuture(tableRef, partition, transactionType));
    }
    final String putQuery = getPutQueryForPossibleTransaction(tableRef, transactionType);
    for (ResultSetFuture resultSetFuture : resultSetFutures) {
        ResultSet resultSet;
        try {
            resultSet = resultSetFuture.getUninterruptibly();
            resultSet.all();
            cqlKeyValueServices.logTracedQuery(putQuery, resultSet, session, cqlStatementCache.normalQuery);
            if (!resultSet.wasApplied()) {
                throw new KeyAlreadyExistsException("This transaction row already exists: " + putQuery);
            }
        } catch (InvalidQueryException e) {
            if (e.getMessage().contains("Batch too large") && !recursive) {
                log.error("Attempted a put to {} that the Cassandra server" + " deemed to be too large to accept. Batch sizes on the Atlas-side" + " have been artificially lowered to the Cassandra default maximum batch sizes.", tableRef);
                limitBatchSizesToServerDefaults = true;
                try {
                    putInternal(tableRef, values, transactionType, ttl, true);
                } catch (Throwable t) {
                    throw Throwables.throwUncheckedException(t);
                }
            } else {
                throw Throwables.throwUncheckedException(e);
            }
        } catch (Throwable t) {
            throw Throwables.throwUncheckedException(t);
        }
    }
}
Also used : Entry(java.util.Map.Entry) ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ResultSet(com.datastax.driver.core.ResultSet) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException)

Example 5 with KeyAlreadyExistsException

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

the class JdbcKeyValueService method putBatch.

private void putBatch(DSLContext ctx, TableReference tableRef, PutBatch batch, boolean allowReinserts) {
    InsertValuesStep4<Record, byte[], byte[], Long, byte[]> query = ctx.insertInto(table(tableName(tableRef)), field(ROW_NAME, byte[].class), field(COL_NAME, byte[].class), field(TIMESTAMP, Long.class), field(VALUE, byte[].class));
    query = batch.addValuesForInsert(query);
    try {
        query.execute();
    } catch (DataAccessException e) {
        if (allowReinserts) {
            Result<? extends Record> records = ctx.select(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP, A_VALUE).from(atlasTable(tableRef).as(ATLAS_TABLE)).where(row(A_ROW_NAME, A_COL_NAME, A_TIMESTAMP).in(batch.getRowsForSelect())).fetch();
            if (records.isEmpty()) {
                throw e;
            }
            PutBatch nextBatch = batch.getNextBatch(records);
            if (nextBatch != null) {
                putBatch(ctx, tableRef, nextBatch, allowReinserts);
                return;
            }
        }
        throw new KeyAlreadyExistsException("Conflict on table " + tableRef, e);
    }
}
Also used : PutBatch(com.palantir.atlasdb.keyvalue.jdbc.impl.PutBatch) MultiTimestampPutBatch(com.palantir.atlasdb.keyvalue.jdbc.impl.MultiTimestampPutBatch) SingleTimestampPutBatch(com.palantir.atlasdb.keyvalue.jdbc.impl.SingleTimestampPutBatch) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) Result(org.jooq.Result) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult)

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