Search in sources :

Example 56 with StreamMetadata

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

the class TestHashComponentsStreamStore method touchMetadataWhileMarkingUsedForConflicts.

@Override
protected void touchMetadataWhileMarkingUsedForConflicts(Transaction t, Iterable<Long> ids) {
    TestHashComponentsStreamMetadataTable metaTable = tables.getTestHashComponentsStreamMetadataTable(t);
    Set<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow> rows = Sets.newHashSet();
    for (Long id : ids) {
        rows.add(TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow.of(id));
    }
    Map<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow, StreamMetadata> metadatas = metaTable.getMetadatas(rows);
    for (Map.Entry<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
        StreamMetadata metadata = e.getValue();
        Preconditions.checkState(metadata.getStatus() == Status.STORED, "Stream: %s has status: %s", e.getKey().getId(), metadata.getStatus());
        metaTable.putMetadata(e.getKey(), metadata);
    }
    SetView<TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow> missingRows = Sets.difference(rows, metadatas.keySet());
    if (!missingRows.isEmpty()) {
        throw new IllegalStateException("Missing metadata rows for:" + missingRows + " rows: " + rows + " metadata: " + metadatas + " txn timestamp: " + t.getTimestamp());
    }
}
Also used : StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 57 with StreamMetadata

use of com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata 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)

Example 58 with StreamMetadata

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

the class UserPhotosMetadataCleanupTask method cellsCleanedUp.

@Override
public boolean cellsCleanedUp(Transaction t, Set<Cell> cells) {
    UserPhotosStreamMetadataTable metaTable = tables.getUserPhotosStreamMetadataTable(t);
    Collection<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow> rows = Lists.newArrayListWithCapacity(cells.size());
    for (Cell cell : cells) {
        rows.add(UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow.BYTES_HYDRATOR.hydrateFromBytes(cell.getRowName()));
    }
    Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> currentMetadata = metaTable.getMetadatas(rows);
    Set<Long> toDelete = Sets.newHashSet();
    for (Map.Entry<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> e : currentMetadata.entrySet()) {
        if (e.getValue().getStatus() != Status.STORED) {
            toDelete.add(e.getKey().getId());
        }
    }
    UserPhotosStreamStore.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 59 with StreamMetadata

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

the class UserPhotosStreamStore method touchMetadataWhileMarkingUsedForConflicts.

@Override
protected void touchMetadataWhileMarkingUsedForConflicts(Transaction t, Iterable<Long> ids) {
    UserPhotosStreamMetadataTable metaTable = tables.getUserPhotosStreamMetadataTable(t);
    Set<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow> rows = Sets.newHashSet();
    for (Long id : ids) {
        rows.add(UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow.of(id));
    }
    Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> metadatas = metaTable.getMetadatas(rows);
    for (Map.Entry<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
        StreamMetadata metadata = e.getValue();
        Preconditions.checkState(metadata.getStatus() == Status.STORED, "Stream: %s has status: %s", e.getKey().getId(), metadata.getStatus());
        metaTable.putMetadata(e.getKey(), metadata);
    }
    SetView<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow> missingRows = Sets.difference(rows, metadatas.keySet());
    if (!missingRows.isEmpty()) {
        throw new IllegalStateException("Missing metadata rows for:" + missingRows + " rows: " + rows + " metadata: " + metadatas + " txn timestamp: " + t.getTimestamp());
    }
}
Also used : StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 60 with StreamMetadata

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

the class UserPhotosStreamStore method deleteStreams.

/**
 * This should only be used from the cleanup tasks.
 */
void deleteStreams(Transaction t, final Set<Long> streamIds) {
    if (streamIds.isEmpty()) {
        return;
    }
    Set<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow> smRows = Sets.newHashSet();
    Multimap<UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow, UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumn> shToDelete = HashMultimap.create();
    for (Long streamId : streamIds) {
        smRows.add(UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow.of(streamId));
    }
    UserPhotosStreamMetadataTable table = tables.getUserPhotosStreamMetadataTable(t);
    Map<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> metadatas = table.getMetadatas(smRows);
    Set<UserPhotosStreamValueTable.UserPhotosStreamValueRow> streamValueToDelete = Sets.newHashSet();
    for (Entry<UserPhotosStreamMetadataTable.UserPhotosStreamMetadataRow, StreamMetadata> e : metadatas.entrySet()) {
        Long streamId = e.getKey().getId();
        long blocks = getNumberOfBlocksFromMetadata(e.getValue());
        for (long i = 0; i < blocks; i++) {
            streamValueToDelete.add(UserPhotosStreamValueTable.UserPhotosStreamValueRow.of(streamId, i));
        }
        ByteString streamHash = e.getValue().getHash();
        Sha256Hash hash = Sha256Hash.EMPTY;
        if (streamHash != com.google.protobuf.ByteString.EMPTY) {
            hash = new Sha256Hash(streamHash.toByteArray());
        } else {
            log.error("Empty hash for stream {}", streamId);
        }
        UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow hashRow = UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxRow.of(hash);
        UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumn column = UserPhotosStreamHashAidxTable.UserPhotosStreamHashAidxColumn.of(streamId);
        shToDelete.put(hashRow, column);
    }
    tables.getUserPhotosStreamHashAidxTable(t).delete(shToDelete);
    tables.getUserPhotosStreamValueTable(t).delete(streamValueToDelete);
    table.delete(smRows);
}
Also used : ByteString(com.google.protobuf.ByteString) Sha256Hash(com.palantir.util.crypto.Sha256Hash) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)

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