Search in sources :

Example 11 with TransactionFailedRetriableException

use of com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException in project atlasdb by palantir.

the class UserPhotosStreamStore method putMetadataAndHashIndexTask.

@Override
protected void putMetadataAndHashIndexTask(Transaction t, Map<Long, StreamMetadata> streamIdsToMetadata) {
    UserPhotosStreamMetadataTable mdTable = tables.getUserPhotosStreamMetadataTable(t);
    Map<Long, StreamMetadata> prevMetadatas = getMetadata(t, streamIdsToMetadata.keySet());
    Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> rowsToStoredMetadata = Maps.newHashMap();
    Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> rowsToUnstoredMetadata = Maps.newHashMap();
    for (Entry<Long, StreamMetadata> e : streamIdsToMetadata.entrySet()) {
        long streamId = e.getKey();
        StreamMetadata metadata = e.getValue();
        StreamMetadata prevMetadata = prevMetadatas.get(streamId);
        if (metadata.getStatus() == Status.STORED) {
            if (prevMetadata == null || prevMetadata.getStatus() != Status.STORING) {
                // This can happen if we cleanup old streams.
                throw new TransactionFailedRetriableException("Cannot mark a stream as stored that isn't currently storing: " + prevMetadata);
            }
            rowsToStoredMetadata.put(UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow.of(streamId), metadata);
        } else if (metadata.getStatus() == Status.STORING) {
            // This will prevent two users trying to store the same id.
            if (prevMetadata != null) {
                throw new TransactionFailedRetriableException("Cannot reuse the same stream id: " + streamId);
            }
            rowsToUnstoredMetadata.put(UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow.of(streamId), metadata);
        }
    }
    putHashIndexTask(t, rowsToStoredMetadata);
    Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> rowsToMetadata = Maps.newHashMap();
    rowsToMetadata.putAll(rowsToStoredMetadata);
    rowsToMetadata.putAll(rowsToUnstoredMetadata);
    mdTable.putMetadata(rowsToMetadata);
}
Also used : TransactionFailedRetriableException(com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)

Aggregations

TransactionFailedRetriableException (com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException)11 StreamMetadata (com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)8 Cell (com.palantir.atlasdb.keyvalue.api.Cell)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 KeyAlreadyExistsException (com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException)1 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)1 Value (com.palantir.atlasdb.keyvalue.api.Value)1 PreCommitCondition (com.palantir.atlasdb.transaction.api.PreCommitCondition)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 SortedMap (java.util.SortedMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)1 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 MutableLong (org.apache.commons.lang3.mutable.MutableLong)1 Test (org.junit.Test)1