guiyanakuang commented on a change in pull request #941: URL: https://github.com/apache/orc/pull/941#discussion_r736331291
########## File path: java/tools/src/test/org/apache/orc/tools/TestFileDump.java ########## @@ -709,6 +710,80 @@ public void testIndexOf() { byte[] bytes = ("OO" + OrcFile.MAGIC).getBytes(StandardCharsets.UTF_8); byte[] pattern = OrcFile.MAGIC.getBytes(StandardCharsets.UTF_8); - assertEquals(FileDump.indexOf(bytes, pattern, 1), 2); + assertEquals(2, FileDump.indexOf(bytes, pattern, 1)); + } + + @Test + public void testRecover() throws Exception { + TypeDescription schema = getMyRecordType(); + Writer writer = OrcFile.createWriter(testFilePath, + OrcFile.writerOptions(conf) + .fileSystem(fs) + .setSchema(schema)); + Random r1 = new Random(1); + String[] words = new String[]{"It", "was", "the", "best", "of", "times,", + "it", "was", "the", "worst", "of", "times,", "it", "was", "the", "age", + "of", "wisdom,", "it", "was", "the", "age", "of", "foolishness,", "it", + "was", "the", "epoch", "of", "belief,", "it", "was", "the", "epoch", + "of", "incredulity,", "it", "was", "the", "season", "of", "Light,", + "it", "was", "the", "season", "of", "Darkness,", "it", "was", "the", + "spring", "of", "hope,", "it", "was", "the", "winter", "of", "despair,", + "we", "had", "everything", "before", "us,", "we", "had", "nothing", + "before", "us,", "we", "were", "all", "going", "direct", "to", + "Heaven,", "we", "were", "all", "going", "direct", "the", "other", + "way"}; + VectorizedRowBatch batch = schema.createRowBatch(1000); + for(int i=0; i < 21000; ++i) { + appendMyRecord(batch, r1.nextInt(), r1.nextLong(), + words[r1.nextInt(words.length)]); + if (batch.size == batch.getMaxSize()) { + writer.addRowBatch(batch); + batch.reset(); + } + } + if (batch.size > 0) { + writer.addRowBatch(batch); + } + writer.close(); + + long fileSize = fs.getFileStatus(testFilePath).getLen(); + + String testFilePathStr = Path.mergePaths( + workDir, Path.mergePaths(new Path(Path.SEPARATOR), testFilePath)) + .toUri().getPath(); + + String copyTestFilePathStr = Path.mergePaths( + workDir, Path.mergePaths(new Path(Path.SEPARATOR), + new Path("CopyTestFileDump.testDump.orc"))) + .toUri().getPath(); + + String testCrcFilePathStr = Path.mergePaths(workDir, + Path.mergePaths(new Path(Path.SEPARATOR), + new Path(".TestFileDump.testDump.orc.crc"))) + .toUri().getPath(); + + try { + Files.copy(Paths.get(testFilePathStr), Paths.get(copyTestFilePathStr)); + + // Append write data to make it a corrupt file + try (FileOutputStream output = new FileOutputStream(testFilePathStr, true)) { + output.write(new byte[1024]); + output.write(OrcFile.MAGIC.getBytes(StandardCharsets.UTF_8)); + output.write(new byte[1024]); + output.flush(); + } + + // Clean up the crc file and append data to avoid checksum read exceptions Review comment: as I append the file through java nio, crc file is not updated, if you do not delete the crc file, hdfs read interface will throw an exception. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@orc.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org