use of org.apache.nifi.flowfile.FlowFile in project kylo by Teradata.
the class InitializeFeed method onTrigger.
/* (non-Javadoc)
* @see org.apache.nifi.processor.AbstractProcessor#onTrigger(org.apache.nifi.processor.ProcessContext, org.apache.nifi.processor.ProcessSession)
*/
@Override
public void onTrigger(ProcessContext context, ProcessSession session) throws ProcessException {
FlowFile inputFF = session.get();
if (inputFF != null) {
inputFF = initialize(context, session, inputFF);
InitializationStatus status = getMetadataRecorder().getInitializationStatus(getFeedId(context, inputFF)).orElse(new InitializationStatus(State.PENDING));
switch(status.getState()) {
case PENDING:
pending(context, session, inputFF);
break;
case IN_PROGRESS:
inProgress(context, session, inputFF);
break;
case FAILED:
failed(context, session, inputFF, status.getTimestamp(), false);
break;
case REINITIALIZE:
reinitialize(context, session, inputFF);
break;
case REINITIALIZE_FAILED:
reinitializeFailed(context, session, inputFF, status.getTimestamp());
break;
case SUCCESS:
success(context, session, inputFF);
}
}
}
use of org.apache.nifi.flowfile.FlowFile in project kylo by Teradata.
the class SetSavepoint method getNextFlowFilex.
private Optional<FlowFile> getNextFlowFilex(ProcessContext context, ProcessSession session, SavepointProvider provider, PropertyValue pvSavepointId) {
long expirationDuration = context.getProperty(EXPIRATION_DURATION).asTimePeriod(TimeUnit.MILLISECONDS);
List<FlowFile> match = new ArrayList<>();
List<FlowFile> noMatch = new LinkedList<>();
session.get(session.getQueueSize().getObjectCount()).stream().sorted(Comparator.comparing(FlowFile::getLastQueueDate).reversed()).forEach(f -> {
boolean isMatch = false;
if (match.isEmpty()) {
final String savepointIdStr = pvSavepointId.evaluateAttributeExpressions(f).getValue();
String processorId = getIdentifier();
SavepointEntry entry = provider.lookupEntry(savepointIdStr);
if (entry == null || entry.getState(processorId) == null || isExpired(f, expirationDuration)) {
isMatch = true;
} else if (SavepointEntry.SavePointState.WAIT != entry.getState(processorId)) {
isMatch = true;
}
// add it
if (isMatch) {
match.add(f);
} else {
noMatch.add(f);
}
} else {
noMatch.add(f);
}
});
// clear those that failed
session.transfer(noMatch);
return match.isEmpty() ? Optional.empty() : Optional.of(match.get(0));
}
use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.
the class TestStandardProcessSession method testWriteToOutputStreamWhileReading.
@Test
public void testWriteToOutputStreamWhileReading() throws IOException {
final ContentClaim claim = contentRepo.create(false);
try (final OutputStream out = contentRepo.write(claim)) {
out.write("hello, world".getBytes());
}
final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().contentClaim(claim).addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).size(12L).build();
flowFileQueue.put(flowFileRecord);
final FlowFile flowFile = session.get();
InputStream in = session.read(flowFile);
try {
session.write(flowFile);
Assert.fail("Was able to obtain an OutputStream for a FlowFile while also holding an InputStream for it");
} catch (final IllegalStateException e) {
// expected
} finally {
in.close();
}
// Should now be okay
try (final OutputStream out = session.write(flowFile)) {
}
}
use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.
the class TestStandardProcessSession method testMigrateWithAppendableStream.
@Test
public void testMigrateWithAppendableStream() throws IOException {
FlowFile flowFile = session.create();
flowFile = session.append(flowFile, out -> out.write("1".getBytes()));
flowFile = session.append(flowFile, out -> out.write("2".getBytes()));
final StandardProcessSession newSession = new StandardProcessSession(context, () -> false);
assertTrue(session.isFlowFileKnown(flowFile));
assertFalse(newSession.isFlowFileKnown(flowFile));
session.migrate(newSession, Collections.singleton(flowFile));
assertFalse(session.isFlowFileKnown(flowFile));
assertTrue(newSession.isFlowFileKnown(flowFile));
flowFile = newSession.append(flowFile, out -> out.write("3".getBytes()));
final byte[] buff = new byte[3];
try (final InputStream in = newSession.read(flowFile)) {
StreamUtils.fillBuffer(in, buff, true);
assertEquals(-1, in.read());
}
assertTrue(Arrays.equals(new byte[] { '1', '2', '3' }, buff));
newSession.remove(flowFile);
newSession.commit();
session.commit();
}
use of org.apache.nifi.flowfile.FlowFile in project nifi by apache.
the class TestStandardProcessSession method testCommitFailureRequeuesFlowFiles.
@Test
public void testCommitFailureRequeuesFlowFiles() {
final FlowFileRecord flowFileRecord = new StandardFlowFileRecord.Builder().addAttribute("uuid", "12345678-1234-1234-1234-123456789012").entryDate(System.currentTimeMillis()).contentClaim(new StandardContentClaim(resourceClaimManager.newResourceClaim("x", "x", "0", true, false), 0L)).contentClaimOffset(0L).size(0L).build();
flowFileQueue.put(flowFileRecord);
final FlowFile originalFlowFile = session.get();
assertTrue(flowFileQueue.isActiveQueueEmpty());
assertEquals(1, flowFileQueue.getUnacknowledgedQueueSize().getObjectCount());
final FlowFile modified = session.write(originalFlowFile, new OutputStreamCallback() {
@Override
public void process(OutputStream out) throws IOException {
out.write("Hello".getBytes());
}
});
session.transfer(modified);
// instruct flowfile repo to throw IOException on update
flowFileRepo.setFailOnUpdate(true);
try {
session.commit();
Assert.fail("Session commit completed, even though FlowFile Repo threw IOException");
} catch (final ProcessException pe) {
// expected behavior because FlowFile Repo will throw IOException
}
assertFalse(flowFileQueue.isActiveQueueEmpty());
assertEquals(1, flowFileQueue.size().getObjectCount());
assertEquals(0, flowFileQueue.getUnacknowledgedQueueSize().getObjectCount());
}
Aggregations