Skip to content

Commit

Permalink
feat: Add Preferred Replica model and prefer those replicas when disc…
Browse files Browse the repository at this point in the history
…overing replicas for shards (#65)

* feat: Add Preferred Replica model and prefer those replicas when discovering replicas for shards

* shard->replica for consistency and correctness
  • Loading branch information
fuziontech authored May 29, 2024
1 parent 1379d7a commit 849261c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
6 changes: 6 additions & 0 deletions housewatch/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.contrib import admin

from housewatch.models.preferred_replica import PreferredReplica


admin.site.register(PreferredReplica)
15 changes: 14 additions & 1 deletion housewatch/clickhouse/clusters.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from collections import defaultdict
from housewatch.clickhouse.client import run_query

from housewatch.models.preferred_replica import PreferredReplica


def get_clusters():
QUERY = """Select cluster, shard_num, shard_weight, replica_num, host_name, host_address, port, is_local, user, default_database, errors_count, slowdowns_count, estimated_recovery_time FROM system.clusters"""
Expand Down Expand Up @@ -28,8 +30,19 @@ def get_shards(cluster):


def get_node_per_shard(cluster):
# We want to return a node per shard, but if we have preferred replicas we should use those

shards = get_shards(cluster)
nodes = []

preferred = PreferredReplica.objects.filter(cluster=cluster).values_list("replica", flat=True)
for shard, n in shards.items():
nodes.append((shard, n[0]))
preferred_replica_found = False
for node in n:
if node["host_name"] in preferred:
nodes.append((shard, node))
preferred_replica_found = True
break
if not preferred_replica_found:
nodes.append((shard, n[0]))
return nodes
23 changes: 23 additions & 0 deletions housewatch/migrations/0012_preferredreplica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.1 on 2024-05-29 16:34

from django.db import migrations, models
import uuid


class Migration(migrations.Migration):

dependencies = [
('housewatch', '0011_scheduledbackup_is_sharded_and_more'),
]

operations = [
migrations.CreateModel(
name='PreferredReplica',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('created_at', models.DateTimeField(auto_now_add=True)),
('cluster', models.CharField(max_length=255)),
('replica', models.CharField(max_length=255)),
],
),
]
9 changes: 9 additions & 0 deletions housewatch/models/preferred_replica.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import uuid
from django.db import models


class PreferredReplica(models.Model):
id: models.UUIDField = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
created_at: models.DateTimeField = models.DateTimeField(auto_now_add=True)
cluster: models.CharField = models.CharField(max_length=255)
replica: models.CharField = models.CharField(max_length=255)

0 comments on commit 849261c

Please sign in to comment.