Search in sources :

Example 6 with InvalidSetpointException

use of com.thinkbiganalytics.nifi.v2.core.savepoint.InvalidSetpointException in project kylo by Teradata.

the class SetSavepoint method processFlowFile.

private void processFlowFile(FlowFile flowFile, final ProcessContext context, final ProcessSession session, final SavepointContextData contextData) {
    final ComponentLog logger = getLogger();
    final SavepointController controller = contextData.getController();
    final SavepointProvider provider = contextData.getProvider();
    final PropertyValue pvSavepointId = contextData.getSavepointId();
    final String processorId = contextData.getProcessorId();
    // We do processing on each flowfile here
    final String savepointIdStr = pvSavepointId.evaluateAttributeExpressions(flowFile).getValue();
    final String flowfileId = flowFile.getAttribute(CoreAttributes.UUID.key());
    Lock lock = null;
    try {
        lock = provider.lock(savepointIdStr);
        if (lock != null) {
            SavepointEntry entry = provider.lookupEntry(savepointIdStr);
            if (isExpired(context, session, provider, flowFile, savepointIdStr, lock)) {
                return;
            }
            String waitStartTimestamp;
            // add the processor id for the current savepoint
            // this will be used to check on the next save point if the flow file should be examined and processed.
            flowFile = session.putAttribute(flowFile, SAVEPOINT_PROCESSOR_ID, getIdentifier());
            if (entry == null || entry.getState(processorId) == null) {
                // Register new
                provider.register(savepointIdStr, processorId, flowfileId, lock);
                flowFile = tryFlowFile(session, flowFile, "-1");
                // add in timestamps
                // Set wait start timestamp if it's not set yet
                waitStartTimestamp = flowFile.getAttribute(SAVEPOINT_START_TIMESTAMP);
                if (waitStartTimestamp == null) {
                    waitStartTimestamp = String.valueOf(System.currentTimeMillis());
                    flowFile = session.putAttribute(flowFile, SAVEPOINT_START_TIMESTAMP, waitStartTimestamp);
                }
                session.transfer(flowFile);
            } else {
                SavepointEntry.SavePointState state = entry.getState(processorId);
                switch(state) {
                    case RELEASE_SUCCESS:
                        provider.commitRelease(savepointIdStr, processorId, lock);
                        // add provenance to indicate success
                        flowFile = session.putAttribute(flowFile, SavepointProvenanceProperties.RELEASE_STATUS_KEY, SavepointProvenanceProperties.RELEASE_STATUS.SUCCESS.name());
                        session.transfer(flowFile, REL_RELEASE_SUCCESS);
                        break;
                    case RELEASE_FAILURE:
                        provider.commitRelease(savepointIdStr, processorId, lock);
                        // add provenance to indicate failure
                        flowFile = session.putAttribute(flowFile, SavepointProvenanceProperties.RELEASE_STATUS_KEY, SavepointProvenanceProperties.RELEASE_STATUS.FAILURE.name());
                        session.transfer(flowFile, REL_RELEASE_FAILURE);
                        break;
                    case RETRY:
                        String retryCount = flowFile.getAttribute(SAVEPOINT_RETRY_COUNT);
                        if (retryCount == null) {
                            retryCount = "0";
                        }
                        provider.commitRetry(savepointIdStr, processorId, lock);
                        flowFile = tryFlowFile(session, flowFile, retryCount);
                        session.transfer(flowFile);
                        break;
                    case WAIT:
                        session.transfer(flowFile, REL_SELF);
                        break;
                    default:
                        logger.warn("Unexpected savepoint state.");
                        session.transfer(flowFile, REL_FAILURE);
                }
            }
        } else {
            // Lock busy so try again later
            // add it back to cache
            controller.putFlowfileBack(processorId, flowfileId, true);
            logger.info("Unable to obtain lock.  It is already locked by another process.  Adding back to queue {} ", new Object[] { flowfileId });
            session.transfer(flowFile, REL_SELF);
        }
    } catch (IOException | InvalidLockException | InvalidSetpointException e) {
        logger.warn("Failed to process flowfile {} for savepoint {}", new String[] { flowfileId, savepointIdStr }, e);
        flowFile = session.putAttribute(flowFile, SAVEPOINT_EXCEPTION, "Failed to process flowfile " + flowfileId + " for savepoint " + savepointIdStr + ". " + e.getMessage());
        session.transfer(flowFile, REL_FAILURE);
    } finally {
        if (lock != null) {
            try {
                provider.unlock(lock);
            } catch (IOException e) {
                logger.warn("Unable to unlock {}", new String[] { savepointIdStr });
            }
        }
    }
}
Also used : InvalidSetpointException(com.thinkbiganalytics.nifi.v2.core.savepoint.InvalidSetpointException) SavepointController(com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointController) SavepointProvider(com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointProvider) PropertyValue(org.apache.nifi.components.PropertyValue) InvalidLockException(com.thinkbiganalytics.nifi.v2.core.savepoint.InvalidLockException) IOException(java.io.IOException) SavepointEntry(com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointEntry) ComponentLog(org.apache.nifi.logging.ComponentLog) Lock(com.thinkbiganalytics.nifi.v2.core.savepoint.Lock)

Example 7 with InvalidSetpointException

use of com.thinkbiganalytics.nifi.v2.core.savepoint.InvalidSetpointException in project kylo by Teradata.

the class SetSavepointTest method testWaitRelease.

@Test
public void testWaitRelease() throws InitializationException, IOException, InvalidLockException, InvalidSetpointException {
    enqueue("sp1");
    final TestIteration testIteration = new TestIteration();
    testIteration.expectedQueueSize = 1;
    testIteration.expectedRetry.add("1");
    testIteration.run();
    testIteration.expectedQueueSize = 1;
    testIteration.run();
    Lock lock = provider.lock(savepointId);
    assertNotNull("Expecting lock", lock);
    provider.release(savepointId, lock, true);
    provider.unlock(lock);
    testIteration.expectedQueueSize = 0;
    testIteration.expectedReleasedSuccess.add("1");
    testIteration.run();
}
Also used : Lock(com.thinkbiganalytics.nifi.v2.core.savepoint.Lock) Test(org.junit.Test)

Aggregations

Lock (com.thinkbiganalytics.nifi.v2.core.savepoint.Lock)7 Test (org.junit.Test)5 InvalidLockException (com.thinkbiganalytics.nifi.v2.core.savepoint.InvalidLockException)2 InvalidSetpointException (com.thinkbiganalytics.nifi.v2.core.savepoint.InvalidSetpointException)2 SavepointController (com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointController)2 SavepointEntry (com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointEntry)2 SavepointProvider (com.thinkbiganalytics.nifi.v2.core.savepoint.SavepointProvider)2 IOException (java.io.IOException)2 PropertyValue (org.apache.nifi.components.PropertyValue)2 ComponentLog (org.apache.nifi.logging.ComponentLog)2 FlowFile (org.apache.nifi.flowfile.FlowFile)1