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

[multi-DB] Part 3: Python API changes #52

Merged
merged 2 commits into from
Oct 10, 2019
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
26 changes: 1 addition & 25 deletions src/swsssdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,8 @@
logger.setLevel(logging.INFO)
logger.addHandler(logging.NullHandler())

import json
import os


def _load_connector_map():
"""
Get database map from the packaged config.
"""
global _connector_map
db_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config', 'database.json')

try:
with open(db_file, 'r') as f:
_connector_map = json.load(f)
except (OSError, IOError):
# Missing configuration means we can't configure the database index. Fatal error.
msg = "Could not open database index '{}'".format(db_file)
logger.exception(msg)
raise RuntimeError(msg)


_connector_map = {}
_load_connector_map()

try:
from .dbconnector import SonicV2Connector
from .dbconnector import SonicDBConfig, SonicV2Connector
from .configdb import ConfigDBConnector
except (KeyError, ValueError):
msg = "Failed to database connector objects -- incorrect database config schema."
Expand Down
31 changes: 0 additions & 31 deletions src/swsssdk/config/database.json

This file was deleted.

57 changes: 57 additions & 0 deletions src/swsssdk/config/database_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"INSTANCES": {
"redis":{
"hostname" : "127.0.0.1",
"port" : 6379,
"unix_socket_path" : "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
}
},
"VERSION" : "1.0"
}
26 changes: 13 additions & 13 deletions src/swsssdk/configdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def __init__(self, **kwargs):
self.handlers = {}

def __wait_for_db_init(self):
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
pubsub = client.pubsub()
initialized = client.get(self.INIT_INDICATOR)
if not initialized:
pattern = "__keyspace@{}__:{}".format(self.db_map[self.db_name]['db'], self.INIT_INDICATOR)
pattern = "__keyspace@{}__:{}".format(self.get_dbid(self.db_name), self.INIT_INDICATOR)
pubsub.psubscribe(pattern)
for item in pubsub.listen():
if item['type'] == 'pmessage':
Expand All @@ -55,7 +55,7 @@ def __wait_for_db_init(self):

def db_connect(self, dbname, wait_for_init=False, retry_on=False):
self.db_name = dbname
self.KEY_SEPARATOR = self.TABLE_NAME_SEPARATOR = self.db_map[self.db_name]['separator']
self.KEY_SEPARATOR = self.TABLE_NAME_SEPARATOR = self.get_db_separator(self.db_name)
SonicV2Connector.connect(self, self.db_name, retry_on)
if wait_for_init:
self.__wait_for_db_init()
Expand Down Expand Up @@ -89,15 +89,15 @@ def __fire(self, table, key, data):
def listen(self):
"""Start listen Redis keyspace events and will trigger corresponding handlers when content of a table changes.
"""
self.pubsub = self.redis_clients[self.db_name].pubsub()
self.pubsub.psubscribe("__keyspace@{}__:*".format(self.db_map[self.db_name]['db']))
self.pubsub = self.get_redis_client(self.db_name).pubsub()
self.pubsub.psubscribe("__keyspace@{}__:*".format(self.get_dbid(self.db_name)))
for item in self.pubsub.listen():
if item['type'] == 'pmessage':
key = item['channel'].split(':', 1)[1]
try:
(table, row) = key.split(self.TABLE_NAME_SEPARATOR, 1)
if self.handlers.has_key(table):
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
data = self.__raw_to_typed(client.hgetall(key))
self.__fire(table, row, data)
except ValueError:
Expand Down Expand Up @@ -171,7 +171,7 @@ def set_entry(self, table, key, data):
Pass None as data will delete the entry.
"""
key = self.serialize_key(key)
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
_hash = '{}{}{}'.format(table.upper(), self.TABLE_NAME_SEPARATOR, key)
if data == None:
client.delete(_hash)
Expand All @@ -193,7 +193,7 @@ def mod_entry(self, table, key, data):
Pass None as data will delete the entry.
"""
key = self.serialize_key(key)
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
_hash = '{}{}{}'.format(table.upper(), self.TABLE_NAME_SEPARATOR, key)
if data == None:
client.delete(_hash)
Expand All @@ -210,7 +210,7 @@ def get_entry(self, table, key):
Empty dictionary if table does not exist or entry does not exist.
"""
key = self.serialize_key(key)
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
_hash = '{}{}{}'.format(table.upper(), self.TABLE_NAME_SEPARATOR, key)
return self.__raw_to_typed(client.hgetall(_hash))

Expand All @@ -223,7 +223,7 @@ def get_keys(self, table, split=True):
Returns:
List of keys.
"""
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
pattern = '{}{}*'.format(table.upper(), self.TABLE_NAME_SEPARATOR)
keys = client.keys(pattern)
data = []
Expand All @@ -250,7 +250,7 @@ def get_table(self, table):
or { ('l1_key', 'l2_key', ...): {'column_key': value, ...}, ...} for a multi-key table.
Empty dictionary if table does not exist.
"""
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
pattern = '{}{}*'.format(table.upper(), self.TABLE_NAME_SEPARATOR)
keys = client.keys(pattern)
data = {}
Expand All @@ -274,7 +274,7 @@ def delete_table(self, table):
Args:
table: Table name.
"""
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
pattern = '{}{}*'.format(table.upper(), self.TABLE_NAME_SEPARATOR)
keys = client.keys(pattern)
data = {}
Expand Down Expand Up @@ -307,7 +307,7 @@ def get_config(self):
...
}
"""
client = self.redis_clients[self.db_name]
client = self.get_redis_client(self.db_name)
keys = client.keys('*')
data = {}
for key in keys:
Expand Down
Loading