Search in sources :

Example 1 with InsertQuery

use of me.botsko.prism.database.InsertQuery in project Prism-Bukkit by prism.

the class RecordingTask method insertActionsIntoDatabase.

/**
 * Create a Insertion.
 */
void insertActionsIntoDatabase() {
    int actionsRecorded = 0;
    int perBatch = actionsPerInsert;
    if (perBatch < 1) {
        perBatch = 1000;
    }
    if (!RecordingQueue.getQueue().isEmpty()) {
        if (Prism.getPrismDataSource().isPaused()) {
            Prism.log("Prism database paused. An external actor has paused database processing..." + "scheduling next recording");
            scheduleNextRecording();
            return;
        }
        long start = System.currentTimeMillis();
        Prism.debug("Beginning batch insert from queue. " + start);
        try (Connection conn = Prism.getPrismDataSource().getConnection()) {
            if ((conn == null) || (conn.isClosed())) {
                if (RecordingManager.failedDbConnectionCount == 0) {
                    Prism.log("Prism database error. Connection should be there but it's not. " + "Leaving actions to log in queue.");
                }
                RecordingManager.failedDbConnectionCount++;
                if (RecordingManager.failedDbConnectionCount > plugin.getConfig().getInt("prism.query.max-failures-before-wait")) {
                    Prism.log("Too many problems connecting. Giving up for a bit.");
                    scheduleNextRecording();
                }
                Prism.debug("Database connection still missing, incrementing count.");
                return;
            } else {
                RecordingManager.failedDbConnectionCount = 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            Prism.getPrismDataSource().handleDataSourceException(e);
            return;
        }
        InsertQuery batchedQuery;
        try {
            batchedQuery = Prism.getPrismDataSource().getDataInsertionQuery();
            batchedQuery.createBatch();
        } catch (Exception e) {
            e.printStackTrace();
            if (e instanceof SQLException) {
                Prism.getPrismDataSource().handleDataSourceException((SQLException) e);
            }
            Prism.debug("Database connection issue;");
            RecordingManager.failedDbConnectionCount++;
            return;
        }
        int i = 0;
        while (!RecordingQueue.getQueue().isEmpty()) {
            final Handler a = RecordingQueue.getQueue().poll();
            // poll() returns null if queue is empty
            if (a == null) {
                break;
            }
            if (a.isCanceled()) {
                continue;
            }
            batchedQuery.insertActionIntoDatabase(a);
            actionsRecorded++;
            // Break out of the loop and just commit what we have
            if (i >= perBatch) {
                Prism.debug("Recorder: Batch max exceeded, running insert. Queue remaining: " + RecordingQueue.getQueue().size());
                break;
            }
            i++;
        }
        long batchDoneTime = System.currentTimeMillis();
        long batchingTime = batchDoneTime - start;
        // The main delay is here
        try {
            batchedQuery.processBatch();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // Save the current count to the queue for short historical data
        long batchProcessedEnd = System.currentTimeMillis();
        long batchRunTime = batchProcessedEnd - batchDoneTime;
        plugin.queueStats.addRunInfo(new QueueStats.TaskRunInfo(actionsRecorded, batchingTime, batchRunTime));
    }
}
Also used : InsertQuery(me.botsko.prism.database.InsertQuery) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Handler(me.botsko.prism.api.actions.Handler) QueueStats(me.botsko.prism.measurement.QueueStats) SQLException(java.sql.SQLException)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Handler (me.botsko.prism.api.actions.Handler)1 InsertQuery (me.botsko.prism.database.InsertQuery)1 QueueStats (me.botsko.prism.measurement.QueueStats)1