Redis connection get from sentinel master
This commit is contained in:
@@ -1,23 +1,12 @@
|
|||||||
-- Get the connection to redis from cache or create a new one
|
|
||||||
CREATE OR REPLACE FUNCTION cdb_geocoder_server._connect_to_redis(user_id name)
|
|
||||||
RETURNS boolean AS $$
|
|
||||||
if user_id in GD and 'redis_connection' in GD[user_id]:
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
from cartodb_geocoder import redis_helper
|
|
||||||
config_params = plpy.execute("select c.host, c.port, c.timeout, c.db from cdb_geocoder_server._get_redis_conf() c;")[0]
|
|
||||||
redis_connection = redis_helper.RedisHelper(config_params['host'], config_params['port'], config_params['db']).redis_connection()
|
|
||||||
GD[user_id] = {'redis_connection': redis_connection}
|
|
||||||
return True
|
|
||||||
$$ LANGUAGE plpythonu;
|
|
||||||
|
|
||||||
CREATE TYPE cdb_geocoder_server._redis_conf_params AS (
|
CREATE TYPE cdb_geocoder_server._redis_conf_params AS (
|
||||||
host text,
|
sentinel_host text,
|
||||||
port int,
|
sentinel_port int,
|
||||||
timeout float,
|
sentinel_master_id text,
|
||||||
db text
|
redis_db text,
|
||||||
|
timeout float
|
||||||
);
|
);
|
||||||
|
|
||||||
|
-- Get the Redis configuration from the _conf table --
|
||||||
CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_redis_conf()
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._get_redis_conf()
|
||||||
RETURNS cdb_geocoder_server._redis_conf_params AS $$
|
RETURNS cdb_geocoder_server._redis_conf_params AS $$
|
||||||
conf = plpy.execute("SELECT cdb_geocoder_server._config_get('redis_conf') conf")[0]['conf']
|
conf = plpy.execute("SELECT cdb_geocoder_server._config_get('redis_conf') conf")[0]['conf']
|
||||||
@@ -26,5 +15,30 @@ RETURNS cdb_geocoder_server._redis_conf_params AS $$
|
|||||||
else:
|
else:
|
||||||
import json
|
import json
|
||||||
params = json.loads(conf)
|
params = json.loads(conf)
|
||||||
return { "host": params['host'], "port": params['port'], 'timeout': params['timeout'], 'db': params['db'] }
|
return {
|
||||||
|
"sentinel_host": params['sentinel_host'],
|
||||||
|
"sentinel_port": params['sentinel_port'],
|
||||||
|
"sentinel_master_id": params['sentinel_master_id'],
|
||||||
|
"timeout": params['timeout'],
|
||||||
|
"redis_db": params['redis_db']
|
||||||
|
}
|
||||||
|
$$ LANGUAGE plpythonu;
|
||||||
|
|
||||||
|
-- Get the connection to redis from cache or create a new one
|
||||||
|
CREATE OR REPLACE FUNCTION cdb_geocoder_server._connect_to_redis(user_id name)
|
||||||
|
RETURNS boolean AS $$
|
||||||
|
if user_id in GD and 'redis_connection' in GD[user_id]:
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
from cartodb_geocoder import redis_helper
|
||||||
|
config_params = plpy.execute("""select c.sentinel_host, c.sentinel_port,
|
||||||
|
c.sentinel_master_id, c.timeout, c.redis_db
|
||||||
|
from cdb_geocoder_server._get_redis_conf() c;""")[0]
|
||||||
|
redis_connection = redis_helper.RedisHelper(config_params['sentinel_host'],
|
||||||
|
config_params['sentinel_port'],
|
||||||
|
config_params['sentinel_master_id'],
|
||||||
|
timeout=config_params['timeout'],
|
||||||
|
redis_db=config_params['redis_db']).redis_connection()
|
||||||
|
GD[user_id] = {'redis_connection': redis_connection}
|
||||||
|
return True
|
||||||
$$ LANGUAGE plpythonu;
|
$$ LANGUAGE plpythonu;
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
import redis
|
from redis.sentinel import Sentinel
|
||||||
|
|
||||||
class RedisHelper:
|
class RedisHelper:
|
||||||
|
|
||||||
REDIS_DEFAULT_USER_DB = 5
|
REDIS_DEFAULT_USER_DB = 5
|
||||||
REDIS_DEFAULT_HOST = 'localhost'
|
REDIS_TIMEOUT = 2 #seconds
|
||||||
REDIS_DEFAULT_PORT = 6379
|
|
||||||
|
|
||||||
def __init__(self, host, port=REDIS_DEFAULT_PORT, db=REDIS_DEFAULT_USER_DB):
|
def __init__(self, sentinel_host, sentinel_port, sentinel_master_id, redis_db=REDIS_DEFAULT_USER_DB, **kwargs):
|
||||||
self.host = host
|
self.sentinel_host = sentinel_host
|
||||||
self.port = port
|
self.sentinel_port = sentinel_port
|
||||||
self.db = db
|
self.sentinel_master_id = sentinel_master_id
|
||||||
|
self.timeout = kwargs['timeout'] if 'timeout' in kwargs else REDIS_DEFAULT_TIMEOUT
|
||||||
|
self.redis_db = redis_db
|
||||||
|
|
||||||
def redis_connection(self):
|
def redis_connection(self):
|
||||||
return self.__create_redis_connection()
|
return self.__create_redis_connection()
|
||||||
|
|
||||||
def __create_redis_connection(self):
|
def __create_redis_connection(self):
|
||||||
#TODO Change to use Sentinel
|
sentinel = Sentinel([(self.sentinel_host, 26379)], socket_timeout=self.timeout)
|
||||||
pool = redis.ConnectionPool(host=self.host, port=self.port, db=self.db)
|
return sentinel.master_for(self.sentinel_master_id, socket_timeout=self.timeout, db=self.redis_db)
|
||||||
return redis.Redis(connection_pool=pool)
|
|
||||||
Reference in New Issue
Block a user