remove term lock
Change-Id: Ia19323d2bb1e30f2c62ddda7c6269928c4d73fb3
Showing
1 changed file
with
22 additions
and
10 deletions
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | package org.onlab.onos.store.mastership.impl; | 16 | package org.onlab.onos.store.mastership.impl; |
17 | 17 | ||
18 | import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED; | 18 | import static org.onlab.onos.mastership.MastershipEvent.Type.MASTER_CHANGED; |
19 | +import static org.apache.commons.lang3.concurrent.ConcurrentUtils.putIfAbsent; | ||
19 | 20 | ||
20 | import java.util.Map; | 21 | import java.util.Map; |
21 | import java.util.Set; | 22 | import java.util.Set; |
... | @@ -273,8 +274,7 @@ implements MastershipStore { | ... | @@ -273,8 +274,7 @@ implements MastershipStore { |
273 | case MASTER: | 274 | case MASTER: |
274 | event = reelect(nodeId, deviceId, rv); | 275 | event = reelect(nodeId, deviceId, rv); |
275 | if (event != null) { | 276 | if (event != null) { |
276 | - Integer term = terms.get(deviceId); | 277 | + updateTerm(deviceId); |
277 | - terms.put(deviceId, ++term); | ||
278 | } | 278 | } |
279 | //fall through to reinforce relinquishment | 279 | //fall through to reinforce relinquishment |
280 | case STANDBY: | 280 | case STANDBY: |
... | @@ -341,16 +341,28 @@ implements MastershipStore { | ... | @@ -341,16 +341,28 @@ implements MastershipStore { |
341 | 341 | ||
342 | //adds or updates term information. | 342 | //adds or updates term information. |
343 | private void updateTerm(DeviceId deviceId) { | 343 | private void updateTerm(DeviceId deviceId) { |
344 | - terms.lock(deviceId); | 344 | + Integer term = terms.get(deviceId); |
345 | - try { | 345 | + if (term == null) { |
346 | - Integer term = terms.get(deviceId); | 346 | + term = terms.putIfAbsent(deviceId, INIT); |
347 | if (term == null) { | 347 | if (term == null) { |
348 | - terms.put(deviceId, INIT); | 348 | + // initial term set successfully |
349 | - } else { | 349 | + return; |
350 | - terms.put(deviceId, ++term); | ||
351 | } | 350 | } |
352 | - } finally { | 351 | + // concurrent initialization detected, |
353 | - terms.unlock(deviceId); | 352 | + // fall through to try incrementing |
353 | + } | ||
354 | + Integer nextTerm = term + 1; | ||
355 | + boolean success = terms.replace(deviceId, term, nextTerm); | ||
356 | + while (!success) { | ||
357 | + term = terms.get(deviceId); | ||
358 | + if (term == null) { | ||
359 | + // something is very wrong, but write something to avoid | ||
360 | + // infinite loop. | ||
361 | + log.warn("Term info for {} disappeared.", deviceId); | ||
362 | + term = putIfAbsent(terms, deviceId, nextTerm); | ||
363 | + } | ||
364 | + nextTerm = term + 1; | ||
365 | + success = terms.replace(deviceId, term, nextTerm); | ||
354 | } | 366 | } |
355 | } | 367 | } |
356 | 368 | ... | ... |
-
Please register or login to post a comment