Add unique_key table constant and document it in the API

To facilitate database snap-shotting, we should standardize the way
unique_key is stored by the drivers.. This patch adds the expectation
to our DB API of how unique_key data can be accessed, extracted, and
restored.

Change-Id: I15c3c8d7b1b6aa257eebd8f11f9dbdd6eaa6bef5
This commit is contained in:
Dima Kuznetsov 2017-09-27 09:27:31 +03:00
parent 8db59405e3
commit 444d23ea3e
9 changed files with 22 additions and 9 deletions

View File

@ -19,11 +19,12 @@ from dragonflow.cli import utils as cli_utils
from dragonflow.common import exceptions as df_exceptions
from dragonflow.common import utils as df_utils
from dragonflow import conf as cfg
from dragonflow.db import db_common
from dragonflow.db import model_framework
from dragonflow.db import models
from dragonflow.db.models import all # noqa
db_tables = list(model_framework.iter_tables()) + ['unique_key']
db_tables = list(model_framework.iter_tables()) + [db_common.UNIQUE_KEY_TABLE]
def print_tables():

View File

@ -149,6 +149,10 @@ class DbApi(object):
def allocate_unique_key(self, table):
"""Allocate a unique id in the controller
The allocation information should be managed in the 'unique_key'
table and stored by table name, s.t. the state can be extracted and
restored by get_key/set_ket calls.
:table: The name of resource table
:returns: Unique id
"""

View File

@ -16,6 +16,7 @@ from oslo_utils import timeutils
SEND_ALL_TOPIC = 'D'
DB_SYNC_MINIMUM_INTERVAL = 180
UNIQUE_KEY_TABLE = 'unique_key'
class DbUpdate(object):

View File

@ -18,12 +18,13 @@ from oslo_log import log
from dragonflow.common import exceptions as df_exceptions
from dragonflow import conf as cfg
from dragonflow.db import db_api
from dragonflow.db import db_common
LOG = log.getLogger(__name__)
ROOT_KS = 'openstack'
CAS_TABLE = 'unique_key'
CAS_TABLE = db_common.UNIQUE_KEY_TABLE
# NOTE(nick-ma-z): http://datastax.github.io/python-driver/
# api/cassandra.html

View File

@ -21,6 +21,7 @@ from urllib3 import exceptions
from dragonflow.common import exceptions as df_exceptions
from dragonflow.db import db_api
from dragonflow.db import db_common
LOG = log.getLogger(__name__)
@ -148,10 +149,10 @@ class EtcdDbDriver(db_api.DbApi):
return res
def _allocate_unique_key(self, table):
key = '/unique_key/%s' % table
key = self._make_key(db_common.UNIQUE_KEY_TABLE, table)
prev_value = 0
try:
prev_value = int(self.get_key('unique_key', table))
prev_value = int(self.get_key(db_common.UNIQUE_KEY_TABLE, table))
except df_exceptions.DBKeyNotFound:
if prev_value == 0:
# Create new key

View File

@ -13,6 +13,7 @@
import ramcloud
from dragonflow.db import db_api
from dragonflow.db import db_common
class RamCloudDbDriver(db_api.DbApi):
@ -77,7 +78,7 @@ class RamCloudDbDriver(db_api.DbApi):
return res
def _allocate_unique_key(self, table):
table_id = self.client.get_table_id('unique_key')
table_id = self.client.get_table_id(db_common.UNIQUE_KEY_TABLE)
key = table
while True:
try:

View File

@ -18,6 +18,7 @@ from redis import exceptions
from dragonflow.common import exceptions as df_exceptions
from dragonflow.db import db_api
from dragonflow.db import db_common
from dragonflow.db.drivers import redis_mgt
LOG = log.getLogger(__name__)
@ -303,7 +304,7 @@ class RedisDbDriver(db_api.DbApi):
return m.group(1)
def _allocate_unique_key(self, table):
local_key = self._uuid_to_key('unique_key', table, None)
local_key = self._uuid_to_key(db_common.UNIQUE_KEY_TABLE, table, None)
ip_port = None
try:
client = self._update_client(local_key)

View File

@ -18,6 +18,7 @@ import rethinkdb as rdb
from dragonflow.common import exceptions
from dragonflow import conf as cfg
from dragonflow.db import db_api
from dragonflow.db import db_common
_DF_DATABASE = 'dragonflow'
@ -116,9 +117,10 @@ class RethinkDbDriver(db_api.DbApi):
return [entry['id'] for entry in cursor]
def allocate_unique_key(self, table_name):
self._ensure_table_exists('unique_key')
unique_key_table = db_common.UNIQUE_KEY_TABLE
self._ensure_table_exists(unique_key_table)
with self._get_conn() as conn:
res = rdb.table('unique_key').get(table_name).replace(
res = rdb.table(unique_key_table).get(table_name).replace(
lambda post: {'id': table_name,
'key': post['key'].default(0).add(1)},
return_changes=True,

View File

@ -19,6 +19,7 @@ import six
from dragonflow.common import exceptions as df_exceptions
from dragonflow.common import utils
from dragonflow.db import db_api
from dragonflow.db import db_common
ROOT_NS = '/openstack'
@ -163,7 +164,7 @@ class ZookeeperDbDriver(db_api.DbApi):
raise df_exceptions.DBKeyNotFound(key=table)
def _allocate_unique_key(self, table):
path = self._generate_path('unique_key', table)
path = self._generate_path(db_common.UNIQUE_KEY_TABLE, table)
prev_value = 0
while True: