Repository navigation
ref(clusters): Get Clickhouse connections from cluster - #897
Conversation
This is a follow up to #860. Clickhouse connections for the cleanup, optimize and replacer scripts now come from the cluster, rather than from the soon to be deprecated global CLICKHOUSE_HOST and CLICKHOUSE_PORT values. The cluster now lazily creates Clickhouse connections if required, rather than creating each possible configuration on instantiation.
a113af2 to
7c2c721
Compare
fpacifici
left a comment
There was a problem hiding this comment.
I think there are a few implementation issue and we need to clarify a topic about connection settings. See comment inline
| clickhouse_connections = [ | ||
| cluster.get_connection(send_receive_timeout=timeout) for cluster in CLUSTERS | ||
| ] |
There was a problem hiding this comment.
Same as for cleanup. This works on the local table so on different hosts.
| class ClickhouseClientSettings(Enum): | ||
| READONLY = {"readonly": True} | ||
| READWRITE: Mapping[str, Any] = {} | ||
| REPLACER = { |
There was a problem hiding this comment.
I think this way of defining connection parameters seems quite ad-hoc for the specific use cases we have right now. I think we may be missing a decision to make:
given that the cluster is provided in configuration and the application should move towards a place where it does not care much about the connection. parameters, should connection settings be:
- hardcoded and classified in categories so that the code picks up the connection. it needs
- in settings and classified in categories so that the code keeps identifying the connection by a logical name, that then gets resolved to parameters
- entirely up to the application, such that the application code directly provides the connection settings it needs.
(I don't like the third) but we should agree on the direction.
Also:
Do you have already an idea about whether we can transition the replacer to use reader and writer and not be Clickhouse specific ? I think we should have at least an idea on how to do that, because this ad-hoc connection seems inconsistent with the other two.
Looking at settings, having a generic readonly and readwrite makes sense, but mixing it with an application specific definition (replacer) makes it hard to understand which kinds of connections are supposed to be added here.
On the other hand we could move totally the opposite way and have connection identifying the use case, like query, insert, replace, optimize, etc.
Which is the direction you plan to move towards ?
There was a problem hiding this comment.
I don't know of any reasons why most this these options will need to be configurable by users (apart from the replacer_max_block_size and replacer_max_memory_usage settings that already exists). Maybe in the future being able to adjust more of these options might be needed. Right now they seem largely application dependent to me. e.g. We'll always want readonly for querying and readwrite for DDL, etc.
Unfortunately, we can't do away with the replacer specific options and have generic read and readwrite only. So moving to the second option you mentioned (connection by use case) seems the most viable option to me.
| for cluster in CLUSTERS | ||
| ] | ||
|
|
||
| for connection in clickhouse_connections: |
There was a problem hiding this comment.
I think this will be quite fragile.
We are running this run_cleanup on all clusters and not only on the clusters where the table is present.
This incidentally works now because run_cleanup would pick up the parts to clean by selecting the parts where table = xxx. But this is not guaranteed.
Passing invalid arguments to a method with the assumption that it will handle them gracefully and generate correct results anyway is dangerous and easy to break.
Any way we can focus only on the cluster that handles the writer for the dataset passed in input ?
There was a problem hiding this comment.
Yes, thanks this was an oversight
There was a problem hiding this comment.
I think this is still to broad.
we take all clusters from a dataset. Not only the writer ones.
Then we run the cleanup for the same table on all of them, while we should be running the cleanup only on the connection for the cluster that holds the table.
There was a problem hiding this comment.
Ok. Cleanup only makes sense for the writable storages right? Do you know if there's any use case for running cleanup on a table that is not our writable storage table?
fpacifici
left a comment
There was a problem hiding this comment.
I think we are pretty close. Only some lower level comments
| from snuba.writer import BatchWriter, WriterTableRow | ||
|
|
||
|
|
||
| class ClickhouseClientSettings(Enum): |
There was a problem hiding this comment.
Do you expect these settings to be different when running on a single node deployment vs running it on the cluster as a whole (through query nodes) vs running on a cluster but getting a connection to a specific node ?
There was a problem hiding this comment.
I don't expect them to be different. I think it's a future possibility, like if we add some task that only runs on the multi node version. But right now this is mostly a refactor of the existing settings which are identical for both single node and cluster.
| elif not local_dataset_mode(): | ||
| raise click.ClickException("Provide Clickhouse host and port for cleanup") |
There was a problem hiding this comment.
Have you verified we never rely on the default in any environment where we may be running in dist mode and changing the settings to connect to the specific node instead of passing the parameters in the CLI ?
There was a problem hiding this comment.
We do actually pass a ClickHouse host without a port (relying on default) in production right now. I think it's worth keeping this change and making sure we update there before this PR is merged though.
| for cluster in CLUSTERS | ||
| ] | ||
|
|
||
| for connection in clickhouse_connections: |
There was a problem hiding this comment.
I think this is still to broad.
we take all clusters from a dataset. Not only the writer ones.
Then we run the cleanup for the same table on all of them, while we should be running the cleanup only on the connection for the cluster that holds the table.
| def get_connection( | ||
| self, | ||
| client_settings: ClickhouseClientSettings, | ||
| send_receive_timeout: Optional[int] = None, |
There was a problem hiding this comment.
I don't see this ever being used. Is this intentional ?
Also, aside from the fact that we pass this parameter during the initialization of ClickhousePool separately from the settings, is there any reason not to keep this hardcoded with the rest of the settings in ClickhouseClientSettings?
The fact that clickhouse takes settings and timeout as separate parameters is a clickhouse driver implmentation detail that should not drive our api. If we make the callsite pick the use case without specifying parameters, we should not allow the callsite to specify the timeout either.
There was a problem hiding this comment.
Hmm, yes. It's supposed to be an argument passed by user from the optimize CLI script (which is why it can't be provided on initialization). Fixing.
There was a problem hiding this comment.
Do we actually know the timeout is actually something we need? If we could remove it we will cut away quite a bit of complexity. I feel having this ad hoc parameter that is configured on the command line while all other connection parameter come from the cluster configuration is going against our design.
Could you please verify whether we absolutely need to maintain this field ?
fpacifici
left a comment
There was a problem hiding this comment.
Let's just verify whether we really need to preserve the timeout CLI parameter instead of getting rid of it in favor of making it part of the cluster configuration like all other connection settings.
Otherwise it is ready.
| def get_connection( | ||
| self, | ||
| client_settings: ClickhouseClientSettings, | ||
| send_receive_timeout: Optional[int] = None, |
There was a problem hiding this comment.
Do we actually know the timeout is actually something we need? If we could remove it we will cut away quite a bit of complexity. I feel having this ad hoc parameter that is configured on the command line while all other connection parameter come from the cluster configuration is going against our design.
Could you please verify whether we absolutely need to maintain this field ?
|
You're right, it seems like no one was passing a different timeout value to optimize - removed the option. |
| num_dropped = run_optimize(clickhouse, database, table, before=today) | ||
| logger.info("Optimized %s partitions on %s" % (num_dropped, clickhouse_host)) | ||
|
|
||
| if clickhouse_host and clickhouse_port: |
There was a problem hiding this comment.
If I get this correctly, in distributed mode, we are now providing host and port but in the future we should be able to run this loading the cluster from the configuration.
If my understanding is correct, mind adding a TODO that explains that this is going to evolve and we are not planning to pass host an port as parameters forever in distributed mode ?
| from snuba.clickhouse.native import ClickhousePool, NativeDriverReader | ||
| from snuba.clickhouse.sql import SqlQuery | ||
| from snuba.clusters.storage_sets import StorageSetKey | ||
| from snuba.optimize import OPTIMIZE_SEND_RECEIVE_TIMEOUT |
There was a problem hiding this comment.
I don't think the cluster (which is a low level abstraction should depend on the optimize process. At best the opposite should be true. This seems a settings of one specific use case of clickhouse client settings (which is here). What about defining it here and make optimize depend on this instead?
There was a problem hiding this comment.
Sounds good, moved it into the client settings now.
| from snuba.writer import BatchWriter, WriterTableRow | ||
|
|
||
|
|
||
| ClickhouseClientSettingsType = Tuple[Mapping[str, Any], Optional[int]] |
There was a problem hiding this comment.
This seems a good candidate for a NamedTuple so people know what that Optional[int] is.
| connection. | ||
| """ | ||
| if client_settings not in self.__connection_cache: | ||
| [settings, timeout] = client_settings.value |
There was a problem hiding this comment.
Why not
settings, timeout = client_settings.value
?
|
|
||
| from snuba import util | ||
|
|
||
| OPTIMIZE_SEND_RECEIVE_TIMEOUT = 10000 |
There was a problem hiding this comment.
See my comment on clusters
This is a follow up to #860.
Clickhouse connections for the cleanup, optimize and replacer scripts
now come from the cluster, rather than from the soon to be deprecated
global CLICKHOUSE_HOST and CLICKHOUSE_PORT values.
The cluster now lazily creates Clickhouse connections if required,
rather than creating each possible configuration on instantiation.
Note that it is no longer possible to pass a clickhouse host without a port via the CLI
Before this PR is merged we need to: