Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AVRO-4086][C++] Fix missing data file reader close handle on windows #3230

Merged
merged 7 commits into from
Dec 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-lang-js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:

interop:
name: Node ${{ matrix.node }} Interop
runs-on: ubuntu-latest
runs-on: ubuntu-24,04
martin-g marked this conversation as resolved.
Show resolved Hide resolved
strategy:
matrix:
node:
Expand Down
6 changes: 6 additions & 0 deletions lang/c++/impl/DataFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ boost::iostreams::zlib_params get_zlib_params() {
ret.noheader = true;
return ret;
}

} // namespace

DataFileWriterBase::DataFileWriterBase(const char *filename, const ValidSchema &schema, size_t syncInterval,
Expand Down Expand Up @@ -442,6 +443,11 @@ void DataFileReaderBase::readDataBlock() {
}

void DataFileReaderBase::close() {
stream_.reset();
eof_ = true;
objectCount_ = 0;
blockStart_ = 0;
blockEnd_ = 0;
}

static string toString(const vector<uint8_t> &v) {
Expand Down
2 changes: 1 addition & 1 deletion lang/c++/include/avro/DataFile.hh
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public:
*/
class AVRO_DECL DataFileReaderBase : boost::noncopyable {
const std::string filename_;
const std::unique_ptr<InputStream> stream_;
std::unique_ptr<InputStream> stream_;
const DecoderPtr decoder_;
int64_t objectCount_;
bool eof_;
Expand Down
3 changes: 3 additions & 0 deletions lang/c++/test/CommonsSchemasTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ void testCommonSchema(const std::filesystem::path &dir_path) {
}
BOOST_CHECK(!readerNew.read(datumNew));

readerNew.close();
readerOrig.close();

std::filesystem::remove(outputDataFile);
}

Expand Down
44 changes: 39 additions & 5 deletions lang/c++/test/DataFileTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -471,9 +471,7 @@ class DataFileTest {
void testReaderSplits() {
boost::mt19937 random(static_cast<uint32_t>(time(nullptr)));
avro::DataFileReader<ComplexInteger> df(filename, writerSchema);
std::ifstream just_for_length(
filename, std::ifstream::ate | std::ifstream::binary);
int length = static_cast<int>(just_for_length.tellg());
int length = static_cast<int>(boost::filesystem::file_size(filename));
int splits = 10;
int end = length; // end of split
int remaining = end; // bytes remaining
Expand Down Expand Up @@ -658,6 +656,27 @@ class DataFileTest {
BOOST_CHECK_EQUAL(root->leafAt(5)->getDoc(), "extra slashes\\\\");
}
}

void testClosedReader() {
const auto isNonSeekableInputStreamError = [](const avro::Exception &e) { return e.what() == std::string("seek not supported on non-SeekableInputStream"); };

avro::DataFileReader<ComplexDouble> df(filename, writerSchema);
df.close();
ComplexDouble unused;
BOOST_CHECK(!df.read(unused)); // closed stream can't be read
BOOST_CHECK_EQUAL(df.previousSync(), 0ul); // closed stream always returns begin position
BOOST_CHECK(df.pastSync(10l)); // closed stream always point after position // closed stream always returns begin position
BOOST_CHECK_EQUAL(df.previousSync(), 0u); // closed stream always point at position 0 // closed stream always returns begin position
BOOST_CHECK_EXCEPTION(df.sync(10l), avro::Exception, isNonSeekableInputStreamError); // closed stream always returns begin position
BOOST_CHECK_EXCEPTION(df.seek(10l), avro::Exception, isNonSeekableInputStreamError); // closed stream always returns begin position
}

void testClosedWriter() {
avro::DataFileWriter<ComplexDouble> df(filename, writerSchema);
df.close();
ComplexDouble unused;
BOOST_CHECK_NO_THROW(df.write(unused)); // write has not effect on closed stream
}
};

void addReaderTests(test_suite *ts, const shared_ptr<DataFileTest> &t) {
Expand Down Expand Up @@ -696,7 +715,7 @@ struct codec_traits<ReaderObj> {
if (auto *rd =
dynamic_cast<avro::ResolvingDecoder *>(&d)) {
const std::vector<size_t> fo = rd->fieldOrder();
for (unsigned long it : fo) {
for (const auto it : fo) {
switch (it) {
case 0: {
avro::decode(d, v.s2);
Expand Down Expand Up @@ -1097,7 +1116,7 @@ init_unit_test_suite(int, char *[]) {
shared_ptr<DataFileTest> t9(new DataFileTest("test9.df", sch, sch));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testWrite, t9));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testReaderSyncSeek, t9));
//ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t9));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t9));
boost::unit_test::framework::master_test_suite().add(ts);
}
{
Expand Down Expand Up @@ -1125,6 +1144,21 @@ init_unit_test_suite(int, char *[]) {
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t));
boost::unit_test::framework::master_test_suite().add(ts);
}
{
auto *ts = BOOST_TEST_SUITE("DataFile tests: test13.df");
shared_ptr<DataFileTest> t(new DataFileTest("test13.df", ischWithDoc, ischWithDoc));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testWrite, t));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testClosedReader, t));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t));
boost::unit_test::framework::master_test_suite().add(ts);
}
{
auto *ts = BOOST_TEST_SUITE("DataFile tests: test14.df");
shared_ptr<DataFileTest> t(new DataFileTest("test14.df", ischWithDoc, ischWithDoc));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testClosedWriter, t));
ts->add(BOOST_CLASS_TEST_CASE(&DataFileTest::testCleanup, t));
boost::unit_test::framework::master_test_suite().add(ts);
}

boost::unit_test::framework::master_test_suite().add(BOOST_TEST_CASE(&testSkipStringNullCodec));
boost::unit_test::framework::master_test_suite().add(BOOST_TEST_CASE(&testSkipStringDeflateCodec));
Expand Down