Added API and tests for CASSANDRA-10145#823
Conversation
beltran
left a comment
There was a problem hiding this comment.
The main concerns for me are:
- Should we use
ProtocolVersion.uses_keyspace_flagwhen building a BatchMessage, QueryMessage and PreparedMessage or not raise an exception from inside encoding? - Should we raise an exception if the use does something like
session.execute(SimpleStatement("SELECT * from system.local", keyspace="system")), which would break the current API. - Should we add the parameter
keyspacetosesion.prepareandsession.prepare_on_all_hosts
| if not success: | ||
| log.debug("Got unexpected response when preparing " | ||
| "statement on host %s: %r", host, response) | ||
| """ |
There was a problem hiding this comment.
This is my opinion where we get the biggest improvement, we don't have to set the keyspace of the connection, we can just send the query with the keyspace parameter, not sure how to add it though but a big if ProtocolVersion.uses_keyspace_flag(self._protocol_version)... else
There was a problem hiding this comment.
Yeah, that's a cool side effect! I'm ok with a big if statement, or adding a new function to deal with keyspace-setting.
|
|
||
| if isinstance(query, SimpleStatement): | ||
| query_string = query.query_string | ||
| statement_keyspace = query.keyspace |
There was a problem hiding this comment.
The reason we don't compare here if
keyspace = query.keyspace if ProtocolVersion.uses_keyspace_flag(self._protocol_version) else None
is because if the use creates a statement like
SimpleStatement("SELECT * from system.local".format(self.table_name), keyspace="system") I think a exception should be raised. This breakes the current API, since some applications doing this (may be bugged applications) will now raise an Exception if they are using a lower protocol.
| "BatchStatement execution is only supported with protocol version " | ||
| "2 or higher (supported in Cassandra 2.0 and higher). Consider " | ||
| "setting Cluster.protocol_version to 2 to support this operation.") | ||
| statement_keyspace = query.keyspace if ProtocolVersion.uses_keyspace_flag(self._protocol_version) else None |
There was a problem hiding this comment.
We need to compare here because a BoundStatment always has a keyspace set and this would raise an Exception under V5 protocol.
| log.exception("Error preparing query:") | ||
| raise | ||
|
|
||
| prepared_keyspace = message.keyspace if message.keyspace else self.keyspace |
There was a problem hiding this comment.
This will always be self.keyspace since the message is built like message = PrepareMessage(query=query). We can add the keyspace parameter to session.prepare but that's probably a different ticket.
|
|
||
|
|
||
| def get_default_protocol(): | ||
| def set_default_beta_flag_true(): |
There was a problem hiding this comment.
This set the flag allow_beta_protocol_version to True by default so we don't have to pass the argument. This is temporary since it looks like V5 won't be beta in Cassandra 4.0 although it is now.
| return prepared_statement | ||
|
|
||
| def prepare_on_all_hosts(self, query, excluded_host): | ||
| def prepare_on_all_hosts(self, query, excluded_host, keyspace=None): |
There was a problem hiding this comment.
Same as with prepare, should keyspace be added here as a parameter? I'm more inclined to add it here but maybe the best is to add it together with session.prepare since it's only called from inside that function.
| # set on queries with protocol version 5 or higher. Consider setting Cluster.protocol_version to 5.',), | ||
| # <Host: 127.0.0.2 datacenter1>: ConnectionException('Host has been marked down or removed',), | ||
| # <Host: 127.0.0.1 datacenter1>: ConnectionException('Host has been marked down or removed',)}) | ||
| with self.assertRaises(NoHostAvailable): |
There was a problem hiding this comment.
Should setting the keyspace in SimpleStatement raise UnsupportedOperation or UnsupportedOperation for old versions? As mentioned in a previous comment, raising something would break the API, since I believe now the keyspace is just ignored-
There was a problem hiding this comment.
Ah, good point. For now, let's ignore it, but maybe in the next major we want to change that. Could you add a note to PYTHON-504?
|
Well, as for the second point, we are doing that in CQLEngine when we create |
mambocab
left a comment
There was a problem hiding this comment.
Should we use
ProtocolVersion.uses_keyspace_flag when building aBatchMessage,QueryMessageandPreparedMessage` or not raise an exception from inside encoding?
I'm not entirely sure what choice you mean here, but I think it'd be correct for the encoding step to ignore keyspace if it's set but unsupported in the current protocol version.
Should we raise an exception if the use does something like
session.execute(SimpleStatement("SELECT * from system.local", keyspace="system")), which would break the current API.
I think I addressed this in my comments above -- I think we want to ignore stuff and not break the current API.
Should we add the parameter keyspace to sesion.prepare and session.prepare_on_all_hosts
I think we should -- the Java analogue takes a keyspace indirectly, but we can take it more directly.
| # set on queries with protocol version 5 or higher. Consider setting Cluster.protocol_version to 5.',), | ||
| # <Host: 127.0.0.2 datacenter1>: ConnectionException('Host has been marked down or removed',), | ||
| # <Host: 127.0.0.1 datacenter1>: ConnectionException('Host has been marked down or removed',)}) | ||
| with self.assertRaises(NoHostAvailable): |
There was a problem hiding this comment.
Ah, good point. For now, let's ignore it, but maybe in the next major we want to change that. Could you add a note to PYTHON-504?
| if not success: | ||
| log.debug("Got unexpected response when preparing " | ||
| "statement on host %s: %r", host, response) | ||
| """ |
There was a problem hiding this comment.
Yeah, that's a cool side effect! I'm ok with a big if statement, or adding a new function to deal with keyspace-setting.
| if not success: | ||
| log.debug("Got unexpected response when preparing " | ||
| "statement on host %s: %r", host, response) | ||
| """ |
There was a problem hiding this comment.
Sorry if I've missed something -- is this meant to be uncommented on merge?
mambocab
left a comment
There was a problem hiding this comment.
Small style change request for the send_chunks for sure -- and maybe we add docs for the new API?
| for i in range(0, len(ks_statements), 10): | ||
| chunks.append(ks_statements[i:i + 10]) | ||
|
|
||
| def send_chunks(chunks, set_keyspace=False): |
There was a problem hiding this comment.
I'd bring this out to a class-level function, but otherwise I love it
| chunks = [] | ||
| for i in range(0, len(statements), 10): | ||
| chunks.append(statements[i:i + 10]) | ||
| send_chunks(chunks, True) |
There was a problem hiding this comment.
You were right, this is totally sick
| fn(response_future, *args, **kwargs) | ||
|
|
||
| def prepare(self, query, custom_payload=None): | ||
| def prepare(self, query, custom_payload=None, keyspace=None): |
There was a problem hiding this comment.
So the next question here is this: should we document the keyspace param here? or is this mostly intended for internal consumption? What does a user gain from using this parameter? I can see it either way, as it's functionally redundant with specifying the keyspace in your query string for many/most users.
There was a problem hiding this comment.
I'm more inclined to document it, it shouldn't be long and it may be helpful for some users
|
|
||
| @greaterthanorequalcass40 | ||
| class SimpleWithKeyspaceTests(QueryKeyspaceTests, unittest.TestCase): | ||
| @unittest.skip |
There was a problem hiding this comment.
Why are we skipping this now?
There was a problem hiding this comment.
Specifying the keyspace before V5 is allowed right now and raising an exception would break the API. The keyspace is ignored bu there may be people doing it (like us in CQLEngine), I added that as part of PYTHON-504, we can remove the skip after that.
|
I've expanded the new documentation a little -- other than that, looks ready to go! |
PYTHON-678
1f6a338 to
e080510
Compare
No description provided.