Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
controller
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
TFS
controller
Commits
63b4503a
Commit
63b4503a
authored
1 year ago
by
Lluis Gifre Renom
Browse files
Options
Downloads
Patches
Plain Diff
Framework - Mutex Queues:
- Added logs to identify issue
parent
892a1e93
No related branches found
No related tags found
2 merge requests
!235
Release TeraFlowSDN 3.0
,
!216
Resolve "Automate end-to-end tests and integrate them in CI/CD pipeline"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/common/tools/mutex_queues/MutexQueues.py
+34
-5
34 additions, 5 deletions
src/common/tools/mutex_queues/MutexQueues.py
with
34 additions
and
5 deletions
src/common/tools/mutex_queues/MutexQueues.py
+
34
−
5
View file @
63b4503a
...
@@ -34,10 +34,12 @@
...
@@ -34,10 +34,12 @@
# driver.configure(settings)
# driver.configure(settings)
# self.mutex_queues.signal_done(device_uuid)
# self.mutex_queues.signal_done(device_uuid)
import
threading
import
logging
,
threading
from
queue
import
Queue
,
Empty
from
queue
import
Queue
,
Empty
from
typing
import
Dict
from
typing
import
Dict
LOGGER
=
logging
.
getLogger
(
__name__
)
class
MutexQueues
:
class
MutexQueues
:
def
__init__
(
self
)
->
None
:
def
__init__
(
self
)
->
None
:
# lock to protect dictionary updates
# lock to protect dictionary updates
...
@@ -46,36 +48,63 @@ class MutexQueues:
...
@@ -46,36 +48,63 @@ class MutexQueues:
# dictionaty of queues of mutexes: queue_name => queue[mutex]
# dictionaty of queues of mutexes: queue_name => queue[mutex]
# first mutex is the running one
# first mutex is the running one
self
.
mutex_queues
:
Dict
[
str
,
Queue
[
threading
.
Event
]]
=
dict
()
self
.
mutex_queues
:
Dict
[
str
,
Queue
[
threading
.
Event
]]
=
dict
()
def
add_alias
(
self
,
queue_name_a
:
str
,
queue_name_b
:
str
)
->
None
:
with
self
.
lock
:
if
queue_name_a
in
self
.
mutex_queues
and
queue_name_b
not
in
self
.
mutex_queues
:
self
.
mutex_queues
[
queue_name_b
]
=
self
.
mutex_queues
[
queue_name_a
]
elif
queue_name_b
in
self
.
mutex_queues
and
queue_name_a
not
in
self
.
mutex_queues
:
self
.
mutex_queues
[
queue_name_a
]
=
self
.
mutex_queues
[
queue_name_b
]
def
wait_my_turn
(
self
,
queue_name
:
str
)
->
None
:
def
wait_my_turn
(
self
,
queue_name
:
str
)
->
None
:
LOGGER
.
warning
(
'
[wait_my_turn] begin queue_name={:s}
'
.
format
(
str
(
queue_name
)))
# create my mutex and enqueue it
# create my mutex and enqueue it
mutex
=
threading
.
Event
()
mutex
=
threading
.
Event
()
LOGGER
.
warning
(
'
[wait_my_turn] [lock] queue_name={:s} mutex={:s}
'
.
format
(
str
(
queue_name
),
str
(
mutex
)))
with
self
.
lock
:
with
self
.
lock
:
LOGGER
.
warning
(
'
[wait_my_turn] [lock] queue_name={:s} mutex_queues={:s}
'
.
format
(
str
(
queue_name
),
str
(
self
.
mutex_queues
)))
queue
:
Queue
=
self
.
mutex_queues
.
setdefault
(
queue_name
,
Queue
())
queue
:
Queue
=
self
.
mutex_queues
.
setdefault
(
queue_name
,
Queue
())
first_in_queue
=
(
queue
.
qsize
()
==
0
)
first_in_queue
=
(
queue
.
qsize
()
==
0
)
LOGGER
.
warning
(
'
[wait_my_turn] [lock] queue_name={:s} first_in_queue={:s}
'
.
format
(
str
(
queue_name
),
str
(
first_in_queue
)))
queue
.
put_nowait
(
mutex
)
queue
.
put_nowait
(
mutex
)
# if I'm the first in the queue upon addition, means there are no running tasks
# if I'm the first in the queue upon addition, means there are no running tasks
# directly return without waiting
# directly return without waiting
if
first_in_queue
:
return
if
first_in_queue
:
LOGGER
.
warning
(
'
[wait_my_turn] end first_in_queue queue_name={:s}
'
.
format
(
str
(
queue_name
)))
return
# otherwise, wait for my turn in the queue
# otherwise, wait for my turn in the queue
LOGGER
.
warning
(
'
[wait_my_turn] waiting queue_name={:s}
'
.
format
(
str
(
queue_name
)))
mutex
.
wait
()
mutex
.
wait
()
LOGGER
.
warning
(
'
[wait_my_turn] end wait queue_name={:s}
'
.
format
(
str
(
queue_name
)))
def
signal_done
(
self
,
queue_name
:
str
)
->
None
:
def
signal_done
(
self
,
queue_name
:
str
)
->
None
:
LOGGER
.
warning
(
'
[signal_done] begin queue_name={:s}
'
.
format
(
str
(
queue_name
)))
# I'm done with my work
# I'm done with my work
with
self
.
lock
:
with
self
.
lock
:
LOGGER
.
warning
(
'
[wait_my_turn] [lock] queue_name={:s} mutex_queues={:s}
'
.
format
(
str
(
queue_name
),
str
(
self
.
mutex_queues
)))
queue
:
Queue
=
self
.
mutex_queues
.
setdefault
(
queue_name
,
Queue
())
queue
:
Queue
=
self
.
mutex_queues
.
setdefault
(
queue_name
,
Queue
())
LOGGER
.
warning
(
'
[wait_my_turn] [lock] queue_name={:s} queue={:s}
'
.
format
(
str
(
queue_name
),
str
(
queue
)))
# remove myself from the queue
# remove myself from the queue
try
:
try
:
queue
.
get
(
block
=
True
,
timeout
=
0.1
)
LOGGER
.
warning
(
'
[wait_my_turn] [lock] before get queue_name={:s}
'
.
format
(
str
(
queue_name
)))
mutex
=
queue
.
get
(
block
=
True
,
timeout
=
0.1
)
LOGGER
.
warning
(
'
[wait_my_turn] [lock] after get queue_name={:s} mutex={:s}
'
.
format
(
str
(
queue_name
),
str
(
mutex
)))
except
Empty
:
except
Empty
:
LOGGER
.
warning
(
'
[wait_my_turn] [lock] empty queue_name={:s}
'
.
format
(
str
(
queue_name
)))
pass
pass
# if there are no other tasks queued, return
# if there are no other tasks queued, return
if
queue
.
qsize
()
==
0
:
return
if
queue
.
qsize
()
==
0
:
LOGGER
.
warning
(
'
[wait_my_turn] end queue.qsize==0 queue_name={:s}
'
.
format
(
str
(
queue_name
)))
return
# otherwise, signal the next task in the queue to start
# otherwise, signal the next task in the queue to start
next_mutex
:
threading
.
Event
=
queue
.
queue
[
0
]
next_mutex
:
threading
.
Event
=
queue
.
queue
[
0
]
LOGGER
.
warning
(
'
[wait_my_turn] [lock] before set queue_name={:s} next_mutex={:s}
'
.
format
(
str
(
queue_name
),
str
(
next_mutex
)))
next_mutex
.
set
()
next_mutex
.
set
()
LOGGER
.
warning
(
'
[wait_my_turn] [lock] after set queue_name={:s} next_mutex={:s}
'
.
format
(
str
(
queue_name
),
str
(
next_mutex
)))
LOGGER
.
warning
(
'
[signal_done] end set queue_name={:s}
'
.
format
(
str
(
queue_name
)))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment