@Test public void testDeleteOldRowsIfTooMany() { final StatementLog log = insert1Log(); assertEquals(1, countRowsInTable("statement_log")); repositoryUpdate.deleteOldRowsIfTooMany(); assertEquals(1, countRowsInTable("statement_log")); assertNull(repositoryUpdate.getLastLostMessageTime()); repositoryUpdate.setLastLostMessageTime(System.currentTimeMillis() - 1); for (int i = 1; i < 2 * LogRepositoryUpdateJdbc.NB_ROWS_MAX; i++) { final StatementLog newLog = new StatementLog(log.getConnectionUuid(), randomUUID(), System.currentTimeMillis(), StatementType.BASE_NON_PREPARED_STMT, "myrawsql" + i, Thread.currentThread().getName(), i, i % 2 == 0, i % 4); repositoryUpdate.addStatementLog(newLog); } assertNotNull(repositoryUpdate.getLastLostMessageTime()); assertEquals(2 * LogRepositoryUpdateJdbc.NB_ROWS_MAX, countRowsInTable("statement_log")); repositoryUpdate.deleteOldRowsIfTooMany(); assertTrue(countRowsInTable("statement_log") <= LogRepositoryUpdateJdbc.NB_ROWS_MAX); assertNull(repositoryUpdate.getLastLostMessageTime()); }
public void deleteOldRowsIfTooMany() { final long startTime = System.currentTimeMillis(); LOGGER.debug("searching for most recent timestamp of log statements to delete"); try (Connection connection = createDbConnection(dbName)) { try (PreparedStatement selectTstampStmt = connection.prepareStatement( "select tstamp from statement_log order by tstamp desc limit 1 offset " + NB_ROWS_MAX); ResultSet tstampResultSet = selectTstampStmt.executeQuery()) { if (tstampResultSet.next()) { final Timestamp timestamp = tstampResultSet.getTimestamp(1); int nbRowsDeleted; { LOGGER.debug("Will delete all log statements earlier than {}", timestamp); try (PreparedStatement deleteStmt = connection .prepareStatement("delete from statement_log where tstamp <= ?")) { deleteStmt.setTimestamp(1, timestamp); nbRowsDeleted = deleteStmt.executeUpdate(); LOGGER.debug("Deleted {} old statements", nbRowsDeleted); } } if (nbRowsDeleted > 0) { try (Statement deleteStmt = connection.createStatement()) { nbRowsDeleted = deleteStmt .executeUpdate("delete from batched_statement_log where logId not in " + "(select logId from statement_log)"); LOGGER.debug("Deleted {} old batched_statements", nbRowsDeleted); lastModificationTime = System.currentTimeMillis(); } } final Long lastLostMessageTime2 = lastLostMessageTime; if (lastLostMessageTime2 != null && lastLostMessageTime2 < timestamp.getTime()) { setLastLostMessageTime(null); } } } LOGGER.debug("Peformed deleteOldRowsIfTooMany in {}ms", System.currentTimeMillis() - startTime); } catch (final SQLException e) { throw new RuntimeException(e); } }
LogRepositoryUpdateJdbc implements LogRepositoryUpdate { public void deleteOldRowsIfTooMany() { final long startTime = System.currentTimeMillis(); LOGGER.debug("searching for most recent timestamp of log statements to delete"); try (Connection connection = createDbConnection(dbName)) { try (PreparedStatement selectTstampStmt = connection.prepareStatement( "select tstamp from statement_log order by tstamp desc limit 1 offset " + NB_ROWS_MAX); ResultSet tstampResultSet = selectTstampStmt.executeQuery()) { if (tstampResultSet.next()) { final Timestamp timestamp = tstampResultSet.getTimestamp(1); int nbRowsDeleted; { LOGGER.debug("Will delete all log statements earlier than {}", timestamp); try (PreparedStatement deleteStmt = connection .prepareStatement("delete from statement_log where tstamp <= ?")) { deleteStmt.setTimestamp(1, timestamp); nbRowsDeleted = deleteStmt.executeUpdate(); LOGGER.debug("Deleted {} old statements", nbRowsDeleted); } } if (nbRowsDeleted > 0) { try (Statement deleteStmt = connection.createStatement()) { nbRowsDeleted = deleteStmt .executeUpdate("delete from batched_statement_log where logId not in " + "(select logId from statement_log)"); LOGGER.debug("Deleted {} old batched_statements", nbRowsDeleted); lastModificationTime = System.currentTimeMillis(); } } final Long lastLostMessageTime2 = lastLostMessageTime; if (lastLostMessageTime2 != null && lastLostMessageTime2 < timestamp.getTime()) { setLastLostMessageTime(null); } } } LOGGER.debug("Peformed deleteOldRowsIfTooMany in {}ms", System.currentTimeMillis() - startTime); } catch (final SQLException e) { throw new RuntimeException(e); } } }
LogRepositoryUpdateJdbc implements LogRepositoryUpdate { public void deleteOldRowsIfTooMany() { final long startTime = System.currentTimeMillis(); LOGGER.debug("searching for most recent timestamp of log statements to delete"); try (Connection connection = createDbConnection(dbName)) { try (PreparedStatement selectTstampStmt = connection.prepareStatement( "select tstamp from statement_log order by tstamp desc limit 1 offset " + NB_ROWS_MAX); ResultSet tstampResultSet = selectTstampStmt.executeQuery()) { if (tstampResultSet.next()) { final Timestamp timestamp = tstampResultSet.getTimestamp(1); int nbRowsDeleted; { LOGGER.debug("Will delete all log statements earlier than {}", timestamp); try (PreparedStatement deleteStmt = connection .prepareStatement("delete from statement_log where tstamp <= ?")) { deleteStmt.setTimestamp(1, timestamp); nbRowsDeleted = deleteStmt.executeUpdate(); LOGGER.debug("Deleted {} old statements", nbRowsDeleted); } } if (nbRowsDeleted > 0) { try (Statement deleteStmt = connection.createStatement()) { nbRowsDeleted = deleteStmt .executeUpdate("delete from batched_statement_log where logId not in " + "(select logId from statement_log)"); LOGGER.debug("Deleted {} old batched_statements", nbRowsDeleted); lastModificationTime = System.currentTimeMillis(); } } final Long lastLostMessageTime2 = lastLostMessageTime; if (lastLostMessageTime2 != null && lastLostMessageTime2 < timestamp.getTime()) { setLastLostMessageTime(null); } } } LOGGER.debug("Peformed deleteOldRowsIfTooMany in {}ms", System.currentTimeMillis() - startTime); } catch (final SQLException e) { throw new RuntimeException(e); } } LogRepositoryUpdateJdbc(final String name); }
LogRepositoryUpdateJdbc implements LogRepositoryUpdate { public void deleteOldRowsIfTooMany() { final long startTime = System.currentTimeMillis(); LOGGER.debug("searching for most recent timestamp of log statements to delete"); try (Connection connection = createDbConnection(dbName)) { try (PreparedStatement selectTstampStmt = connection.prepareStatement( "select tstamp from statement_log order by tstamp desc limit 1 offset " + NB_ROWS_MAX); ResultSet tstampResultSet = selectTstampStmt.executeQuery()) { if (tstampResultSet.next()) { final Timestamp timestamp = tstampResultSet.getTimestamp(1); int nbRowsDeleted; { LOGGER.debug("Will delete all log statements earlier than {}", timestamp); try (PreparedStatement deleteStmt = connection .prepareStatement("delete from statement_log where tstamp <= ?")) { deleteStmt.setTimestamp(1, timestamp); nbRowsDeleted = deleteStmt.executeUpdate(); LOGGER.debug("Deleted {} old statements", nbRowsDeleted); } } if (nbRowsDeleted > 0) { try (Statement deleteStmt = connection.createStatement()) { nbRowsDeleted = deleteStmt .executeUpdate("delete from batched_statement_log where logId not in " + "(select logId from statement_log)"); LOGGER.debug("Deleted {} old batched_statements", nbRowsDeleted); lastModificationTime = System.currentTimeMillis(); } } final Long lastLostMessageTime2 = lastLostMessageTime; if (lastLostMessageTime2 != null && lastLostMessageTime2 < timestamp.getTime()) { setLastLostMessageTime(null); } } } LOGGER.debug("Peformed deleteOldRowsIfTooMany in {}ms", System.currentTimeMillis() - startTime); } catch (final SQLException e) { throw new RuntimeException(e); } } LogRepositoryUpdateJdbc(final String name); @Override void dispose(); @Override synchronized void addStatementLog(final StatementLog log); @Override synchronized void addStatementFullyExecutedLog(final Collection<StatementFullyExecutedLog> logs); @Override synchronized void updateLogAfterExecution(final StatementExecutedLog log); @Override synchronized void updateLogWithResultSetLog(final ResultSetLog log); @Override synchronized void addBatchedPreparedStatementsLog(final BatchedPreparedStatementsLog log); @Override synchronized void addBatchedNonPreparedStatementsLog(final BatchedNonPreparedStatementsLog log); @Override synchronized void addConnection(final ConnectionInfo connectionInfo); @Override synchronized void addTxCompletionLog(final TxCompleteLog log); @Override void clear(); @Override void deleteStatementLog(final long... logIds); void deleteOldRowsIfTooMany(); @Override synchronized long getLastModificationTime(); @Override void setLastLostMessageTime(@Nullable final Long timestamp); @Override @Nullable Long getLastLostMessageTime(); }
LogRepositoryUpdateJdbc implements LogRepositoryUpdate { public void deleteOldRowsIfTooMany() { final long startTime = System.currentTimeMillis(); LOGGER.debug("searching for most recent timestamp of log statements to delete"); try (Connection connection = createDbConnection(dbName)) { try (PreparedStatement selectTstampStmt = connection.prepareStatement( "select tstamp from statement_log order by tstamp desc limit 1 offset " + NB_ROWS_MAX); ResultSet tstampResultSet = selectTstampStmt.executeQuery()) { if (tstampResultSet.next()) { final Timestamp timestamp = tstampResultSet.getTimestamp(1); int nbRowsDeleted; { LOGGER.debug("Will delete all log statements earlier than {}", timestamp); try (PreparedStatement deleteStmt = connection .prepareStatement("delete from statement_log where tstamp <= ?")) { deleteStmt.setTimestamp(1, timestamp); nbRowsDeleted = deleteStmt.executeUpdate(); LOGGER.debug("Deleted {} old statements", nbRowsDeleted); } } if (nbRowsDeleted > 0) { try (Statement deleteStmt = connection.createStatement()) { nbRowsDeleted = deleteStmt .executeUpdate("delete from batched_statement_log where logId not in " + "(select logId from statement_log)"); LOGGER.debug("Deleted {} old batched_statements", nbRowsDeleted); lastModificationTime = System.currentTimeMillis(); } } final Long lastLostMessageTime2 = lastLostMessageTime; if (lastLostMessageTime2 != null && lastLostMessageTime2 < timestamp.getTime()) { setLastLostMessageTime(null); } } } LOGGER.debug("Peformed deleteOldRowsIfTooMany in {}ms", System.currentTimeMillis() - startTime); } catch (final SQLException e) { throw new RuntimeException(e); } } LogRepositoryUpdateJdbc(final String name); @Override void dispose(); @Override synchronized void addStatementLog(final StatementLog log); @Override synchronized void addStatementFullyExecutedLog(final Collection<StatementFullyExecutedLog> logs); @Override synchronized void updateLogAfterExecution(final StatementExecutedLog log); @Override synchronized void updateLogWithResultSetLog(final ResultSetLog log); @Override synchronized void addBatchedPreparedStatementsLog(final BatchedPreparedStatementsLog log); @Override synchronized void addBatchedNonPreparedStatementsLog(final BatchedNonPreparedStatementsLog log); @Override synchronized void addConnection(final ConnectionInfo connectionInfo); @Override synchronized void addTxCompletionLog(final TxCompleteLog log); @Override void clear(); @Override void deleteStatementLog(final long... logIds); void deleteOldRowsIfTooMany(); @Override synchronized long getLastModificationTime(); @Override void setLastLostMessageTime(@Nullable final Long timestamp); @Override @Nullable Long getLastLostMessageTime(); static final int SCHEMA_VERSION; }