Repository navigation
Conversation
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.
fpacifici
left a comment
There was a problem hiding this comment.
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.
tkaemming
left a comment
There was a problem hiding this comment.
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.
|
|
||
| @abstractmethod | ||
| def load(self, writer: BufferedWriterWrapper) -> None: | ||
| def load(self, writer: BufferedWriterWrapper, clickhouse_ro: ClickhousePool) -> None: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 ?
| cluster = Cluster( | ||
| host=settings.CLICKHOUSE_HOST, | ||
| port=settings.CLICKHOUSE_PORT, | ||
| http_port=settings.CLICKHOUSE_HTTP_PORT, | ||
| ) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
| from snuba.clickhouse.query import ClickhouseQuery | ||
| from snuba.reader import Reader | ||
|
|
||
| class Cluster: |
There was a problem hiding this comment.
I think it would make sense to split this into two classes (both concrete):
- A
Clusterthat exposesget_reader. - A
ClickhouseClusterthat subclassesClusterand 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.
| def raw_query( | ||
| request: Request, | ||
| query: ClickhouseQuery, | ||
| reader: Reader, |
There was a problem hiding this comment.
Doesn't this need a bound type variable?
fpacifici
left a comment
There was a problem hiding this comment.
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.
| def get_cluster(self) -> Cluster: | ||
| return get_cluster(self.__storage_key) |
There was a problem hiding this comment.
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.
| (set(_registered_storage_sets)) | ||
| ), "Storage set registered to more than one cluster" | ||
|
|
||
| assert set([s for s in StorageSetKey]) == set( |
There was a problem hiding this comment.
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.
| _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 |
There was a problem hiding this comment.
I think you should be able to build this with a dict comprehension.
|
|
||
| @abstractmethod | ||
| def load(self, writer: BufferedWriterWrapper) -> None: | ||
| def load(self, writer: BufferedWriterWrapper, clickhouse_ro: ClickhousePool) -> None: |
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
Sorry I think I missed one comment last night.
| from snuba.reader import Reader | ||
|
|
||
|
|
||
| class ClickhouseCluster: |
There was a problem hiding this comment.
Wait, I think this hierarchy is inverted.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
This was a mistake, inverted these classes now.
fpacifici
left a comment
There was a problem hiding this comment.
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.
| return {StorageSetKey(storage_set) for storage_set in self.__storage_sets} | ||
|
|
||
| @abstractmethod | ||
| def get_reader(self) -> Reader[ClickhouseQuery]: |
There was a problem hiding this comment.
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
| self.__reader: Reader[ClickhouseQuery] = NativeDriverReader( | ||
| self.__clickhouse_ro | ||
| ) | ||
| super().__init__(storage_sets) |
There was a problem hiding this comment.
nit: putting the parent class initialization at the beginning makes it teasier to see the correctness of the method.
| return self.__reader | ||
|
|
||
|
|
||
| CLUSTERS = [ |
There was a problem hiding this comment.
Since now we have clickhouse clusters you may want to call this CLICKHOUSE_CLUSTERS, but won't block for this. Up to you.
tkaemming
left a comment
There was a problem hiding this comment.
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.
| def get_storage_sets(self) -> Set[StorageSetKey]: | ||
| return {StorageSetKey(storage_set) for storage_set in self.__storage_sets} |
There was a problem hiding this comment.
For consistency with: https://github.com/getsentry/snuba/pull/860/files#r413246184
| 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} |
fpacifici
left a comment
There was a problem hiding this comment.
thanks for doing this.
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.
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.
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.