Search in sources :

Example 1 with CacheReleaseListener

use of com.swiftmq.impl.store.standard.cache.CacheReleaseListener in project swiftmq-ce by iitsoftware.

the class LogManager method process.

protected void process(Object[] bulk, int n) {
    if (closed && !checkPointPending)
        return;
    if (ctx.traceSpace.enabled)
        ctx.traceSpace.trace("sys$store", toString() + "/process,length=" + n);
    try {
        if (logManagerListener != null)
            logManagerListener.startProcessing();
        boolean checkPointNow = false;
        for (int i = 0; i < n; i++) {
            LogOperation oper = (LogOperation) bulk[i];
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, operation=" + oper);
            switch(oper.getOperationType()) {
                case LogOperation.OPER_LOG_REC:
                    LogRecord lr = (LogRecord) oper;
                    logFile.write(lr);
                    Semaphore s = lr.getSemaphore();
                    if (s != null)
                        semList.add(s);
                    CacheReleaseListener crl = lr.getCacheReleaseListener();
                    if (crl != null)
                        releaseList.add(crl);
                    AsyncCompletionCallback cb = lr.getCallback();
                    if (cb != null)
                        callbackList.add(cb);
                    // Releasing locks on shared message pages
                    List messagePageRefs = lr.getMessagePageRefs();
                    if (messagePageRefs != null) {
                        for (int j = 0; j < messagePageRefs.size(); j++) {
                            ((MessagePageReference) messagePageRefs.get(j)).unMarkActive();
                        }
                    }
                    break;
                case LogOperation.OPER_CLOSE_LOG:
                    closeSem = ((CloseLogOperation) oper).getSemaphore();
                    closed = true;
                    break;
                case LogOperation.OPER_INITIATE_SYNC:
                    checkPointInitiated = true;
                    break;
                case LogOperation.OPER_SYNC_LOG:
                    checkPointNow = true;
                    break;
            }
        }
        // write the log to the log file
        if (logFile.getFlushSize() > 0)
            logFile.flush(forceSync);
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, processed log objects=" + n);
        if (checkPointNow) {
            if (checkPointVerbose)
                System.out.println("+++ CHECKPOINT ... ");
            long start = System.currentTimeMillis();
            // Trigger CP Handler to flush the cache.
            checkPointHandler.performCheckPoint();
            // Start new log file
            logFile.reset(forceSync);
            // continue normal operation
            checkPointHandler.checkPointDone();
            checkPointPending = false;
            if (checkPointVerbose)
                System.out.println("+++ CHECKPOINT DONE in " + (System.currentTimeMillis() - start) + " milliseconds");
            if (closed) {
                if (logManagerListener != null)
                    logManagerListener.stopProcessing();
                closeSem.notifySingleWaiter();
                return;
            }
        }
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, releaseList.size=" + releaseList.size() + ", semList.size=" + semList.size() + ", callbackList.size=" + callbackList.size());
        // call back cache release listeners
        if (releaseList.size() > 0) {
            for (int i = 0; i < releaseList.size(); i++) {
                CacheReleaseListener crl = (CacheReleaseListener) releaseList.get(i);
                crl.releaseCache();
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, " + releaseList.size() + " CacheReleaseListeners called");
            releaseList.clear();
        }
        if (logManagerListener != null)
            logManagerListener.stopProcessing();
        // notify Semaphores
        if (semList.size() > 0) {
            for (int i = 0; i < semList.size(); i++) {
                Semaphore ls = (Semaphore) semList.get(i);
                ls.notifySingleWaiter();
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, " + semList.size() + " semaphores notified");
            semList.clear();
        }
        // notify callbacks
        if (callbackList.size() > 0) {
            for (int i = 0; i < callbackList.size(); i++) {
                AsyncCompletionCallback cb = (AsyncCompletionCallback) callbackList.get(i);
                cb.notifyCallbackStack(true);
            }
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, " + callbackList.size() + " AsyncCompletionCallbacks called");
            callbackList.clear();
        }
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, logFile.length=" + logFile.getPosition());
        // SyncLogOperation into the queue.
        if (!checkPointPending && (closed || checkPointInitiated || logFile.getPosition() >= maxLogSize)) {
            if (ctx.traceSpace.enabled)
                ctx.traceSpace.trace("sys$store", toString() + "/process, checkPointHandler.lockForCheckPoint()");
            checkPointHandler.lockForCheckPoint();
            checkPointPending = true;
            checkPointInitiated = false;
        }
    } catch (Exception e) {
        // PANIC
        if (ctx.traceSpace.enabled)
            ctx.traceSpace.trace("sys$store", toString() + "/process, exception occurred=" + e);
        ctx.logSwiftlet.logError("sys$store", toString() + "/process, PANIC! EXITING! Exception occurred=" + e);
        e.printStackTrace();
        SwiftletManager.getInstance().disableShutdownHook();
        System.exit(-1);
    }
}
Also used : MessagePageReference(com.swiftmq.impl.store.standard.index.MessagePageReference) AsyncCompletionCallback(com.swiftmq.tools.concurrent.AsyncCompletionCallback) ArrayList(java.util.ArrayList) List(java.util.List) Semaphore(com.swiftmq.tools.concurrent.Semaphore) CacheReleaseListener(com.swiftmq.impl.store.standard.cache.CacheReleaseListener)

Aggregations

CacheReleaseListener (com.swiftmq.impl.store.standard.cache.CacheReleaseListener)1 MessagePageReference (com.swiftmq.impl.store.standard.index.MessagePageReference)1 AsyncCompletionCallback (com.swiftmq.tools.concurrent.AsyncCompletionCallback)1 Semaphore (com.swiftmq.tools.concurrent.Semaphore)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1