Search in sources :

Example 41 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestFileSystemRepository method testSizeWithNoContent.

@Test(expected = ContentNotFoundException.class)
public void testSizeWithNoContent() throws IOException {
    final ContentClaim claim = new StandardContentClaim(new StandardResourceClaim(claimManager, "container1", "section 1", "1", false), 0L);
    assertEquals(0L, repository.size(claim));
}
Also used : ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) StandardResourceClaim(org.apache.nifi.controller.repository.claim.StandardResourceClaim) Test(org.junit.Test)

Example 42 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestFileSystemRepository method testWriteCannotProvideNullOutput.

@Test
public void testWriteCannotProvideNullOutput() throws IOException {
    FileSystemRepository repository = null;
    try {
        final List<Path> archivedPathsWithOpenStream = Collections.synchronizedList(new ArrayList<Path>());
        // We are creating our own 'local' repository in this test so shut down the one created in the setup() method
        shutdown();
        repository = new FileSystemRepository(nifiProperties) {

            @Override
            protected boolean archive(Path curPath) throws IOException {
                if (getOpenStreamCount() > 0) {
                    archivedPathsWithOpenStream.add(curPath);
                }
                return true;
            }
        };
        final StandardResourceClaimManager claimManager = new StandardResourceClaimManager();
        repository.initialize(claimManager);
        repository.purge();
        final ContentClaim claim = repository.create(false);
        assertEquals(1, claimManager.getClaimantCount(claim.getResourceClaim()));
        int claimantCount = claimManager.decrementClaimantCount(claim.getResourceClaim());
        assertEquals(0, claimantCount);
        assertTrue(archivedPathsWithOpenStream.isEmpty());
        OutputStream out = repository.write(claim);
        out.close();
        repository.decrementClaimantCount(claim);
        ContentClaim claim2 = repository.create(false);
        assertEquals(claim.getResourceClaim(), claim2.getResourceClaim());
        out = repository.write(claim2);
        final boolean archived = repository.archive(claim.getResourceClaim());
        assertFalse(archived);
    } finally {
        if (repository != null) {
            repository.shutdown();
        }
    }
}
Also used : Path(java.nio.file.Path) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) StandardResourceClaimManager(org.apache.nifi.controller.repository.claim.StandardResourceClaimManager) IOException(java.io.IOException) Test(org.junit.Test)

Example 43 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestFileSystemRepository method testReadWithContentArchived.

@Test
public void testReadWithContentArchived() throws IOException {
    // skip if on windows
    assumeFalse(isWindowsEnvironment());
    final ContentClaim claim = repository.create(true);
    final Path path = getPath(claim);
    Files.deleteIfExists(path);
    Path archivePath = FileSystemRepository.getArchivePath(path);
    Files.createDirectories(archivePath.getParent());
    final byte[] data = "The quick brown fox jumps over the lazy dog".getBytes();
    try (final OutputStream out = Files.newOutputStream(archivePath, StandardOpenOption.WRITE, StandardOpenOption.CREATE)) {
        out.write(data);
    }
    try (final InputStream inStream = repository.read(claim)) {
        assertNotNull(inStream);
        final byte[] dataRead = readFully(inStream, data.length);
        assertArrayEquals(data, dataRead);
    }
}
Also used : Path(java.nio.file.Path) ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Test(org.junit.Test)

Example 44 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestFileSystemRepository method testMerge.

