forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 49
Optimize column_encryption_policy checks in recv_results_rows #630
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
Open
mykaul
wants to merge
1
commit into
scylladb:master
Choose a base branch
from
mykaul:remove_enc_cols
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+177
−9
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| # Copyright DataStax, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import io | ||
| import unittest | ||
| from unittest.mock import Mock | ||
|
|
||
| from cassandra import ProtocolVersion | ||
| from cassandra.cqltypes import Int32Type, UTF8Type | ||
| from cassandra.marshal import int32_pack | ||
| from cassandra.policies import ColDesc | ||
| from cassandra.protocol import ResultMessage, RESULT_KIND_ROWS | ||
|
|
||
|
|
||
| class DecodeOptimizationTest(unittest.TestCase): | ||
| """ | ||
| Tests to verify the optimization of column_encryption_policy checks | ||
| in recv_results_rows. The optimization checks if the policy exists once | ||
| per result message, avoiding the redundant 'column_encryption_policy and ...' | ||
| check for every value. | ||
| """ | ||
|
|
||
| def _create_mock_result_metadata(self): | ||
| """Create mock result metadata for testing""" | ||
| return [ | ||
| ('keyspace1', 'table1', 'col1', Int32Type), | ||
| ('keyspace1', 'table1', 'col2', UTF8Type), | ||
| ] | ||
|
|
||
| def _create_mock_result_message(self): | ||
| """Create a mock result message with data""" | ||
| msg = ResultMessage(kind=RESULT_KIND_ROWS) | ||
| msg.column_metadata = self._create_mock_result_metadata() | ||
| msg.recv_results_metadata = Mock() | ||
| msg.recv_row = Mock(side_effect=[ | ||
| [int32_pack(42), b'hello'], | ||
| [int32_pack(100), b'world'], | ||
| ]) | ||
| return msg | ||
|
|
||
| def _create_mock_stream(self): | ||
| """Create a mock stream for reading rows""" | ||
| # Pack rowcount (2 rows) | ||
| data = int32_pack(2) | ||
| return io.BytesIO(data) | ||
|
|
||
| def test_decode_without_encryption_policy(self): | ||
| """ | ||
| Test that decoding works correctly without column encryption policy. | ||
| This should use the optimized simple path. | ||
| """ | ||
| msg = self._create_mock_result_message() | ||
| f = self._create_mock_stream() | ||
|
|
||
| msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, None) | ||
|
|
||
| # Verify results | ||
| self.assertEqual(len(msg.parsed_rows), 2) | ||
| self.assertEqual(msg.parsed_rows[0][0], 42) | ||
| self.assertEqual(msg.parsed_rows[0][1], 'hello') | ||
| self.assertEqual(msg.parsed_rows[1][0], 100) | ||
| self.assertEqual(msg.parsed_rows[1][1], 'world') | ||
|
|
||
| def test_decode_with_encryption_policy_no_encrypted_columns(self): | ||
| """ | ||
| Test that decoding works with encryption policy when no columns are encrypted. | ||
| """ | ||
| msg = self._create_mock_result_message() | ||
| f = self._create_mock_stream() | ||
|
|
||
| # Create mock encryption policy that has no encrypted columns | ||
| mock_policy = Mock() | ||
| mock_policy.contains_column = Mock(return_value=False) | ||
|
|
||
| msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, mock_policy) | ||
|
|
||
| # Verify results | ||
| self.assertEqual(len(msg.parsed_rows), 2) | ||
| self.assertEqual(msg.parsed_rows[0][0], 42) | ||
| self.assertEqual(msg.parsed_rows[0][1], 'hello') | ||
|
|
||
| # Verify contains_column was called for each value (but policy existence check happens once) | ||
| # Should be called 4 times (2 rows × 2 columns) | ||
| self.assertEqual(mock_policy.contains_column.call_count, 4) | ||
|
|
||
| def test_decode_with_encryption_policy_with_encrypted_column(self): | ||
| """ | ||
| Test that decoding works with encryption policy when one column is encrypted. | ||
| """ | ||
| msg = self._create_mock_result_message() | ||
| f = self._create_mock_stream() | ||
|
|
||
| # Create mock encryption policy where first column is encrypted | ||
| mock_policy = Mock() | ||
| def contains_column_side_effect(col_desc): | ||
| return col_desc.col == 'col1' | ||
| mock_policy.contains_column = Mock(side_effect=contains_column_side_effect) | ||
| mock_policy.column_type = Mock(return_value=Int32Type) | ||
| mock_policy.decrypt = Mock(side_effect=lambda col_desc, val: val) | ||
|
|
||
| msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, mock_policy) | ||
|
|
||
| # Verify results | ||
| self.assertEqual(len(msg.parsed_rows), 2) | ||
| self.assertEqual(msg.parsed_rows[0][0], 42) | ||
| self.assertEqual(msg.parsed_rows[0][1], 'hello') | ||
|
|
||
| # Verify contains_column was called for each value (but policy existence check happens once) | ||
| # Should be called 4 times (2 rows × 2 columns) | ||
| self.assertEqual(mock_policy.contains_column.call_count, 4) | ||
|
|
||
| # Verify decrypt was called for each encrypted value (2 rows * 1 encrypted column) | ||
| self.assertEqual(mock_policy.decrypt.call_count, 2) | ||
|
|
||
| def test_optimization_efficiency(self): | ||
| """ | ||
| Verify that the optimization checks policy existence once per result message. | ||
| The key optimization is checking 'if column_encryption_policy:' once, | ||
| rather than 'column_encryption_policy and ...' for every value. | ||
| """ | ||
| msg = self._create_mock_result_message() | ||
|
|
||
| # Create more rows to make the check pattern clear | ||
| msg.recv_row = Mock(side_effect=[ | ||
| [int32_pack(i), f'text{i}'.encode()] for i in range(100) | ||
| ]) | ||
|
|
||
| # Create mock stream with 100 rows | ||
| f = io.BytesIO(int32_pack(100)) | ||
|
|
||
| mock_policy = Mock() | ||
| mock_policy.contains_column = Mock(return_value=False) | ||
|
|
||
| msg.recv_results_rows(f, ProtocolVersion.V4, {}, None, mock_policy) | ||
|
|
||
| # With optimization: policy existence checked once, contains_column called per value | ||
| # = 100 rows * 2 columns = 200 calls to contains_column | ||
| # The key is we avoid checking 'column_encryption_policy and ...' 200 times | ||
| self.assertEqual(mock_policy.contains_column.call_count, 200, | ||
| "contains_column should be called for each value when policy exists") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
|
Comment on lines
+154
to
+155
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please drop it. |
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move this class to
tests/unit/test_protocol.pyand rename it toResultTest