Search in sources :

Example 41 with StreamMetadata

use of com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata in project atlasdb by palantir.

the class StreamTestMaxMemStreamStore method putMetadataAndHashIndexTask.

@Override
protected void putMetadataAndHashIndexTask(Transaction t, Map<Long, StreamMetadata> streamIdsToMetadata) {
    StreamTestMaxMemStreamMetadataTable mdTable = tables.getStreamTestMaxMemStreamMetadataTable(t);
    Map<Long, StreamMetadata> prevMetadatas = getMetadata(t, streamIdsToMetadata.keySet());
    Map<StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow, StreamMetadata> rowsToStoredMetadata = Maps.newHashMap();
    Map<StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow, 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(StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow.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(StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow.of(streamId), metadata);
        }
    }
    putHashIndexTask(t, rowsToStoredMetadata);
    Map<StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow, 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)

Example 42 with StreamMetadata

use of com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata in project atlasdb by palantir.

the class StreamTestMaxMemStreamStore method putHashIndexTask.

private void putHashIndexTask(Transaction t, Map<StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow, StreamMetadata> rowsToMetadata) {
    Multimap<StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxRow, StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumnValue> indexMap = HashMultimap.create();
    for (Entry<StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow, StreamMetadata> e : rowsToMetadata.entrySet()) {
        StreamTestMaxMemStreamMetadataTable.StreamTestMaxMemStreamMetadataRow row = e.getKey();
        StreamMetadata metadata = e.getValue();
        Preconditions.checkArgument(metadata.getStatus() == Status.STORED, "Should only index successfully stored streams.");
        Sha256Hash hash = Sha256Hash.EMPTY;
        if (metadata.getHash() != com.google.protobuf.ByteString.EMPTY) {
            hash = new Sha256Hash(metadata.getHash().toByteArray());
        }
        StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxRow hashRow = StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxRow.of(hash);
        StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumn column = StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumn.of(row.getId());
        StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumnValue columnValue = StreamTestMaxMemStreamHashAidxTable.StreamTestMaxMemStreamHashAidxColumnValue.of(column, 0L);
        indexMap.put(hashRow, columnValue);
    }
    StreamTestMaxMemStreamHashAidxTable hiTable = tables.getStreamTestMaxMemStreamHashAidxTable(t);
    hiTable.put(indexMap);
}
Also used : Sha256Hash(com.palantir.util.crypto.Sha256Hash) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)

Example 43 with StreamMetadata

use of com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata in project atlasdb by palantir.

the class StreamTestMetadataCleanupTask method cellsCleanedUp.

@Override
public boolean cellsCleanedUp(Transaction t, Set<Cell> cells) {
    StreamTestStreamMetadataTable metaTable = tables.getStreamTestStreamMetadataTable(t);
    Collection<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow> rows = Lists.newArrayListWithCapacity(cells.size());
    for (Cell cell : cells) {
        rows.add(StreamTestStreamMetadataTable.StreamTestStreamMetadataRow.BYTES_HYDRATOR.hydrateFromBytes(cell.getRowName()));
    }
    Map<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, StreamMetadata> currentMetadata = metaTable.getMetadatas(rows);
    Set<Long> toDelete = Sets.newHashSet();
    for (Map.Entry<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, StreamMetadata> e : currentMetadata.entrySet()) {
        if (e.getValue().getStatus() != Status.STORED) {
            toDelete.add(e.getKey().getId());
        }
    }
    StreamTestStreamStore.of(tables).deleteStreams(t, toDelete);
    return false;
}
Also used : StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Map(java.util.Map)

Example 44 with StreamMetadata

use of com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata in project atlasdb by palantir.

the class StreamTestStreamStore method putMetadataAndHashIndexTask.

@Override
protected void putMetadataAndHashIndexTask(Transaction t, Map<Long, StreamMetadata> streamIdsToMetadata) {
    StreamTestStreamMetadataTable mdTable = tables.getStreamTestStreamMetadataTable(t);
    Map<Long, StreamMetadata> prevMetadatas = getMetadata(t, streamIdsToMetadata.keySet());
    Map<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, StreamMetadata> rowsToStoredMetadata = Maps.newHashMap();
    Map<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, 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(StreamTestStreamMetadataTable.StreamTestStreamMetadataRow.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(StreamTestStreamMetadataTable.StreamTestStreamMetadataRow.of(streamId), metadata);
        }
    }
    putHashIndexTask(t, rowsToStoredMetadata);
    Map<StreamTestStreamMetadataTable.StreamTestStreamMetadataRow, 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)

Example 45 with StreamMetadata

use of com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata in project atlasdb by palantir.

the class StreamTestStreamStore method touchMetadataWhileStoringForConflicts.

private void touchMetadataWhileStoringForConflicts(Transaction t, Long id, long blockNumber) {
    StreamTestStreamMetadataTable metaTable = tables.getStreamTestStreamMetadataTable(t);
    StreamTestStreamMetadataTable.StreamTestStreamMetadataRow row = StreamTestStreamMetadataTable.StreamTestStreamMetadataRow.of(id);
    StreamMetadata metadata = metaTable.getMetadatas(ImmutableSet.of(row)).values().iterator().next();
    Preconditions.checkState(metadata.getStatus() == Status.STORING, "This stream is being cleaned up while storing blocks: %s", id);
    Builder builder = StreamMetadata.newBuilder(metadata);
    builder.setLength(blockNumber * BLOCK_SIZE_IN_BYTES + 1);
    metaTable.putMetadata(row, builder.build());
}
Also used : StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) Builder(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata.Builder)

Aggregations

StreamMetadata (com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)62 Sha256Hash (com.palantir.util.crypto.Sha256Hash)27 Map (java.util.Map)25 ImmutableMap (com.google.common.collect.ImmutableMap)17 ByteString (com.google.protobuf.ByteString)9 Cell (com.palantir.atlasdb.keyvalue.api.Cell)9 Builder (com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata.Builder)9 TransactionFailedRetriableException (com.palantir.atlasdb.transaction.api.TransactionFailedRetriableException)9 CountingInputStream (com.google.common.io.CountingInputStream)2 IOException (java.io.IOException)2 Functions (com.google.common.base.Functions)1 Preconditions (com.google.common.base.Preconditions)1 ArrayListMultimap (com.google.common.collect.ArrayListMultimap)1 Collections2 (com.google.common.collect.Collections2)1 HashMultimap (com.google.common.collect.HashMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Multimap (com.google.common.collect.Multimap)1 Multimaps (com.google.common.collect.Multimaps)1