Search in sources :

Example 16 with Duration

use of de.invesdwin.util.time.duration.Duration 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 17 with Duration

use of de.invesdwin.util.time.duration.Duration in project invesdwin-context-persistence by subes.

the class ConfiguredHikariCPDataSource method createDelegate.

@Override
protected DataSource createDelegate() {
    final HikariConfig config = new HikariConfig();
    config.setJdbcUrl(context.getConnectionUrl());
    config.setUsername(context.getConnectionUser());
    config.setPassword(context.getConnectionPassword());
    config.setDriverClassName(context.getConnectionDriver());
    config.setIdleTimeout(new Duration(1, FTimeUnit.MINUTES).intValue(FTimeUnit.MILLISECONDS));
    config.setMaximumPoolSize(100);
    config.setMinimumIdle(1);
    enableStatementCache(config);
    final HikariDataSource ds = new HikariDataSource(config);
    Assertions.assertThat(this.closeableDs).isNull();
    this.closeableDs = ds;
    if (logging && PersistenceProperties.IS_P6SPY_AVAILABLE) {
        final com.p6spy.engine.spy.P6DataSource proxy = new com.p6spy.engine.spy.P6DataSource(ds);
        return proxy;
    } else {
        return ds;
    }
}
Also used : HikariDataSource(com.zaxxer.hikari.HikariDataSource) Duration(de.invesdwin.util.time.duration.Duration) HikariConfig(com.zaxxer.hikari.HikariConfig)

Example 18 with Duration

use of de.invesdwin.util.time.duration.Duration in project invesdwin-context-persistence by subes.

the class ConfiguredC3p0DataSource method createDelegate.

@Override
protected DataSource createDelegate() {
    final ComboPooledDataSource ds = new ComboPooledDataSource();
    try {
        ds.setDriverClass(context.getConnectionDriver());
    } catch (final PropertyVetoException e1) {
        throw Err.process(e1);
    }
    ds.setJdbcUrl(context.getConnectionUrl());
    ds.setUser(context.getConnectionUser());
    ds.setPassword(context.getConnectionPassword());
    ds.setMaxStatements(5000);
    ds.setMaxStatementsPerConnection(50);
    // fix apparent deadlocks
    ds.setStatementCacheNumDeferredCloseThreads(1);
    ds.setMaxPoolSize(100);
    ds.setMinPoolSize(1);
    ds.setMaxIdleTime(new Duration(1, FTimeUnit.MINUTES).intValue(FTimeUnit.SECONDS));
    ds.setTestConnectionOnCheckout(true);
    ds.setAutoCommitOnClose(true);
    Assertions.assertThat(this.closeableDs).isNull();
    this.closeableDs = ds;
    if (logging && PersistenceProperties.IS_P6SPY_AVAILABLE) {
        final com.p6spy.engine.spy.P6DataSource proxy = new com.p6spy.engine.spy.P6DataSource(ds);
        return proxy;
    } else {
        return ds;
    }
}
Also used : PropertyVetoException(java.beans.PropertyVetoException) ComboPooledDataSource(com.mchange.v2.c3p0.ComboPooledDataSource) Duration(de.invesdwin.util.time.duration.Duration)

Example 19 with Duration

use of de.invesdwin.util.time.duration.Duration in project invesdwin-context-persistence by subes.

the class ALazyDataUpdater method doUpdate.

