Refactored database clean task out of the database

Fixed SonarCloud smells:

Level blocker:
- Removed 'dbType' field from CreateIndexTransaction (is in Transaction)
- Removed unused 'dbSystem' from AnalysisContainer
This commit is contained in:
Rsl1122 2019-02-19 12:51:52 +02:00
parent 0d8c2f2824
commit 4f63a3771b
11 changed files with 108 additions and 55 deletions

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.system.tasks;
import com.djrapitops.plan.Plan;
import com.djrapitops.plan.ShutdownHook;
import com.djrapitops.plan.db.tasks.DBCleanTask;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.bukkit.BukkitTPSCountTimer;
@ -46,6 +47,7 @@ public class BukkitTaskSystem extends ServerTaskSystem {
private final ShutdownHook shutdownHook;
private final PingCountTimerBukkit pingCountTimer;
private final ConfigStoreTask configStoreTask;
private final DBCleanTask dbCleanTask;
@Inject
public BukkitTaskSystem(
@ -60,7 +62,8 @@ public class BukkitTaskSystem extends ServerTaskSystem {
PingCountTimerBukkit pingCountTimer,
LogsFolderCleanTask logsFolderCleanTask,
PlayersPageRefreshTask playersPageRefreshTask,
ConfigStoreTask configStoreTask
ConfigStoreTask configStoreTask,
DBCleanTask dbCleanTask
) {
super(
runnableFactory,
@ -74,6 +77,7 @@ public class BukkitTaskSystem extends ServerTaskSystem {
this.shutdownHook = shutdownHook;
this.pingCountTimer = pingCountTimer;
this.configStoreTask = configStoreTask;
this.dbCleanTask = dbCleanTask;
}
@Override
@ -88,6 +92,11 @@ public class BukkitTaskSystem extends ServerTaskSystem {
// +40 ticks / 2 seconds so that update check task runs first.
long storeDelay = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS) + 40;
registerTask("Config Store Task", configStoreTask).runTaskLaterAsynchronously(storeDelay);
registerTask("DB Clean Task", dbCleanTask).runTaskTimerAsynchronously(
TimeAmount.toTicks(20, TimeUnit.SECONDS),
TimeAmount.toTicks(config.get(TimeSettings.CLEAN_DATABASE_PERIOD), TimeUnit.MILLISECONDS)
);
} catch (ExceptionInInitializerError | NoClassDefFoundError ignore) {
// Running CraftBukkit
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.system.tasks;
import com.djrapitops.plan.PlanBungee;
import com.djrapitops.plan.db.tasks.DBCleanTask;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.bungee.BungeeTPSCountTimer;
@ -43,6 +44,7 @@ public class BungeeTaskSystem extends TaskSystem {
private final LogsFolderCleanTask logsFolderCleanTask;
private final PlayersPageRefreshTask playersPageRefreshTask;
private final NetworkConfigStoreTask networkConfigStoreTask;
private final DBCleanTask dbCleanTask;
@Inject
public BungeeTaskSystem(
@ -54,7 +56,8 @@ public class BungeeTaskSystem extends TaskSystem {
PingCountTimerBungee pingCountTimer,
LogsFolderCleanTask logsFolderCleanTask,
PlayersPageRefreshTask playersPageRefreshTask,
NetworkConfigStoreTask networkConfigStoreTask
NetworkConfigStoreTask networkConfigStoreTask,
DBCleanTask dbCleanTask
) {
super(runnableFactory, bungeeTPSCountTimer);
this.plugin = plugin;
@ -65,6 +68,7 @@ public class BungeeTaskSystem extends TaskSystem {
this.logsFolderCleanTask = logsFolderCleanTask;
this.playersPageRefreshTask = playersPageRefreshTask;
this.networkConfigStoreTask = networkConfigStoreTask;
this.dbCleanTask = dbCleanTask;
}
@Override
@ -87,5 +91,10 @@ public class BungeeTaskSystem extends TaskSystem {
// +40 ticks / 2 seconds so that update check task runs first.
long storeDelay = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS) + 40;
registerTask("Config Store Task", networkConfigStoreTask).runTaskLaterAsynchronously(storeDelay);
registerTask("DB Clean Task", dbCleanTask).runTaskTimerAsynchronously(
TimeAmount.toTicks(20, TimeUnit.SECONDS),
TimeAmount.toTicks(config.get(TimeSettings.CLEAN_DATABASE_PERIOD), TimeUnit.MILLISECONDS)
);
}
}

View File

@ -24,7 +24,6 @@ import com.djrapitops.plan.data.store.keys.ServerKeys;
import com.djrapitops.plan.data.store.mutators.*;
import com.djrapitops.plan.data.store.mutators.health.HealthInformation;
import com.djrapitops.plan.data.time.WorldTimes;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.properties.ServerProperties;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.settings.config.PlanConfig;
@ -67,7 +66,6 @@ public class AnalysisContainer extends DynamicDataContainer {
private final Locale locale;
private final PlanConfig config;
private final Theme theme;
private final DBSystem dbSystem;
private final ServerProperties serverProperties;
private final Formatters formatters;
private final Graphs graphs;
@ -81,7 +79,6 @@ public class AnalysisContainer extends DynamicDataContainer {
Locale locale,
PlanConfig config,
Theme theme,
DBSystem dbSystem,
ServerProperties serverProperties,
Formatters formatters,
Graphs graphs,
@ -94,7 +91,6 @@ public class AnalysisContainer extends DynamicDataContainer {
this.locale = locale;
this.config = config;
this.theme = theme;
this.dbSystem = dbSystem;
this.serverProperties = serverProperties;
this.formatters = formatters;
this.graphs = graphs;
@ -508,7 +504,6 @@ public class AnalysisContainer extends DynamicDataContainer {
private final PlanConfig config;
private final Locale locale;
private final Theme theme;
private final DBSystem dbSystem;
private final ServerProperties serverProperties;
private final Formatters formatters;
private final Graphs graphs;
@ -522,7 +517,6 @@ public class AnalysisContainer extends DynamicDataContainer {
PlanConfig config,
Locale locale,
Theme theme,
DBSystem dbSystem,
ServerProperties serverProperties,
Formatters formatters,
Graphs graphs,
@ -534,7 +528,6 @@ public class AnalysisContainer extends DynamicDataContainer {
this.config = config;
this.locale = locale;
this.theme = theme;
this.dbSystem = dbSystem;
this.serverProperties = serverProperties;
this.formatters = formatters;
this.graphs = graphs;
@ -550,7 +543,6 @@ public class AnalysisContainer extends DynamicDataContainer {
locale,
config,
theme,
dbSystem,
serverProperties,
formatters,
graphs,

View File

@ -67,9 +67,6 @@ public interface Database {
*/
DBType getType();
@Deprecated
void scheduleClean(long delay);
State getState();
enum State {

View File

@ -22,21 +22,18 @@ import com.djrapitops.plan.api.exceptions.database.FatalDBException;
import com.djrapitops.plan.data.store.containers.NetworkContainer;
import com.djrapitops.plan.db.access.Query;
import com.djrapitops.plan.db.access.transactions.Transaction;
import com.djrapitops.plan.db.access.transactions.init.CleanTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateIndexTransaction;
import com.djrapitops.plan.db.access.transactions.init.CreateTablesTransaction;
import com.djrapitops.plan.db.access.transactions.init.OperationCriticalTransaction;
import com.djrapitops.plan.db.patches.*;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.utilities.java.ThrowableUtils;
import com.djrapitops.plugin.api.TimeAmount;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.console.PluginLogger;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import com.djrapitops.plugin.task.AbsRunnable;
import com.djrapitops.plugin.task.PluginTask;
import com.djrapitops.plugin.task.RunnableFactory;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -65,8 +62,6 @@ public abstract class SQLDB extends AbstractDatabase {
protected final PluginLogger logger;
protected final ErrorHandler errorHandler;
private PluginTask dbCleanTask;
private Supplier<ExecutorService> transactionExecutorServiceProvider;
private ExecutorService transactionExecutor;
@ -125,28 +120,6 @@ public abstract class SQLDB extends AbstractDatabase {
return Collections.emptyList();
}
@Override
public void scheduleClean(long secondsDelay) {
dbCleanTask = runnableFactory.create("DB Clean Task", new AbsRunnable() {
@Override
public void run() {
try {
if (getState() != State.CLOSED) {
executeTransaction(new CleanTransaction(serverUUIDSupplier.get(),
config.get(TimeSettings.KEEP_INACTIVE_PLAYERS), logger, locale)
);
}
} catch (DBOpException e) {
errorHandler.log(L.ERROR, this.getClass(), e);
cancel();
}
}
}).runTaskTimerAsynchronously(
TimeAmount.toTicks(secondsDelay, TimeUnit.SECONDS),
TimeAmount.toTicks(config.get(TimeSettings.CLEAN_DATABASE_PERIOD), TimeUnit.MILLISECONDS)
);
}
Patch[] patches() {
return new Patch[]{
new Version10Patch(),
@ -197,7 +170,7 @@ public abstract class SQLDB extends AbstractDatabase {
runnableFactory.create("Database Index Creation", new AbsRunnable() {
@Override
public void run() {
executeTransaction(new CreateIndexTransaction(getType()));
executeTransaction(new CreateIndexTransaction());
}
}).runTaskLaterAsynchronously(TimeAmount.toTicks(1, TimeUnit.MINUTES));
} catch (Exception ignore) {
@ -216,9 +189,6 @@ public abstract class SQLDB extends AbstractDatabase {
public void close() {
setState(State.CLOSED);
closeTransactionExecutor(transactionExecutor);
if (dbCleanTask != null) {
dbCleanTask.cancel();
}
}
public abstract Connection getConnection() throws SQLException;

View File

@ -29,12 +29,6 @@ import org.apache.commons.text.TextStringBuilder;
*/
public class CreateIndexTransaction extends Transaction {
private final DBType dbType;
public CreateIndexTransaction(DBType dbType) {
this.dbType = dbType;
}
@Override
protected void performOperations() {
createIndex(UsersTable.TABLE_NAME, "plan_users_uuid_index",

View File

@ -0,0 +1,66 @@
package com.djrapitops.plan.db.tasks;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.db.Database;
import com.djrapitops.plan.db.access.transactions.init.CleanTransaction;
import com.djrapitops.plan.system.database.DBSystem;
import com.djrapitops.plan.system.info.server.ServerInfo;
import com.djrapitops.plan.system.locale.Locale;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plugin.logging.L;
import com.djrapitops.plugin.logging.console.PluginLogger;
import com.djrapitops.plugin.logging.error.ErrorHandler;
import com.djrapitops.plugin.task.AbsRunnable;
import javax.inject.Inject;
import javax.inject.Singleton;
/**
* Task for cleaning the active database.
*
* @author Rsl1122
*/
@Singleton
public class DBCleanTask extends AbsRunnable {
private final Locale locale;
private final DBSystem dbSystem;
private final PlanConfig config;
private final ServerInfo serverInfo;
private final PluginLogger logger;
private final ErrorHandler errorHandler;
@Inject
public DBCleanTask(
PlanConfig config,
Locale locale,
DBSystem dbSystem,
ServerInfo serverInfo,
PluginLogger logger,
ErrorHandler errorHandler
) {
this.locale = locale;
this.dbSystem = dbSystem;
this.config = config;
this.serverInfo = serverInfo;
this.logger = logger;
this.errorHandler = errorHandler;
}
@Override
public void run() {
Database database = dbSystem.getDatabase();
try {
if (database.getState() != Database.State.CLOSED) {
database.executeTransaction(new CleanTransaction(serverInfo.getServerUUID(),
config.get(TimeSettings.KEEP_INACTIVE_PLAYERS), logger, locale)
);
}
} catch (DBOpException e) {
errorHandler.log(L.ERROR, this.getClass(), e);
cancel();
}
}
}

View File

@ -97,7 +97,6 @@ public abstract class DBSystem implements SubSystem {
public void enable() throws EnableException {
try {
db.init();
db.scheduleClean(20L);
logger.info(locale.getString(PluginLang.ENABLED_DATABASE, db.getType().getName()));
} catch (DBInitException e) {
Throwable cause = e.getCause();

View File

@ -900,7 +900,6 @@ public abstract class CommonDBTest {
system.getConfigSystem().getConfig(),
system.getLocaleSystem().getLocale(),
system.getConfigSystem().getTheme(),
dbSystem,
system.getServerInfo().getServerProperties(),
system.getHtmlUtilities().getFormatters(),
system.getHtmlUtilities().getGraphs(),
@ -991,7 +990,7 @@ public abstract class CommonDBTest {
@Test
public void indexCreationWorksWithoutErrors() {
db.executeTransaction(new CreateIndexTransaction(db.getType()));
db.executeTransaction(new CreateIndexTransaction());
}
@Test

View File

@ -18,6 +18,7 @@ package com.djrapitops.plan.system.tasks;
import com.djrapitops.plan.PlanSponge;
import com.djrapitops.plan.ShutdownHook;
import com.djrapitops.plan.db.tasks.DBCleanTask;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.server.BootAnalysisTask;
@ -39,6 +40,7 @@ public class SpongeTaskSystem extends ServerTaskSystem {
private final ShutdownHook shutdownHook;
private final PingCountTimerSponge pingCountTimer;
private final ConfigStoreTask configStoreTask;
private final DBCleanTask dbCleanTask;
@Inject
public SpongeTaskSystem(
@ -52,7 +54,8 @@ public class SpongeTaskSystem extends ServerTaskSystem {
PingCountTimerSponge pingCountTimer,
LogsFolderCleanTask logsFolderCleanTask,
PlayersPageRefreshTask playersPageRefreshTask,
ConfigStoreTask configStoreTask
ConfigStoreTask configStoreTask,
DBCleanTask dbCleanTask
) {
super(
runnableFactory,
@ -66,6 +69,7 @@ public class SpongeTaskSystem extends ServerTaskSystem {
this.shutdownHook = shutdownHook;
this.pingCountTimer = pingCountTimer;
this.configStoreTask = configStoreTask;
this.dbCleanTask = dbCleanTask;
}
@Override
@ -81,6 +85,11 @@ public class SpongeTaskSystem extends ServerTaskSystem {
long storeDelay = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS) + 40;
registerTask("Config Store Task", configStoreTask).runTaskLaterAsynchronously(storeDelay);
registerTask("DB Clean Task", dbCleanTask).runTaskTimerAsynchronously(
TimeAmount.toTicks(20, TimeUnit.SECONDS),
TimeAmount.toTicks(config.get(TimeSettings.CLEAN_DATABASE_PERIOD), TimeUnit.MILLISECONDS)
);
shutdownHook.register();
}

View File

@ -17,6 +17,7 @@
package com.djrapitops.plan.system.tasks;
import com.djrapitops.plan.PlanVelocity;
import com.djrapitops.plan.db.tasks.DBCleanTask;
import com.djrapitops.plan.system.settings.config.PlanConfig;
import com.djrapitops.plan.system.settings.paths.TimeSettings;
import com.djrapitops.plan.system.tasks.proxy.NetworkConfigStoreTask;
@ -43,6 +44,7 @@ public class VelocityTaskSystem extends TaskSystem {
private final LogsFolderCleanTask logsFolderCleanTask;
private final PlayersPageRefreshTask playersPageRefreshTask;
private final NetworkConfigStoreTask networkConfigStoreTask;
private final DBCleanTask dbCleanTask;
@Inject
public VelocityTaskSystem(
@ -54,7 +56,8 @@ public class VelocityTaskSystem extends TaskSystem {
PingCountTimerVelocity pingCountTimer,
LogsFolderCleanTask logsFolderCleanTask,
PlayersPageRefreshTask playersPageRefreshTask,
NetworkConfigStoreTask networkConfigStoreTask
NetworkConfigStoreTask networkConfigStoreTask,
DBCleanTask dbCleanTask
) {
super(runnableFactory, velocityTPSCountTimer);
this.plugin = plugin;
@ -65,6 +68,7 @@ public class VelocityTaskSystem extends TaskSystem {
this.logsFolderCleanTask = logsFolderCleanTask;
this.playersPageRefreshTask = playersPageRefreshTask;
this.networkConfigStoreTask = networkConfigStoreTask;
this.dbCleanTask = dbCleanTask;
}
@Override
@ -87,5 +91,10 @@ public class VelocityTaskSystem extends TaskSystem {
// +40 ticks / 2 seconds so that update check task runs first.
long storeDelay = TimeAmount.toTicks(config.get(TimeSettings.CONFIG_UPDATE_INTERVAL), TimeUnit.MILLISECONDS) + 40;
registerTask("Config Store Task", networkConfigStoreTask).runTaskLaterAsynchronously(storeDelay);
registerTask("DB Clean Task", dbCleanTask).runTaskTimerAsynchronously(
TimeAmount.toTicks(20, TimeUnit.SECONDS),
TimeAmount.toTicks(config.get(TimeSettings.CLEAN_DATABASE_PERIOD), TimeUnit.MILLISECONDS)
);
}
}