Search in sources :

Example 26 with KeyValueService

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

the class CleanTransactionRange method executeTimestampCommand.

@Override
protected int executeTimestampCommand(AtlasDbServices services) {
    KeyValueService kvs = services.getKeyValueService();
    ClosableIterator<RowResult<Value>> range = kvs.getRange(TransactionConstants.TRANSACTION_TABLE, RangeRequest.all(), Long.MAX_VALUE);
    Multimap<Cell, Long> toDelete = HashMultimap.create();
    while (range.hasNext()) {
        RowResult<Value> row = range.next();
        byte[] rowName = row.getRowName();
        long startTs = TransactionConstants.getTimestampForValue(rowName);
        Value value;
        try {
            value = row.getOnlyColumnValue();
        } catch (IllegalStateException e) {
            // this should never happen
            printer.error("Found a row in the transactions table that didn't have 1" + " and only 1 column value: start={}", SafeArg.of("startTs", startTs));
            continue;
        }
        long commitTs = TransactionConstants.getTimestampForValue(value.getContents());
        if (commitTs <= timestamp) {
            // this is a valid transaction
            continue;
        }
        printer.info("Found and cleaning possibly inconsistent transaction: [start={}, commit={}]", SafeArg.of("startTs", startTs), SafeArg.of("commitTs", commitTs));
        Cell key = Cell.create(rowName, TransactionConstants.COMMIT_TS_COLUMN);
        // value.getTimestamp() should always be 0L
        toDelete.put(key, value.getTimestamp());
    }
    if (!toDelete.isEmpty()) {
        kvs.delete(TransactionConstants.TRANSACTION_TABLE, toDelete);
        printer.info("Delete completed.");
    } else {
        printer.info("Found no transactions after the given timestamp to delete.");
    }
    return 0;
}
Also used : RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 27 with KeyValueService

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

the class TestSweepCommand method testSweepNamespace.

@Test
public void testSweepNamespace() throws Exception {
    try (SingleBackendCliTestRunner runner = makeRunner(paramsWithDryRunSet(SWEEP_COMMAND, "-n", NS1.getName()))) {
        TestAtlasDbServices services = runner.connect(moduleFactory);
        SerializableTransactionManager txm = services.getTransactionManager();
        TimestampService tss = services.getTimestampService();
        KeyValueService kvs = services.getKeyValueService();
        createTable(kvs, TABLE_ONE, TableMetadataPersistence.SweepStrategy.CONSERVATIVE);
        createTable(kvs, TABLE_TWO, TableMetadataPersistence.SweepStrategy.CONSERVATIVE);
        createTable(kvs, TABLE_THREE, TableMetadataPersistence.SweepStrategy.CONSERVATIVE);
        long ts1 = put(txm, TABLE_ONE, "foo", "bar");
        long ts2 = put(txm, TABLE_TWO, "foo", "tar");
        long ts3 = put(txm, TABLE_THREE, "foo", "jar");
        long ts4 = put(txm, TABLE_ONE, "foo", "baz");
        long ts5 = put(txm, TABLE_THREE, "foo", "jaz");
        long ts6 = put(txm, TABLE_TWO, "foo", "taz");
        long ts7 = tss.getFreshTimestamp();
        sweep(runner, ts7);
        Assert.assertEquals("baz", get(kvs, TABLE_ONE, "foo", ts7));
        Assert.assertEquals(deletedValue("bar"), get(kvs, TABLE_ONE, "foo", mid(ts1, ts2)));
        Assert.assertEquals(ImmutableSet.of(deletedTimestamp(ts1), ts4), getAllTs(kvs, TABLE_ONE, "foo"));
        Assert.assertEquals("taz", get(kvs, TABLE_TWO, "foo", ts7));
        Assert.assertEquals(deletedValue("tar"), get(kvs, TABLE_TWO, "foo", mid(ts4, ts6)));
        Assert.assertEquals(ImmutableSet.of(deletedTimestamp(ts2), ts6), getAllTs(kvs, TABLE_TWO, "foo"));
        Assert.assertEquals("jaz", get(kvs, TABLE_THREE, "foo", ts7));
        Assert.assertEquals("jar", get(kvs, TABLE_THREE, "foo", mid(ts3, ts5)));
        Assert.assertEquals(ImmutableSet.of(ts3, ts5), getAllTs(kvs, TABLE_THREE, "foo"));
    }
}
Also used : KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) SingleBackendCliTestRunner(com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner) SerializableTransactionManager(com.palantir.atlasdb.transaction.impl.SerializableTransactionManager) DaggerTestAtlasDbServices(com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices) TestAtlasDbServices(com.palantir.atlasdb.services.test.TestAtlasDbServices) TimestampService(com.palantir.timestamp.TimestampService) Test(org.junit.Test)

