Browse Source

Fix wamp session synchronization on the database.

Improved and fixed the session synchronization on the wamp agent.

Change-Id: I4e94d975b06c87f08c6680084b976fe8146bb424
Fabio Verboso 4 months ago
parent
commit
36c20a1ef6

+ 1
- 1
iotronic/db/api.py View File

@@ -268,7 +268,7 @@ class Connection(object):
268 268
         """
269 269
 
270 270
     @abc.abstractmethod
271
-    def get_valid_wpsessions_list(self):
271
+    def get_valid_wpsessions_list(self, agent):
272 272
         """Return a list of wpsession."""
273 273
 
274 274
     @abc.abstractmethod

+ 7
- 2
iotronic/db/sqlalchemy/api.py View File

@@ -508,8 +508,13 @@ class Connection(api.Connection):
508 508
         except NoResultFound:
509 509
             return None
510 510
 
511
-    def get_valid_wpsessions_list(self):
512
-        query = model_query(models.SessionWP).filter_by(valid=1)
511
+    def get_valid_wpsessions_list(self, agent):
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 518
         return query.all()
514 519
 
515 520
     # WAMPAGENT api

+ 2
- 2
iotronic/objects/sessionwp.py View File

@@ -82,14 +82,14 @@ class SessionWP(base.IotronicObject):
82 82
         return session
83 83
 
84 84
     @base.remotable_classmethod
85
-    def valid_list(cls, context):
85
+    def valid_list(cls, context, agent):
86 86
         """Return a list of SessionWP objects.
87 87
 
88 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 93
         return [SessionWP._from_db_object(cls(context), x) for x in db_list]
94 94
 
95 95
     @base.remotable

+ 1
- 1
iotronic/wamp/agent.py View File

@@ -243,7 +243,7 @@ class WampManager(object):
243 243
 
244 244
             session_l = await session.call(u'wamp.session.list')
245 245
             session_l.remove(details.session)
246
-            fun.update_sessions(session_l)
246
+            fun.update_sessions(session_l, AGENT_HOST)
247 247
 
248 248
         @comp.on_leave
249 249
         async def onLeave(session, details):

+ 37
- 13
iotronic/wamp/functions.py View File

@@ -59,25 +59,40 @@ def alive():
59 59
         '%Y-%m-%dT%H:%M:%S.%f')
60 60
 
61 61
 
62
-def update_sessions(session_list):
62
+def update_sessions(session_list, agent):
63 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 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 69
     if session_list == list_db:
68 70
         LOG.debug('Sessions on the database are updated.')
69 71
         return
70 72
 
73
+    # list of board not connected anymore
71 74
     old_connected = list_db.difference(session_list)
75
+
76
+    LOG.debug('no more valid session list: %s', old_connected)
77
+
72 78
     for elem in old_connected:
73 79
         old_session = objects.SessionWP.get(ctxt, elem)
74
-        old_session.valid = False
75
-        old_session.save()
76
-        LOG.debug('%s has been put offline.', old_session.board_uuid)
80
+        if old_session.valid:
81
+            old_session.valid = False
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 89
     if old_connected:
78 90
         LOG.warning('Some boards have been updated: status offline')
79 91
 
92
+    # list of board still connected
80 93
     keep_connected = list_db.intersection(session_list)
94
+    LOG.debug('still valid session list: %s', keep_connected)
95
+
81 96
     for elem in keep_connected:
82 97
         for x in list_from_db:
83 98
             if x.session_id == str(elem):
@@ -91,13 +106,18 @@ def board_on_leave(session_id):
91 106
     LOG.debug('A board with %s disconnectd', session_id)
92 107
     try:
93 108
         old_session = objects.SessionWP.get(ctxt, session_id)
94
-        old_session.valid = False
95
-        old_session.save()
96
-        board = objects.Board.get_by_uuid(ctxt, old_session.board_uuid)
97
-        board.status = states.OFFLINE
98
-        board.save()
99
-        LOG.debug('Session updated. Board %s is now  %s', board.uuid,
100
-                  states.OFFLINE)
109
+
110
+        if old_session.valid:
111
+            old_session.valid = False
112
+            old_session.save()
113
+            board = objects.Board.get_by_uuid(ctxt, old_session.board_uuid)
114
+            board.status = 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 121
     except Exception:
102 122
         LOG.debug('session %s not found', session_id)
103 123
 
@@ -111,13 +131,14 @@ def connection(uuid, session):
111 131
         msg = exc.message % {'board': uuid}
112 132
         LOG.error(msg)
113 133
         return wm.WampError(msg).serialize()
114
-
115 134
     try:
116 135
         old_ses = objects.SessionWP(ctxt)
117 136
         old_ses = old_ses.get_session_by_board_uuid(ctxt, board.uuid,
118 137
                                                     valid=True)
119 138
         old_ses.valid = False
120 139
         old_ses.save()
140
+        LOG.debug('old session for %s found: %s', board.uuid,
141
+                  old_ses.session_id)
121 142
 
122 143
     except Exception:
123 144
         LOG.debug('valid session for %s not found', board.uuid)
@@ -127,10 +148,13 @@ def connection(uuid, session):
127 148
                     'session_id': session}
128 149
     session = objects.SessionWP(ctxt, **session_data)
129 150
     session.create()
151
+    LOG.debug('new session for %s saved %s', board.uuid,
152
+              session.session_id)
130 153
     board.status = states.ONLINE
131 154
     board.save()
132 155
     LOG.info('Board %s (%s) is now  %s', board.uuid,
133 156
              board.name, states.ONLINE)
157
+
134 158
     return wm.WampSuccess('').serialize()
135 159
 
136 160
 

Loading…
Cancel
Save