17
17
18
18
from neutron_lib .db import api as db_api
19
19
from oslo_config import cfg
20
+ from oslo_log import log
20
21
from oslo_utils import timeutils
21
22
from oslo_utils import uuidutils
22
23
23
24
from neutron .db .models import ovn as ovn_models
24
25
25
26
CONF = cfg .CONF
27
+ LOG = log .getLogger (__name__ )
26
28
27
29
28
30
# NOTE(ralonsoh): this was migrated from networking-ovn to neutron and should
@@ -34,6 +36,8 @@ def add_node(context, group_name, node_uuid=None):
34
36
with db_api .CONTEXT_WRITER .using (context ):
35
37
context .session .add (ovn_models .OVNHashRing (
36
38
node_uuid = node_uuid , hostname = CONF .host , group_name = group_name ))
39
+ LOG .info ('Node %s from host "%s" and group "%s" added to the Hash Ring' ,
40
+ node_uuid , CONF .host , group_name )
37
41
return node_uuid
38
42
39
43
@@ -42,6 +46,8 @@ def remove_nodes_from_host(context, group_name):
42
46
context .session .query (ovn_models .OVNHashRing ).filter (
43
47
ovn_models .OVNHashRing .hostname == CONF .host ,
44
48
ovn_models .OVNHashRing .group_name == group_name ).delete ()
49
+ LOG .info ('Nodes from host "%s" and group "%s" removed from the Hash Ring' ,
50
+ CONF .host , group_name )
45
51
46
52
47
53
def _touch (context , ** filter_args ):
@@ -58,12 +64,31 @@ def touch_node(context, node_uuid):
58
64
_touch (context , node_uuid = node_uuid )
59
65
60
66
61
- def get_active_nodes (context , interval , group_name , from_host = False ):
67
+ def _get_nodes_query (context , interval , group_name , offline = False ,
68
+ from_host = False ):
62
69
limit = timeutils .utcnow () - datetime .timedelta (seconds = interval )
63
- with db_api .CONTEXT_READER .using (context ):
64
- query = context .session .query (ovn_models .OVNHashRing ).filter (
65
- ovn_models .OVNHashRing .updated_at >= limit ,
66
- ovn_models .OVNHashRing .group_name == group_name )
67
- if from_host :
68
- query = query .filter_by (hostname = CONF .host )
69
- return query .all ()
70
+ query = context .session .query (ovn_models .OVNHashRing ).filter (
71
+ ovn_models .OVNHashRing .group_name == group_name )
72
+
73
+ if offline :
74
+ query = query .filter (ovn_models .OVNHashRing .updated_at < limit )
75
+ else :
76
+ query = query .filter (ovn_models .OVNHashRing .updated_at >= limit )
77
+
78
+ if from_host :
79
+ query = query .filter_by (hostname = CONF .host )
80
+
81
+ return query
82
+
83
+
84
+ @db_api .CONTEXT_READER
85
+ def get_active_nodes (context , interval , group_name , from_host = False ):
86
+ query = _get_nodes_query (context , interval , group_name ,
87
+ from_host = from_host )
88
+ return query .all ()
89
+
90
+
91
+ @db_api .CONTEXT_READER
92
+ def count_offline_nodes (context , interval , group_name ):
93
+ query = _get_nodes_query (context , interval , group_name , offline = True )
94
+ return query .count ()
0 commit comments