Example 28 with KeyValueService

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

the class TestSweepCommand method testSweepTable.

@Test
public void testSweepTable() throws Exception {
    try (SingleBackendCliTestRunner runner = makeRunner(paramsWithDryRunSet(SWEEP_COMMAND, "-t", TABLE_ONE.getQualifiedName()))) {
        TestAtlasDbServices services = runner.connect(moduleFactory);
        SerializableTransactionManager txm = services.getTransactionManager();
        TimestampService tss = services.getTimestampService();
        KeyValueService kvs = services.getKeyValueService();
        createTable(kvs, TABLE_ONE, TableMetadataPersistence.SweepStrategy.CONSERVATIVE);
        createTable(kvs, TABLE_TWO, TableMetadataPersistence.SweepStrategy.CONSERVATIVE);
        long ts1 = put(txm, TABLE_ONE, "foo", "bar");
        long ts2 = put(txm, TABLE_TWO, "foo", "tar");
        long ts3 = put(txm, TABLE_ONE, "foo", "baz");
        long ts4 = put(txm, TABLE_TWO, "foo", "taz");
        long ts5 = tss.getFreshTimestamp();
        String stdout = sweep(runner, ts5);
        Scanner scanner = new Scanner(stdout);
        final long cellValuesExamined = Long.parseLong(scanner.findInLine("\\d+ cell values").split(" ")[0]);
        final long deletedCells = Long.parseLong(scanner.findInLine("deleted \\d+ stale versions of those cells").split(" ")[1]);
        Assert.assertEquals(2, cellValuesExamined);
        Assert.assertEquals(1, deletedCells);
        Assert.assertEquals("baz", get(kvs, TABLE_ONE, "foo", ts5));
        Assert.assertEquals(deletedValue("bar"), get(kvs, TABLE_ONE, "foo", mid(ts1, ts3)));
        Assert.assertEquals(ImmutableSet.of(deletedTimestamp(ts1), ts3), getAllTs(kvs, TABLE_ONE, "foo"));
        Assert.assertEquals("taz", get(kvs, TABLE_TWO, "foo", ts5));
        Assert.assertEquals("tar", get(kvs, TABLE_TWO, "foo", mid(ts3, ts4)));
        Assert.assertEquals(ImmutableSet.of(ts2, ts4), getAllTs(kvs, TABLE_TWO, "foo"));
    }
}
Also used : Scanner(java.util.Scanner) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) SingleBackendCliTestRunner(com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner) SerializableTransactionManager(com.palantir.atlasdb.transaction.impl.SerializableTransactionManager) DaggerTestAtlasDbServices(com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices) TestAtlasDbServices(com.palantir.atlasdb.services.test.TestAtlasDbServices) TimestampService(com.palantir.timestamp.TimestampService) Test(org.junit.Test)

Example 29 with KeyValueService

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

the class TestSweepCommand method testSweepStartRow.

@Test
public void testSweepStartRow() throws Exception {
    try (SingleBackendCliTestRunner runner = makeRunner(paramsWithDryRunSet(SWEEP_COMMAND, "-t", TABLE_ONE.getQualifiedName(), "-r", BaseEncoding.base16().encode("foo".getBytes(StandardCharsets.UTF_8))))) {
        TestAtlasDbServices services = runner.connect(moduleFactory);
        SerializableTransactionManager txm = services.getTransactionManager();
        TimestampService tss = services.getTimestampService();
        KeyValueService kvs = services.getKeyValueService();
        createTable(kvs, TABLE_ONE, TableMetadataPersistence.SweepStrategy.CONSERVATIVE);
        long ts1 = put(txm, TABLE_ONE, "foo", "bar");
        long ts2 = put(txm, TABLE_ONE, "foo", "biz");
        long ts3 = put(txm, TABLE_ONE, "boo", "biz");
        long ts4 = put(txm, TABLE_ONE, "foo", "baz");
        long ts5 = tss.getFreshTimestamp();
        sweep(runner, ts5);
        Assert.assertEquals("baz", get(kvs, TABLE_ONE, "foo", ts5));
        Assert.assertEquals(deletedValue("bar"), get(kvs, TABLE_ONE, "foo", mid(ts1, ts3)));
        Assert.assertEquals(deletedValue("biz"), get(kvs, TABLE_ONE, "foo", mid(ts2, ts4)));
        Assert.assertEquals("biz", get(kvs, TABLE_ONE, "boo", mid(ts3, ts5)));
        Assert.assertEquals(ImmutableSet.of(deletedTimestamp(ts1), deletedTimestamp(ts2), ts4), getAllTs(kvs, TABLE_ONE, "foo"));
        Assert.assertEquals(ImmutableSet.of(ts3), getAllTs(kvs, TABLE_ONE, "boo"));
    }
}
Also used : KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) SingleBackendCliTestRunner(com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner) SerializableTransactionManager(com.palantir.atlasdb.transaction.impl.SerializableTransactionManager) DaggerTestAtlasDbServices(com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices) TestAtlasDbServices(com.palantir.atlasdb.services.test.TestAtlasDbServices) TimestampService(com.palantir.timestamp.TimestampService) Test(org.junit.Test)

Example 30 with KeyValueService

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

the class CassandraKeyValueServiceIntegrationTest method testCfEqualityChecker.

@Test
public void testCfEqualityChecker() throws TException {
    CassandraKeyValueServiceImpl kvs;
    if (keyValueService instanceof CassandraKeyValueService) {
        kvs = (CassandraKeyValueServiceImpl) keyValueService;
    } else if (keyValueService instanceof TableSplittingKeyValueService) {
        // scylla tests
        KeyValueService delegate = ((TableSplittingKeyValueService) keyValueService).getDelegate(testTable);
        assertTrue("The nesting of Key Value Services has apparently changed", delegate instanceof CassandraKeyValueService);
        kvs = (CassandraKeyValueServiceImpl) delegate;
    } else {
        throw new IllegalArgumentException("Can't run this cassandra-specific test against a non-cassandra KVS");
    }
    kvs.createTable(testTable, tableMetadata);
    List<CfDef> knownCfs = kvs.getClientPool().runWithRetry(client -> client.describe_keyspace("atlasdb").getCf_defs());
    CfDef clusterSideCf = Iterables.getOnlyElement(knownCfs.stream().filter(cf -> cf.getName().equals(getInternalTestTableName())).collect(Collectors.toList()));
    assertTrue("After serialization and deserialization to database, Cf metadata did not match.", ColumnFamilyDefinitions.isMatchingCf(kvs.getCfForTable(testTable, tableMetadata, FOUR_DAYS_IN_SECONDS), clusterSideCf));
}
Also used : TableSplittingKeyValueService(com.palantir.atlasdb.keyvalue.impl.TableSplittingKeyValueService) KeyValueService(com.palantir.atlasdb.keyvalue.api.KeyValueService) TableSplittingKeyValueService(com.palantir.atlasdb.keyvalue.impl.TableSplittingKeyValueService) CfDef(org.apache.cassandra.thrift.CfDef) AbstractKeyValueServiceTest(com.palantir.atlasdb.keyvalue.impl.AbstractKeyValueServiceTest) Test(org.junit.Test)

Aggregations

KeyValueService (com.palantir.atlasdb.keyvalue.api.KeyValueService)30 Test (org.junit.Test)16 InMemoryKeyValueService (com.palantir.atlasdb.keyvalue.impl.InMemoryKeyValueService)12 TableReference (com.palantir.atlasdb.keyvalue.api.TableReference)7 SerializableTransactionManager (com.palantir.atlasdb.transaction.impl.SerializableTransactionManager)6 ImmutableMap (com.google.common.collect.ImmutableMap)5 Cell (com.palantir.atlasdb.keyvalue.api.Cell)5 TrackingKeyValueService (com.palantir.atlasdb.keyvalue.impl.TrackingKeyValueService)5 TimestampService (com.palantir.timestamp.TimestampService)5 Map (java.util.Map)5 SingleBackendCliTestRunner (com.palantir.atlasdb.cli.runner.SingleBackendCliTestRunner)4 ForwardingKeyValueService (com.palantir.atlasdb.keyvalue.impl.ForwardingKeyValueService)4 TracingKeyValueService (com.palantir.atlasdb.keyvalue.impl.TracingKeyValueService)4 DaggerTestAtlasDbServices (com.palantir.atlasdb.services.test.DaggerTestAtlasDbServices)4 TestAtlasDbServices (com.palantir.atlasdb.services.test.TestAtlasDbServices)4 Before (org.junit.Before)4 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)3 LockService (com.palantir.lock.LockService)3 LegacyTimelockService (com.palantir.lock.impl.LegacyTimelockService)3 IdentityHashMap (java.util.IdentityHashMap)3