summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFabio Verboso <fverboso@unime.it>2018-12-13 17:57:49 +0100
committerFabio Verboso <fverboso@unime.it>2018-12-13 17:57:49 +0100
commit36c20a1ef6bd472a62be32fa0e83fcff41691bff (patch)
treea40a8c74e7cf72697b8c5b1955614b41e288ab49
parent30fa19b1cfb153a8f24802ff34ca930d4a6783de (diff)
Fix wamp session synchronization on the database.
Improved and fixed the session synchronization on the wamp agent. Change-Id: I4e94d975b06c87f08c6680084b976fe8146bb424
Notes
Notes (review): Code-Review+2: Fabio Verboso <fverboso@unime.it> Workflow+1: Fabio Verboso <fverboso@unime.it> Verified+2: Zuul Submitted-by: Zuul Submitted-at: Thu, 13 Dec 2018 17:24:18 +0000 Reviewed-on: https://review.openstack.org/625053 Project: openstack/iotronic Branch: refs/heads/master
-rw-r--r--iotronic/db/api.py2
-rw-r--r--iotronic/db/sqlalchemy/api.py9
-rw-r--r--iotronic/objects/sessionwp.py4
-rw-r--r--iotronic/wamp/agent.py2
-rw-r--r--iotronic/wamp/functions.py50
5 files changed, 48 insertions, 19 deletions
diff --git a/iotronic/db/api.py b/iotronic/db/api.py
index ca8a111..d6ffdfb 100644
--- a/iotronic/db/api.py
+++ b/iotronic/db/api.py
@@ -268,7 +268,7 @@ class Connection(object):
268 """ 268 """
269 269
270 @abc.abstractmethod 270 @abc.abstractmethod
271 def get_valid_wpsessions_list(self): 271 def get_valid_wpsessions_list(self, agent):
272 """Return a list of wpsession.""" 272 """Return a list of wpsession."""
273 273
274 @abc.abstractmethod 274 @abc.abstractmethod
diff --git a/iotronic/db/sqlalchemy/api.py b/iotronic/db/sqlalchemy/api.py
index 4b0c981..cdffdde 100644
--- a/iotronic/db/sqlalchemy/api.py
+++ b/iotronic/db/sqlalchemy/api.py
@@ -508,8 +508,13 @@ class Connection(api.Connection):
508 except NoResultFound: 508 except NoResultFound:
509 return None 509 return None
510 510
511 def get_valid_wpsessions_list(self): 511 def get_valid_wpsessions_list(self, agent):
512 query = model_query(models.SessionWP).filter_by(valid=1) 512 query = model_query(models.SessionWP)
513 query = query.filter_by(valid=1)
514 query = query.join(models.Board,
515 models.SessionWP.board_id == models.Board.id)
516 query = query.filter_by(agent=agent)
517
513 return query.all() 518 return query.all()
514 519
515 # WAMPAGENT api 520 # WAMPAGENT api
diff --git a/iotronic/objects/sessionwp.py b/iotronic/objects/sessionwp.py
index e4aaf92..aec3340 100644
--- a/iotronic/objects/sessionwp.py
+++ b/iotronic/objects/sessionwp.py
@@ -82,14 +82,14 @@ class SessionWP(base.IotronicObject):
82 return session 82 return session
83 83
84 @base.remotable_classmethod 84 @base.remotable_classmethod
85 def valid_list(cls, context): 85 def valid_list(cls, context, agent):
86 """Return a list of SessionWP objects. 86 """Return a list of SessionWP objects.
87 87
88 :returns: a list of valid session_id 88 :returns: a list of valid session_id
89 89
90 """ 90 """
91 91
92 db_list = cls.dbapi.get_valid_wpsessions_list() 92 db_list = cls.dbapi.get_valid_wpsessions_list(agent)
93 return [SessionWP._from_db_object(cls(context), x) for x in db_list] 93 return [SessionWP._from_db_object(cls(context), x) for x in db_list]
94 94
95 @base.remotable 95 @base.remotable
diff --git a/iotronic/wamp/agent.py b/iotronic/wamp/agent.py
index fd978c4..4b6c6e0 100644
--- a/iotronic/wamp/agent.py
+++ b/iotronic/wamp/agent.py
@@ -243,7 +243,7 @@ class WampManager(object):
243 243
244 session_l = await session.call(u'wamp.session.list') 244 session_l = await session.call(u'wamp.session.list')
245 session_l.remove(details.session) 245 session_l.remove(details.session)
246 fun.update_sessions(session_l) 246 fun.update_sessions(session_l, AGENT_HOST)
247 247
248 @comp.on_leave 248 @comp.on_leave
249 async def onLeave(session, details): 249 async def onLeave(session, details):
diff --git a/iotronic/wamp/functions.py b/iotronic/wamp/functions.py
index b0415d1..6b39d29 100644
--- a/iotronic/wamp/functions.py
+++ b/iotronic/wamp/functions.py
@@ -59,25 +59,40 @@ def alive():
59 '%Y-%m-%dT%H:%M:%S.%f') 59 '%Y-%m-%dT%H:%M:%S.%f')
60 60
61 61
62def update_sessions(session_list): 62def update_sessions(session_list, agent):
63 session_list = set(session_list) 63 session_list = set(session_list)
64 list_from_db = objects.SessionWP.valid_list(ctxt) 64 list_from_db = objects.SessionWP.valid_list(ctxt, agent)
65 list_db = set([int(elem.session_id) for elem in list_from_db]) 65 list_db = set([int(elem.session_id) for elem in list_from_db])
66 LOG.debug('Wamp session list: %s', session_list)
67 LOG.debug('DB session list: %s', list_db)
66 68
67 if session_list == list_db: 69 if session_list == list_db:
68 LOG.debug('Sessions on the database are updated.') 70 LOG.debug('Sessions on the database are updated.')
69 return 71 return
70 72
73 # list of board not connected anymore
71 old_connected = list_db.difference(session_list) 74 old_connected = list_db.difference(session_list)
75
76 LOG.debug('no more valid session list: %s', old_connected)
77
72 for elem in old_connected: 78 for elem in old_connected:
73 old_session = objects.SessionWP.get(ctxt, elem) 79 old_session = objects.SessionWP.get(ctxt, elem)
74 old_session.valid = False 80 if old_session.valid:
75 old_session.save() 81 old_session.valid = False
76 LOG.debug('%s has been put offline.', old_session.board_uuid) 82 old_session.save()
83 board = objects.Board.get_by_uuid(ctxt, old_session.board_uuid)
84 board.status = states.OFFLINE
85 board.save()
86 LOG.debug('Session updated. Board %s is now %s', board.uuid,
87 states.OFFLINE)
88
77 if old_connected: 89 if old_connected:
78 LOG.warning('Some boards have been updated: status offline') 90 LOG.warning('Some boards have been updated: status offline')
79 91
92 # list of board still connected
80 keep_connected = list_db.intersection(session_list) 93 keep_connected = list_db.intersection(session_list)
94 LOG.debug('still valid session list: %s', keep_connected)
95
81 for elem in keep_connected: 96 for elem in keep_connected:
82 for x in list_from_db: 97 for x in list_from_db:
83 if x.session_id == str(elem): 98 if x.session_id == str(elem):
@@ -91,13 +106,18 @@ def board_on_leave(session_id):
91 LOG.debug('A board with %s disconnectd', session_id) 106 LOG.debug('A board with %s disconnectd', session_id)
92 try: 107 try:
93 old_session = objects.SessionWP.get(ctxt, session_id) 108 old_session = objects.SessionWP.get(ctxt, session_id)
94 old_session.valid = False 109
95 old_session.save() 110 if old_session.valid:
96 board = objects.Board.get_by_uuid(ctxt, old_session.board_uuid) 111 old_session.valid = False
97 board.status = states.OFFLINE 112 old_session.save()
98 board.save() 113 board = objects.Board.get_by_uuid(ctxt, old_session.board_uuid)
99 LOG.debug('Session updated. Board %s is now %s', board.uuid, 114 board.status = states.OFFLINE
100 states.OFFLINE) 115 board.save()
116 LOG.debug('Session updated. Board %s is now %s', board.uuid,
117 states.OFFLINE)
118 return
119
120 LOG.debug('Session %s already set to not valid', session_id)
101 except Exception: 121 except Exception:
102 LOG.debug('session %s not found', session_id) 122 LOG.debug('session %s not found', session_id)
103 123
@@ -111,13 +131,14 @@ def connection(uuid, session):
111 msg = exc.message % {'board': uuid} 131 msg = exc.message % {'board': uuid}
112 LOG.error(msg) 132 LOG.error(msg)
113 return wm.WampError(msg).serialize() 133 return wm.WampError(msg).serialize()
114
115 try: 134 try:
116 old_ses = objects.SessionWP(ctxt) 135 old_ses = objects.SessionWP(ctxt)
117 old_ses = old_ses.get_session_by_board_uuid(ctxt, board.uuid, 136 old_ses = old_ses.get_session_by_board_uuid(ctxt, board.uuid,
118 valid=True) 137 valid=True)
119 old_ses.valid = False 138 old_ses.valid = False
120 old_ses.save() 139 old_ses.save()
140 LOG.debug('old session for %s found: %s', board.uuid,
141 old_ses.session_id)
121 142
122 except Exception: 143 except Exception:
123 LOG.debug('valid session for %s not found', board.uuid) 144 LOG.debug('valid session for %s not found', board.uuid)
@@ -127,10 +148,13 @@ def connection(uuid, session):
127 'session_id': session} 148 'session_id': session}
128 session = objects.SessionWP(ctxt, **session_data) 149 session = objects.SessionWP(ctxt, **session_data)
129 session.create() 150 session.create()
151 LOG.debug('new session for %s saved %s', board.uuid,
152 session.session_id)
130 board.status = states.ONLINE 153 board.status = states.ONLINE
131 board.save() 154 board.save()
132 LOG.info('Board %s (%s) is now %s', board.uuid, 155 LOG.info('Board %s (%s) is now %s', board.uuid,
133 board.name, states.ONLINE) 156 board.name, states.ONLINE)
157
134 return wm.WampSuccess('').serialize() 158 return wm.WampSuccess('').serialize()
135 159
136 160