Skip to content

Commit

Permalink
fix distributed tests (#45)
Browse files Browse the repository at this point in the history
* Fix distributed and replicated table engine tests, add test guide to README.md.

* refactor migrations patch

* fix flush command
  • Loading branch information
jayvynl committed Sep 16, 2023
1 parent 4e16435 commit 3cd414f
Show file tree
Hide file tree
Showing 16 changed files with 264 additions and 330 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Use [flake8](https://flake8.pycqa.org/) to lint code.
- Add GitHub action which runs tests.
- Add test coverage to ci and send data to coveralls, add coverage badge.
- Fix distributed and replicated table engine tests, add test guide to README.md.

### 1.1.1
- [Black](https://github.com/psf/black) code style.
Expand Down
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,10 @@ DATABASES = {
"OPTIONS": {
"migration_cluster": "cluster",
"settings": {
"mutations_sync": 1,
"mutations_sync": 2,
"insert_distributed_sync": 1,
"insert_quorum": 2,
"alter_sync": 2,
},
},
"TEST": {"cluster": "cluster"},
Expand All @@ -397,35 +399,41 @@ DATABASES = {
"OPTIONS": {
"migration_cluster": "cluster",
"settings": {
"mutations_sync": 1,
"mutations_sync": 2,
"insert_distributed_sync": 1,
"insert_quorum": 2,
"alter_sync": 2,
},
},
"TEST": {"cluster": "cluster", "managed": False},
"TEST": {"cluster": "cluster", "managed": False, "DEPENDENCIES": ["default"]},
},
"s2r1": {
"ENGINE": "clickhouse_backend.backend",
"PORT": 9002,
"OPTIONS": {
"migration_cluster": "cluster",
"settings": {
"mutations_sync": 1,
"mutations_sync": 2,
"insert_distributed_sync": 1,
"insert_quorum": 2,
"alter_sync": 2,
},
},
"TEST": {"cluster": "cluster", "managed": False},
"TEST": {"cluster": "cluster", "managed": False, "DEPENDENCIES": ["default"]},
},
"s2r2": {
"ENGINE": "clickhouse_backend.backend",
"PORT": 9003,
"OPTIONS": {
"migration_cluster": "cluster",
"settings": {
"mutations_sync": 1,
"mutations_sync": 2,
"insert_distributed_sync": 1,
"insert_quorum": 2,
"alter_sync": 2,
},
},
"TEST": {"cluster": "cluster", "managed": False},
"TEST": {"cluster": "cluster", "managed": False, "DEPENDENCIES": ["default"]},
},
}
```
Expand All @@ -434,15 +442,25 @@ Extra settings explanation:

- `"migration_cluster": "cluster"`
Migration table will be created on this cluster if this setting is specified, otherwise only local migration table is created.
- `"mutations_sync": 1`
This is suggested if you want to test [data mutations](https://clickhouse.com/docs/en/guides/developer/mutations).
- `"mutations_sync": 2`
This is suggested if you want to test [data mutations](https://clickhouse.com/docs/en/guides/developer/mutations) on replicated table.
*Don't* set this in production environment.
- `"insert_distributed_sync": 1`
This is suggested if you want to test inserting data into distributed table.
- `"TEST": {"cluster": "cluster", "managed": False}`
*Don't* set this in production environment.
- `"insert_quorum": 2`
This is suggested if you want to test inserting data into replicated table.
The value is set to replica number.
- `"alter_sync": 2`
This is suggested if you want to test altering or truncating replicated table.
*Don't* set this in production environment.
- `"TEST": {"cluster": "cluster", "managed": False, "DEPENDENCIES": ["default"]}`
Test database will be created on this cluster.
If you have multiple database connections to the same cluster and want to run tests over all these connections,
then only one connection should set `"managed": True`(the default value), other connections should set `"managed": False`.
So that test database will not be created multiple times.

If your managed database alias is not `s1r2`, `"DEPENDENCIES": ["s1r2"]` should be set to ensure the [creation order for test databases](https://docs.djangoproject.com/en/4.2/topics/testing/advanced/#controlling-creation-order-for-test-databases).

Do not hardcode database name when you define replicated table or distributed table.
Because test database name is different from deployed database name.
Expand Down
4 changes: 2 additions & 2 deletions clickhouse-config/node1/remote-servers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<cluster>
<secret>mysecretphrase</secret>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node1</host>
<port>9000</port>
Expand All @@ -14,7 +14,7 @@
</replica>
</shard>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node3</host>
<port>9000</port>
Expand Down
4 changes: 2 additions & 2 deletions clickhouse-config/node2/remote-servers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<cluster>
<secret>mysecretphrase</secret>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node1</host>
<port>9000</port>
Expand All @@ -14,7 +14,7 @@
</replica>
</shard>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node3</host>
<port>9000</port>
Expand Down
4 changes: 2 additions & 2 deletions clickhouse-config/node3/remote-servers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<cluster>
<secret>mysecretphrase</secret>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node1</host>
<port>9000</port>
Expand All @@ -14,7 +14,7 @@
</replica>
</shard>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node3</host>
<port>9000</port>
Expand Down
4 changes: 2 additions & 2 deletions clickhouse-config/node4/remote-servers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<cluster>
<secret>mysecretphrase</secret>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node1</host>
<port>9000</port>
Expand All @@ -14,7 +14,7 @@
</replica>
</shard>
<shard>
<internal_replication>false</internal_replication>
<internal_replication>true</internal_replication>
<replica>
<host>node3</host>
<port>9000</port>
Expand Down
37 changes: 20 additions & 17 deletions clickhouse_backend/backend/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,27 +278,30 @@ def regex_lookup(self, lookup_type):
def table_model_dict(self):
return {model._meta.db_table: model for model in apps.get_models()}

def _get_on_cluster(self, style, table, sync=False):
if table in self.table_model_dict:
cluster = getattr(self.table_model_dict[table], "cluster", None)
if cluster:
if sync:
return (
f"ON CLUSTER {style.SQL_FIELD(self.quote_name(cluster))} SYNC"
)
return f"ON CLUSTER {style.SQL_FIELD(self.quote_name(cluster))}"
return ""
def sql_flush_table(self, style, table):
from clickhouse_backend import models

def sql_flush(self, style, tables, *, reset_sequences=False, allow_cascade=False):
return [
"%s %s %s"
% (
cluster = None
if table in self.table_model_dict:
opts = self.table_model_dict[table]._meta
engine = getattr(opts, "engine", None)
if isinstance(engine, models.Distributed):
table = engine.table
cluster = engine.cluster

if cluster:
return "%s %s ON CLUSTER %s" % (
style.SQL_KEYWORD("TRUNCATE"),
style.SQL_FIELD(self.quote_name(table)),
self._get_on_cluster(style, table),
style.SQL_FIELD(self.quote_name(cluster)),
)
for table in tables
]
return "%s %s" % (
style.SQL_KEYWORD("TRUNCATE"),
style.SQL_FIELD(self.quote_name(table)),
)

def sql_flush(self, style, tables, *, reset_sequences=False, allow_cascade=False):
return [self.sql_flush_table(style, table) for table in tables]

def prep_for_iexact_query(self, x):
return x
Expand Down
2 changes: 2 additions & 0 deletions clickhouse_backend/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ def get_queryset(self):

class ClickhouseModel(models.Model):
objects = ClickhouseManager()
_overwrite_base_manager = ClickhouseManager()

class Meta:
abstract = True
base_manager_name = "_overwrite_base_manager"

def _do_update(self, base_qs, using, pk_val, values, update_fields, forced_update):
"""
Expand Down

0 comments on commit 3cd414f

Please sign in to comment.