Committed by
Gerrit Code Review
Adds abstract distributed primitive builder + Refactored AtomicCounter and Atomi…
…cValue builder to make use of it. Change-Id: I56cef62673fabc54ca29634c27e4ff1f41ba6a88
Showing
17 changed files
with
280 additions
and
246 deletions
... | @@ -67,11 +67,11 @@ public class CounterTestIncrementCommand extends AbstractShellCommand { | ... | @@ -67,11 +67,11 @@ public class CounterTestIncrementCommand extends AbstractShellCommand { |
67 | atomicCounter = storageService.atomicCounterBuilder() | 67 | atomicCounter = storageService.atomicCounterBuilder() |
68 | .withName(counter) | 68 | .withName(counter) |
69 | .withPartitionsDisabled() | 69 | .withPartitionsDisabled() |
70 | - .buildAsyncCounter(); | 70 | + .build(); |
71 | } else { | 71 | } else { |
72 | atomicCounter = storageService.atomicCounterBuilder() | 72 | atomicCounter = storageService.atomicCounterBuilder() |
73 | .withName(counter) | 73 | .withName(counter) |
74 | - .buildAsyncCounter(); | 74 | + .build(); |
75 | } | 75 | } |
76 | CompletableFuture<Long> result; | 76 | CompletableFuture<Long> result; |
77 | if (delta != null) { | 77 | if (delta != null) { | ... | ... |
... | @@ -13,7 +13,7 @@ | ... | @@ -13,7 +13,7 @@ |
13 | * See the License for the specific language governing permissions and | 13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | -package org.onosproject.store.primitives.impl; | 16 | +package org.onosproject.store.primitives; |
17 | 17 | ||
18 | import org.onosproject.store.service.AsyncAtomicCounter; | 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
19 | import org.onosproject.store.service.AtomicCounter; | 19 | import org.onosproject.store.service.AtomicCounter; |
... | @@ -26,20 +26,17 @@ import java.util.concurrent.TimeUnit; | ... | @@ -26,20 +26,17 @@ import java.util.concurrent.TimeUnit; |
26 | import java.util.concurrent.TimeoutException; | 26 | import java.util.concurrent.TimeoutException; |
27 | 27 | ||
28 | /** | 28 | /** |
29 | - * Default implementation for a distributed AtomicCounter backed by | 29 | + * Default implementation for a {@code AtomicCounter} backed by a {@link AsyncAtomicCounter}. |
30 | - * partitioned Raft DB. | ||
31 | - * <p> | ||
32 | - * The initial value will be zero. | ||
33 | */ | 30 | */ |
34 | public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implements AtomicCounter { | 31 | public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implements AtomicCounter { |
35 | 32 | ||
36 | - private static final int OPERATION_TIMEOUT_MILLIS = 5000; | ||
37 | - | ||
38 | private final AsyncAtomicCounter asyncCounter; | 33 | private final AsyncAtomicCounter asyncCounter; |
34 | + private final long operationTimeoutMillis; | ||
39 | 35 | ||
40 | - public DefaultAtomicCounter(AsyncAtomicCounter asyncCounter) { | 36 | + public DefaultAtomicCounter(AsyncAtomicCounter asyncCounter, long operationTimeoutMillis) { |
41 | super(asyncCounter); | 37 | super(asyncCounter); |
42 | this.asyncCounter = asyncCounter; | 38 | this.asyncCounter = asyncCounter; |
39 | + this.operationTimeoutMillis = operationTimeoutMillis; | ||
43 | } | 40 | } |
44 | 41 | ||
45 | @Override | 42 | @Override |
... | @@ -77,9 +74,9 @@ public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implem | ... | @@ -77,9 +74,9 @@ public class DefaultAtomicCounter extends Synchronous<AsyncAtomicCounter> implem |
77 | return complete(asyncCounter.get()); | 74 | return complete(asyncCounter.get()); |
78 | } | 75 | } |
79 | 76 | ||
80 | - private static <T> T complete(CompletableFuture<T> future) { | 77 | + private <T> T complete(CompletableFuture<T> future) { |
81 | try { | 78 | try { |
82 | - return future.get(OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); | 79 | + return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
83 | } catch (InterruptedException e) { | 80 | } catch (InterruptedException e) { |
84 | Thread.currentThread().interrupt(); | 81 | Thread.currentThread().interrupt(); |
85 | throw new StorageException.Interrupted(); | 82 | throw new StorageException.Interrupted(); | ... | ... |
... | @@ -13,10 +13,12 @@ | ... | @@ -13,10 +13,12 @@ |
13 | * See the License for the specific language governing permissions and | 13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. | 14 | * limitations under the License. |
15 | */ | 15 | */ |
16 | -package org.onosproject.store.primitives.impl; | 16 | +package org.onosproject.store.primitives; |
17 | 17 | ||
18 | import java.util.concurrent.CompletableFuture; | 18 | import java.util.concurrent.CompletableFuture; |
19 | +import java.util.concurrent.ExecutionException; | ||
19 | import java.util.concurrent.TimeUnit; | 20 | import java.util.concurrent.TimeUnit; |
21 | +import java.util.concurrent.TimeoutException; | ||
20 | 22 | ||
21 | import org.onosproject.store.service.AsyncAtomicValue; | 23 | import org.onosproject.store.service.AsyncAtomicValue; |
22 | import org.onosproject.store.service.AtomicValue; | 24 | import org.onosproject.store.service.AtomicValue; |
... | @@ -24,21 +26,20 @@ import org.onosproject.store.service.AtomicValueEventListener; | ... | @@ -24,21 +26,20 @@ import org.onosproject.store.service.AtomicValueEventListener; |
24 | import org.onosproject.store.service.StorageException; | 26 | import org.onosproject.store.service.StorageException; |
25 | import org.onosproject.store.service.Synchronous; | 27 | import org.onosproject.store.service.Synchronous; |
26 | 28 | ||
27 | -import com.google.common.util.concurrent.Futures; | ||
28 | - | ||
29 | /** | 29 | /** |
30 | - * Default implementation of {@link AtomicValue}. | 30 | + * Default implementation for a {@code AtomicValue} backed by a {@link AsyncAtomicValue}. |
31 | * | 31 | * |
32 | * @param <V> value type | 32 | * @param <V> value type |
33 | */ | 33 | */ |
34 | public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> implements AtomicValue<V> { | 34 | public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> implements AtomicValue<V> { |
35 | 35 | ||
36 | - private static final int OPERATION_TIMEOUT_MILLIS = 5000; | ||
37 | private final AsyncAtomicValue<V> asyncValue; | 36 | private final AsyncAtomicValue<V> asyncValue; |
37 | + private final long operationTimeoutMillis; | ||
38 | 38 | ||
39 | - public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue) { | 39 | + public DefaultAtomicValue(AsyncAtomicValue<V> asyncValue, long operationTimeoutMillis) { |
40 | super(asyncValue); | 40 | super(asyncValue); |
41 | this.asyncValue = asyncValue; | 41 | this.asyncValue = asyncValue; |
42 | + this.operationTimeoutMillis = operationTimeoutMillis; | ||
42 | } | 43 | } |
43 | 44 | ||
44 | @Override | 45 | @Override |
... | @@ -71,7 +72,16 @@ public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> impl | ... | @@ -71,7 +72,16 @@ public class DefaultAtomicValue<V> extends Synchronous<AsyncAtomicValue<V>> impl |
71 | complete(asyncValue.removeListener(listener)); | 72 | complete(asyncValue.removeListener(listener)); |
72 | } | 73 | } |
73 | 74 | ||
74 | - private static <V> V complete(CompletableFuture<V> future) { | 75 | + private <T> T complete(CompletableFuture<T> future) { |
75 | - return Futures.getChecked(future, StorageException.class, OPERATION_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); | 76 | + try { |
77 | + return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); | ||
78 | + } catch (InterruptedException e) { | ||
79 | + Thread.currentThread().interrupt(); | ||
80 | + throw new StorageException.Interrupted(); | ||
81 | + } catch (TimeoutException e) { | ||
82 | + throw new StorageException.Timeout(); | ||
83 | + } catch (ExecutionException e) { | ||
84 | + throw new StorageException(e.getCause()); | ||
85 | + } | ||
76 | } | 86 | } |
77 | } | 87 | } |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016 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 | + */ | ||
16 | +package org.onosproject.store.primitives; | ||
17 | + | ||
18 | +import org.onosproject.core.ApplicationId; | ||
19 | +import org.onosproject.store.service.DistributedPrimitive; | ||
20 | +import org.onosproject.store.service.Serializer; | ||
21 | + | ||
22 | +/** | ||
23 | + * Abstract builder for distributed primitives. | ||
24 | + * | ||
25 | + * @param <T> distributed primitive type | ||
26 | + */ | ||
27 | +public abstract class DistributedPrimitiveBuilder<T extends DistributedPrimitive> { | ||
28 | + | ||
29 | + private DistributedPrimitive.Type type; | ||
30 | + private String name; | ||
31 | + private ApplicationId applicationId; | ||
32 | + private Serializer serializer; | ||
33 | + private boolean partitionsDisabled = false; | ||
34 | + private boolean meteringDisabled = false; | ||
35 | + | ||
36 | + public DistributedPrimitiveBuilder(DistributedPrimitive.Type type) { | ||
37 | + this.type = type; | ||
38 | + } | ||
39 | + | ||
40 | + /** | ||
41 | + * Sets the primitive name. | ||
42 | + * | ||
43 | + * @param name primitive name | ||
44 | + * @return this builder | ||
45 | + */ | ||
46 | + public DistributedPrimitiveBuilder<T> withName(String name) { | ||
47 | + this.name = name; | ||
48 | + return this; | ||
49 | + } | ||
50 | + | ||
51 | + /** | ||
52 | + * Sets the serializer to use for transcoding info held in the primitive. | ||
53 | + * | ||
54 | + * @param serializer serializer | ||
55 | + * @return this builder | ||
56 | + */ | ||
57 | + public DistributedPrimitiveBuilder<T> withSerializer(Serializer serializer) { | ||
58 | + this.serializer = serializer; | ||
59 | + return this; | ||
60 | + } | ||
61 | + | ||
62 | + /** | ||
63 | + * Sets the application id that owns this primitive. | ||
64 | + * | ||
65 | + * @param applicationId application identifier | ||
66 | + * @return this builder | ||
67 | + */ | ||
68 | + public DistributedPrimitiveBuilder<T> withApplicationId(ApplicationId applicationId) { | ||
69 | + this.applicationId = applicationId; | ||
70 | + return this; | ||
71 | + } | ||
72 | + | ||
73 | + /** | ||
74 | + * Creates this primitive on a special partition that comprises of all members in the cluster. | ||
75 | + * @deprecated usage of this method is discouraged for most common scenarios. Eventually it will be replaced | ||
76 | + * with a better alternative that does not exposes low level details. Until then avoid using this method. | ||
77 | + * @return this builder | ||
78 | + */ | ||
79 | + @Deprecated | ||
80 | + public DistributedPrimitiveBuilder<T> withPartitionsDisabled() { | ||
81 | + this.partitionsDisabled = true; | ||
82 | + return this; | ||
83 | + } | ||
84 | + | ||
85 | + /** | ||
86 | + * Disables recording usage stats for this primitive. | ||
87 | + * @deprecated usage of this method is discouraged for most common scenarios. | ||
88 | + * @return this builder | ||
89 | + */ | ||
90 | + @Deprecated | ||
91 | + public DistributedPrimitiveBuilder<T> withMeteringDisabled() { | ||
92 | + this.meteringDisabled = true; | ||
93 | + return this; | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * Returns if metering is enabled. | ||
98 | + * | ||
99 | + * @return {@code true} if yes; {@code false} otherwise | ||
100 | + */ | ||
101 | + public final boolean meteringEnabled() { | ||
102 | + return !meteringDisabled; | ||
103 | + } | ||
104 | + | ||
105 | + /** | ||
106 | + * Returns if partitions are disabled. | ||
107 | + * | ||
108 | + * @return {@code true} if yes; {@code false} otherwise | ||
109 | + */ | ||
110 | + public final boolean partitionsDisabled() { | ||
111 | + return partitionsDisabled; | ||
112 | + } | ||
113 | + | ||
114 | + /** | ||
115 | + * Returns the serializer. | ||
116 | + * | ||
117 | + * @return serializer | ||
118 | + */ | ||
119 | + public final Serializer serializer() { | ||
120 | + return serializer; | ||
121 | + } | ||
122 | + | ||
123 | + /** | ||
124 | + * Returns the application identifier. | ||
125 | + * | ||
126 | + * @return application id | ||
127 | + */ | ||
128 | + public final ApplicationId applicationId() { | ||
129 | + return applicationId; | ||
130 | + } | ||
131 | + | ||
132 | + /** | ||
133 | + * Returns the name of the primitive. | ||
134 | + * | ||
135 | + * @return primitive name | ||
136 | + */ | ||
137 | + public final String name() { | ||
138 | + return name; | ||
139 | + } | ||
140 | + | ||
141 | + /** | ||
142 | + * Returns the primitive type. | ||
143 | + * | ||
144 | + * @return primitive type | ||
145 | + */ | ||
146 | + public final DistributedPrimitive.Type type() { | ||
147 | + return type; | ||
148 | + } | ||
149 | + | ||
150 | + /** | ||
151 | + * Constructs an instance of the distributed primitive. | ||
152 | + * @return distributed primitive | ||
153 | + */ | ||
154 | + public abstract T build(); | ||
155 | +} |
... | @@ -17,6 +17,8 @@ package org.onosproject.store.service; | ... | @@ -17,6 +17,8 @@ package org.onosproject.store.service; |
17 | 17 | ||
18 | import java.util.concurrent.CompletableFuture; | 18 | import java.util.concurrent.CompletableFuture; |
19 | 19 | ||
20 | +import org.onosproject.store.primitives.DefaultAtomicCounter; | ||
21 | + | ||
20 | /** | 22 | /** |
21 | * An async atomic counter dispenses monotonically increasing values. | 23 | * An async atomic counter dispenses monotonically increasing values. |
22 | */ | 24 | */ |
... | @@ -81,4 +83,23 @@ public interface AsyncAtomicCounter extends DistributedPrimitive { | ... | @@ -81,4 +83,23 @@ public interface AsyncAtomicCounter extends DistributedPrimitive { |
81 | * @return true if the update occurred and the expected value was equal to the current value, false otherwise | 83 | * @return true if the update occurred and the expected value was equal to the current value, false otherwise |
82 | */ | 84 | */ |
83 | CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue); | 85 | CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue); |
86 | + | ||
87 | + /** | ||
88 | + * Returns a new {@link AtomicCounter} that is backed by this instance. | ||
89 | + * | ||
90 | + * @param timeoutMillis timeout duration for the returned ConsistentMap operations | ||
91 | + * @return new {@code ConsistentMap} instance | ||
92 | + */ | ||
93 | + default AtomicCounter asAtomicCounter(long timeoutMillis) { | ||
94 | + return new DefaultAtomicCounter(this, timeoutMillis); | ||
95 | + } | ||
96 | + | ||
97 | + /** | ||
98 | + * Returns a new {@link AtomicCounter} that is backed by this instance and with a default operation timeout. | ||
99 | + * | ||
100 | + * @return new {@code ConsistentMap} instance | ||
101 | + */ | ||
102 | + default AtomicCounter asAtomicCounter() { | ||
103 | + return new DefaultAtomicCounter(this, DEFAULT_OPERTATION_TIMEOUT_MILLIS); | ||
104 | + } | ||
84 | } | 105 | } | ... | ... |
... | @@ -17,6 +17,8 @@ package org.onosproject.store.service; | ... | @@ -17,6 +17,8 @@ package org.onosproject.store.service; |
17 | 17 | ||
18 | import java.util.concurrent.CompletableFuture; | 18 | import java.util.concurrent.CompletableFuture; |
19 | 19 | ||
20 | +import org.onosproject.store.primitives.DefaultAtomicValue; | ||
21 | + | ||
20 | /** | 22 | /** |
21 | * Distributed version of java.util.concurrent.atomic.AtomicReference. | 23 | * Distributed version of java.util.concurrent.atomic.AtomicReference. |
22 | * <p> | 24 | * <p> |
... | @@ -80,4 +82,23 @@ public interface AsyncAtomicValue<V> extends DistributedPrimitive { | ... | @@ -80,4 +82,23 @@ public interface AsyncAtomicValue<V> extends DistributedPrimitive { |
80 | * @return CompletableFuture that will be completed when the operation finishes | 82 | * @return CompletableFuture that will be completed when the operation finishes |
81 | */ | 83 | */ |
82 | CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener); | 84 | CompletableFuture<Void> removeListener(AtomicValueEventListener<V> listener); |
85 | + | ||
86 | + /** | ||
87 | + * Returns a new {@link AtomicValue} that is backed by this instance. | ||
88 | + * | ||
89 | + * @param timeoutMillis timeout duration for the returned ConsistentMap operations | ||
90 | + * @return new {@code AtomicValue} instance | ||
91 | + */ | ||
92 | + default AtomicValue<V> asAtomicValue(long timeoutMillis) { | ||
93 | + return new DefaultAtomicValue<>(this, timeoutMillis); | ||
94 | + } | ||
95 | + | ||
96 | + /** | ||
97 | + * Returns a new {@link AtomicValue} that is backed by this instance and with a default operation timeout. | ||
98 | + * | ||
99 | + * @return new {@code AtomicValue} instance | ||
100 | + */ | ||
101 | + default AtomicValue<V> asAtomicValue() { | ||
102 | + return new DefaultAtomicValue<>(this, DEFAULT_OPERTATION_TIMEOUT_MILLIS); | ||
103 | + } | ||
83 | } | 104 | } | ... | ... |
... | @@ -15,61 +15,13 @@ | ... | @@ -15,61 +15,13 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.service; | 16 | package org.onosproject.store.service; |
17 | 17 | ||
18 | +import org.onosproject.store.primitives.DistributedPrimitiveBuilder; | ||
19 | + | ||
18 | /** | 20 | /** |
19 | * Builder for AtomicCounter. | 21 | * Builder for AtomicCounter. |
20 | */ | 22 | */ |
21 | -public interface AtomicCounterBuilder { | 23 | +public abstract class AtomicCounterBuilder extends DistributedPrimitiveBuilder<AsyncAtomicCounter> { |
22 | - | 24 | + public AtomicCounterBuilder() { |
23 | - /** | 25 | + super(DistributedPrimitive.Type.COUNTER); |
24 | - * Sets the name for the atomic counter. | 26 | + } |
25 | - * <p> | 27 | +} |
26 | - * Each atomic counter is identified by a unique name. | ||
27 | - * </p> | ||
28 | - * <p> | ||
29 | - * Note: This is a mandatory parameter. | ||
30 | - * </p> | ||
31 | - * | ||
32 | - * @param name name of the atomic counter | ||
33 | - * @return this AtomicCounterBuilder | ||
34 | - */ | ||
35 | - AtomicCounterBuilder withName(String name); | ||
36 | - | ||
37 | - /** | ||
38 | - * Creates this counter on the partition that spans the entire cluster. | ||
39 | - * <p> | ||
40 | - * When partitioning is disabled, the counter state will be | ||
41 | - * ephemeral and does not survive a full cluster restart. | ||
42 | - * </p> | ||
43 | - * <p> | ||
44 | - * Note: By default partitions are enabled. | ||
45 | - * </p> | ||
46 | - * @return this AtomicCounterBuilder | ||
47 | - */ | ||
48 | - AtomicCounterBuilder withPartitionsDisabled(); | ||
49 | - | ||
50 | - /** | ||
51 | - * Instantiates Metering service to gather usage and performance metrics. | ||
52 | - * By default, usage data will be stored. | ||
53 | - * | ||
54 | - * @return this AtomicCounterBuilder | ||
55 | - */ | ||
56 | - AtomicCounterBuilder withMeteringDisabled(); | ||
57 | - | ||
58 | - /** | ||
59 | - * Builds a AtomicCounter based on the configuration options | ||
60 | - * supplied to this builder. | ||
61 | - * | ||
62 | - * @return new AtomicCounter | ||
63 | - * @throws java.lang.RuntimeException if a mandatory parameter is missing | ||
64 | - */ | ||
65 | - AtomicCounter build(); | ||
66 | - | ||
67 | - /** | ||
68 | - * Builds a AsyncAtomicCounter based on the configuration options | ||
69 | - * supplied to this builder. | ||
70 | - * | ||
71 | - * @return new AsyncAtomicCounter | ||
72 | - * @throws java.lang.RuntimeException if a mandatory parameter is missing | ||
73 | - */ | ||
74 | - AsyncAtomicCounter buildAsyncCounter(); | ||
75 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -15,65 +15,16 @@ | ... | @@ -15,65 +15,16 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.service; | 16 | package org.onosproject.store.service; |
17 | 17 | ||
18 | +import org.onosproject.store.primitives.DistributedPrimitiveBuilder; | ||
19 | + | ||
18 | /** | 20 | /** |
19 | * Builder for constructing new AtomicValue instances. | 21 | * Builder for constructing new AtomicValue instances. |
20 | * | 22 | * |
21 | * @param <V> atomic value type | 23 | * @param <V> atomic value type |
22 | */ | 24 | */ |
23 | -public interface AtomicValueBuilder<V> { | 25 | +public abstract class AtomicValueBuilder<V> extends DistributedPrimitiveBuilder<AsyncAtomicValue<V>> { |
24 | - /** | ||
25 | - * Sets the name for the atomic value. | ||
26 | - * <p> | ||
27 | - * Each atomic value is identified by a unique name. | ||
28 | - * </p> | ||
29 | - * <p> | ||
30 | - * Note: This is a mandatory parameter. | ||
31 | - * </p> | ||
32 | - * | ||
33 | - * @param name name of the atomic value | ||
34 | - * @return this AtomicValueBuilder for method chaining | ||
35 | - */ | ||
36 | - AtomicValueBuilder<V> withName(String name); | ||
37 | - | ||
38 | - /** | ||
39 | - * Sets a serializer that can be used to serialize the value. | ||
40 | - * <p> | ||
41 | - * Note: This is a mandatory parameter. | ||
42 | - * </p> | ||
43 | - * | ||
44 | - * @param serializer serializer | ||
45 | - * @return this AtomicValueBuilder for method chaining | ||
46 | - */ | ||
47 | - AtomicValueBuilder<V> withSerializer(Serializer serializer); | ||
48 | - | ||
49 | - /** | ||
50 | - * Creates this atomic value on the partition that spans the entire cluster. | ||
51 | - * <p> | ||
52 | - * When partitioning is disabled, the value state will be | ||
53 | - * ephemeral and does not survive a full cluster restart. | ||
54 | - * </p> | ||
55 | - * <p> | ||
56 | - * Note: By default partitions are enabled. | ||
57 | - * </p> | ||
58 | - * @return this AtomicValueBuilder for method chaining | ||
59 | - */ | ||
60 | - AtomicValueBuilder<V> withPartitionsDisabled(); | ||
61 | - | ||
62 | - /** | ||
63 | - * Builds a AsyncAtomicValue based on the configuration options | ||
64 | - * supplied to this builder. | ||
65 | - * | ||
66 | - * @return new AsyncAtomicValue | ||
67 | - * @throws java.lang.RuntimeException if a mandatory parameter is missing | ||
68 | - */ | ||
69 | - AsyncAtomicValue<V> buildAsyncValue(); | ||
70 | 26 | ||
71 | - /** | 27 | + public AtomicValueBuilder() { |
72 | - * Builds a AtomicValue based on the configuration options | 28 | + super(DistributedPrimitive.Type.VALUE); |
73 | - * supplied to this builder. | 29 | + } |
74 | - * | ||
75 | - * @return new AtomicValue | ||
76 | - * @throws java.lang.RuntimeException if a mandatory parameter is missing | ||
77 | - */ | ||
78 | - AtomicValue<V> build(); | ||
79 | } | 30 | } | ... | ... |
... | @@ -64,6 +64,8 @@ public interface DistributedPrimitive { | ... | @@ -64,6 +64,8 @@ public interface DistributedPrimitive { |
64 | LEADER_ELECTOR | 64 | LEADER_ELECTOR |
65 | } | 65 | } |
66 | 66 | ||
67 | + static final long DEFAULT_OPERTATION_TIMEOUT_MILLIS = 5000L; | ||
68 | + | ||
67 | /** | 69 | /** |
68 | * Returns the name of this primitive. | 70 | * Returns the name of this primitive. |
69 | * @return name | 71 | * @return name | ... | ... |
... | @@ -15,12 +15,13 @@ | ... | @@ -15,12 +15,13 @@ |
15 | */ | 15 | */ |
16 | package org.onosproject.store.service; | 16 | package org.onosproject.store.service; |
17 | 17 | ||
18 | +import java.util.concurrent.CompletableFuture; | ||
18 | import java.util.concurrent.atomic.AtomicLong; | 19 | import java.util.concurrent.atomic.AtomicLong; |
19 | 20 | ||
20 | /** | 21 | /** |
21 | * Test implementation of atomic counter. | 22 | * Test implementation of atomic counter. |
22 | */ | 23 | */ |
23 | -public final class TestAtomicCounter implements AtomicCounter { | 24 | +public final class TestAtomicCounter implements AsyncAtomicCounter { |
24 | final AtomicLong value; | 25 | final AtomicLong value; |
25 | 26 | ||
26 | @Override | 27 | @Override |
... | @@ -28,77 +29,53 @@ public final class TestAtomicCounter implements AtomicCounter { | ... | @@ -28,77 +29,53 @@ public final class TestAtomicCounter implements AtomicCounter { |
28 | return null; | 29 | return null; |
29 | } | 30 | } |
30 | 31 | ||
31 | - @Override | ||
32 | - public Type type() { | ||
33 | - return Type.COUNTER; | ||
34 | - } | ||
35 | - | ||
36 | private TestAtomicCounter() { | 32 | private TestAtomicCounter() { |
37 | value = new AtomicLong(); | 33 | value = new AtomicLong(); |
38 | } | 34 | } |
39 | 35 | ||
40 | @Override | 36 | @Override |
41 | - public long incrementAndGet() { | 37 | + public CompletableFuture<Long> incrementAndGet() { |
42 | - return value.incrementAndGet(); | 38 | + return CompletableFuture.completedFuture(value.incrementAndGet()); |
43 | } | 39 | } |
44 | 40 | ||
45 | @Override | 41 | @Override |
46 | - public long getAndIncrement() { | 42 | + public CompletableFuture<Long> getAndIncrement() { |
47 | - return value.getAndIncrement(); | 43 | + return CompletableFuture.completedFuture(value.getAndIncrement()); |
48 | } | 44 | } |
49 | 45 | ||
50 | @Override | 46 | @Override |
51 | - public long getAndAdd(long delta) { | 47 | + public CompletableFuture<Long> getAndAdd(long delta) { |
52 | - return value.getAndAdd(delta); | 48 | + return CompletableFuture.completedFuture(value.getAndAdd(delta)); |
53 | } | 49 | } |
54 | 50 | ||
55 | @Override | 51 | @Override |
56 | - public long addAndGet(long delta) { | 52 | + public CompletableFuture<Long> addAndGet(long delta) { |
57 | - return value.addAndGet(delta); | 53 | + return CompletableFuture.completedFuture(value.addAndGet(delta)); |
58 | } | 54 | } |
59 | 55 | ||
60 | @Override | 56 | @Override |
61 | - public void set(long value) { | 57 | + public CompletableFuture<Void> set(long value) { |
62 | this.value.set(value); | 58 | this.value.set(value); |
59 | + return CompletableFuture.completedFuture(null); | ||
63 | } | 60 | } |
64 | 61 | ||
65 | @Override | 62 | @Override |
66 | - public boolean compareAndSet(long expectedValue, long updateValue) { | 63 | + public CompletableFuture<Boolean> compareAndSet(long expectedValue, long updateValue) { |
67 | - return value.compareAndSet(expectedValue, updateValue); | 64 | + return CompletableFuture.completedFuture(value.compareAndSet(expectedValue, updateValue)); |
68 | } | 65 | } |
69 | 66 | ||
70 | @Override | 67 | @Override |
71 | - public long get() { | 68 | + public CompletableFuture<Long> get() { |
72 | - return value.get(); | 69 | + return CompletableFuture.completedFuture(value.get()); |
73 | } | 70 | } |
74 | 71 | ||
75 | public static AtomicCounterBuilder builder() { | 72 | public static AtomicCounterBuilder builder() { |
76 | return new Builder(); | 73 | return new Builder(); |
77 | } | 74 | } |
78 | 75 | ||
79 | - public static class Builder implements AtomicCounterBuilder { | 76 | + public static class Builder extends AtomicCounterBuilder { |
80 | - @Override | ||
81 | - public AtomicCounterBuilder withName(String name) { | ||
82 | - return this; | ||
83 | - } | ||
84 | - | ||
85 | - @Override | ||
86 | - public AtomicCounterBuilder withPartitionsDisabled() { | ||
87 | - return this; | ||
88 | - } | ||
89 | - | ||
90 | - @Override | ||
91 | - public AtomicCounterBuilder withMeteringDisabled() { | ||
92 | - return this; | ||
93 | - } | ||
94 | - | ||
95 | - @Override | ||
96 | - public AsyncAtomicCounter buildAsyncCounter() { | ||
97 | - throw new UnsupportedOperationException("Async Counter is not supported"); | ||
98 | - } | ||
99 | - | ||
100 | @Override | 77 | @Override |
101 | - public AtomicCounter build() { | 78 | + public AsyncAtomicCounter build() { |
102 | return new TestAtomicCounter(); | 79 | return new TestAtomicCounter(); |
103 | } | 80 | } |
104 | } | 81 | } | ... | ... |
... | @@ -69,7 +69,8 @@ public class ConsistentApplicationIdStore implements ApplicationIdStore { | ... | @@ -69,7 +69,8 @@ public class ConsistentApplicationIdStore implements ApplicationIdStore { |
69 | public void activate() { | 69 | public void activate() { |
70 | appIdCounter = storageService.atomicCounterBuilder() | 70 | appIdCounter = storageService.atomicCounterBuilder() |
71 | .withName("onos-app-id-counter") | 71 | .withName("onos-app-id-counter") |
72 | - .build(); | 72 | + .build() |
73 | + .asAtomicCounter(); | ||
73 | 74 | ||
74 | registeredIds = storageService.<String, ApplicationId>consistentMapBuilder() | 75 | registeredIds = storageService.<String, ApplicationId>consistentMapBuilder() |
75 | .withName("onos-app-ids") | 76 | .withName("onos-app-ids") | ... | ... |
... | @@ -69,7 +69,8 @@ public class ConsistentIdBlockStore implements IdBlockStore { | ... | @@ -69,7 +69,8 @@ public class ConsistentIdBlockStore implements IdBlockStore { |
69 | .computeIfAbsent(topic, | 69 | .computeIfAbsent(topic, |
70 | name -> storageService.atomicCounterBuilder() | 70 | name -> storageService.atomicCounterBuilder() |
71 | .withName(name) | 71 | .withName(name) |
72 | - .build()); | 72 | + .build() |
73 | + .asAtomicCounter()); | ||
73 | Long blockBase = Tools.retryable(counter::getAndAdd, | 74 | Long blockBase = Tools.retryable(counter::getAndAdd, |
74 | StorageException.class, | 75 | StorageException.class, |
75 | MAX_TRIES, | 76 | MAX_TRIES, | ... | ... |
... | @@ -50,7 +50,8 @@ public class LogicalClockManager implements LogicalClockService { | ... | @@ -50,7 +50,8 @@ public class LogicalClockManager implements LogicalClockService { |
50 | atomicCounter = storageService.atomicCounterBuilder() | 50 | atomicCounter = storageService.atomicCounterBuilder() |
51 | .withName(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME) | 51 | .withName(SYSTEM_LOGICAL_CLOCK_COUNTER_NAME) |
52 | .withPartitionsDisabled() | 52 | .withPartitionsDisabled() |
53 | - .build(); | 53 | + .build() |
54 | + .asAtomicCounter(); | ||
54 | log.info("Started"); | 55 | log.info("Started"); |
55 | } | 56 | } |
56 | 57 | ... | ... |
... | @@ -68,7 +68,8 @@ public class DistributedFlowObjectiveStore | ... | @@ -68,7 +68,8 @@ public class DistributedFlowObjectiveStore |
68 | 68 | ||
69 | nextIds = storageService.atomicCounterBuilder() | 69 | nextIds = storageService.atomicCounterBuilder() |
70 | .withName("next-objective-counter") | 70 | .withName("next-objective-counter") |
71 | - .build(); | 71 | + .build() |
72 | + .asAtomicCounter(); | ||
72 | 73 | ||
73 | log.info("Started"); | 74 | log.info("Started"); |
74 | } | 75 | } | ... | ... |
... | @@ -16,22 +16,17 @@ | ... | @@ -16,22 +16,17 @@ |
16 | package org.onosproject.store.primitives.impl; | 16 | package org.onosproject.store.primitives.impl; |
17 | 17 | ||
18 | import org.onosproject.store.service.AsyncAtomicCounter; | 18 | import org.onosproject.store.service.AsyncAtomicCounter; |
19 | -import org.onosproject.store.service.AtomicCounter; | ||
20 | import org.onosproject.store.service.AtomicCounterBuilder; | 19 | import org.onosproject.store.service.AtomicCounterBuilder; |
21 | 20 | ||
22 | -import static com.google.common.base.Preconditions.checkArgument; | 21 | +import static com.google.common.base.Preconditions.checkNotNull; |
23 | -import static com.google.common.base.Preconditions.checkState; | ||
24 | 22 | ||
25 | /** | 23 | /** |
26 | * Default implementation of AtomicCounterBuilder. | 24 | * Default implementation of AtomicCounterBuilder. |
27 | */ | 25 | */ |
28 | -public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder { | 26 | +public class DefaultAtomicCounterBuilder extends AtomicCounterBuilder { |
29 | 27 | ||
30 | - private String name; | ||
31 | - private boolean partitionsEnabled = true; | ||
32 | private final Database partitionedDatabase; | 28 | private final Database partitionedDatabase; |
33 | private final Database inMemoryDatabase; | 29 | private final Database inMemoryDatabase; |
34 | - private boolean metering = true; | ||
35 | 30 | ||
36 | public DefaultAtomicCounterBuilder(Database inMemoryDatabase, Database partitionedDatabase) { | 31 | public DefaultAtomicCounterBuilder(Database inMemoryDatabase, Database partitionedDatabase) { |
37 | this.inMemoryDatabase = inMemoryDatabase; | 32 | this.inMemoryDatabase = inMemoryDatabase; |
... | @@ -39,37 +34,8 @@ public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder { | ... | @@ -39,37 +34,8 @@ public class DefaultAtomicCounterBuilder implements AtomicCounterBuilder { |
39 | } | 34 | } |
40 | 35 | ||
41 | @Override | 36 | @Override |
42 | - public AtomicCounterBuilder withName(String name) { | 37 | + public AsyncAtomicCounter build() { |
43 | - checkArgument(name != null && !name.isEmpty()); | 38 | + Database database = partitionsDisabled() ? inMemoryDatabase : partitionedDatabase; |
44 | - this.name = name; | 39 | + return new DefaultAsyncAtomicCounter(checkNotNull(name()), database, meteringEnabled()); |
45 | - return this; | ||
46 | - } | ||
47 | - | ||
48 | - @Override | ||
49 | - public AtomicCounterBuilder withPartitionsDisabled() { | ||
50 | - partitionsEnabled = false; | ||
51 | - return this; | ||
52 | - } | ||
53 | - | ||
54 | - @Override | ||
55 | - public AtomicCounter build() { | ||
56 | - return new DefaultAtomicCounter(buildAsyncCounter()); | ||
57 | - } | ||
58 | - | ||
59 | - @Override | ||
60 | - public AsyncAtomicCounter buildAsyncCounter() { | ||
61 | - validateInputs(); | ||
62 | - Database database = partitionsEnabled ? partitionedDatabase : inMemoryDatabase; | ||
63 | - return new DefaultAsyncAtomicCounter(name, database, metering); | ||
64 | - } | ||
65 | - | ||
66 | - @Override | ||
67 | - public AtomicCounterBuilder withMeteringDisabled() { | ||
68 | - metering = false; | ||
69 | - return this; | ||
70 | - } | ||
71 | - | ||
72 | - private void validateInputs() { | ||
73 | - checkState(name != null, "name must be specified"); | ||
74 | } | 40 | } |
75 | } | 41 | } | ... | ... |
... | @@ -18,20 +18,18 @@ package org.onosproject.store.primitives.impl; | ... | @@ -18,20 +18,18 @@ package org.onosproject.store.primitives.impl; |
18 | import java.util.function.Supplier; | 18 | import java.util.function.Supplier; |
19 | 19 | ||
20 | import org.onosproject.store.service.AsyncAtomicValue; | 20 | import org.onosproject.store.service.AsyncAtomicValue; |
21 | -import org.onosproject.store.service.AtomicValue; | ||
22 | import org.onosproject.store.service.AtomicValueBuilder; | 21 | import org.onosproject.store.service.AtomicValueBuilder; |
23 | import org.onosproject.store.service.ConsistentMapBuilder; | 22 | import org.onosproject.store.service.ConsistentMapBuilder; |
24 | -import org.onosproject.store.service.Serializer; | 23 | + |
24 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
25 | 25 | ||
26 | /** | 26 | /** |
27 | * Default implementation of AtomicValueBuilder. | 27 | * Default implementation of AtomicValueBuilder. |
28 | * | 28 | * |
29 | * @param <V> value type | 29 | * @param <V> value type |
30 | */ | 30 | */ |
31 | -public class DefaultAtomicValueBuilder<V> implements AtomicValueBuilder<V> { | 31 | +public class DefaultAtomicValueBuilder<V> extends AtomicValueBuilder<V> { |
32 | 32 | ||
33 | - private String name; | ||
34 | - private Serializer serializer; | ||
35 | private ConsistentMapBuilder<String, byte[]> mapBuilder; | 33 | private ConsistentMapBuilder<String, byte[]> mapBuilder; |
36 | 34 | ||
37 | public DefaultAtomicValueBuilder(Supplier<ConsistentMapBuilder<String, byte[]>> mapBuilderSupplier) { | 35 | public DefaultAtomicValueBuilder(Supplier<ConsistentMapBuilder<String, byte[]>> mapBuilderSupplier) { |
... | @@ -39,30 +37,9 @@ public class DefaultAtomicValueBuilder<V> implements AtomicValueBuilder<V> { | ... | @@ -39,30 +37,9 @@ public class DefaultAtomicValueBuilder<V> implements AtomicValueBuilder<V> { |
39 | } | 37 | } |
40 | 38 | ||
41 | @Override | 39 | @Override |
42 | - public AtomicValueBuilder<V> withName(String name) { | 40 | + public AsyncAtomicValue<V> build() { |
43 | - this.name = name; | 41 | + return new DefaultAsyncAtomicValue<>(checkNotNull(name()), |
44 | - return this; | 42 | + checkNotNull(serializer()), |
45 | - } | 43 | + mapBuilder.buildAsyncMap()); |
46 | - | ||
47 | - @Override | ||
48 | - public AtomicValueBuilder<V> withSerializer(Serializer serializer) { | ||
49 | - mapBuilder.withSerializer(serializer); | ||
50 | - return this; | ||
51 | - } | ||
52 | - | ||
53 | - @Override | ||
54 | - public AtomicValueBuilder<V> withPartitionsDisabled() { | ||
55 | - mapBuilder.withPartitionsDisabled(); | ||
56 | - return this; | ||
57 | - } | ||
58 | - | ||
59 | - @Override | ||
60 | - public AsyncAtomicValue<V> buildAsyncValue() { | ||
61 | - return new DefaultAsyncAtomicValue<>(name, serializer, mapBuilder.buildAsyncMap()); | ||
62 | - } | ||
63 | - | ||
64 | - @Override | ||
65 | - public AtomicValue<V> build() { | ||
66 | - return new DefaultAtomicValue<>(buildAsyncValue()); | ||
67 | } | 44 | } |
68 | -} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
45 | +} | ... | ... |
... | @@ -183,7 +183,8 @@ public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, M | ... | @@ -183,7 +183,8 @@ public class MeterManager extends AbstractListenerProviderRegistry<MeterEvent, M |
183 | private AtomicCounter allocateCounter(DeviceId deviceId) { | 183 | private AtomicCounter allocateCounter(DeviceId deviceId) { |
184 | return storageService.atomicCounterBuilder() | 184 | return storageService.atomicCounterBuilder() |
185 | .withName(String.format(METERCOUNTERIDENTIFIER, deviceId)) | 185 | .withName(String.format(METERCOUNTERIDENTIFIER, deviceId)) |
186 | - .build(); | 186 | + .build() |
187 | + .asAtomicCounter(); | ||
187 | } | 188 | } |
188 | 189 | ||
189 | private class InternalMeterProviderService | 190 | private class InternalMeterProviderService | ... | ... |
-
Please register or login to post a comment