Old extension data removed after configurable time

Affects issues:
- Close #1131
This commit is contained in:
Rsl1122 2019-09-26 18:24:40 +03:00
parent 1b652952cf
commit a567d87cdd
5 changed files with 185 additions and 0 deletions

View File

@ -36,6 +36,7 @@ public class TimeSettings {
public static final Setting<Long> DELETE_INACTIVE_PLAYERS_AFTER = new TimeSetting("Time.Thresholds.Remove_inactive_player_data_after");
public static final Setting<Long> DELETE_TPS_DATA_AFTER = new TimeSetting("Time.Thresholds.Remove_time_series_data_after");
public static final Setting<Long> DELETE_PING_DATA_AFTER = new TimeSetting("Time.Thresholds.Remove_ping_data_after");
public static final Setting<Long> DELETE_EXTENSION_DATA_AFTER = new TimeSetting("Time.Thresholds.Remove_disabled_extension_data_after");
public static final Setting<Long> EXTENSION_DATA_REFRESH_PERIOD = new TimeSetting("Time.Periodic_tasks.Extension_data_refresh_every");
public static final Setting<Long> CLEAN_DATABASE_PERIOD = new TimeSetting("Time.Periodic_tasks.Clean_Database_every");
public static final Setting<Long> CONFIG_UPDATE_INTERVAL = new TimeSetting("Time.Periodic_tasks.Check_DB_for_server_config_files_every");

View File

@ -0,0 +1,169 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.storage.database.transactions.init;
import com.djrapitops.plan.storage.database.queries.Query;
import com.djrapitops.plan.storage.database.queries.QueryStatement;
import com.djrapitops.plan.storage.database.sql.tables.*;
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
import com.djrapitops.plan.storage.database.transactions.Transaction;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;
import java.util.UUID;
import static com.djrapitops.plan.storage.database.sql.parsing.Sql.*;
/**
* Transaction that removes outdated plugin's data after configurable threshold.
*
* @author Rsl1122
*/
public class RemoveOldExtensionsTransaction extends Transaction {
private final long deleteOlder;
private final UUID serverUUID;
public RemoveOldExtensionsTransaction(long deleteAfterMs, UUID serverUUID) {
deleteOlder = System.currentTimeMillis() - deleteAfterMs;
this.serverUUID = serverUUID;
}
@Override
protected void performOperations() {
for (Integer providerID : query(inactiveProviderIDsQuery())) {
removeValues(providerID);
}
for (Integer providerID : query(inactiveTableProviderIDsQuery())) {
removeTableValues(providerID);
}
removeProviders();
}
private void removeValues(int providerID) {
for (String table : new String[]{
ExtensionPlayerValueTable.TABLE_NAME,
ExtensionServerValueTable.TABLE_NAME,
ExtensionGroupsTable.TABLE_NAME
}) {
execute(new ExecStatement(DELETE_FROM + table + WHERE + "provider_id=?") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, providerID);
}
});
}
}
private void removeTableValues(Integer providerID) {
for (String table : new String[]{
ExtensionPlayerTableValueTable.TABLE_NAME,
ExtensionServerTableValueTable.TABLE_NAME
}) {
execute(new ExecStatement(DELETE_FROM + table + WHERE + "table_id=?") {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setInt(1, providerID);
}
});
}
}
private void removeProviders() {
execute(new ExecStatement(
DELETE_FROM + ExtensionProviderTable.TABLE_NAME +
WHERE + ExtensionProviderTable.PLUGIN_ID +
" IN (" +
SELECT + ExtensionPluginTable.ID +
FROM + ExtensionPluginTable.TABLE_NAME +
WHERE + ExtensionPluginTable.LAST_UPDATED + "<?" +
AND + ExtensionPluginTable.SERVER_UUID + "=?)"
) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, deleteOlder);
statement.setString(2, serverUUID.toString());
}
});
execute(new ExecStatement(
DELETE_FROM + ExtensionTableProviderTable.TABLE_NAME +
WHERE + ExtensionTableProviderTable.PLUGIN_ID +
" IN (" +
SELECT + ExtensionPluginTable.ID +
FROM + ExtensionPluginTable.TABLE_NAME +
WHERE + ExtensionPluginTable.LAST_UPDATED + "<?" +
AND + ExtensionPluginTable.SERVER_UUID + "=?)"
) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, deleteOlder);
statement.setString(2, serverUUID.toString());
}
});
}
private Query<Collection<Integer>> inactiveProviderIDsQuery() {
String sql = SELECT + "pr." + ExtensionProviderTable.ID +
FROM + ExtensionProviderTable.TABLE_NAME + " pr" +
INNER_JOIN + ExtensionPluginTable.TABLE_NAME + " pl on pl." + ExtensionPluginTable.ID + "=pr." + ExtensionProviderTable.PLUGIN_ID +
WHERE + ExtensionPluginTable.LAST_UPDATED + "<?" +
AND + ExtensionPluginTable.SERVER_UUID + "=?";
return new QueryStatement<Collection<Integer>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, deleteOlder);
statement.setString(2, serverUUID.toString());
}
@Override
public Collection<Integer> processResults(ResultSet set) throws SQLException {
Collection<Integer> providerIds = new HashSet<>();
while (set.next()) {
providerIds.add(set.getInt(ExtensionProviderTable.ID));
}
return providerIds;
}
};
}
private Query<Collection<Integer>> inactiveTableProviderIDsQuery() {
String sql = SELECT + "pr." + ExtensionTableProviderTable.ID +
FROM + ExtensionTableProviderTable.TABLE_NAME + " pr" +
INNER_JOIN + ExtensionPluginTable.TABLE_NAME + " pl on pl." + ExtensionPluginTable.ID + "=pr." + ExtensionTableProviderTable.PLUGIN_ID +
WHERE + ExtensionPluginTable.LAST_UPDATED + "<?" +
AND + ExtensionPluginTable.SERVER_UUID + "=?";
return new QueryStatement<Collection<Integer>>(sql, 100) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setLong(1, deleteOlder);
statement.setString(2, serverUUID.toString());
}
@Override
public Collection<Integer> processResults(ResultSet set) throws SQLException {
Collection<Integer> providerIds = new HashSet<>();
while (set.next()) {
providerIds.add(set.getInt(ExtensionProviderTable.ID));
}
return providerIds;
}
};
}
}

