Thomas Vachuska
Committed by Gerrit Code Review

Adding retry to id block store.

Change-Id: I274a928a13d2999ccfeb79fc45caf292dbb81f5b
...@@ -13,6 +13,7 @@ import org.apache.felix.scr.annotations.Service; ...@@ -13,6 +13,7 @@ import org.apache.felix.scr.annotations.Service;
13 import org.onosproject.core.IdBlock; 13 import org.onosproject.core.IdBlock;
14 import org.onosproject.core.IdBlockStore; 14 import org.onosproject.core.IdBlockStore;
15 import org.onosproject.store.service.AtomicCounter; 15 import org.onosproject.store.service.AtomicCounter;
16 +import org.onosproject.store.service.StorageException;
16 import org.onosproject.store.service.StorageService; 17 import org.onosproject.store.service.StorageService;
17 import org.slf4j.Logger; 18 import org.slf4j.Logger;
18 19
...@@ -25,6 +26,8 @@ import com.google.common.collect.Maps; ...@@ -25,6 +26,8 @@ import com.google.common.collect.Maps;
25 @Service 26 @Service
26 public class ConsistentIdBlockStore implements IdBlockStore { 27 public class ConsistentIdBlockStore implements IdBlockStore {
27 28
29 + private static final int MAX_TRIES = 3;
30 +
28 private final Logger log = getLogger(getClass()); 31 private final Logger log = getLogger(getClass());
29 private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap(); 32 private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap();
30 33
...@@ -45,11 +48,23 @@ public class ConsistentIdBlockStore implements IdBlockStore { ...@@ -45,11 +48,23 @@ public class ConsistentIdBlockStore implements IdBlockStore {
45 48
46 @Override 49 @Override
47 public IdBlock getIdBlock(String topic) { 50 public IdBlock getIdBlock(String topic) {
48 - AtomicCounter counter = topicCounters.computeIfAbsent(topic, 51 + AtomicCounter counter = topicCounters
52 + .computeIfAbsent(topic,
49 name -> storageService.atomicCounterBuilder() 53 name -> storageService.atomicCounterBuilder()
50 .withName(name) 54 .withName(name)
51 .build()); 55 .build());
56 + Throwable exc = null;
57 + for (int i = 0; i < MAX_TRIES; i++) {
58 + try {
52 Long blockBase = counter.getAndAdd(DEFAULT_BLOCK_SIZE); 59 Long blockBase = counter.getAndAdd(DEFAULT_BLOCK_SIZE);
53 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE); 60 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
61 + } catch (StorageException e) {
62 + log.warn("Unable to allocate ID block due to {}; retrying...",
63 + e.getMessage());
64 + exc = e;
65 + }
54 } 66 }
67 + throw new IllegalStateException("Unable to allocate ID block", exc);
68 + }
69 +
55 } 70 }
......