Debian Codegraph @

ansible_collections/community/aws/tests/integration/targets/ses_rule_set/tasks/obtain-lock.yaml

raw
1 # ============================================================
2 # This file attempts to obtain a global lock (for a given
3 # region / account combination.
4 #
5 # This makes one attempt to get the lock and will set the
6 # won_lock variable to True or False to indicate whether
7 # or not we got the lock.
8 #
9 # It's expected that this will be executed in a retry loop
10 # so that if we don't get the lock we delay then try again.
11 #
12 # This should only be used in a block with cleanup-lock.yaml
13 # included in the always clause to ensure the lock is released.
14 #
15 # There are several variables that control the locking behaviour:
16 # * lock_timeout_seconds
17 # How old a lock must be before it's assumed to be an expired
18 # lock that was not cleaned up by the owner. Any locks older
19 # than this will not prevent a lock being obtained and will
20 # be deleted when a new process obtains the lock.
21 # * lock_log_group_prefix
22 # The log_group prefix that represents the lock being obtained.
23 # This must be the same across all processes trying to obtain
24 # the lock.
25 # * lock_process_id
26 # A unique identifier of this process. Each process that might
27 # attempt to lock the process must have a different identifier.
28 # This defaults to the resource_prefix which is generally
29 # appropriate.
30 # * max_obtain_lock_attempts
31 # How many attempts to make to get the lock before giving up
32 # NB: This is actually done in main.yaml
33 # * obtain_lock_delay_seconds:
34 # How long to delay after failing to get the lock before
35 # trying again.
36 # NB: This is actually done in obtain-lock-wrapper.yaml
37 #
38 # The locking here is based around creating cloudwatch log groups.
39 # This resource was chosen because:
40 # A) it's free
41 # B) we have a built in grouping concept because of the hierarchy
42 # that allows us to easily group attempts for the same lock
43 # C) the creation time is tracked and returned which gives us
44 # a mechanism for deterministically picking a winner
45 #
46 # Each lock is represented by a log group prefix. Each attempt
47 # to obtain the lock is a log group of the lock_process_id below
48 # that prefix.
49 #
50 # The winning lock is the one with the earliest creation time.
51 #
52 # To prevent a hanging lock from permanently hanging the build
53 # lock attempts older than the lock timeout are ignored and
54 # cleaned up by the next process to win the lock.
55 # ============================================================
56
57 - name: Set lock_attempt_log_group_name
58 set_fact:
59 lock_attempt_log_group_name: "{{ lock_log_group_prefix }}/{{ lock_process_id|default(resource_prefix) }}"
60
61 # Note the overwrite below to ensure that the creation time
62 # is upated. This is important as we calculate expiry relative
63 # the attempt creation.
64 #
65 # Because of this it's imporatnt that we delete the attempt
66 # if we don't get the lock. Otherwise we can get a deadlock
67 # where the stale atttempt from one process wins, but then
68 # because that process updates the creation date it doesn't
69 # consider its self to havewone.
70 - name: Create Lock Attempt Log Group
71 cloudwatchlogs_log_group:
72 log_group_name: "{{ lock_attempt_log_group_name }}"
73 state: present
74 overwrite: True
75 register: lock_attempt_log_group_result
76
77 - name: Get Lock Attempt Lock Groups
78 cloudwatchlogs_log_group_info:
79 log_group_name: "{{ lock_log_group_prefix }}/"
80 register: lock_attempt_log_groups
81
82 - name: Calculate Expired Lock Attempt Timestamp
83 set_fact:
84 expired_lock_timestamp: "{{ lock_attempt_log_group_result.creation_time - (lock_timeout_seconds * 1000) }}"
85
86 - name: Get Expired and Active Lock Attempts
87 set_fact:
88 expired_lock_attempts: "{{ lock_attempt_log_groups.log_groups|selectattr('creation_time', 'lt', expired_lock_timestamp|int)|list }}"
89 active_lock_attempts: "{{ lock_attempt_log_groups.log_groups|selectattr('creation_time', 'ge', expired_lock_timestamp|int)|list }}"
90
91 - name: Pick Winning Lock Attempt
92 set_fact:
93 winning_lock_attempt: "{{ active_lock_attempts|sort(attribute='creation_time')|first }}"
94
95 - name: Determine if Won Lock
96 set_fact:
97 won_lock: "{{ winning_lock_attempt.log_group_name == lock_attempt_log_group_name }}"
98
99 # Remove the lock attempt if we didn't get the lock. This prevents
100 # our stale lock attempt blocking another process from getting the lock.
101 # See more detailed comment above Create Lock Attempt Log Group
102 - name: Remove Failed Lock Attempt Log Group
103 cloudwatchlogs_log_group:
104 log_group_name: "{{ lock_attempt_log_group_name }}"
105 state: absent
106 when: "not won_lock|bool"
107
108 - name: Delete Expired Lock Attempts
109 cloudwatchlogs_log_group:
110 log_group_name: "{{ item.log_group_name }}"
111 state: absent
112 when: "won_lock|bool"
113 loop: "{{ expired_lock_attempts }}"