Skip to content

feat: Introduce clusters - #860

Merged
lynnagara merged 46 commits into
masterfrom
cluster
Apr 28, 2020
Merged

lynnagara merged 46 commits into
masterfrom
cluster

Conversation

@lynnagara

Copy link
Copy Markdown
Contributor

Introduce the cluster class. The cluster provides a reader
and clickhouse connections shared by all storages that are
located on the cluster.

This PR does not yet change the settings format in any way and hardcodes
a single global cluster (essentially the same as what we had before).
This will be added in future.

Introduce the cluster class. The cluster provides a reader
and clickhouse connections shared by all storages that are
located on the cluster.

This PR does not yet change the settings format in any way and hardcodes
a single global cluster (essentially the same as what we had before).
This will be addedin future.
@lynnagara
lynnagara requested a review from a team April 1, 2020 18:02

@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.

First round of high level comments. I think the most important issue is that the lack of StorageSet (which should be a very simple abstraction is making it hard to grasp how this could work with queries that span multiple storages.
I would consider adding that here even if it makes the PR a little longer.

Comment thread snuba/clickhouse/dictquery.py Resolved
Comment thread snuba/clusters/__init__.py Resolved
Comment thread snuba/datasets/dataset.py Resolved
Comment thread snuba/clusters/__init__.py Resolved
Comment thread snuba/clusters/__init__.py Resolved
Comment thread snuba/datasets/storage.py Resolved
Comment thread snuba/datasets/groups.py Resolved
Comment thread snuba/datasets/storages/factory.py Resolved
Comment thread snuba/web/query.py Resolved

@tkaemming tkaemming 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.

Some scattershot comments inline from my first pass over this.

I think that already being familiar with the ideas here has helped me a lot as a reviewer to understand what is going on here without having to reverse engineer the intent of the code in front of me, I hope it made implementation similarly focused, this seems like a fast turnaround for such a major abstraction and overall a really good start.

Comment thread snuba/cli/bulk_load.py Resolved

@abstractmethod
def load(self, writer: BufferedWriterWrapper) -> None:
def load(self, writer: BufferedWriterWrapper, clickhouse_ro: ClickhousePool) -> 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 think we anticipated this as one of the impacted areas of this change, but this seems similar to the reader in that we'll want to find some sort of way to generalize this over time to break the dependency on the ClickHouse client directly.

Both the writer and the ClickHouse client here are derived the same writable storage when they are passed in from the command line tool. Should the writable storage be the thing that is passed here instead?

@lynnagara lynnagara Apr 14, 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.

Yeah, I think passing the writable storage here makes sense (at least for now). But it does feel a little odd since table writer is a property of storage itself.

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 did not yet find a good way to do this without introducing a circular dependency where the writer depends on the higher level storage.

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 method had this interface from the beginning, because, at the time, datasets had not ideas of readers and writers. clickhouse connection information (host/port or even the reader) could be passed through the CLI.
This PR is getting rid of that behavior and introducing a structural relationship between storage and storageset that, at runtime is enough to get the reader and the writer we should use for loading bulk data.
This means, the reader/writer should still be resolved at runtime, but not externally to the BulkLoader. If the BulkLoader is instantiated with the storageset (since it cannot get an instance of storage) that should be enough to resolve the cluster and fetch reader and writer.
I think it is worth making this change here since this PR is changing exactly the part of the infrastructure that required this pattern in the existing code. Fixing it would make the BulkLoader consistent with the new architecture.

The writer is a little different. I think it should be removed as well, since it should be possible to instantiate it in the same way: through the storageset. Though I noticed writers are still instantiated through setting.CLICKHOUSE_HOST instead of relying on the Cluster object.
See table_storage.py: TableWriter.get_writer().
Is that intentional or is it an oversight ?

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.

It's intentional in that I think many other changes need to happen here and I don't want to tackle it within the scope of this PR.

  • There are quite a number of places including this one where settings.CLICKHOUSE_HOST and friends are still being referenced, these all need to be removed of getting the information from the cluster. (optimize, cleanup, etc)
  • I think we should probably just pass a storage into the bulk load command and remove the ability to pass a dataset and destination table. The dest_table comes from the storage.
  • Also update confirm_load to do the same
  • It depends on moving the writer (assuming we want to move it into storage set), which is not in scope of this PR

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 am ok holding off on this. Could you just mention with a TODO that this interface is going to change and in which direction ?

Comment thread snuba/clusters/__init__.py Outdated
Comment on lines +43 to +47
cluster = Cluster(
host=settings.CLICKHOUSE_HOST,
port=settings.CLICKHOUSE_PORT,
http_port=settings.CLICKHOUSE_HTTP_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.

This works here, but I think trying to consolidate these resources in snuba.environment will be helpful over the long term because it enables us to eventually consolidate all of the different object instances that need to have some sort of lifecycle control.

There is a tech debt ticket for this, and some more details here, but I guess as of right now this is just a personal preference and not anything we've really discussed the overall merits and direction of… maybe we should?

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.

ping

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.

Agree we should decide on a consistent approach, can we hold off for now? Most other resources are not handled in that way yet. There's a bit more going on in here now and I'd prefer to keep it centralised in clusters until we make a decision.

Comment thread snuba/clusters/__init__.py Outdated
from snuba.clickhouse.query import ClickhouseQuery
from snuba.reader import Reader

class Cluster:

@tkaemming tkaemming Apr 2, 2020 •

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 it would make sense to split this into two classes (both concrete):

  1. A Cluster that exposes get_reader.
  2. A ClickhouseCluster that subclasses Cluster and exposes the additional methods that return the ClickHouse clients.

I think in an idealized model we'd be able to only depend on Cluster and all of the ClickHouse implementation details would be abstracted away, but it in the mean time this would allow us to understand what is still dependent on the ClickHouse-specific functionality.

In a way I think the strict dependency on ClickHouse would be considered tech debt, but the low-risk, low-interest type that we don't really need to be in any hurry to pay off. It's beneficial to know that it exists, though.

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.

ping

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.

Updated

Comment thread snuba/clusters/__init__.py Resolved
Comment thread snuba/datasets/dataset.py Resolved
Comment thread snuba/web/db_query.py Outdated
def raw_query(
request: Request,
query: ClickhouseQuery,
reader: Reader,

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.

Doesn't this need a bound type variable?

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.

it does.

lynnagara added a commit that referenced this pull request Apr 8, 2020
This simply moves the DictClickhouseQuery into a separate file from
ClickhouseQuery.

This has come up before in #844 and #860, and causes circular
dependencies since DictClickhouseQuery (which will be deprecated)
currently depends on Dataset.
lynnagara added a commit that referenced this pull request Apr 8, 2020
This simply moves the DictClickhouseQuery into a separate file from
ClickhouseQuery.

This has come up before in #844 and #860, and causes circular
dependencies since DictClickhouseQuery (which will be deprecated)
currently depends on Dataset.

@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 for adding the storageset. I think we should be more restrictive on the typing there. See comments inline. There are quite a few guarantees of correctness we can enforce with the type checker instead of at runtime.
Also, please see my comments around leaking the storage to web/query. They still apply.

Comment thread snuba/clusters/cluster.py Resolved
Comment thread snuba/clusters/cluster.py Resolved
Comment thread snuba/datasets/dataset.py Resolved
Comment thread snuba/datasets/plans/query_plan.py Resolved
Comment thread snuba/clusters/storage_sets.py Resolved
Comment thread snuba/datasets/storage.py Outdated
Comment on lines +40 to +41
def get_cluster(self) -> Cluster:
return get_cluster(self.__storage_key)

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 would be confusing. It would make people think that storages are directly assigned to clusters while they are assigned only through StorageSet. I think you could either document that relationship here (in details) considering this is the method in the abstract class, or just return the storage set that just makes it clear.

Comment thread snuba/datasets/storages/errors.py Resolved
Comment thread snuba/web/query.py Resolved
Comment thread snuba/clusters/storage_sets.py Resolved
Comment thread snuba/clusters/cluster.py Resolved
Comment thread snuba/clusters/storage_sets.py Resolved
Comment thread snuba/clusters/storage_sets.py Resolved
Comment thread snuba/web/query.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 this is solid, thanks for doing this and for applying all the comments.

Comment thread snuba/clusters/cluster.py Outdated
(set(_registered_storage_sets))
), "Storage set registered to more than one cluster"

assert set([s for s in StorageSetKey]) == set(

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.

you are using the set of registered stored set twice. You can assign it to a variable so you do not need to recreate it.
Also you do not need to create a list of StorageKeys first and then turn it into a set. set(s for s in StorageSetKey) would work.

Comment thread snuba/clusters/cluster.py Outdated
Comment on lines +88 to +91
_STORAGE_SET_CLUSTER_MAP: MutableMapping[StorageSetKey, Cluster] = {}
for cluster in CLUSTERS:
for storage_set_key in cluster.get_storage_sets():
_STORAGE_SET_CLUSTER_MAP[storage_set_key] = cluster

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 you should be able to build this with a dict comprehension.

Comment thread snuba/datasets/storage.py Resolved

@abstractmethod
def load(self, writer: BufferedWriterWrapper) -> None:
def load(self, writer: BufferedWriterWrapper, clickhouse_ro: ClickhousePool) -> 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 am ok holding off on this. Could you just mention with a TODO that this interface is going to change and in which direction ?

@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.

Sorry I think I missed one comment last night.

Comment thread snuba/clusters/cluster.py Outdated
from snuba.reader import Reader


class ClickhouseCluster:

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.

Wait, I think this hierarchy is inverted.

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.

Cluster should be the parent class and only expose the abstract concepts like reader and storage set, while the subclass (clickhouse should be used only when you need the clickhouse specific concept). This allows us to drive the design in a direction that only depends on the abstract class (and as a consequence being independent on the implementation).
Was there a reason to make the parent class, clickhouse specific?

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.

This was a mistake, inverted these classes now.

@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 this is fine. Just a couple of remarks, if you want to implement them. I know Ted wanted to review this as well, so I will wait for that before accepting.

Comment thread snuba/clusters/cluster.py Outdated
return {StorageSetKey(storage_set) for storage_set in self.__storage_sets}

@abstractmethod
def get_reader(self) -> Reader[ClickhouseQuery]:

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.

well, indeed, making the Reader clickhouse specific means this does not work anymore because the Cluster interface becomes clickhouse specific.
You can make this class generic itself and based on the abstract generic reader class and materialize the parameter type in the clickhouse implementation.

class Cluster(ABC, Generic[TQuery]):
     def get_reader(self) -> Reader[TQuery]

class ClickhouseCluster(Cluster[ClickhouseQuery]):
     def get_reader(self) -> Reader[ClickhouseQuery]

I think the callsites should still type check because the everywhere this is used, we already reference the ClickhouseCluster explicitly

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.

Ah, thank you.

Comment thread snuba/clusters/cluster.py Outdated
self.__reader: Reader[ClickhouseQuery] = NativeDriverReader(
self.__clickhouse_ro
)
super().__init__(storage_sets)

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.

nit: putting the parent class initialization at the beginning makes it teasier to see the correctness of the method.

Comment thread snuba/clusters/cluster.py Resolved
Comment thread snuba/datasets/storage.py Resolved
Comment thread snuba/clusters/cluster.py
return self.__reader


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.

Since now we have clickhouse clusters you may want to call this CLICKHOUSE_CLUSTERS, but won't block for this. Up to you.

@tkaemming tkaemming 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.

This looks good and should be very helpful moving forward. I noted one minor thing inline that was a bit hard to understand at a glance.

Comment thread snuba/clusters/cluster.py Outdated
Comment on lines +32 to +33
def get_storage_sets(self) -> Set[StorageSetKey]:
return {StorageSetKey(storage_set) for storage_set in self.__storage_sets}

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.

For consistency with: https://github.com/getsentry/snuba/pull/860/files#r413246184

Suggested change
def get_storage_sets(self) -> Set[StorageSetKey]:
return {StorageSetKey(storage_set) for storage_set in self.__storage_sets}
def get_storage_set_keys(self) -> Set[StorageSetKey]:
return {StorageSetKey(storage_set) for storage_set in self.__storage_sets}

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.

Thank you

@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 for doing this.

@lynnagara
lynnagara merged commit d718e6c into master Apr 28, 2020
lynnagara added a commit that referenced this pull request Apr 28, 2020
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 added a commit that referenced this pull request May 14, 2020
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. This has been updated in production.
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.

4 participants