use of com.palantir.atlasdb.keyvalue.api.KeyValueService in project atlasdb by palantir.
the class SnapshotTransactionTest method testLockAfterGet.
// If lock happens concurrent with get, we aren't sure that we can rollback the transaction
@Test
public void testLockAfterGet() throws Exception {
byte[] rowName = PtBytes.toBytes("1");
Mockery m = new Mockery();
final KeyValueService kvMock = m.mock(KeyValueService.class);
final LockService lockMock = m.mock(LockService.class);
LockService lock = MultiDelegateProxy.newProxyInstance(LockService.class, lockService, lockMock);
final Cell cell = Cell.create(rowName, rowName);
timestampService.getFreshTimestamp();
final long startTs = timestampService.getFreshTimestamp();
final long transactionTs = timestampService.getFreshTimestamp();
keyValueService.put(TABLE, ImmutableMap.of(cell, PtBytes.EMPTY_BYTE_ARRAY), startTs);
m.checking(new Expectations() {
{
oneOf(kvMock).get(TABLE, ImmutableMap.of(cell, transactionTs));
will(throwException(new RuntimeException()));
never(lockMock).lockWithFullLockResponse(with(LockClient.ANONYMOUS), with(any(LockRequest.class)));
}
});
SnapshotTransaction snapshot = new SnapshotTransaction(kvMock, new LegacyTimelockService(timestampService, lock, lockClient), transactionService, NoOpCleaner.INSTANCE, transactionTs, TestConflictDetectionManagers.createWithStaticConflictDetection(ImmutableMap.of(TABLE, ConflictHandler.RETRY_ON_WRITE_WRITE)), AtlasDbConstraintCheckingMode.NO_CONSTRAINT_CHECKING, TransactionReadSentinelBehavior.THROW_EXCEPTION, timestampCache, getRangesExecutor, defaultGetRangesConcurrency, sweepQueue);
try {
snapshot.get(TABLE, ImmutableSet.of(cell));
fail();
} catch (RuntimeException e) {
// expected
}
m.assertIsSatisfied();
}
use of com.palantir.atlasdb.keyvalue.api.KeyValueService in project atlasdb by palantir.
the class SnapshotTransactionTest method testTransactionAtomicity.
@Test
public void testTransactionAtomicity() throws Exception {
// This test runs multiple transactions in parallel, with KeyValueService.put calls throwing
// a RuntimeException from time to time and hanging other times. which effectively kills the
// thread. We ensure that every transaction either adds 5 rows to the table or adds 0 rows
// by checking at the end that the number of rows is a multiple of 5.
final TableReference tableRef = TABLE;
Random random = new Random(1);
final UnstableKeyValueService unstableKvs = new UnstableKeyValueService(keyValueService, random);
final TestTransactionManager unstableTransactionManager = new TestTransactionManagerImpl(unstableKvs, timestampService, lockClient, lockService, transactionService, conflictDetectionManager, sweepStrategyManager, sweepQueue);
ScheduledExecutorService service = PTExecutors.newScheduledThreadPool(20);
for (int i = 0; i < 30; i++) {
final int threadNumber = i;
service.schedule((Callable<Void>) () -> {
if (threadNumber == 10) {
unstableKvs.setRandomlyThrow(true);
}
if (threadNumber == 20) {
unstableKvs.setRandomlyHang(true);
}
Transaction transaction = unstableTransactionManager.createNewTransaction();
BatchingVisitable<RowResult<byte[]>> results = transaction.getRange(tableRef, RangeRequest.builder().build());
final MutableInt nextIndex = new MutableInt(0);
results.batchAccept(1, AbortingVisitors.batching((AbortingVisitor<RowResult<byte[]>, Exception>) row -> {
byte[] dataBytes = row.getColumns().get(PtBytes.toBytes("data"));
BigInteger dataValue = new BigInteger(dataBytes);
nextIndex.setValue(Math.max(nextIndex.toInteger(), dataValue.intValue() + 1));
return true;
}));
// rows to the table.
for (int j = 0; j < 5; j++) {
int rowNumber = nextIndex.toInteger() + j;
Cell cell = Cell.create(PtBytes.toBytes("row" + rowNumber), PtBytes.toBytes("data"));
transaction.put(tableRef, ImmutableMap.of(cell, BigInteger.valueOf(rowNumber).toByteArray()));
Thread.yield();
}
transaction.commit();
return null;
}, i * 20, TimeUnit.MILLISECONDS);
}
service.shutdown();
service.awaitTermination(1, TimeUnit.SECONDS);
// Verify each table has a number of rows that's a multiple of 5
Transaction verifyTransaction = txManager.createNewTransaction();
BatchingVisitable<RowResult<byte[]>> results = verifyTransaction.getRange(tableRef, RangeRequest.builder().build());
final MutableInt numRows = new MutableInt(0);
results.batchAccept(1, AbortingVisitors.batching((AbortingVisitor<RowResult<byte[]>, Exception>) row -> {
numRows.increment();
return true;
}));
Assert.assertEquals(0, numRows.toInteger() % 5);
}
use of com.palantir.atlasdb.keyvalue.api.KeyValueService in project atlasdb by palantir.
the class AtlasDbTestCase method setUp.
@Before
public void setUp() throws Exception {
timestampService = new InMemoryTimestampService();
KeyValueService kvs = getBaseKeyValueService();
keyValueServiceWithStats = new StatsTrackingKeyValueService(kvs);
keyValueService = new TrackingKeyValueService(keyValueServiceWithStats);
TransactionTables.createTables(kvs);
transactionService = TransactionServices.createTransactionService(kvs);
conflictDetectionManager = ConflictDetectionManagers.createWithoutWarmingCache(keyValueService);
sweepStrategyManager = SweepStrategyManagers.createDefault(keyValueService);
serializableTxManager = new TestTransactionManagerImpl(keyValueService, timestampService, lockClient, lockService, transactionService, conflictDetectionManager, sweepStrategyManager, wrappingSweepQueue);
txManager = new CachingTestTransactionManager(serializableTxManager);
}
use of com.palantir.atlasdb.keyvalue.api.KeyValueService in project atlasdb by palantir.
the class AtlasDbTestCase method getBaseKeyValueService.
protected KeyValueService getBaseKeyValueService() {
ExecutorService executor = PTExecutors.newSingleThreadExecutor(PTExecutors.newNamedThreadFactory(true));
InMemoryKeyValueService inMemoryKvs = new InMemoryKeyValueService(false, executor);
KeyValueService tracingKvs = TracingKeyValueService.create(inMemoryKvs);
return AtlasDbMetrics.instrument(KeyValueService.class, tracingKvs);
}
use of com.palantir.atlasdb.keyvalue.api.KeyValueService in project atlasdb by palantir.
the class SweepPriorityStoreTest method setup.
@Before
public void setup() {
exec = PTExecutors.newCachedThreadPool();
KeyValueService kvs = new InMemoryKeyValueService(false, exec);
txManager = SweepTestUtils.setupTxManager(kvs);
priorityStore = SweepPriorityStoreImpl.create(kvs, SweepTableFactory.of(), false);
}
Aggregations