Search in sources :

Example 96 with TableReference

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

the class SweepStatsKeyValueService method flushWrites.

private void flushWrites(Multiset<TableReference> writes, Set<TableReference> clears) {
    if (writes.isEmpty() && clears.isEmpty()) {
        log.info("No writes to flush");
        return;
    }
    log.info("Flushing stats for {} writes and {} clears", SafeArg.of("writes", writes.size()), SafeArg.of("clears", clears.size()));
    log.trace("Flushing writes: {}", UnsafeArg.of("writes", writes));
    log.trace("Flushing clears: {}", UnsafeArg.of("clears", clears));
    try {
        Set<TableReference> tableNames = Sets.difference(writes.elementSet(), clears);
        Collection<byte[]> rows = Collections2.transform(Collections2.transform(tableNames, t -> t.getQualifiedName()), Functions.compose(Persistables.persistToBytesFunction(), SweepPriorityRow.fromFullTableNameFun()));
        Map<Cell, Value> oldWriteCounts = delegate().getRows(SWEEP_PRIORITY_TABLE, rows, SweepPriorityTable.getColumnSelection(SweepPriorityNamedColumn.WRITE_COUNT), Long.MAX_VALUE);
        Map<Cell, byte[]> newWriteCounts = Maps.newHashMapWithExpectedSize(writes.elementSet().size());
        byte[] col = SweepPriorityNamedColumn.WRITE_COUNT.getShortName();
        for (TableReference tableRef : tableNames) {
            Preconditions.checkState(!tableRef.getQualifiedName().startsWith(AtlasDbConstants.NAMESPACE_PREFIX), "The sweep stats kvs should wrap the namespace mapping kvs, not the other way around.");
            byte[] row = SweepPriorityRow.of(tableRef.getQualifiedName()).persistToBytes();
            Cell cell = Cell.create(row, col);
            Value oldValue = oldWriteCounts.get(cell);
            long oldCount = oldValue == null || oldValue.getContents().length == 0 ? 0 : SweepPriorityTable.WriteCount.BYTES_HYDRATOR.hydrateFromBytes(oldValue.getContents()).getValue();
            long newValue = clears.contains(tableRef) ? writes.count(tableRef) : oldCount + writes.count(tableRef);
            log.debug("Sweep priority for {} has {} writes (was {})", tableRef, newValue, oldCount);
            newWriteCounts.put(cell, SweepPriorityTable.WriteCount.of(newValue).persistValue());
        }
        long timestamp = timestampService.getFreshTimestamp();
        // Committing before writing is intentional, we want the start timestamp to
        // show up in the transaction table before we write do our writes.
        commit(timestamp);
        delegate().put(SWEEP_PRIORITY_TABLE, newWriteCounts, timestamp);
    } catch (RuntimeException e) {
        if (Thread.interrupted()) {
            return;
        }
        Set<TableReference> allTableNames = delegate().getAllTableNames();
        if (!allTableNames.contains(SWEEP_PRIORITY_TABLE) || !allTableNames.contains(TransactionConstants.TRANSACTION_TABLE)) {
            // ignore problems when sweep or transaction tables don't exist
            log.warn("Ignoring failed sweep stats flush due to ", e);
        }
        log.error("Unable to flush sweep stats for writes {} and clears {}: ", writes, clears, e);
        throw e;
    }
}
Also used : Multiset(com.google.common.collect.Multiset) LoggerFactory(org.slf4j.LoggerFactory) SweepSchema(com.palantir.atlasdb.schema.SweepSchema) SweepPriorityRow(com.palantir.atlasdb.schema.generated.SweepPriorityTable.SweepPriorityRow) Collections2(com.google.common.collect.Collections2) Multimap(com.google.common.collect.Multimap) Supplier(java.util.function.Supplier) Persistables(com.palantir.common.persist.Persistables) SweepPriorityNamedColumn(com.palantir.atlasdb.schema.generated.SweepPriorityTable.SweepPriorityNamedColumn) ClusterAvailabilityStatus(com.palantir.atlasdb.keyvalue.api.ClusterAvailabilityStatus) SafeArg(com.palantir.logsafe.SafeArg) PTExecutors(com.palantir.common.concurrent.PTExecutors) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ImmutableMultiset(com.google.common.collect.ImmutableMultiset) Map(java.util.Map) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) TimestampService(com.palantir.timestamp.TimestampService) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) Functions(com.google.common.base.Functions) Logger(org.slf4j.Logger) ImmutableSet(com.google.common.collect.ImmutableSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Value(com.palantir.atlasdb.keyvalue.api.Value) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Maps(com.google.common.collect.Maps) Sets(com.google.common.collect.Sets) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) SweepPriorityTable(com.palantir.atlasdb.schema.generated.SweepPriorityTable) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Lock(java.util.concurrent.locks.Lock) TransactionConstants(com.palantir.atlasdb.transaction.impl.TransactionConstants) ConcurrentHashMultiset(com.google.common.collect.ConcurrentHashMultiset) UnsafeArg(com.palantir.logsafe.UnsafeArg) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) Entry(java.util.Map.Entry) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 97 with TableReference

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

