private InMemoryTimestampService getSingletonTimestampService() { if (inMemoryTimestampService == null) { inMemoryTimestampService = new InMemoryTimestampService(); } return inMemoryTimestampService; } }
public static CassandraMutationTimestampProvider getMutationProviderWithStartingTimestamp(long timestamp) { InMemoryTimestampService timestampService = new InMemoryTimestampService(); timestampService.fastForwardTimestamp(timestamp); return CassandraMutationTimestampProviders.singleLongSupplierBacked(timestampService::getFreshTimestamp); } }
@Test public void canSwitchBetweenSchemaVersions() { assertThat(manager.tryInstallNewTransactionsSchemaVersion(2)).isTrue(); fastForwardTimestampByOneHundredMillion(); assertThat(manager.getTransactionsSchemaVersion(timestamps.getFreshTimestamp())).isEqualTo(2); assertThat(manager.tryInstallNewTransactionsSchemaVersion(1)).isTrue(); fastForwardTimestampByOneHundredMillion(); assertThat(manager.getTransactionsSchemaVersion(timestamps.getFreshTimestamp())).isEqualTo(1); }
private void fastForwardTimestampByOneHundredMillion() { long currentTimestamp = timestamps.getFreshTimestamp(); timestamps.fastForwardTimestamp(currentTimestamp + ONE_HUNDRED_MILLION); } }
@Test public void canReadMultipleAgreedValuesEvenAfterAdditionalCoordinations() { timestampService.fastForwardTimestamp(COORDINATION_QUANTUM); writeTransactionService.putUnlessExists(1L, 8L); writeTransactionService.putUnlessExists(COORDINATION_QUANTUM + 1L, COORDINATION_QUANTUM + 5L); assertThat(readOnlyTransactionService.get( ImmutableList.of(1L, COORDINATION_QUANTUM + 1L, 2 * COORDINATION_QUANTUM + 1L))) .isEqualTo(ImmutableMap.of(1L, 8L, COORDINATION_QUANTUM + 1L, COORDINATION_QUANTUM + 5L)); } }
@Test public void canReadAlreadyAgreedValuesEvenAfterAdditionalCoordinations() { writeTransactionService.putUnlessExists(1L, 8L); assertThat(readOnlyTransactionService.get(1L)).isEqualTo(8L); assertThat(readOnlyTransactionService.get(8L)).isNull(); assertThat(readOnlyTransactionService.get(COORDINATION_QUANTUM + 1L)).isNull(); timestampService.fastForwardTimestamp(COORDINATION_QUANTUM); writeTransactionService.putUnlessExists(COORDINATION_QUANTUM + 1L, COORDINATION_QUANTUM + 5L); assertThat(readOnlyTransactionService.get(COORDINATION_QUANTUM + 1L)).isEqualTo(COORDINATION_QUANTUM + 5L); assertThat(readOnlyTransactionService.get(Long.MAX_VALUE)).isNull(); }
@Override public TimestampService createTimestampService( KeyValueService rawKvs, Optional<TableReference> unused, boolean initializeAsync) { if (initializeAsync) { log.warn("Asynchronous initialization not implemented, will initialize synchronously."); } AtlasDbVersion.ensureVersionReported(); return new InMemoryTimestampService(); }
@Test public void newSchemaVersionsCanBeInstalledWithinOneHundredMillionTimestamps() { assertThat(manager.tryInstallNewTransactionsSchemaVersion(2)).isTrue(); fastForwardTimestampByOneHundredMillion(); assertThat(manager.getTransactionsSchemaVersion(timestamps.getFreshTimestamp())).isEqualTo(2); }
protected TransactionManager createAndRegisterManager() { InMemoryTimestampService tsService = new InMemoryTimestampService(); TransactionManager manager = SweepTestUtils.setupTxManager(kvs, tsService, tsService, ssm, txService); tmManager.registerTransactionManager(manager); return manager; }
private long storeStream(PersistentStreamStore store, byte[] bytesToStore, byte[] reference) { final long id = timestampService.getFreshTimestamp(); txManager.runTaskWithRetry(t -> { store.storeStreams(t, ImmutableMap.of(id, new ByteArrayInputStream(bytesToStore))); store.markStreamAsUsed(t, id, reference); return null; }); return id; }
public static TransactionManager setupTxManager(KeyValueService kvs) { InMemoryTimestampService ts = new InMemoryTimestampService(); return setupTxManager( kvs, ts, ts, SweepStrategyManagers.createDefault(kvs), TransactionServices.createForTesting(kvs, ts, false)); }
@Test public void testLookupStreamIdsByHash() throws Exception { final byte[] bytes1 = new byte[2 * StreamTestStreamStore.BLOCK_SIZE_IN_BYTES]; final byte[] bytes2 = new byte[2 * StreamTestStreamStore.BLOCK_SIZE_IN_BYTES]; long id1 = timestampService.getFreshTimestamp(); long id2 = timestampService.getFreshTimestamp(); Random rand = new Random(); rand.nextBytes(bytes1); rand.nextBytes(bytes2); Sha256Hash hash1 = Sha256Hash.computeHash(bytes1); Sha256Hash hash2 = Sha256Hash.computeHash(bytes2); Sha256Hash hash3 = Sha256Hash.EMPTY; ImmutableMap<Long, InputStream> streams = ImmutableMap.of( id1, new ByteArrayInputStream(bytes1), id2, new ByteArrayInputStream(bytes2)); txManager.runTaskWithRetry(t -> defaultStore.storeStreams(t, streams)); Map<Sha256Hash, Long> sha256HashLongMap = txManager.runTaskWithRetry( t -> defaultStore.lookupStreamIdsByHash(t, ImmutableSet.of(hash1, hash2, hash3))); assertEquals(id1, sha256HashLongMap.get(hash1).longValue()); assertEquals(id2, sha256HashLongMap.get(hash2).longValue()); assertNull(sha256HashLongMap.get(hash3)); }
private TransactionManagers.LockAndTimestampServices getLockAndTimestampServices() { InMemoryTimestampService ts = new InMemoryTimestampService(); return TransactionManagers.createLockAndTimestampServices( metricsManager, config, () -> runtimeConfig, environment, LockServiceImpl::create, () -> ts, () -> ts, invalidator, USER_AGENT); }
@Test public void testStoreCopy() { final byte[] bytes = new byte[2 * StreamTestStreamStore.BLOCK_SIZE_IN_BYTES]; Random rand = new Random(); rand.nextBytes(bytes); long id1 = timestampService.getFreshTimestamp(); long id2 = timestampService.getFreshTimestamp(); ImmutableMap<Long, InputStream> streams = ImmutableMap.of( id1, new ByteArrayInputStream(bytes), id2, new ByteArrayInputStream(bytes)); txManager.runTaskWithRetry(t -> defaultStore.storeStreams(t, streams)); Pair<Long, Sha256Hash> idAndHash1 = defaultStore.storeStream(new ByteArrayInputStream(bytes)); Pair<Long, Sha256Hash> idAndHash2 = defaultStore.storeStream(new ByteArrayInputStream(bytes)); assertThat(idAndHash1.getRhSide(), equalTo(idAndHash2.getRhSide())); //verify hashes are the same assertThat(idAndHash1.getLhSide(), not(equalTo(idAndHash2.getLhSide()))); //verify ids are different }
private TransactionManagers.LockAndTimestampServices createLockAndTimestampServicesForConfig( AtlasDbConfig atlasDbConfig, AtlasDbRuntimeConfig atlasDbRuntimeConfig) { InMemoryTimestampService ts = new InMemoryTimestampService(); return TransactionManagers.createLockAndTimestampServices( metricsManager, atlasDbConfig, () -> atlasDbRuntimeConfig, environment, LockServiceImpl::create, () -> ts, () -> ts, invalidator, USER_AGENT); } }
@Test public void testStreamMetadataConflictDeleteFirst() throws Exception { long streamId = timestampService.getFreshTimestamp(); runConflictingTasksConcurrently(streamId, new TwoConflictingTasks() { @Override public void startFirstAndFail(Transaction tx, long streamId) { StreamTestStreamStore ss = StreamTestStreamStore.of(txManager, StreamTestTableFactory.of()); ss.storeStreams(tx, ImmutableMap.of(streamId, new ByteArrayInputStream(new byte[1]))); } @Override public void startSecondAndFinish(Transaction tx, long streamId) { DeletingStreamStore deletingStreamStore = new DeletingStreamStore( StreamTestStreamStore.of(txManager, StreamTestTableFactory.of())); deletingStreamStore.deleteStreams(tx, ImmutableSet.of(streamId)); } }); assertStreamDoesNotExist(streamId); }
@Test public void canFailToInstallNewVersions() { TransactionSchemaManager newManager = createTransactionSchemaManager(new InMemoryTimestampService()); // Always need to seed the default value, if it's not there assertThat(newManager.tryInstallNewTransactionsSchemaVersion(5)).isFalse(); assertThat(newManager.tryInstallNewTransactionsSchemaVersion(5)).isTrue(); }
@Test public void testStreamMetadataConflictWriteFirst() throws Exception { long streamId = timestampService.getFreshTimestamp(); runConflictingTasksConcurrently(streamId, new TwoConflictingTasks() { @Override public void startFirstAndFail(Transaction tx, long streamId) { DeletingStreamStore deletingStreamStore = new DeletingStreamStore( StreamTestStreamStore.of(txManager, StreamTestTableFactory.of())); deletingStreamStore.deleteStreams(tx, ImmutableSet.of(streamId)); } @Override public void startSecondAndFinish(Transaction tx, long streamId) { StreamTestStreamStore ss = StreamTestStreamStore.of(txManager, StreamTestTableFactory.of()); ss.storeStreams(tx, ImmutableMap.of(streamId, new ByteArrayInputStream(new byte[1]))); } }); Optional<InputStream> stream = getStream(streamId); assertTrue(stream.isPresent()); assertNotNull(stream.get()); }
@Override public TimestampService createTimestampService( KeyValueService rawKvs, Optional<TableReference> unused, boolean initializeAsync) { AtlasDbVersion.ensureVersionReported(); if (initializeAsync) { return AsyncInitializeableInMemoryTimestampService.initializeWhenKvsIsReady(rawKvs); } return new InMemoryTimestampService(); } }
@Test public void testImmutableTs() throws Exception { final long firstTs = timestampService.getFreshTimestamp(); long startTs = txManager.runTaskThrowOnConflict(t -> { Assert.assertTrue(firstTs < txManager.getImmutableTimestamp()); Assert.assertTrue(txManager.getImmutableTimestamp() < t.getTimestamp()); Assert.assertTrue(t.getTimestamp() < timestampService.getFreshTimestamp()); return t.getTimestamp(); }); Assert.assertTrue(firstTs < txManager.getImmutableTimestamp()); Assert.assertTrue(startTs < txManager.getImmutableTimestamp()); }