protected final FDate doUpdate(final FDate estimatedTo) throws IncompleteUpdateFoundException {
    try {
        final ALoggingTimeSeriesUpdater<K, V> updater = new ALoggingTimeSeriesUpdater<K, V>(key, getTable(), log) {

            @Override
            protected FDate extractEndTime(final V element) {
                return ALazyDataUpdater.this.extractEndTime(element);
            }

            @Override
            protected ICloseableIterable<? extends V> getSource(final FDate updateFrom) {
                final ICloseableIterable<? extends V> downloadElements = ALazyDataUpdater.this.downloadElements(getKey(), updateFrom);
                return downloadElements;
            }

            @Override
            protected String keyToString(final K key) {
                return ALazyDataUpdater.this.keyToString(key);
            }

            @Override
            protected String getElementsName() {
                return ALazyDataUpdater.this.getElementsName();
            }

            @Override
            public Percent getProgress() {
                if (estimatedTo == null) {
                    return null;
                }
                final FDate from = getMinTime();
                if (from == null) {
                    return null;
                }
                final FDate curTime = getMaxTime();
                if (curTime == null) {
                    return null;
                }
                return new Percent(new Duration(from, curTime), new Duration(from, estimatedTo)).orLower(Percent.ONE_HUNDRED_PERCENT);
            }
        };
        final Callable<FDate> task = new Callable<FDate>() {

            @Override
            public FDate call() throws Exception {
                updater.update();
                return updater.getMaxTime();
            }
        };
        final String taskName = "Loading " + getElementsName() + " for " + keyToString(getKey());
        final Callable<Percent> progress = newProgressCallable(estimatedTo, updater);
        return TaskInfoCallable.of(taskName, task, progress).call();
    } catch (final IncompleteUpdateFoundException e) {
        throw e;
    } catch (final Throwable e) {
        throw Throwables.propagate(e);
    }
}
Also used : Percent(de.invesdwin.util.math.decimal.scaled.Percent) Duration(de.invesdwin.util.time.duration.Duration) FDate(de.invesdwin.util.time.date.FDate) Callable(java.util.concurrent.Callable) TaskInfoCallable(de.invesdwin.util.concurrent.taskinfo.provider.TaskInfoCallable) IncompleteUpdateFoundException(de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)

Example 20 with Duration

use of de.invesdwin.util.time.duration.Duration in project invesdwin-context-persistence by subes.

the class ALoggingTimeSeriesUpdater method logFlush.

private void logFlush() {
    Assertions.assertThat(lastFlushIndex).isNotNull();
    // if we are too fast, only print status once a second
    if (lastFlushTime == null || lastFlushTime.isGreaterThan(Duration.ONE_SECOND)) {
        final Duration flushDuration = updateStart.toDuration();
        final Percent progress = getProgress();
        if (progress != null) {
            log.info("Persisted %s. %s batch for [%s]. Reached [%s] at time [%s]. Processed [%s] during %s", lastFlushIndex, getElementsName(), keyToString(getKey()), progress.asScale(PercentScale.PERCENT), lastFlushMaxTime, new ProcessedEventsRateString(flushElementCount, flushDuration), flushDuration);
        } else {
            log.info("Persisted %s. %s batch for [%s]. Reached time [%s]. Processed [%s] during %s", lastFlushIndex, getElementsName(), keyToString(getKey()), lastFlushMaxTime, new ProcessedEventsRateString(flushElementCount, flushDuration), flushDuration);
        }
        lastFlushTime = new Instant();
    }
    lastFlushIndex = null;
    lastFlushMaxTime = null;
}
Also used : ProcessedEventsRateString(de.invesdwin.util.lang.ProcessedEventsRateString) Percent(de.invesdwin.util.math.decimal.scaled.Percent) Instant(de.invesdwin.util.time.Instant) Duration(de.invesdwin.util.time.duration.Duration)

Aggregations

Duration (de.invesdwin.util.time.duration.Duration)34 ProcessedEventsRateString (de.invesdwin.util.lang.ProcessedEventsRateString)16 Percent (de.invesdwin.util.math.decimal.scaled.Percent)14 FDate (de.invesdwin.util.time.date.FDate)13 ICloseableIterable (de.invesdwin.util.collections.iterable.ICloseableIterable)12 AHistoricalCache (de.invesdwin.util.collections.loadingcache.historical.AHistoricalCache)12 ISerde (de.invesdwin.util.marshallers.serde.ISerde)12 TimeRange (de.invesdwin.util.time.range.TimeRange)12 File (java.io.File)12 Instant (de.invesdwin.util.time.Instant)4 ComboPooledDataSource (com.mchange.v2.c3p0.ComboPooledDataSource)2 ATest (de.invesdwin.context.test.ATest)2 IEvaluateGenericFDate (de.invesdwin.util.math.expression.lambda.IEvaluateGenericFDate)2 IFDateProvider (de.invesdwin.util.time.date.IFDateProvider)2 PropertyVetoException (java.beans.PropertyVetoException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Test (org.junit.Test)2 HikariConfig (com.zaxxer.hikari.HikariConfig)1 HikariDataSource (com.zaxxer.hikari.HikariDataSource)1 IncompleteUpdateFoundException (de.invesdwin.context.persistence.timeseriesdb.IncompleteUpdateFoundException)1