Skip to content

ref(clusters): Get Clickhouse connections from cluster - #897

Merged
lynnagara merged 16 commits into
masterfrom
refactor-cluster-connection
May 14, 2020
Merged

lynnagara merged 16 commits into
masterfrom
refactor-cluster-connection

Conversation

@lynnagara

@lynnagara lynnagara commented Apr 24, 2020 •

Copy link
Copy Markdown
Contributor

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:

  • Update cleanup script in production to pass a ClickHouse port
  • Update optimize script in production to pass a ClickHouse port

@lynnagara
lynnagara changed the base branch from cluster to master April 28, 2020 17:22
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.
@lynnagara
lynnagara force-pushed the refactor-cluster-connection branch from a113af2 to 7c2c721 Compare April 28, 2020 17:25
@lynnagara
lynnagara marked this pull request as ready for review April 28, 2020 17:26
@lynnagara
lynnagara requested a review from a team April 28, 2020 17:26

@fpacifici fpacifici left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there are a few implementation issue and we need to clarify a topic about connection settings. See comment inline

Comment thread snuba/clusters/cluster.py Resolved
Comment thread snuba/cli/cleanup.py Resolved
Comment thread snuba/cli/optimize.py Outdated
Comment on lines +64 to +66
clickhouse_connections = [
cluster.get_connection(send_receive_timeout=timeout) for cluster in CLUSTERS
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as for cleanup. This works on the local table so on different hosts.

Comment thread snuba/clusters/cluster.py Outdated
class ClickhouseClientSettings(Enum):
READONLY = {"readonly": True}
READWRITE: Mapping[str, Any] = {}
REPLACER = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread snuba/cli/cleanup.py Outdated
for cluster in CLUSTERS
]

for connection in clickhouse_connections:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thanks this was an oversight

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread snuba/cli/optimize.py Resolved

@fpacifici fpacifici left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are pretty close. Only some lower level comments

Comment thread snuba/clusters/cluster.py
from snuba.writer import BatchWriter, WriterTableRow


class ClickhouseClientSettings(Enum):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

@lynnagara lynnagara May 8, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread snuba/cli/cleanup.py Outdated
Comment on lines +56 to +57
elif not local_dataset_mode():
raise click.ClickException("Provide Clickhouse host and port for cleanup")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread snuba/cli/cleanup.py Outdated
for cluster in CLUSTERS
]

for connection in clickhouse_connections:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread snuba/clusters/cluster.py Outdated
def get_connection(
self,
client_settings: ClickhouseClientSettings,
send_receive_timeout: Optional[int] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 fpacifici left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread snuba/clusters/cluster.py Outdated
def get_connection(
self,
client_settings: ClickhouseClientSettings,
send_receive_timeout: Optional[int] = None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

@lynnagara

Copy link
Copy Markdown
Contributor Author

You're right, it seems like no one was passing a different timeout value to optimize - removed the option.

@fpacifici fpacifici left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two minor issues. Please ping me when they are corrected. Otherwise this is ready.

Comment thread snuba/cli/optimize.py
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:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Comment thread snuba/clusters/cluster.py Outdated
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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, moved it into the client settings now.

Comment thread snuba/clusters/cluster.py Outdated
from snuba.writer import BatchWriter, WriterTableRow


ClickhouseClientSettingsType = Tuple[Mapping[str, Any], Optional[int]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems a good candidate for a NamedTuple so people know what that Optional[int] is.

Comment thread snuba/clusters/cluster.py Outdated
connection.
"""
if client_settings not in self.__connection_cache:
[settings, timeout] = client_settings.value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

settings, timeout = client_settings.value

?

Comment thread snuba/optimize.py Outdated

from snuba import util

OPTIMIZE_SEND_RECEIVE_TIMEOUT = 10000

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment on clusters

@fpacifici fpacifici left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@fpacifici fpacifici left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

@lynnagara
lynnagara merged commit 46d58fc into master May 14, 2020
@lynnagara
lynnagara deleted the refactor-cluster-connection branch May 14, 2020 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants