[ https://issues.apache.org/jira/browse/BEAM-7866?focusedWorklogId=293507&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-293507 ]
ASF GitHub Bot logged work on BEAM-7866: ---------------------------------------- Author: ASF GitHub Bot Created on: 13/Aug/19 00:35 Start Date: 13/Aug/19 00:35 Worklog Time Spent: 10m Work Description: chamikaramj commented on pull request #9233: [BEAM-7866] Fix python ReadFromMongoDB potential data loss issue URL: https://github.com/apache/beam/pull/9233#discussion_r313182132 ########## File path: sdks/python/apache_beam/io/mongodbio_test.py ########## @@ -210,6 +323,74 @@ def test_write_to_mongodb_with_generated_id(self, mock_client): return_value.bulk_write.assert_called_with(expected_update) +class ObjectIdHelperTest(TestCase): + def test_conversion(self): + test_cases = [ + (objectid.ObjectId('000000000000000000000000'), 0), + (objectid.ObjectId('000000000000000100000000'), 2**32), + (objectid.ObjectId('0000000000000000ffffffff'), 2**32 - 1), + (objectid.ObjectId('000000010000000000000000'), 2**64), + (objectid.ObjectId('00000000ffffffffffffffff'), 2**64 - 1), + (objectid.ObjectId('ffffffffffffffffffffffff'), 2**96 - 1), + ] + for (id, number) in test_cases: + self.assertEqual(id, _ObjectIdHelper.int_to_id(number)) + self.assertEqual(number, _ObjectIdHelper.id_to_int(id)) + + # random tests + for _ in range(100): + id = objectid.ObjectId() + if sys.version_info[0] < 3: + number = int(id.binary.encode('hex'), 16) + else: # PY3 + number = int(id.binary.hex(), 16) + self.assertEqual(id, _ObjectIdHelper.int_to_id(number)) + self.assertEqual(number, _ObjectIdHelper.id_to_int(id)) + + def test_increment_id(self): + test_cases = [ + (objectid.ObjectId('000000000000000100000000'), + objectid.ObjectId('0000000000000000ffffffff')), + (objectid.ObjectId('000000010000000000000000'), + objectid.ObjectId('00000000ffffffffffffffff')), + ] + for (first, second) in test_cases: + self.assertEqual(second, _ObjectIdHelper.increment_id(first, -1)) + self.assertEqual(first, _ObjectIdHelper.increment_id(second, 1)) + + for _ in range(100): + id = objectid.ObjectId() + self.assertLess(id, _ObjectIdHelper.increment_id(id, 1)) + self.assertGreater(id, _ObjectIdHelper.increment_id(id, -1)) + + +class ObjectRangeTrackerTest(TestCase): + def test_fraction_position_conversion(self): + start_int = 0 + stop_int = 2**96 - 1 + start = _ObjectIdHelper.int_to_id(start_int) + stop = _ObjectIdHelper.int_to_id(stop_int) + test_cases = ([start_int, stop_int, 2**32, 2**32 - 1, 2**64, 2**64 - 1] + + [random.randint(start_int, stop_int) for _ in range(100)]) + tracker = _ObjectIdRangeTracker() + for pos in test_cases: + id = _ObjectIdHelper.int_to_id(pos - start_int) + desired_fraction = (pos - start_int) / (stop_int - start_int) + self.assertAlmostEqual(tracker.position_to_fraction(id, start, stop), Review comment: Please run this test 10k times and make sure that this is not flaky. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org Issue Time Tracking ------------------- Worklog Id: (was: 293507) Time Spent: 10h 50m (was: 10h 40m) > Python MongoDB IO performance and correctness issues > ---------------------------------------------------- > > Key: BEAM-7866 > URL: https://issues.apache.org/jira/browse/BEAM-7866 > Project: Beam > Issue Type: Bug > Components: sdk-py-core > Reporter: Eugene Kirpichov > Assignee: Yichi Zhang > Priority: Major > Fix For: 2.16.0 > > Time Spent: 10h 50m > Remaining Estimate: 0h > > https://github.com/apache/beam/blob/master/sdks/python/apache_beam/io/mongodbio.py > splits the query result by computing number of results in constructor, and > then in each reader re-executing the whole query and getting an index > sub-range of those results. > This is broken in several critical ways: > - The order of query results returned by find() is not necessarily > deterministic, so the idea of index ranges on it is meaningless: each shard > may basically get random, possibly overlapping subsets of the total results > - Even if you add order by `_id`, the database may be changing concurrently > to reading and splitting. E.g. if the database contained documents with ids > 10 20 30 40 50, and this was split into shards 0..2 and 3..5 (under the > assumption that these shards would contain respectively 10 20 30, and 40 50), > and then suppose shard 10 20 30 is read and then document 25 is inserted - > then the 3..5 shard will read 30 40 50, i.e. document 30 is duplicated and > document 25 is lost. > - Every shard re-executes the query and skips the first start_offset items, > which in total is quadratic complexity > - The query is first executed in the constructor in order to count results, > which 1) means the constructor can be super slow and 2) it won't work at all > if the database is unavailable at the time the pipeline is constructed (e.g. > if this is a template). > Unfortunately, none of these issues are caught by SourceTestUtils: this class > has extensive coverage with it, and the tests pass. This is because the tests > return the same results in the same order. I don't know how to catch this > automatically, and I don't know how to catch the performance issue > automatically, but these would all be important follow-up items after the > actual fix. > CC: [~chamikara] as reviewer. -- This message was sent by Atlassian JIRA (v7.6.14#76016)