Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support querying public ToolsDB databases #26

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion quarry/default_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ worker_prefetch_multiplier: 1 # Tasks can run for a long time
REPLICA_DOMAIN: '' # Change to `analytics.db.svc.wikimedia.cloud` for live replicas
REPLICA_USER: 'repl' # For live replicas, your replica.my.cnf username
REPLICA_PASSWORD: 'repl' # For live replicas, your replica.my.cnf password

REPLICA_PORT: 3306

TOOLS_DB_HOST: 'tools.db.svc.wikimedia.cloud'
TOOLS_DB_PORT: 3306
TOOLS_DB_USER: ''
TOOLS_DB_PASSWORD: ''

OUTPUT_PATH_TEMPLATE: '/results/%s/%s/%s.sqlite'
REDIS_HOST: 'redis'
REDIS_PORT: 6379
Expand Down
31 changes: 19 additions & 12 deletions quarry/web/replica.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ class Replica:
def __init__(self, config):
self.config = config
self.dbname = ""
self.is_tools_db = False

def _db_name_mangler(self):
if self.dbname == "":
raise ReplicaConnectionException(
"Attempting connection before a database is selected"
)

if self.dbname == "meta" or self.dbname == "meta_p":
if "__" in self.dbname and self.dbname.endswith("_p"):
self.is_tools_db = True
self.database_p = self.dbname
elif self.dbname == "meta" or self.dbname == "meta_p":
self.database_name = "s7"

self.database_p = "meta_p"
elif self.dbname == "centralauth" or self.dbname == "centralauth_p":
self.database_name = "s7"
Expand All @@ -36,6 +38,13 @@ def _db_name_mangler(self):
else "{}_p".format(self.dbname)
)

def get_host_name(self):
if self.is_tools_db:
return self.config["TOOLS_DB_HOST"]
if self.config["REPLICA_DOMAIN"]:
return f"{self.database_name}.{self.config['REPLICA_DOMAIN']}"
return self.database_name

@property
def connection(self):
self._replica.ping(reconnect=True)
Expand All @@ -52,22 +61,20 @@ def connection(self, db):

self.dbname = db
self._db_name_mangler()
repl_host = (
f"{self.database_name}.{self.config['REPLICA_DOMAIN']}"
if self.config["REPLICA_DOMAIN"]
else self.database_name
)
host = self.get_host_name()
conf_prefix = "TOOLS_DB" if self.is_tools_db else "REPLICA"
port = self.config[f"{conf_prefix}_PORT"]
connect_opts = {
"db": self.database_p,
"user": self.config["REPLICA_USER"],
"passwd": self.config["REPLICA_PASSWORD"],
"user": self.config[f"{conf_prefix}_USER"],
"passwd": self.config[f"{conf_prefix}_PASSWORD"],
"charset": "utf8",
"client_flag": pymysql.constants.CLIENT.MULTI_STATEMENTS,
}

if not self.config.get("REPLICA_SOCKS5_PROXY_HOST"):
self._replica = pymysql.connect(
host=repl_host, port=self.config["REPLICA_PORT"], **connect_opts
host=host, port=port, **connect_opts
)
else:
self._replica = pymysql.connect(defer_connect=True, **connect_opts)
Expand All @@ -78,7 +85,7 @@ def connection(self, db):
addr=self.config["REPLICA_SOCKS5_PROXY_HOST"],
port=self.config["REPLICA_SOCKS5_PROXY_PORT"],
)
sock.connect((repl_host, self.config["REPLICA_PORT"]))
sock.connect((host, port))
self._replica.connect(sock=sock)

@connection.deleter
Expand Down
2 changes: 1 addition & 1 deletion quarry/web/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


VALID_DB_NAMES = re.compile(
r"^(?:(?:(?:centralauth|meta|[0-9a-z_]*wik[a-z]+)(?:_p)?)|quarry)$"
r"^(?:(?:(?:centralauth|meta|[0-9a-z_]*wik[a-z]+)(?:_p)?)|quarry|s\d+__\w+_p)$"
)


Expand Down
Loading