private void testMerge(final String header, final String footer, final String demarcator) throws IOException {
    final int count = 4;
    final String content = "The quick brown fox jumps over the lazy dog";
    final List<ContentClaim> claims = new ArrayList<>();
    for (int i = 0; i < count; i++) {
        final ContentClaim claim = repository.create(true);
        claims.add(claim);
        try (final OutputStream out = repository.write(claim)) {
            out.write(content.getBytes());
        }
    }
    final ContentClaim destination = repository.create(true);
    final byte[] headerBytes = header == null ? null : header.getBytes();
    final byte[] footerBytes = footer == null ? null : footer.getBytes();
    final byte[] demarcatorBytes = demarcator == null ? null : demarcator.getBytes();
    repository.merge(claims, destination, headerBytes, footerBytes, demarcatorBytes);
    final StringBuilder sb = new StringBuilder();
    if (header != null) {
        sb.append(header);
    }
    for (int i = 0; i < count; i++) {
        sb.append(content);
        if (demarcator != null && i != count - 1) {
            sb.append(demarcator);
        }
    }
    if (footer != null) {
        sb.append(footer);
    }
    final String expectedText = sb.toString();
    final byte[] expected = expectedText.getBytes();
    final ByteArrayOutputStream baos = new ByteArrayOutputStream((int) destination.getLength());
    try (final InputStream in = repository.read(destination)) {
        StreamUtils.copy(in, baos);
    }
    final byte[] actual = baos.toByteArray();
    assertTrue(Arrays.equals(expected, actual));
}
Also used : ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 45 with ContentClaim

use of org.apache.nifi.controller.repository.claim.ContentClaim in project nifi by apache.

the class TestFileSystemRepository method testWritePerformance.

@Test
@Ignore("Intended for manual testing only, in order to judge changes to performance")
public void testWritePerformance() throws IOException {
    final long bytesToWrite = 1_000_000_000L;
    final int contentSize = 100;
    final int iterations = (int) (bytesToWrite / contentSize);
    final byte[] content = new byte[contentSize];
    final Random random = new Random();
    random.nextBytes(content);
    // final ContentClaimWriteCache cache = new ContentClaimWriteCache(repository);
    final long start = System.nanoTime();
    for (int i = 0; i < iterations; i++) {
        final ContentClaim claim = repository.create(false);
        try (final OutputStream out = repository.write(claim)) {
            out.write(content);
        }
    // final ContentClaim claim = cache.getContentClaim();
    // try (final OutputStream out = cache.write(claim)) {
    // out.write(content);
    // }
    }
    final long millis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
    final long mb = bytesToWrite / (1024 * 1024);
    final long seconds = millis / 1000L;
    final double mbps = (double) mb / (double) seconds;
    System.out.println("Took " + millis + " millis to write " + contentSize + " bytes " + iterations + " times (total of " + NumberFormat.getNumberInstance(Locale.US).format(bytesToWrite) + " bytes) for a write rate of " + mbps + " MB/s");
}
Also used : ContentClaim(org.apache.nifi.controller.repository.claim.ContentClaim) StandardContentClaim(org.apache.nifi.controller.repository.claim.StandardContentClaim) Random(java.util.Random) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ContentClaim (org.apache.nifi.controller.repository.claim.ContentClaim)79 StandardContentClaim (org.apache.nifi.controller.repository.claim.StandardContentClaim)51 Test (org.junit.Test)40 OutputStream (java.io.OutputStream)39 ByteArrayOutputStream (java.io.ByteArrayOutputStream)30 IOException (java.io.IOException)26 InputStream (java.io.InputStream)22 ResourceClaim (org.apache.nifi.controller.repository.claim.ResourceClaim)22 ByteArrayInputStream (java.io.ByteArrayInputStream)20 FlowFile (org.apache.nifi.flowfile.FlowFile)19 Path (java.nio.file.Path)18 ArrayList (java.util.ArrayList)16 HashMap (java.util.HashMap)16 FlowFileQueue (org.apache.nifi.controller.queue.FlowFileQueue)14 Map (java.util.Map)13 FileOutputStream (java.io.FileOutputStream)12 FilterOutputStream (java.io.FilterOutputStream)12 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 FlowFileAccessException (org.apache.nifi.processor.exception.FlowFileAccessException)12 ProvenanceEventRecord (org.apache.nifi.provenance.ProvenanceEventRecord)12