use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.
the class DatabasePerformanceTest method testTimeSeriesDbPerformance.
@Test
public void testTimeSeriesDbPerformance() throws IncompleteUpdateFoundException {
final ATimeSeriesDB<String, FDate> table = new ATimeSeriesDB<String, FDate>("testTimeSeriesDbPerformance") {
@Override
protected Serde<FDate> newValueSerde() {
return FDateSerde.GET;
}
@Override
protected Integer newFixedLength() {
return FDateSerde.FIXED_LENGTH;
}
@Override
protected String hashKeyToString(final String key) {
return "testTimeSeriesDbPerformance_" + key;
}
@Override
protected FDate extractTime(final FDate value) {
return value;
}
};
final Instant writesStart = new Instant();
final ATimeSeriesUpdater<String, FDate> updater = new ATimeSeriesUpdater<String, FDate>(HASH_KEY, table) {
@Override
protected ICloseableIterable<FDate> getSource(final FDate updateFrom) {
return newValues();
}
@Override
protected void onUpdateFinished(final Instant updateStart) {
printProgress("WritesFinished", writesStart, VALUES, VALUES);
}
@Override
protected void onUpdateStart() {
}
@Override
protected FDate extractTime(final FDate element) {
return element;
}
@Override
protected FDate extractEndTime(final FDate element) {
return element;
}
@Override
protected void onFlush(final int flushIndex, final Instant flushStart, final ATimeSeriesUpdater<String, FDate>.UpdateProgress updateProgress) {
printProgress("Writes", writesStart, updateProgress.getCount() * flushIndex, VALUES);
}
};
Assertions.checkTrue(updater.update());
final Instant readsStart = new Instant();
for (int reads = 1; reads <= READS; reads++) {
FDate prevValue = null;
final ICloseableIterator<? extends FDate> range = table.rangeValues(HASH_KEY, null, null);
int count = 0;
while (true) {
try {
final FDate value = range.next();
if (prevValue != null) {
Assertions.checkTrue(prevValue.isBefore(value));
}
prevValue = value;
count++;
} catch (final NoSuchElementException e) {
break;
}
}
Assertions.checkEquals(count, VALUES);
printProgress("Reads", readsStart, VALUES * reads, VALUES * READS);
}
printProgress("ReadsFinished", readsStart, VALUES * READS, VALUES * READS);
}
use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.
the class ADataUpdater method doUpdate.
protected final FDate doUpdate() throws IncompleteUpdateFoundException {
final ATimeSeriesUpdater<K, V> updater = new ATimeSeriesUpdater<K, V>(key, getTable()) {
private static final int BATCH_LOG_INTERVAL = 100_000 / BATCH_FLUSH_INTERVAL;
@GuardedBy("this")
private Integer lastFlushIndex;
@GuardedBy("this")
private Duration flushDuration;
@GuardedBy("this")
private long flushElementCount;
@GuardedBy("this")
private FDate lastFlushMaxTime;
@GuardedBy("this")
private Instant lastFlushTime;
@Override
protected FDate extractTime(final V element) {
return ADataUpdater.this.extractTime(element);
}
@Override
protected FDate extractEndTime(final V element) {
return ADataUpdater.this.extractEndTime(element);
}
@Override
protected ICloseableIterable<? extends V> getSource(final FDate updateFrom) {
final ICloseableIterable<? extends V> downloadElements = downloadElements(key, updateFrom);
return downloadElements;
}
@Override
protected void onUpdateStart() {
log.info("Updating %s for [%s]", getElementsName(), keyToString(key));
}
@Override
protected synchronized void onFlush(final int flushIndex, final Instant flushStart, final UpdateProgress progress) {
lastFlushIndex = Integers.max(lastFlushIndex, flushIndex);
if (flushDuration == null) {
flushDuration = flushStart.toDuration();
} else {
flushDuration = flushDuration.add(flushStart.toDuration());
}
flushElementCount += progress.getCount();
lastFlushMaxTime = FDates.max(lastFlushMaxTime, progress.getMaxTime());
if (flushIndex % BATCH_LOG_INTERVAL == 0) {
logFlush();
}
}
private void logFlush() {
Assertions.assertThat(lastFlushIndex).isNotNull();
// if we are too fast, only print status once a second
if (lastFlushTime == null || lastFlushTime.toDuration().isGreaterThan(Duration.ONE_SECOND)) {
log.info("Persisted %s. %s batch for [%s]. Reached time [%s]. Processed [%s] during %s", lastFlushIndex, getElementsName(), keyToString(key), lastFlushMaxTime, new ProcessedEventsRateString(flushElementCount, flushDuration), flushDuration);
lastFlushTime = new Instant();
}
lastFlushIndex = null;
flushDuration = null;
flushElementCount = 0;
lastFlushMaxTime = null;
}
@Override
protected synchronized void onUpdateFinished(final Instant updateStart) {
if (lastFlushIndex != null) {
logFlush();
}
log.info("Finished updating %s %s for [%s] from [%s] to [%s] after %s", getCount(), getElementsName(), keyToString(key), getMinTime(), getMaxTime(), updateStart);
}
};
updater.update();
return updater.getMaxTime();
}
use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.
the class ChronicleMapPerformanceTest method readIterator.
private void readIterator(final ChronicleMap<Long, Long> table) throws InterruptedException {
final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
final Instant readsStart = new Instant();
for (int reads = 1; reads <= READS; reads++) {
// FDate prevValue = null;
final Iterator<Long> range = table.values().iterator();
int count = 0;
while (true) {
try {
final FDate value = new FDate(range.next());
// if (prevValue != null) {
// Assertions.checkTrue(prevValue.isBefore(value));
// }
// prevValue = value;
count++;
} catch (final NoSuchElementException e) {
break;
}
}
Assertions.checkEquals(count, VALUES);
if (loopCheck.check()) {
printProgress("Reads", readsStart, VALUES * reads, VALUES * READS);
}
}
printProgress("ReadsFinished", readsStart, VALUES * READS, VALUES * READS);
}
use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.
the class PersistentChronicleMapPerformanceTest method testChronicleMapPerformance.
@Test
public void testChronicleMapPerformance() throws InterruptedException {
@SuppressWarnings("resource") final APersistentMap<FDate, FDate> table = new APersistentMap<FDate, FDate>("testChronicleMapPerformance") {
@Override
public File getBaseDirectory() {
return ContextProperties.TEMP_DIRECTORY;
}
@Override
public ISerde<FDate> newKeySerde() {
return FDateSerde.GET;
}
@Override
public ISerde<FDate> newValueSerde() {
return FDateSerde.GET;
}
@Override
protected IPersistentMapFactory<FDate, FDate> newFactory() {
return new PersistentChronicleMapFactory<FDate, FDate>() {
@SuppressWarnings("rawtypes")
@Override
protected ChronicleMapBuilder configureChronicleMap(final IPersistentMapConfig<FDate, FDate> config, final ChronicleMapBuilder builder) {
return builder.averageKeySize(FDate.BYTES).averageValueSize(FDate.BYTES).entries(VALUES);
}
};
}
};
final LoopInterruptedCheck loopCheck = new LoopInterruptedCheck(Duration.ONE_SECOND);
final Instant writesStart = new Instant();
int i = 0;
for (final FDate date : newValues()) {
table.put(date, date);
i++;
if (i % FLUSH_INTERVAL == 0) {
if (loopCheck.check()) {
printProgress("Writes", writesStart, i, VALUES);
}
}
}
printProgress("WritesFinished", writesStart, VALUES, VALUES);
readIterator(table);
readGet(table);
table.deleteTable();
}
use of de.invesdwin.util.time.Instant in project invesdwin-context-persistence by subes.
the class ATimeSeriesUpdater method update.
@Override
public final boolean update() throws IncompleteUpdateFoundException {
final ILock writeLock = table.getTableLock(key).writeLock();
try {
if (!writeLock.tryLock(1, TimeUnit.MINUTES)) {
throw Locks.getLockTrace().handleLockException(writeLock.getName(), new RetryLaterRuntimeException("Write lock could not be acquired for table [" + table.getName() + "] and key [" + key + "]. Please ensure all iterators are closed!"));
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
final File updateLockSyncFile = new File(updateLockFile.getAbsolutePath() + ".sync");
try (FileChannelLock updateLockSyncFileLock = new FileChannelLock(updateLockSyncFile)) {
if (updateLockSyncFile.exists() || !updateLockSyncFileLock.tryLock() || updateLockFile.exists()) {
throw new IncompleteUpdateFoundException("Incomplete update found for table [" + table.getName() + "] and key [" + key + "], need to clean everything up to restore all from scratch.");
}
try {
try {
Files.touch(updateLockFile);
} catch (final IOException e) {
throw new RuntimeException(e);
}
final Instant updateStart = new Instant();
onUpdateStart();
doUpdate();
onUpdateFinished(updateStart);
Assertions.assertThat(updateLockFile.delete()).isTrue();
return true;
} catch (final Throwable t) {
final IncompleteUpdateFoundException incompleteException = Throwables.getCauseByType(t, IncompleteUpdateFoundException.class);
if (incompleteException != null) {
throw incompleteException;
} else {
throw new IncompleteUpdateFoundException("Something unexpected went wrong", t);
}
}
} finally {
writeLock.unlock();
}
}
Aggregations