@@ -99,24 +99,44 @@ def get_vacant_binding_index(num_agents, bindings, lowest_binding_index,
99
99
always return an index, even if this number
100
100
exceeds the maximum configured number of agents.
101
101
"""
102
- binding_indices = [b .binding_index for b in bindings ]
103
- all_indices = set (range (lowest_binding_index , num_agents + 1 ))
104
- open_slots = sorted (list (all_indices - set (binding_indices )))
102
+ def get_open_slots (binding_indices , lowest_binding_index , max_number ):
103
+ """Returns an ordered list of free slots
104
+
105
+ This list starts from the lowest available binding index. The number
106
+ of open slots and "binding_indices" (those already taken), must be
107
+ equal to "max_number". The list returned can be [], if
108
+ len(max_number) == len(binding_indices) (that means there are no free
109
+ slots).
110
+ """
111
+ # NOTE(ralonsoh): check LP#2006496 for more context. The DHCP/router
112
+ # binding indexes could not be a sequential list starting from
113
+ # lowest_binding_index (that is usually 1).
114
+ open_slots = set (binding_indices )
115
+ idx = lowest_binding_index
116
+ while len (open_slots ) < max_number :
117
+ # Increase sequentially the "open_slots" set until we have the
118
+ # required number of slots, that is "num_agents".
119
+ open_slots .add (idx )
120
+ idx += 1
121
+
122
+ # Remove those indices already used.
123
+ open_slots -= set (binding_indices )
124
+ return sorted (list (open_slots ))
105
125
126
+ binding_indices = [b .binding_index for b in bindings ]
127
+ open_slots = get_open_slots (binding_indices , lowest_binding_index ,
128
+ num_agents )
106
129
if open_slots :
107
130
return open_slots [0 ]
108
131
109
132
if not force_scheduling :
110
133
return - 1
111
134
112
- # Last chance: if this is a manual scheduling, we're gonna allow
135
+ # Last chance: if this is a manual scheduling, we're going to allow
113
136
# creation of a binding_index even if it will exceed
114
- # dhcp_agents_per_network.
115
- if max (binding_indices ) == len (binding_indices ):
116
- return max (binding_indices ) + 1
117
- else :
118
- # Find binding index set gaps and return first free one.
119
- all_indices = set (range (lowest_binding_index ,
120
- max (binding_indices ) + 1 ))
121
- open_slots = sorted (list (all_indices - set (binding_indices )))
122
- return open_slots [0 ]
137
+ # dhcp_agents_per_network/max_l3_agents_per_router.
138
+ while not open_slots :
139
+ num_agents += 1
140
+ open_slots = get_open_slots (binding_indices , lowest_binding_index ,
141
+ num_agents )
142
+ return open_slots [0 ]
0 commit comments