Committed by
Gerrit Code Review
New and direct StorageService method for creating an AtomicCounter
Change-Id: I8c189584dde590842075bea7e03c4c8ecf8d72c2
Showing
9 changed files
with
30 additions
and
55 deletions
| ... | @@ -39,10 +39,6 @@ public class CounterTestIncrementCommand extends AbstractShellCommand { | ... | @@ -39,10 +39,6 @@ public class CounterTestIncrementCommand extends AbstractShellCommand { |
| 39 | 39 | ||
| 40 | private final Logger log = getLogger(getClass()); | 40 | private final Logger log = getLogger(getClass()); |
| 41 | 41 | ||
| 42 | - @Option(name = "-i", aliases = "--inMemory", description = "use in memory map?", | ||
| 43 | - required = false, multiValued = false) | ||
| 44 | - private boolean inMemory = false; | ||
| 45 | - | ||
| 46 | @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding", | 42 | @Option(name = "-g", aliases = "--getFirst", description = "get the counter's value before adding", |
| 47 | required = false, multiValued = false) | 43 | required = false, multiValued = false) |
| 48 | private boolean getFirst = false; | 44 | private boolean getFirst = false; |
| ... | @@ -63,16 +59,7 @@ public class CounterTestIncrementCommand extends AbstractShellCommand { | ... | @@ -63,16 +59,7 @@ public class CounterTestIncrementCommand extends AbstractShellCommand { |
| 63 | @Override | 59 | @Override |
| 64 | protected void execute() { | 60 | protected void execute() { |
| 65 | StorageService storageService = get(StorageService.class); | 61 | StorageService storageService = get(StorageService.class); |
| 66 | - if (inMemory) { | 62 | + atomicCounter = storageService.getAsyncAtomicCounter(counter); |
| 67 | - atomicCounter = storageService.atomicCounterBuilder() | ||
| 68 | - .withName(counter) | ||
| 69 | - .withPartitionsDisabled() | ||
| 70 | - .build(); | ||
| 71 | - } else { | ||
| 72 | - atomicCounter = storageService.atomicCounterBuilder() | ||
| 73 | - .withName(counter) | ||
| 74 | - .build(); | ||
| 75 | - } | ||
| 76 | CompletableFuture<Long> result; | 63 | CompletableFuture<Long> result; |
| 77 | if (delta != null) { | 64 | if (delta != null) { |
| 78 | if (getFirst) { | 65 | if (getFirst) { | ... | ... |
| ... | @@ -95,9 +95,8 @@ public class DistributedConsensusLoadTest { | ... | @@ -95,9 +95,8 @@ public class DistributedConsensusLoadTest { |
| 95 | appId = coreService.registerApplication("org.onosproject.loadtest"); | 95 | appId = coreService.registerApplication("org.onosproject.loadtest"); |
| 96 | log.info("Started with {}", appId); | 96 | log.info("Started with {}", appId); |
| 97 | for (int i = 0; i < TOTAL_COUNTERS; ++i) { | 97 | for (int i = 0; i < TOTAL_COUNTERS; ++i) { |
| 98 | - AsyncAtomicCounter counter = storageService.atomicCounterBuilder() | 98 | + AsyncAtomicCounter counter = |
| 99 | - .withName(String.format("onos-app-loadtest-counter-%d", i)) | 99 | + storageService.getAsyncAtomicCounter(String.format("onos-app-loadtest-counter-%d", i)); |
| 100 | - .build(); | ||
| 101 | counters.add(counter); | 100 | counters.add(counter); |
| 102 | } | 101 | } |
| 103 | reporter.scheduleWithFixedDelay(() -> { | 102 | reporter.scheduleWithFixedDelay(() -> { | ... | ... |
| ... | @@ -87,4 +87,24 @@ public interface StorageService { | ... | @@ -87,4 +87,24 @@ public interface StorageService { |
| 87 | * @return a builder for a transaction context. | 87 | * @return a builder for a transaction context. |
| 88 | */ | 88 | */ |
| 89 | TransactionContextBuilder transactionContextBuilder(); | 89 | TransactionContextBuilder transactionContextBuilder(); |
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * Returns an instance of {@code AsyncAtomicCounter} with specified name. | ||
| 93 | + * @param name counter name | ||
| 94 | + * | ||
| 95 | + * @return AsyncAtomicCounter instance | ||
| 96 | + */ | ||
| 97 | + default AsyncAtomicCounter getAsyncAtomicCounter(String name) { | ||
| 98 | + return atomicCounterBuilder().withName(name).build(); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * Returns an instance of {@code AtomicCounter} with specified name. | ||
| 103 | + * @param name counter name | ||
| 104 | + * | ||
| 105 | + * @return AtomicCounter instance | ||
| 106 | + */ | ||
| 107 | + default AtomicCounter getAtomicCounter(String name) { | ||
| 108 | + return getAsyncAtomicCounter(name).asAtomicCounter(); | ||
| 109 | + } | ||
| 90 | } | 110 | } | ... | ... |
| ... | @@ -67,10 +67,7 @@ public class ConsistentApplicationIdStore implements ApplicationIdStore { | ... | @@ -67,10 +67,7 @@ public class ConsistentApplicationIdStore implements ApplicationIdStore { |
| 67 | 67 | ||
| 68 | @Activate | 68 | @Activate |
| 69 | public void activate() { | 69 | public void activate() { |
| 70 | - appIdCounter = storageService.atomicCounterBuilder() | 70 | + appIdCounter = storageService.getAtomicCounter("onos-app-id-counter"); |
| 71 | - .withName("onos-app-id-counter") | ||
| 72 | - .build() | ||
| 73 | - .asAtomicCounter(); | ||
| 74 | 71 | ||
| 75 | registeredIds = storageService.<String, ApplicationId>consistentMapBuilder() | 72 | registeredIds = storageService.<String, ApplicationId>consistentMapBuilder() |
| 76 | .withName("onos-app-ids") | 73 | .withName("onos-app-ids") | ... | ... |
| ... | @@ -23,11 +23,9 @@ import org.apache.felix.scr.annotations.Deactivate; | ... | @@ -23,11 +23,9 @@ import org.apache.felix.scr.annotations.Deactivate; |
| 23 | import org.apache.felix.scr.annotations.Reference; | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; | 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; | 25 | import org.apache.felix.scr.annotations.Service; |
| 26 | -import org.onlab.util.Tools; | ||
| 27 | import org.onosproject.core.IdBlock; | 26 | import org.onosproject.core.IdBlock; |
| 28 | import org.onosproject.core.IdBlockStore; | 27 | import org.onosproject.core.IdBlockStore; |
| 29 | import org.onosproject.store.service.AtomicCounter; | 28 | import org.onosproject.store.service.AtomicCounter; |
| 30 | -import org.onosproject.store.service.StorageException; | ||
| 31 | import org.onosproject.store.service.StorageService; | 29 | import org.onosproject.store.service.StorageService; |
| 32 | import org.slf4j.Logger; | 30 | import org.slf4j.Logger; |
| 33 | 31 | ||
| ... | @@ -42,9 +40,6 @@ import static org.slf4j.LoggerFactory.getLogger; | ... | @@ -42,9 +40,6 @@ import static org.slf4j.LoggerFactory.getLogger; |
| 42 | @Service | 40 | @Service |
| 43 | public class ConsistentIdBlockStore implements IdBlockStore { | 41 | public class ConsistentIdBlockStore implements IdBlockStore { |
| 44 | 42 | ||
| 45 | - private static final int MAX_TRIES = 5; | ||
| 46 | - private static final int RETRY_DELAY_MS = 2_000; | ||
| 47 | - | ||
| 48 | private final Logger log = getLogger(getClass()); | 43 | private final Logger log = getLogger(getClass()); |
| 49 | private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap(); | 44 | private final Map<String, AtomicCounter> topicCounters = Maps.newConcurrentMap(); |
| 50 | 45 | ||
| ... | @@ -65,16 +60,7 @@ public class ConsistentIdBlockStore implements IdBlockStore { | ... | @@ -65,16 +60,7 @@ public class ConsistentIdBlockStore implements IdBlockStore { |
| 65 | 60 | ||
| 66 | @Override | 61 | @Override |
| 67 | public IdBlock getIdBlock(String topic) { | 62 | public IdBlock getIdBlock(String topic) { |
| 68 | - AtomicCounter counter = topicCounters | 63 | + AtomicCounter counter = topicCounters.computeIfAbsent(topic, storageService::getAtomicCounter); |
| 69 | - .computeIfAbsent(topic, | 64 | + return new IdBlock(counter.getAndAdd(DEFAULT_BLOCK_SIZE), DEFAULT_BLOCK_SIZE); |
| 70 | - name -> storageService.atomicCounterBuilder() | ||
| 71 | - .withName(name) | ||
| 72 | - .build() | ||
| 73 | - .asAtomicCounter()); | ||
| 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); | ||
| 79 | } | 65 | } |
| 80 | } | 66 | } | ... | ... |
| ... | @@ -50,11 +50,7 @@ public class LogicalClockManager implements LogicalClockService { | ... | @@ -50,11 +50,7 @@ public class LogicalClockManager implements LogicalClockService { |
| 50 | 50 | ||
| 51 | @Activate | 51 | @Activate |
| 52 | public void activate() { | 52 | public void activate() { |
| 53 | - atomicCounter = storageService.atomicCounterBuilder() | 53 | + atomicCounter = storageService.getAtomicCounter(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME); |
| 54 | - .withName(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME) | ||
| 55 | - .withPartitionsDisabled() | ||
| 56 | - .build() | ||
| 57 | - .asAtomicCounter(); | ||
| 58 | log.info("Started"); | 54 | log.info("Started"); |
| 59 | } | 55 | } |
| 60 | 56 | ... | ... |
| ... | @@ -69,11 +69,7 @@ public class DistributedFlowObjectiveStore | ... | @@ -69,11 +69,7 @@ public class DistributedFlowObjectiveStore |
| 69 | .build())) | 69 | .build())) |
| 70 | .build(); | 70 | .build(); |
| 71 | 71 | ||
| 72 | - nextIds = storageService.atomicCounterBuilder() | 72 | + nextIds = storageService.getAtomicCounter("next-objective-counter"); |
| 73 | - .withName("next-objective-counter") | ||
| 74 | - .build() | ||
| 75 | - .asAtomicCounter(); | ||
| 76 | - | ||
| 77 | log.info("Started"); | 73 | log.info("Started"); |
| 78 | } | 74 | } |
| 79 | 75 | ... | ... |
| ... | @@ -132,10 +132,7 @@ public class Ofdpa2GroupHandler { | ... | @@ -132,10 +132,7 @@ public class Ofdpa2GroupHandler { |
| 132 | this.serviceDirectory = context.directory(); | 132 | this.serviceDirectory = context.directory(); |
| 133 | this.groupService = serviceDirectory.get(GroupService.class); | 133 | this.groupService = serviceDirectory.get(GroupService.class); |
| 134 | this.storageService = serviceDirectory.get(StorageService.class); | 134 | this.storageService = serviceDirectory.get(StorageService.class); |
| 135 | - this.nextIndex = storageService.atomicCounterBuilder() | 135 | + this.nextIndex = storageService.getAtomicCounter("group-id-index-counter"); |
| 136 | - .withName("group-id-index-counter") | ||
| 137 | - .build() | ||
| 138 | - .asAtomicCounter(); | ||
| 139 | 136 | ||
| 140 | pendingNextObjectives = CacheBuilder.newBuilder() | 137 | pendingNextObjectives = CacheBuilder.newBuilder() |
| 141 | .expireAfterWrite(20, TimeUnit.SECONDS) | 138 | .expireAfterWrite(20, TimeUnit.SECONDS) | ... | ... |
| ... | @@ -182,10 +182,7 @@ public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, M | ... | @@ -182,10 +182,7 @@ public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, M |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | private AtomicCounter allocateCounter(DeviceId deviceId) { | 184 | private AtomicCounter allocateCounter(DeviceId deviceId) { |
| 185 | - return storageService.atomicCounterBuilder() | 185 | + return storageService.getAtomicCounter(String.format(METERCOUNTERIDENTIFIER, deviceId)); |
| 186 | - .withName(String.format(METERCOUNTERIDENTIFIER, deviceId)) | ||
| 187 | - .build() | ||
| 188 | - .asAtomicCounter(); | ||
| 189 | } | 186 | } |
| 190 | 187 | ||
| 191 | private class InternalMeterProviderService | 188 | private class InternalMeterProviderService | ... | ... |
-
Please register or login to post a comment