neutron/neutron/db/ovn_hash_ring_db.py
Rodolfo Alonso Hernandez d63da24a13 [OVN] Add hash ring methods
OVNHashRing objects are used by OVN backend to register the nodes
controlled. The methods implemented are:
- add_node: including the group_name and optionally the node_uuid
- remove_nodes_from_host: remove all nodes maching a hostname and
  group_name
- touch_nodes_from_host: update a OVNHashRing register timestamp,
  filtering by hostname and group_name
- touch_node: update a OVNHashRing register timestamp, filtering by
  node_uuid
- get_active_nodes: retrieve any active node (filtering timestamp),
  group_name and hostname
  
Previous paths in networking-ovn tree:
./networking_ovn/db/hash_ring.py -> ./neutron/db/ovn_hash_ring_db.py
./networking_ovn/tests/unit/db/test_hash_ring.py -> 
./neutron/tests/unit/db/test_ovn_hash_ring_db.py


Co-Authored-By: Lucas Alvares Gomes <lucasagomes@gmail.com>
Change-Id: Ibb9c79771c48ccde564c39be9d781f3720802a2d
Partially-Implements: blueprint neutron-ovn-merge
2019-12-03 08:53:45 +00:00

70 lines
2.4 KiB
Python

# Copyright 2019 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
from neutron_lib.db import api as db_api
from oslo_config import cfg
from oslo_utils import timeutils
from oslo_utils import uuidutils
from neutron.db.models import ovn as ovn_models
CONF = cfg.CONF
# NOTE(ralonsoh): this was migrated from networking-ovn to neutron and should
# be refactored to be integrated in a OVO.
def add_node(context, group_name, node_uuid=None):
if node_uuid is None:
node_uuid = uuidutils.generate_uuid()
with db_api.CONTEXT_WRITER.using(context):
context.session.add(ovn_models.OVNHashRing(
node_uuid=node_uuid, hostname=CONF.host, group_name=group_name))
return node_uuid
def remove_nodes_from_host(context, group_name):
with db_api.CONTEXT_WRITER.using(context):
context.session.query(ovn_models.OVNHashRing).filter(
ovn_models.OVNHashRing.hostname == CONF.host,
ovn_models.OVNHashRing.group_name == group_name).delete()
def _touch(context, **filter_args):
with db_api.CONTEXT_WRITER.using(context):
context.session.query(ovn_models.OVNHashRing).filter_by(
**filter_args).update({'updated_at': timeutils.utcnow()})
def touch_nodes_from_host(context, group_name):
_touch(context, hostname=CONF.host, group_name=group_name)
def touch_node(context, node_uuid):
_touch(context, node_uuid=node_uuid)
def get_active_nodes(context, interval, group_name, from_host=False):
limit = timeutils.utcnow() - datetime.timedelta(seconds=interval)
with db_api.CONTEXT_READER.using(context):
query = context.session.query(ovn_models.OVNHashRing).filter(
ovn_models.OVNHashRing.updated_at >= limit,
ovn_models.OVNHashRing.group_name == group_name)
if from_host:
query = query.filter_by(hostname=CONF.host)
return query.all()