/** * Validate streams generate the same output. * * @param expIn Expected input stream. * @param actIn Actual input stream. * @param expSize Expected size of the streams. * @param seek Seek to use async position-based reading or {@code null} to use simple continuous * reading. * @throws IOException In case of any IO exception. */ private void assertEqualStreams( InputStream expIn, GridGgfsInputStream actIn, @Nullable Long expSize, @Nullable Long seek) throws IOException { if (seek != null) expIn.skip(seek); int bufSize = 2345; byte buf1[] = new byte[bufSize]; byte buf2[] = new byte[bufSize]; long pos = 0; long start = System.currentTimeMillis(); while (true) { int read = (int) Math.min(bufSize, expSize - pos); int i1; if (seek == null) i1 = actIn.read(buf1, 0, read); else if (seek % 2 == 0) i1 = actIn.read(pos + seek, buf1, 0, read); else { i1 = read; actIn.readFully(pos + seek, buf1, 0, read); } // Read at least 0 byte, but don't read more then 'i1' or 'read'. int i2 = expIn.read(buf2, 0, Math.max(0, Math.min(i1, read))); if (i1 != i2) { fail( "Expects the same data [read=" + read + ", pos=" + pos + ", seek=" + seek + ", i1=" + i1 + ", i2=" + i2 + ']'); } if (i1 == -1) break; // EOF // i1 == bufSize => compare buffers. // i1 < bufSize => Compare part of buffers, rest of buffers are equal from previous // iteration. assertTrue( "Expects the same data [read=" + read + ", pos=" + pos + ", seek=" + seek + ", i1=" + i1 + ", i2=" + i2 + ']', Arrays.equals(buf1, buf2)); if (read == 0) break; // Nothing more to read. pos += i1; } if (expSize != null) assertEquals(expSize.longValue(), pos); long time = System.currentTimeMillis() - start; if (time != 0 && log.isInfoEnabled()) { log.info( String.format( "Streams were compared in continuous reading " + "[size=%7d, rate=%3.1f MB/sec]", expSize, expSize * 1000. / time / 1024 / 1024)); } }
/** * Checks availability of a classpath resource. * * @param name Resource name. * @return {@code true} if resource is available and ready for read, {@code false} otherwise. */ private boolean resourceAvailable(String name) { InputStream cfgStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name); if (cfgStream == null) { log.error("Classpath resource not found: " + name); return false; } try { // Read a single byte to force actual content access by JVM. cfgStream.read(); return true; } catch (IOException e) { log.error("Failed to read classpath resource: " + name, e); return false; } finally { U.close(cfgStream, log); } }
/** * Test file creation. * * @param path Path to file to store. * @param size Size of file to store. * @param salt Salt for file content generation. * @throws Exception In case of any exception. */ private void testCreateFile(final GridGgfsPath path, final long size, final int salt) throws Exception { info("Create file [path=" + path + ", size=" + size + ", salt=" + salt + ']'); final AtomicInteger cnt = new AtomicInteger(0); final Collection<GridGgfsPath> cleanUp = new ConcurrentLinkedQueue<>(); long time = runMultiThreaded( new Callable<Object>() { @Override public Object call() throws Exception { int id = cnt.incrementAndGet(); GridGgfsPath f = new GridGgfsPath(path.parent(), "asdf" + (id > 1 ? "-" + id : "")); try (GridGgfsOutputStream out = fs.create(f, 0, true, null, 0, 1024, null)) { assertNotNull(out); cleanUp.add(f); // Add all created into cleanup list. U.copy(new GridGgfsTestInputStream(size, salt), out); } return null; } }, WRITING_THREADS_CNT, "perform-multi-thread-writing"); if (time > 0) { double rate = size * 1000. / time / 1024 / 1024; info( String.format( "Write file [path=%s, size=%d kB, rate=%2.1f MB/s]", path, WRITING_THREADS_CNT * size / 1024, WRITING_THREADS_CNT * rate)); } info("Read and validate saved file: " + path); final InputStream expIn = new GridGgfsTestInputStream(size, salt); final GridGgfsInputStream actIn = fs.open(path, CFG_BLOCK_SIZE * READING_THREADS_CNT * 11 / 10); // Validate continuous reading of whole file. assertEqualStreams(expIn, actIn, size, null); // Validate random seek and reading. final Random rnd = new Random(); runMultiThreaded( new Callable<Object>() { @Override public Object call() throws Exception { long skip = Math.abs(rnd.nextLong() % (size + 1)); long range = Math.min(size - skip, rnd.nextInt(CFG_BLOCK_SIZE * 400)); assertEqualStreams(new GridGgfsTestInputStream(size, salt), actIn, range, skip); return null; } }, READING_THREADS_CNT, "validate-multi-thread-reading"); expIn.close(); actIn.close(); info("Get stored file info: " + path); GridGgfsFile desc = fs.info(path); info("Validate stored file info: " + desc); assertNotNull(desc); if (log.isDebugEnabled()) log.debug("File descriptor: " + desc); Collection<GridGgfsBlockLocation> aff = fs.affinity(path, 0, desc.length()); assertFalse("Affinity: " + aff, desc.length() != 0 && aff.isEmpty()); int blockSize = desc.blockSize(); assertEquals("File size", size, desc.length()); assertEquals("Binary block size", CFG_BLOCK_SIZE, blockSize); // assertEquals("Permission", "rwxr-xr-x", desc.getPermission().toString()); // assertEquals("Permission sticky bit marks this is file", false, // desc.getPermission().getStickyBit()); assertEquals("Type", true, desc.isFile()); assertEquals("Type", false, desc.isDirectory()); info("Cleanup files: " + cleanUp); for (GridGgfsPath f : cleanUp) { fs.delete(f, true); assertNull(fs.info(f)); } }