the class LoggingArgs method tableRefs.

public static SafeAndUnsafeTableReferences tableRefs(Collection<TableReference> tableReferences) {
    List<TableReference> safeTableRefs = new ArrayList<>();
    List<TableReference> unsafeTableRefs = new ArrayList<>();
    for (TableReference tableRef : tableReferences) {
        if (logArbitrator.isTableReferenceSafe(tableRef)) {
            safeTableRefs.add(tableRef);
        } else {
            unsafeTableRefs.add(tableRef);
        }
    }
    return ImmutableSafeAndUnsafeTableReferences.builder().safeTableRefs(SafeArg.of("safeTableRefs", safeTableRefs)).unsafeTableRefs(UnsafeArg.of("unsafeTableRefs", unsafeTableRefs)).build();
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) ArrayList(java.util.ArrayList)

Example 98 with TableReference

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

the class TestScrubQueueMigrationCommand method testScrubQueueMigration.

@Test
public void testScrubQueueMigration() {
    TableReference foo = TableReference.createWithEmptyNamespace("foo");
    TableReference bar = TableReference.createWithEmptyNamespace("bar");
    TableReference baz = TableReference.createWithEmptyNamespace("baz");
    Cell cell1 = Cell.create(new byte[] { 1, 2, 3 }, new byte[] { 4, 5, 6 });
    Cell cell2 = Cell.create(new byte[] { 7, 8, 9 }, new byte[] { 4, 5, 6 });
    Cell cell3 = Cell.create(new byte[] { 1, 2, 3 }, new byte[] { 7, 8, 9 });
    kvs.createTable(AtlasDbConstants.OLD_SCRUB_TABLE, new byte[] { 0 });
    kvs.putWithTimestamps(AtlasDbConstants.OLD_SCRUB_TABLE, ImmutableMultimap.of(cell1, Value.create("foo\0bar".getBytes(), 10), cell1, Value.create("baz".getBytes(), 20), cell2, Value.create("foo".getBytes(), 30), cell3, Value.create("foo\0bar\0baz".getBytes(), 40)));
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ScrubQueueMigrationCommand.run(kvs, new PrintWriter(baos, true), 1000);
    BatchingVisitable<SortedMap<Long, Multimap<TableReference, Cell>>> visitable = scrubStore.getBatchingVisitableScrubQueue(Long.MAX_VALUE, PtBytes.EMPTY_BYTE_ARRAY, PtBytes.EMPTY_BYTE_ARRAY);
    Assert.assertEquals(ImmutableSortedMap.of(10L, ImmutableMultimap.of(foo, cell1, bar, cell1), 20L, ImmutableMultimap.of(baz, cell1), 30L, ImmutableMultimap.of(foo, cell2), 40L, ImmutableMultimap.of(foo, cell3, bar, cell3, baz, cell3)), Iterables.getOnlyElement(BatchingVisitables.copyToList(visitable)));
    String output = new String(baos.toByteArray());
    Assert.assertTrue(output, output.contains("Starting iteration 2"));
    Assert.assertTrue(output, output.contains("Moved 4 cells"));
    Assert.assertTrue(output, output.contains("into 7 cells"));
    Assert.assertTrue(output, output.contains("Finished all iterations"));
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) SortedMap(java.util.SortedMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cell(com.palantir.atlasdb.keyvalue.api.Cell) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Example 99 with TableReference

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

the class TestKvsMigrationCommand method checkKvs.

private void checkKvs(AtlasDbServices services, int numTables, int numEntriesPerTable) {
    for (int i = 0; i < numTables; i++) {
        TableReference table = TableReference.create(Namespace.create("ns"), "table" + i);
        services.getKeyValueService().createTable(table, AtlasDbConstants.GENERIC_TABLE_METADATA);
        services.getTransactionManager().runTaskThrowOnConflict(t -> {
            ImmutableSet.Builder<Cell> toRead = ImmutableSet.builder();
            ImmutableMap.Builder<Cell, byte[]> expectedBuilder = ImmutableMap.builder();
            for (int j = 0; j < numEntriesPerTable; j++) {
                Cell cell = Cell.create(PtBytes.toBytes("row" + j), PtBytes.toBytes("col"));
                toRead.add(cell);
                expectedBuilder.put(cell, PtBytes.toBytes("val" + j));
            }
            Map<Cell, byte[]> expected = expectedBuilder.build();
            Map<Cell, byte[]> result = t.get(table, expected.keySet());
            Assert.assertEquals(expected.keySet(), result.keySet());
            for (Entry<Cell, byte[]> e : result.entrySet()) {
                Assert.assertArrayEquals(expected.get(e.getKey()), e.getValue());
            }
            return null;
        });
    }
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) ImmutableSet(com.google.common.collect.ImmutableSet) Cell(com.palantir.atlasdb.keyvalue.api.Cell) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 100 with TableReference

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

the class CassandraKeyValueServiceTableCreationIntegrationTest method testCreateTableCanRestoreLostMetadata.

@Test
public void testCreateTableCanRestoreLostMetadata() {
    // setup a basic table
    TableReference missingMetadataTable = TableReference.createFromFullyQualifiedName("test.metadata_missing");
    byte[] initialMetadata = new TableDefinition() {

        {
            rowName();
            rowComponent("blob", ValueType.BLOB);
            columns();
            column("bar", "b", ValueType.BLOB);
            conflictHandler(ConflictHandler.IGNORE_ALL);
            sweepStrategy(TableMetadataPersistence.SweepStrategy.NOTHING);
        }
    }.toTableMetadata().persistToBytes();
    kvs.createTable(missingMetadataTable, initialMetadata);
    // retrieve the metadata and see that it's the same as what we just put in
    byte[] existingMetadata = kvs.getMetadataForTable(missingMetadataTable);
    assertThat(initialMetadata, is(existingMetadata));
    // Directly get and delete the metadata (`get` necessary to get the fake timestamp putMetadataForTables used)
    Cell cell = Cell.create(missingMetadataTable.getQualifiedName().getBytes(StandardCharsets.UTF_8), "m".getBytes(StandardCharsets.UTF_8));
    Value persistedMetadata = Iterables.getLast(kvs.get(AtlasDbConstants.DEFAULT_METADATA_TABLE, ImmutableMap.of(cell, Long.MAX_VALUE)).values());
    kvs.delete(AtlasDbConstants.DEFAULT_METADATA_TABLE, ImmutableMultimap.of(cell, persistedMetadata.getTimestamp()));
    // pretend we started up again and did a createTable() for our existing table, that no longer has metadata
    kvs.createTable(missingMetadataTable, initialMetadata);
    // retrieve the metadata again and see that it's the same as what we just put in
    existingMetadata = kvs.getMetadataForTable(missingMetadataTable);
    assertThat(initialMetadata, is(existingMetadata));
}
Also used : TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) Value(com.palantir.atlasdb.keyvalue.api.Value) TableDefinition(com.palantir.atlasdb.table.description.TableDefinition) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Aggregations

TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)112 Cell (com.palantir.atlasdb.keyvalue.api.Cell)41 Test (org.junit.Test)41 Map (java.util.Map)17 ImmutableMap (com.google.common.collect.ImmutableMap)13 Multimap (com.google.common.collect.Multimap)13 Entry (java.util.Map.Entry)13 Set (java.util.Set)13 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)12 Value (com.palantir.atlasdb.keyvalue.api.Value)12 Collection (java.util.Collection)12 List (java.util.List)12 ImmutableSet (com.google.common.collect.ImmutableSet)11 KeyValueService (com.palantir.atlasdb.keyvalue.api.KeyValueService)11 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)11 SortedMap (java.util.SortedMap)11 Lists (com.google.common.collect.Lists)10 AtlasDbConstants (com.palantir.atlasdb.AtlasDbConstants)10 Sets (com.google.common.collect.Sets)9 InsufficientConsistencyException (com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException)9