State implements Serializable, Cloneable { public State updateLocalStateWithNewExecTimes(MonitorRunLogPrefix logPrefix, long mainDbStmtExecTimeNanos, long failoverDbStmtExecTimeNanos) { State copy = clone(); try { copy.doUpdateLocalStateWithNewExecTimes( logPrefix, mainDbStmtExecTimeNanos, failoverDbStmtExecTimeNanos ); } catch (RuntimeException e) { logger.error(logPrefix + "exception (shown below) while updating state - " + "reverted all changes made during this operation", e); return this.clone(); } return updateLocalStateWithNewExecTimes(copy); } State(SqlTimeBasedLoadFactory loadFactory,
                 String host,
                 String repoId,
                 int sqlTimesIncludedInAverage,
                 boolean exceptionsIgnoredAfterRecovery,
                 boolean recoveryErasesHistoryIfExceptionsIgnoredAfterRecovery,
                 int currentToUnusedRatio,
                 int backOffMultiplier,
                 int backOffMaxRatio,
                 String mainDbName,
                 String failoverDbName);  State(String host,
                 String repoId,
                 long modifyTimeMillis,
                 boolean failover,
                 long lastMainQueryTimeNanos,
                 long lastFailoverQueryTimeNanos); @Override State clone(); long getModifyTimeMillis(); Average getAvg(); Average getAvgFailover(); MachineState getMachineState(); String getHost(); String getRepoId(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); State updateLocalStateWithNewExecTimes(MonitorRunLogPrefix logPrefix,
                                                  long mainDbStmtExecTimeNanos,
                                                  long failoverDbStmtExecTimeNanos); void doUpdateLocalStateWithNewExecTimes(MonitorRunLogPrefix logPrefix,
                                                   long mainDbStmtExecTimeNanos,
                                                   long failoverDbStmtExecTimeNanos); State copyFrom(MonitorRunLogPrefix logPrefixIfLocalState, State localOrRemote); boolean sqlTimeIsMeasuredInThisCycle(boolean failover); boolean sqlTimeIsMeasuredInThisCycle(MonitorRunLogPrefix logPrefix, boolean failover); int getPeriod(boolean failover); int getCycleModuloPeriod(boolean failover); Load getLoadAfterLastMeasurement(); String getDesc(boolean failover); final static long notMeasuredInThisCycle; }
@Test public void shouldDecreaseLoadOfFailoverDatabaseIfUnusedAfterFirstMeasurement() { int periodWhenUnused = 2; State s = createInitialLocalState(periodWhenUnused, 1); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, low1, low1); assertState(s, true, false, false, normalPeriod, periodWhenUnused, 0, 1); }
@Test public void shouldDecreaseLoadOfMainDatabaseIfUnusedAfterFirstMeasurement() { int periodWhenUnused = 2; int backOffMultiplier = 3; State s = createInitialLocalState(periodWhenUnused, backOffMultiplier); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, high1, low1); assertState(s, false, true, true, backOffMultiplier, normalPeriod, 1, 0); }
@Test public void shouldStartToMonitorFailoverDbThatWasNotUsedIfMainDbBecomesNotOk() { int periodWhenUnused = 5; int backOffMultiplier = 3; State s = createInitialLocalState(periodWhenUnused, backOffMultiplier); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, medium1, low1); assertState(s, true, false, false, normalPeriod, periodWhenUnused, 0, 1); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, high1, State.notMeasuredInThisCycle); assertState(s, false, true, true, backOffMultiplier, normalPeriod, 1, 0); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, State.notMeasuredInThisCycle, low2); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, State.notMeasuredInThisCycle, low2); assertState(s, true, true, true, backOffMultiplier, normalPeriod, 0, 0); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, medium1, low2); assertState(s, false, true, true, periodWhenUnused, normalPeriod, 1, 0); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, State.notMeasuredInThisCycle, high1); assertState(s, true, false, false, normalPeriod, backOffMultiplier, 0, 1); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, high2, State.notMeasuredInThisCycle); assertState(s, false, false, true, backOffMultiplier, backOffMultiplier, 1, 2); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, State.notMeasuredInThisCycle, State.notMeasuredInThisCycle); assertState(s, false, true, true, backOffMultiplier, backOffMultiplier, 2, 0); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, State.notMeasuredInThisCycle, high2); assertState(s, true, false, false, backOffMultiplier, backOffMultiplier * backOffMultiplier, 0, 1); s.updateLocalStateWithNewExecTimes(emptyLogPrefix, medium1, State.notMeasuredInThisCycle); assertState(s, true, false, false, normalPeriod, backOffMultiplier * backOffMultiplier, 0, 2); }