private void fastForwardTimestampByOneHundredMillion() { long currentTimestamp = timestamps.getFreshTimestamp(); timestamps.fastForwardTimestamp(currentTimestamp + ONE_HUNDRED_MILLION); } }
@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 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; }
@Test public void newSchemaVersionsCanBeInstalledWithinOneHundredMillionTimestamps() { assertThat(manager.tryInstallNewTransactionsSchemaVersion(2)).isTrue(); fastForwardTimestampByOneHundredMillion(); assertThat(manager.getTransactionsSchemaVersion(timestamps.getFreshTimestamp())).isEqualTo(2); }
@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 }
@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)); }
@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 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()); }
@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()); }
timestampService.getFreshTimestamp(); final long startTs = timestampService.getFreshTimestamp(); final long transactionTs = timestampService.getFreshTimestamp(); keyValueService.put(TABLE, ImmutableMap.of(cell, PtBytes.EMPTY_BYTE_ARRAY), startTs);
timestampService.getFreshTimestamp(); final long startTs = timestampService.getFreshTimestamp(); final long transactionTs = timestampService.getFreshTimestamp(); keyValueService.put(TABLE, ImmutableMap.of(cell, rowName), startTs);