From a794fb3d31784ec4c6d1c633bc8317d3d35c3569 Mon Sep 17 00:00:00 2001 From: Gonzalo Riestra Date: Fri, 8 Mar 2019 12:03:55 +0100 Subject: [PATCH] read tis config from cdb_conf --- scripts-available/CDB_GhostTables.sql | 33 ++++++++++++++++----------- test/CDB_GhostTables.sql | 2 ++ test/CDB_GhostTables_expect | 2 ++ test/CDB_Username.sql | 1 + 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/scripts-available/CDB_GhostTables.sql b/scripts-available/CDB_GhostTables.sql index c1d916e..c94b64f 100644 --- a/scripts-available/CDB_GhostTables.sql +++ b/scripts-available/CDB_GhostTables.sql @@ -8,37 +8,44 @@ AS $$ if not username: return - client = GD.get('redis', None) + if 'json' not in GD: + import json + GD['json'] = json + else: + json = GD['json'] - retry = 3 error = '' - # TODO: read TIS config from cdb_conf - tis_host = '127.0.0.1' - tis_port = 6379 - tis_timeout = 5 + tis_config = plpy.execute("select cartodb.CDB_Conf_GetConf('invalidation_service');")[0]['cdb_conf_getconf'] + tis_config_dict = json.loads(tis_config) if tis_config else {} + tis_host = tis_config_dict.get('host', '127.0.0.1') + tis_port = tis_config_dict.get('port', 3142) + tis_timeout = tis_config_dict.get('timeout', 5) + tis_retry = tis_config_dict.get('retry', 5) + + client = GD.get('invalidation', None) while True: if not client: try: import redis - client = GD['redis'] = redis.Redis(host=tis_host, port=tis_port, socket_timeout=tis_timeout) + client = GD['invalidation'] = redis.Redis(host=tis_host, port=tis_port, socket_timeout=tis_timeout) except Exception as err: error = "client_error - %s" % str(err) # NOTE: no retries on connection error - plpy.error('Ghost tables connection error: ' + str(err)) + plpy.warning('Invalidation Service connection error: ' + str(err)) break try: - # client.execute_command('DBSCH', db_name, username, ddl_tag) + client.execute_command('DBSCH', db_name, username, ddl_tag) break except Exception as err: error = "request_error - %s" % str(err) - client = GD['redis'] = None # force reconnect - if not retry: - plpy.error('Ghost tables error: ' + str(err)) + client = GD['invalidation'] = None # force reconnect + if not tis_retry: + plpy.warning('Invalidation Service error: ' + str(err)) break - retry -= 1 # try reconnecting + tis_retry -= 1 # try reconnecting $$ LANGUAGE 'plpythonu' VOLATILE PARALLEL UNSAFE; -- Enqueues a job to run Ghost tables linking process for the current user diff --git a/test/CDB_GhostTables.sql b/test/CDB_GhostTables.sql index d80f095..e9aa8fa 100644 --- a/test/CDB_GhostTables.sql +++ b/test/CDB_GhostTables.sql @@ -8,6 +8,7 @@ GRANT EXECUTE ON FUNCTION CDB_Username() TO "fulano"; GRANT EXECUTE ON FUNCTION CDB_LinkGhostTables() TO "fulano"; SELECT CDB_EnableGhostTablesTrigger(); INSERT INTO cdb_conf (key, value) VALUES ('api_keys_fulano', '{"username": "fulanito", "permissions":[]}'); +INSERT INTO cdb_conf (key, value) VALUES ('invalidation_service', '{"host": "fake-tis-host"}'); SET SESSION AUTHORIZATION "fulano"; SET client_min_messages TO notice; \set QUIET off @@ -41,4 +42,5 @@ REVOKE EXECUTE ON FUNCTION CDB_Username() FROM "fulano"; REVOKE SELECT ON cartodb.cdb_ddl_execution FROM "fulano"; REVOKE ALL ON SCHEMA cartodb FROM "fulano"; DROP ROLE "fulano"; +DELETE FROM cdb_conf WHERE key = 'api_keys_fulano' OR key = 'invalidation_service'; \set QUIET off diff --git a/test/CDB_GhostTables_expect b/test/CDB_GhostTables_expect index 33b0945..e6d6fb5 100644 --- a/test/CDB_GhostTables_expect +++ b/test/CDB_GhostTables_expect @@ -1,10 +1,12 @@ +WARNING: Invalidation Service error: Error -2 connecting fake-tis-host:3142. Name or service not known. NOTICE: _CDB_LinkGhostTables() called with username=fulanito, ddl_tag= BEGIN 0 CREATE TABLE 1 +WARNING: Invalidation Service error: Error -2 connecting fake-tis-host:3142. Name or service not known. NOTICE: _CDB_LinkGhostTables() called with username=fulanito, ddl_tag=CREATE TABLE COMMIT diff --git a/test/CDB_Username.sql b/test/CDB_Username.sql index ed344d6..55bd01c 100644 --- a/test/CDB_Username.sql +++ b/test/CDB_Username.sql @@ -19,4 +19,5 @@ SET SESSION AUTHORIZATION postgres; REVOKE USAGE ON SCHEMA cartodb FROM fulano; REVOKE EXECUTE ON FUNCTION CDB_Username() FROM fulano; DROP ROLE fulano; +DELETE FROM cdb_conf WHERE key = 'api_keys_fulano'; \set QUIET off \ No newline at end of file