Search in sources :

Example 1 with ReferenceHolder

use of org.cubeengine.module.log.action.ReferenceHolder in project modules-extra by CubeEngine.

the class QueryManager method doEmptyLogs.

private void doEmptyLogs(int amount) {
    final Queue<BaseAction> logs = new LinkedList<>();
    try {
        // Wait if still doing inserts
        this.latch.acquire();
        if (queuedLogs.isEmpty()) {
            return;
        }
        boolean onlyRefHolders = true;
        for (// log <amount> next logs...
        int i = 0; // log <amount> next logs...
        i < amount; // log <amount> next logs...
        i++) {
            if (this.queuedLogs.peek() instanceof ReferenceHolder) {
                if (onlyRefHolders) {
                    logs.offer(this.queuedLogs.poll());
                }
                break;
            } else {
                onlyRefHolders = false;
                BaseAction toLog = this.queuedLogs.poll();
                if (toLog == null) {
                    break;
                }
                logs.offer(toLog);
            }
        }
        for (BaseAction log : logs) {
            Integer count = this.curStatistics.get(log.getClass());
            if (count == null) {
                count = 0;
            }
            this.curStatistics.put(log.getClass(), ++count);
        }
        Profiler.startProfiling("logging");
        int logSize = logs.size();
        List<DBObject> toLog = new ArrayList<>();
        for (BaseAction log : logs) {
            log.save();
            toLog.add(log.getTarget());
            log.getTarget().put("action", log.getClass().getName());
        }
        // Batch insert
        this.collection.insert(toLog);
        long nanos = Profiler.endProfiling("logging");
        timeSpend += nanos;
        logsLogged += logSize;
        if (logSize == batchSize) {
            timeSpendFullLoad += nanos;
            logsLoggedFullLoad += logSize;
        }
        if (logSize > this.module.getConfiguration().showLogInfoInConsole) {
            this.module.getLog().debug("{} logged in: {} ms | remaining logs: {} | AVG/AVG-FULL {} / {} micros", logSize, TimeUnit.NANOSECONDS.toMillis(nanos), queuedLogs.size(), TimeUnit.NANOSECONDS.toMicros(timeSpend / logsLogged), TimeUnit.NANOSECONDS.toMicros(timeSpendFullLoad / logsLoggedFullLoad));
        }
    } catch (Exception ex) {
        module.getLog().error(ex, "Error while logging!");
        this.queuedLogs.addAll(logs);
        if (latch.availablePermits() == 0) {
            this.latch.release();
        }
        // end profiling so we can start again later
        Profiler.endProfiling("logging");
    } finally {
        if (latch.availablePermits() == 0) {
            latch.release();
        }
        if (!queuedLogs.isEmpty()) {
            this.futureStore = this.storeExecutor.submit(this.storeRunner);
        } else {
            if (shutDownLatch != null) {
                this.shutDownLatch.countDown();
            }
        }
    }
}
Also used : ReferenceHolder(org.cubeengine.module.log.action.ReferenceHolder) ArrayList(java.util.ArrayList) BaseAction(org.cubeengine.module.log.action.BaseAction) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) LinkedList(java.util.LinkedList)

Aggregations

BasicDBObject (com.mongodb.BasicDBObject)1 DBObject (com.mongodb.DBObject)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 BaseAction (org.cubeengine.module.log.action.BaseAction)1 ReferenceHolder (org.cubeengine.module.log.action.ReferenceHolder)1