blob: c6f48a4405b823a61cf38ef7280fb2f4e74e3f66 [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
2 * Copyright 2015 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Madan Jampani04aeb452015-05-02 16:12:24 -070016package org.onosproject.store.core.impl;
17
Thomas Vachuskad0d58542015-06-03 12:38:44 -070018import com.google.common.collect.Maps;
Madan Jampania29c6772015-08-17 13:17:07 -070019
Madan Jampani04aeb452015-05-02 16:12:24 -070020import org.apache.felix.scr.annotations.Activate;
21import org.apache.felix.scr.annotations.Component;
22import org.apache.felix.scr.annotations.Deactivate;
23import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Madan Jampania29c6772015-08-17 13:17:07 -070026import org.onlab.util.Tools;
Madan Jampani04aeb452015-05-02 16:12:24 -070027import org.onosproject.core.IdBlock;
28import org.onosproject.core.IdBlockStore;
29import org.onosproject.store.service.AtomicCounter;
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070030import org.onosproject.store.service.StorageException;
Madan Jampani04aeb452015-05-02 16:12:24 -070031import org.onosproject.store.service.StorageService;
32import org.slf4j.Logger;
33
Thomas Vachuskad0d58542015-06-03 12:38:44 -070034import java.util.Map;
35
Thomas Vachuskad0d58542015-06-03 12:38:44 -070036import static org.slf4j.LoggerFactory.getLogger;
Madan Jampani04aeb452015-05-02 16:12:24 -070037
38/**
39 * Implementation of {@code IdBlockStore} using {@code AtomicCounter}.
40 */
41@Component(immediate = true, enabled = true)
42@Service
43public class ConsistentIdBlockStore implements IdBlockStore {
44
Thomas Vachuskaadba1522015-06-04 15:08:30 -070045 private static final int MAX_TRIES = 5;
Thomas Vachuskad0d58542015-06-03 12:38:44 -070046 private static final int RETRY_DELAY_MS = 2_000;
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070047
Madan Jampani04aeb452015-05-02 16:12:24 -070048 private final Logger log = getLogger(getClass());
49 private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap();
50
51 private static final long DEFAULT_BLOCK_SIZE = 0x100000L;
52
53 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
54 protected StorageService storageService;
55
56 @Activate
57 public void activate() {
58 log.info("Started");
59 }
60
61 @Deactivate
62 public void deactivate() {
63 log.info("Stopped");
64 }
65
66 @Override
67 public IdBlock getIdBlock(String topic) {
Thomas Vachuskaf64c0772015-06-01 10:32:14 -070068 AtomicCounter counter = topicCounters
69 .computeIfAbsent(topic,
70 name -> storageService.atomicCounterBuilder()
71 .withName(name)
72 .build());
Madan Jampania29c6772015-08-17 13:17:07 -070073 Long blockBase = Tools.retryable(counter::getAndAdd,
74 StorageException.class,
75 MAX_TRIES,
76 RETRY_DELAY_MS).apply(DEFAULT_BLOCK_SIZE);
77 return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE);
Madan Jampani04aeb452015-05-02 16:12:24 -070078 }
Madan Jampani04aeb452015-05-02 16:12:24 -070079}