View File

@ -32,6 +32,7 @@ import com.djrapitops.plan.storage.database.queries.QueryStatement;
import com.djrapitops.plan.storage.database.sql.tables.SessionsTable;
import com.djrapitops.plan.storage.database.transactions.commands.RemovePlayerTransaction;
import com.djrapitops.plan.storage.database.transactions.init.RemoveDuplicateUserInfoTransaction;
import com.djrapitops.plan.storage.database.transactions.init.RemoveOldExtensionsTransaction;
import com.djrapitops.plan.storage.database.transactions.init.RemoveOldSampledDataTransaction;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.console.PluginLogger;
@ -65,6 +66,10 @@ public class DBCleanTask extends AbsRunnable {
private final PluginLogger logger;
private final ErrorHandler errorHandler;
// This variable assumes that the system is thrown away on reload and new one is constructed.
// It is to avoid cleaning extension data that has not been updated after uptime longer than the deletion threshold.
private final long lastReload;
@Inject
public DBCleanTask(
PlanConfig config,
@ -83,6 +88,8 @@ public class DBCleanTask extends AbsRunnable {
this.serverInfo = serverInfo;
this.logger = logger;
this.errorHandler = errorHandler;
lastReload = System.currentTimeMillis();
}
@Override
@ -102,6 +109,10 @@ public class DBCleanTask extends AbsRunnable {
if (removed > 0) {
logger.info(locale.getString(PluginLang.DB_NOTIFY_CLEAN, removed));
}
Long deleteExtensionDataAfter = config.get(TimeSettings.DELETE_EXTENSION_DATA_AFTER);
if (System.currentTimeMillis() - lastReload <= deleteExtensionDataAfter) {
database.executeTransaction(new RemoveOldExtensionsTransaction(deleteExtensionDataAfter, serverInfo.getServerUUID()));
}
}
} catch (DBOpException e) {
errorHandler.log(L.ERROR, this.getClass(), e);

View File

@ -90,6 +90,8 @@ Time:
Unit: DAYS
Remove_ping_data_after: 14
Unit: DAYS
Remove_disabled_extension_data_after: 2
Unit: DAYS
Periodic_tasks:
Extension_data_refresh_every: 1
Unit: HOURS

View File

@ -95,6 +95,8 @@ Time:
Unit: DAYS
Remove_ping_data_after: 14
Unit: DAYS
Remove_disabled_extension_data_after: 2
Unit: DAYS
Periodic_tasks:
Extension_data_refresh_every: 1
Unit: HOURS