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));
}
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();
}
}
}
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);
}
}
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));
}
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");
}
Aggregations