blob: 8924423464b3a14d070cbdc71a9cd3d5c390492a [file] [log] [blame]
Madan Jampani04aeb452015-05-02 16:12:24 -07001package org.onosproject.store.core.impl;
2
Thomas Vachuskad0d58542015-06-03 12:38:44 -07003import com.google.common.collect.Maps;
Madan Jampani04aeb452015-05-02 16:12:24 -07004import org.apache.felix.scr.annotations.Activate;
5import org.apache.felix.scr.annotations.Component;
6import org.apache.felix.scr.annotations.Deactivate;
7import org.apache.felix.scr.annotations.Reference;
8import org.apache.felix.scr.annotations.ReferenceCardinality;
9import org.apache.felix.scr.annotations.Service;
10import org.onosproject.core.IdBlock;
11import org.onosproject.core.IdBlockStore;
12import org.onosproject.store.service.AtomicCounter;
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070013import org.onosproject.store.service.StorageException;
Madan Jampani04aeb452015-05-02 16:12:24 -070014import org.onosproject.store.service.StorageService;
15import org.slf4j.Logger;
16
Thomas Vachuskad0d58542015-06-03 12:38:44 -070017import java.util.Map;
18
Thomas Vachuskaadba1522015-06-04 15:08:30 -070019import static org.onlab.util.Tools.randomDelay;
Thomas Vachuskad0d58542015-06-03 12:38:44 -070020import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani04aeb452015-05-02 16:12:24 -070021
22/**
23 * Implementation of {@code IdBlockStore} using {@code AtomicCounter}.
24 */
25@Component(immediate = true, enabled = true)
26@Service
27public class ConsistentIdBlockStore implements IdBlockStore {
28
Thomas Vachuskaadba1522015-06-04 15:08:30 -070029 private static final int MAX_TRIES = 5;
Thomas Vachuskad0d58542015-06-03 12:38:44 -070030 private static final int RETRY_DELAY_MS = 2_000;
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070031
Madan Jampani04aeb452015-05-02 16:12:24 -070032 private final Logger log = getLogger(getClass());
33 private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap();
34
35 private static final long DEFAULT_BLOCK_SIZE = 0x100000L;
36
37 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
38 protected StorageService storageService;
39
40 @Activate
41 public void activate() {
42 log.info("Started");
43 }
44
45 @Deactivate
46 public void deactivate() {
47 log.info("Stopped");
48 }
49
50 @Override
51 public IdBlock getIdBlock(String topic) {
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070052 AtomicCounter counter = topicCounters
53 .computeIfAbsent(topic,
54 name -> storageService.atomicCounterBuilder()
55 .withName(name)
56 .build());
57 Throwable exc = null;
58 for (int i = 0; i < MAX_TRIES; i++) {
59 try {
60 Long blockBase = counter.getAndAdd(DEFAULT_BLOCK_SIZE);
61 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
62 } catch (StorageException e) {
63 log.warn("Unable to allocate ID block due to {}; retrying...",
64 e.getMessage());
65 exc = e;
Thomas Vachuskaadba1522015-06-04 15:08:30 -070066 randomDelay(RETRY_DELAY_MS); // FIXME: This is a deliberate hack; fix in Drake
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070067 }
68 }
69 throw new IllegalStateException("Unable to allocate ID block", exc);
Madan Jampani04aeb452015-05-02 16:12:24 -070070 }
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070071
Madan Jampani04aeb452015-05-02 16:12:24 -070072}