Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
| 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 Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 16 | package org.onosproject.store.core.impl; |
| 17 | |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 18 | import com.google.common.collect.Maps; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 19 | |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 20 | import org.apache.felix.scr.annotations.Activate; |
| 21 | import org.apache.felix.scr.annotations.Component; |
| 22 | import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 27 | import org.onosproject.core.IdBlock; |
| 28 | import org.onosproject.core.IdBlockStore; |
| 29 | import org.onosproject.store.service.AtomicCounter; |
Thomas Vachuska | f64c077 | 2015-06-01 10:32:14 -0700 | [diff] [blame] | 30 | import org.onosproject.store.service.StorageException; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 31 | import org.onosproject.store.service.StorageService; |
| 32 | import org.slf4j.Logger; |
| 33 | |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 34 | import java.util.Map; |
| 35 | |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 36 | import static org.slf4j.LoggerFactory.getLogger; |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Implementation of {@code IdBlockStore} using {@code AtomicCounter}. |
| 40 | */ |
| 41 | @Component(immediate = true, enabled = true) |
| 42 | @Service |
| 43 | public class ConsistentIdBlockStore implements IdBlockStore { |
| 44 | |
Thomas Vachuska | adba152 | 2015-06-04 15:08:30 -0700 | [diff] [blame] | 45 | private static final int MAX_TRIES = 5; |
Thomas Vachuska | d0d5854 | 2015-06-03 12:38:44 -0700 | [diff] [blame] | 46 | private static final int RETRY_DELAY_MS = 2_000; |
Thomas Vachuska | f64c077 | 2015-06-01 10:32:14 -0700 | [diff] [blame] | 47 | |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 48 | 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 Vachuska | f64c077 | 2015-06-01 10:32:14 -0700 | [diff] [blame] | 68 | AtomicCounter counter = topicCounters |
| 69 | .computeIfAbsent(topic, |
| 70 | name -> storageService.atomicCounterBuilder() |
| 71 | .withName(name) |
Madan Jampani | e17d328 | 2016-02-03 15:30:57 -0800 | [diff] [blame] | 72 | .build() |
| 73 | .asAtomicCounter()); |
Madan Jampani | a29c677 | 2015-08-17 13:17:07 -0700 | [diff] [blame] | 74 | Long blockBase = Tools.retryable(counter::getAndAdd, |
| 75 | StorageException.class, |
| 76 | MAX_TRIES, |
| 77 | RETRY_DELAY_MS).apply(DEFAULT_BLOCK_SIZE); |
| 78 | return new IdBlock(blockBase, DEFAULT_BLOCK_SIZE); |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 79 | } |
Madan Jampani | 04aeb45 | 2015-05-02 16:12:24 -0700 | [diff] [blame] | 80 | } |