Search in sources :

Example 51 with Instant

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);
}
Also used : ATimeSeriesUpdater(de.invesdwin.context.persistence.leveldb.timeseries.ATimeSeriesUpdater) ATimeSeriesDB(de.invesdwin.context.persistence.leveldb.timeseries.ATimeSeriesDB) Instant(de.invesdwin.util.time.Instant) ProcessedEventsRateString(de.invesdwin.util.lang.ProcessedEventsRateString) FDate(de.invesdwin.util.time.fdate.FDate) NoSuchElementException(java.util.NoSuchElementException) ATest(de.invesdwin.context.test.ATest) Test(org.junit.Test)

Example 52 with Instant

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();
}
Also used : ProcessedEventsRateString(de.invesdwin.util.lang.ProcessedEventsRateString) Instant(de.invesdwin.util.time.Instant) Duration(de.invesdwin.util.time.duration.Duration) FDate(de.invesdwin.util.time.fdate.FDate)

Example 53 with Instant

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);
}
Also used : Instant(de.invesdwin.util.time.Instant) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate) NoSuchElementException(java.util.NoSuchElementException)

Example 54 with Instant

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();
}
Also used : APersistentMap(de.invesdwin.context.integration.persistentmap.APersistentMap) ChronicleMapBuilder(net.openhft.chronicle.map.ChronicleMapBuilder) Instant(de.invesdwin.util.time.Instant) IPersistentMapConfig(de.invesdwin.context.integration.persistentmap.IPersistentMapConfig) LoopInterruptedCheck(de.invesdwin.util.concurrent.loop.LoopInterruptedCheck) FDate(de.invesdwin.util.time.date.FDate) PersistentChronicleMapFactory(de.invesdwin.context.persistence.chronicle.PersistentChronicleMapFactory) Test(org.junit.jupiter.api.Test)

Example 55 with Instant

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();
    }
}
Also used : FileChannelLock(de.invesdwin.util.concurrent.lock.FileChannelLock) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) Instant(de.invesdwin.util.time.Instant) RetryLaterRuntimeException(de.invesdwin.context.integration.retry.RetryLaterRuntimeException) IOException(java.io.IOException) ILock(de.invesdwin.util.concurrent.lock.ILock) IncompleteUpdateFoundException(de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException) File(java.io.File)

Aggregations

Instant (de.invesdwin.util.time.Instant)93 FDate (de.invesdwin.util.time.date.FDate)78 LoopInterruptedCheck (de.invesdwin.util.concurrent.loop.LoopInterruptedCheck)65 NoSuchElementException (java.util.NoSuchElementException)30 Test (org.junit.jupiter.api.Test)25 File (java.io.File)16 PreparedStatement (java.sql.PreparedStatement)16 ResultSet (java.sql.ResultSet)15 Statement (java.sql.Statement)12 ATest (de.invesdwin.context.test.ATest)8 RecordFile (com.indeed.lsmtree.recordlog.RecordFile)6 ProcessedEventsRateString (de.invesdwin.util.lang.ProcessedEventsRateString)6 TimedDecimal (de.invesdwin.util.math.decimal.TimedDecimal)5 FDate (de.invesdwin.util.time.fdate.FDate)5 IOException (java.io.IOException)5 Connection (java.sql.Connection)5 APersistentMap (de.invesdwin.context.integration.persistentmap.APersistentMap)4 ATimeSeriesUpdater (de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater)4 Test (org.junit.Test)4 SnappyCodec (com.indeed.util.compress.SnappyCodec)3