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