Committed by
Gerrit Code Review
Method name refactor in DistributedPrimitive + Builder for AsyncLeaderElector
Change-Id: I59be6e66665c0b12d02106bd5c722e9fa38dd7a1
Showing
28 changed files
with
276 additions
and
41 deletions
| ... | @@ -21,6 +21,7 @@ import org.onosproject.store.service.DistributedSetBuilder; | ... | @@ -21,6 +21,7 @@ import org.onosproject.store.service.DistributedSetBuilder; |
| 21 | import org.onosproject.store.service.DistributedQueueBuilder; | 21 | import org.onosproject.store.service.DistributedQueueBuilder; |
| 22 | import org.onosproject.store.service.AtomicCounterBuilder; | 22 | import org.onosproject.store.service.AtomicCounterBuilder; |
| 23 | import org.onosproject.store.service.AtomicValueBuilder; | 23 | import org.onosproject.store.service.AtomicValueBuilder; |
| 24 | +import org.onosproject.store.service.LeaderElectorBuilder; | ||
| 24 | import org.onosproject.store.service.TransactionContextBuilder; | 25 | import org.onosproject.store.service.TransactionContextBuilder; |
| 25 | import org.onosproject.store.service.StorageService; | 26 | import org.onosproject.store.service.StorageService; |
| 26 | 27 | ||
| ... | @@ -62,4 +63,9 @@ public class VtnStorageServiceAdapter implements StorageService { | ... | @@ -62,4 +63,9 @@ public class VtnStorageServiceAdapter implements StorageService { |
| 62 | public TransactionContextBuilder transactionContextBuilder() { | 63 | public TransactionContextBuilder transactionContextBuilder() { |
| 63 | return null; | 64 | return null; |
| 64 | } | 65 | } |
| 66 | + | ||
| 67 | + @Override | ||
| 68 | + public LeaderElectorBuilder leaderElectorBuilder() { | ||
| 69 | + return null; | ||
| 70 | + } | ||
| 65 | } | 71 | } | ... | ... |
| 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 java.util.Map; | ||
| 19 | +import java.util.concurrent.CompletableFuture; | ||
| 20 | +import java.util.concurrent.ExecutionException; | ||
| 21 | +import java.util.concurrent.TimeUnit; | ||
| 22 | +import java.util.concurrent.TimeoutException; | ||
| 23 | +import java.util.function.Consumer; | ||
| 24 | + | ||
| 25 | +import org.onosproject.cluster.Leadership; | ||
| 26 | +import org.onosproject.cluster.NodeId; | ||
| 27 | +import org.onosproject.event.Change; | ||
| 28 | +import org.onosproject.store.service.AsyncLeaderElector; | ||
| 29 | +import org.onosproject.store.service.LeaderElector; | ||
| 30 | +import org.onosproject.store.service.StorageException; | ||
| 31 | +import org.onosproject.store.service.Synchronous; | ||
| 32 | + | ||
| 33 | +/** | ||
| 34 | + * Default implementation for a {@code LeaderElector} backed by a {@link AsyncLeaderElector}. | ||
| 35 | + */ | ||
| 36 | +public class DefaultLeaderElector extends Synchronous<AsyncLeaderElector> implements LeaderElector { | ||
| 37 | + | ||
| 38 | + private final AsyncLeaderElector asyncElector; | ||
| 39 | + private final long operationTimeoutMillis; | ||
| 40 | + | ||
| 41 | + public DefaultLeaderElector(AsyncLeaderElector asyncElector, long operationTimeoutMillis) { | ||
| 42 | + super(asyncElector); | ||
| 43 | + this.asyncElector = asyncElector; | ||
| 44 | + this.operationTimeoutMillis = operationTimeoutMillis; | ||
| 45 | + } | ||
| 46 | + | ||
| 47 | + @Override | ||
| 48 | + public Leadership run(String topic, NodeId nodeId) { | ||
| 49 | + return complete(asyncElector.run(topic, nodeId)); | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + @Override | ||
| 53 | + public void withdraw(String topic) { | ||
| 54 | + complete(asyncElector.withdraw(topic)); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + @Override | ||
| 58 | + public boolean anoint(String topic, NodeId nodeId) { | ||
| 59 | + return complete(asyncElector.anoint(topic, nodeId)); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + @Override | ||
| 63 | + public Leadership getLeadership(String topic) { | ||
| 64 | + return complete(asyncElector.getLeadership(topic)); | ||
| 65 | + } | ||
| 66 | + | ||
| 67 | + @Override | ||
| 68 | + public Map<String, Leadership> getLeaderships() { | ||
| 69 | + return complete(asyncElector.getLeaderships()); | ||
| 70 | + } | ||
| 71 | + | ||
| 72 | + @Override | ||
| 73 | + public void addChangeListener(Consumer<Change<Leadership>> consumer) { | ||
| 74 | + complete(asyncElector.addChangeListener(consumer)); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + @Override | ||
| 78 | + public void removeChangeListener(Consumer<Change<Leadership>> consumer) { | ||
| 79 | + complete(asyncElector.removeChangeListener(consumer)); | ||
| 80 | + } | ||
| 81 | + | ||
| 82 | + private <T> T complete(CompletableFuture<T> future) { | ||
| 83 | + try { | ||
| 84 | + return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); | ||
| 85 | + } catch (InterruptedException e) { | ||
| 86 | + Thread.currentThread().interrupt(); | ||
| 87 | + throw new StorageException.Interrupted(); | ||
| 88 | + } catch (TimeoutException e) { | ||
| 89 | + throw new StorageException.Timeout(); | ||
| 90 | + } catch (ExecutionException e) { | ||
| 91 | + throw new StorageException(e.getCause()); | ||
| 92 | + } | ||
| 93 | + } | ||
| 94 | +} |
| ... | @@ -25,7 +25,7 @@ import org.onosproject.store.primitives.DefaultAtomicCounter; | ... | @@ -25,7 +25,7 @@ import org.onosproject.store.primitives.DefaultAtomicCounter; |
| 25 | public interface AsyncAtomicCounter extends DistributedPrimitive { | 25 | public interface AsyncAtomicCounter extends DistributedPrimitive { |
| 26 | 26 | ||
| 27 | @Override | 27 | @Override |
| 28 | - default DistributedPrimitive.Type type() { | 28 | + default DistributedPrimitive.Type primitiveType() { |
| 29 | return DistributedPrimitive.Type.COUNTER; | 29 | return DistributedPrimitive.Type.COUNTER; |
| 30 | } | 30 | } |
| 31 | 31 | ... | ... |
| ... | @@ -32,7 +32,7 @@ import org.onosproject.store.primitives.DefaultAtomicValue; | ... | @@ -32,7 +32,7 @@ import org.onosproject.store.primitives.DefaultAtomicValue; |
| 32 | public interface AsyncAtomicValue<V> extends DistributedPrimitive { | 32 | public interface AsyncAtomicValue<V> extends DistributedPrimitive { |
| 33 | 33 | ||
| 34 | @Override | 34 | @Override |
| 35 | - default DistributedPrimitive.Type type() { | 35 | + default DistributedPrimitive.Type primitiveType() { |
| 36 | return DistributedPrimitive.Type.VALUE; | 36 | return DistributedPrimitive.Type.VALUE; |
| 37 | } | 37 | } |
| 38 | 38 | ... | ... |
| ... | @@ -55,7 +55,7 @@ import org.onosproject.store.primitives.TransactionId; | ... | @@ -55,7 +55,7 @@ import org.onosproject.store.primitives.TransactionId; |
| 55 | public interface AsyncConsistentMap<K, V> extends DistributedPrimitive { | 55 | public interface AsyncConsistentMap<K, V> extends DistributedPrimitive { |
| 56 | 56 | ||
| 57 | @Override | 57 | @Override |
| 58 | - default DistributedPrimitive.Type type() { | 58 | + default DistributedPrimitive.Type primitiveType() { |
| 59 | return DistributedPrimitive.Type.CONSISTENT_MAP; | 59 | return DistributedPrimitive.Type.CONSISTENT_MAP; |
| 60 | } | 60 | } |
| 61 | 61 | ... | ... |
| ... | @@ -33,7 +33,7 @@ import org.onosproject.store.primitives.DefaultDistributedSet; | ... | @@ -33,7 +33,7 @@ import org.onosproject.store.primitives.DefaultDistributedSet; |
| 33 | public interface AsyncDistributedSet<E> extends DistributedPrimitive { | 33 | public interface AsyncDistributedSet<E> extends DistributedPrimitive { |
| 34 | 34 | ||
| 35 | @Override | 35 | @Override |
| 36 | - default DistributedPrimitive.Type type() { | 36 | + default DistributedPrimitive.Type primitiveType() { |
| 37 | return DistributedPrimitive.Type.SET; | 37 | return DistributedPrimitive.Type.SET; |
| 38 | } | 38 | } |
| 39 | 39 | ... | ... |
| ... | @@ -22,6 +22,7 @@ import java.util.function.Consumer; | ... | @@ -22,6 +22,7 @@ import java.util.function.Consumer; |
| 22 | import org.onosproject.cluster.Leadership; | 22 | import org.onosproject.cluster.Leadership; |
| 23 | import org.onosproject.cluster.NodeId; | 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.event.Change; | 24 | import org.onosproject.event.Change; |
| 25 | +import org.onosproject.store.primitives.DefaultLeaderElector; | ||
| 25 | 26 | ||
| 26 | /** | 27 | /** |
| 27 | * Distributed mutual exclusion primitive. | 28 | * Distributed mutual exclusion primitive. |
| ... | @@ -45,7 +46,7 @@ import org.onosproject.event.Change; | ... | @@ -45,7 +46,7 @@ import org.onosproject.event.Change; |
| 45 | public interface AsyncLeaderElector extends DistributedPrimitive { | 46 | public interface AsyncLeaderElector extends DistributedPrimitive { |
| 46 | 47 | ||
| 47 | @Override | 48 | @Override |
| 48 | - default DistributedPrimitive.Type type() { | 49 | + default DistributedPrimitive.Type primitiveType() { |
| 49 | return DistributedPrimitive.Type.LEADER_ELECTOR; | 50 | return DistributedPrimitive.Type.LEADER_ELECTOR; |
| 50 | } | 51 | } |
| 51 | 52 | ||
| ... | @@ -102,4 +103,23 @@ public interface AsyncLeaderElector extends DistributedPrimitive { | ... | @@ -102,4 +103,23 @@ public interface AsyncLeaderElector extends DistributedPrimitive { |
| 102 | * @return CompletableFuture that is completed when the operation completes | 103 | * @return CompletableFuture that is completed when the operation completes |
| 103 | */ | 104 | */ |
| 104 | CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer); | 105 | CompletableFuture<Void> removeChangeListener(Consumer<Change<Leadership>> consumer); |
| 106 | + | ||
| 107 | + /** | ||
| 108 | + * Returns a new {@link LeaderElector} that is backed by this instance. | ||
| 109 | + * | ||
| 110 | + * @param timeoutMillis timeout duration for the returned LeaderElector operations | ||
| 111 | + * @return new {@code LeaderElector} instance | ||
| 112 | + */ | ||
| 113 | + default LeaderElector asLeaderElector(long timeoutMillis) { | ||
| 114 | + return new DefaultLeaderElector(this, timeoutMillis); | ||
| 115 | + } | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * Returns a new {@link LeaderElector} that is backed by this instance and with a default operation timeout. | ||
| 119 | + * | ||
| 120 | + * @return new {@code LeaderElector} instance | ||
| 121 | + */ | ||
| 122 | + default LeaderElector asLeaderElector() { | ||
| 123 | + return asLeaderElector(DEFAULT_OPERTATION_TIMEOUT_MILLIS); | ||
| 124 | + } | ||
| 105 | } | 125 | } | ... | ... |
| ... | @@ -21,7 +21,7 @@ package org.onosproject.store.service; | ... | @@ -21,7 +21,7 @@ package org.onosproject.store.service; |
| 21 | public interface AtomicCounter extends DistributedPrimitive { | 21 | public interface AtomicCounter extends DistributedPrimitive { |
| 22 | 22 | ||
| 23 | @Override | 23 | @Override |
| 24 | - default DistributedPrimitive.Type type() { | 24 | + default DistributedPrimitive.Type primitiveType() { |
| 25 | return DistributedPrimitive.Type.COUNTER; | 25 | return DistributedPrimitive.Type.COUNTER; |
| 26 | } | 26 | } |
| 27 | 27 | ... | ... |
| ... | @@ -81,7 +81,7 @@ public interface DistributedPrimitive { | ... | @@ -81,7 +81,7 @@ public interface DistributedPrimitive { |
| 81 | * Returns the type of primitive. | 81 | * Returns the type of primitive. |
| 82 | * @return primitive type | 82 | * @return primitive type |
| 83 | */ | 83 | */ |
| 84 | - Type type(); | 84 | + Type primitiveType(); |
| 85 | 85 | ||
| 86 | /** | 86 | /** |
| 87 | * Returns the application owning this primitive. | 87 | * Returns the application owning this primitive. | ... | ... |
| ... | @@ -42,7 +42,7 @@ import java.util.function.BiFunction; | ... | @@ -42,7 +42,7 @@ import java.util.function.BiFunction; |
| 42 | public interface EventuallyConsistentMap<K, V> extends DistributedPrimitive { | 42 | public interface EventuallyConsistentMap<K, V> extends DistributedPrimitive { |
| 43 | 43 | ||
| 44 | @Override | 44 | @Override |
| 45 | - default DistributedPrimitive.Type type() { | 45 | + default DistributedPrimitive.Type primitiveType() { |
| 46 | return DistributedPrimitive.Type.EVENTUALLY_CONSISTENT_MAP; | 46 | return DistributedPrimitive.Type.EVENTUALLY_CONSISTENT_MAP; |
| 47 | } | 47 | } |
| 48 | 48 | ... | ... |
| ... | @@ -29,7 +29,7 @@ import org.onosproject.event.Change; | ... | @@ -29,7 +29,7 @@ import org.onosproject.event.Change; |
| 29 | public interface LeaderElector extends DistributedPrimitive { | 29 | public interface LeaderElector extends DistributedPrimitive { |
| 30 | 30 | ||
| 31 | @Override | 31 | @Override |
| 32 | - default DistributedPrimitive.Type type() { | 32 | + default DistributedPrimitive.Type primitiveType() { |
| 33 | return DistributedPrimitive.Type.LEADER_ELECTOR; | 33 | return DistributedPrimitive.Type.LEADER_ELECTOR; |
| 34 | } | 34 | } |
| 35 | 35 | ... | ... |
| 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.service; | ||
| 17 | + | ||
| 18 | +import org.onosproject.store.primitives.DistributedPrimitiveBuilder; | ||
| 19 | + | ||
| 20 | +/** | ||
| 21 | + * Builder for constructing new {@link AsyncLeaderElector} instances. | ||
| 22 | + */ | ||
| 23 | +public abstract class LeaderElectorBuilder | ||
| 24 | + extends DistributedPrimitiveBuilder<LeaderElectorBuilder, AsyncLeaderElector> { | ||
| 25 | + public LeaderElectorBuilder() { | ||
| 26 | + super(DistributedPrimitive.Type.LEADER_ELECTOR); | ||
| 27 | + } | ||
| 28 | +} |
| ... | @@ -21,6 +21,7 @@ import java.util.function.Function; | ... | @@ -21,6 +21,7 @@ import java.util.function.Function; |
| 21 | import org.onosproject.store.primitives.MapUpdate; | 21 | import org.onosproject.store.primitives.MapUpdate; |
| 22 | import org.onosproject.store.primitives.TransactionId; | 22 | import org.onosproject.store.primitives.TransactionId; |
| 23 | 23 | ||
| 24 | +import com.google.common.base.MoreObjects; | ||
| 24 | import com.google.common.collect.ImmutableList; | 25 | import com.google.common.collect.ImmutableList; |
| 25 | import com.google.common.collect.Lists; | 26 | import com.google.common.collect.Lists; |
| 26 | 27 | ||
| ... | @@ -58,6 +59,14 @@ public class MapTransaction<K, V> { | ... | @@ -58,6 +59,14 @@ public class MapTransaction<K, V> { |
| 58 | return updates; | 59 | return updates; |
| 59 | } | 60 | } |
| 60 | 61 | ||
| 62 | + @Override | ||
| 63 | + public String toString() { | ||
| 64 | + return MoreObjects.toStringHelper(getClass()) | ||
| 65 | + .add("transactionId", transactionId) | ||
| 66 | + .add("updates", updates) | ||
| 67 | + .toString(); | ||
| 68 | + } | ||
| 69 | + | ||
| 61 | /** | 70 | /** |
| 62 | * Maps this instance to another {@code MapTransaction} with different key and value types. | 71 | * Maps this instance to another {@code MapTransaction} with different key and value types. |
| 63 | * | 72 | * | ... | ... |
| ... | @@ -75,6 +75,13 @@ public interface StorageService { | ... | @@ -75,6 +75,13 @@ public interface StorageService { |
| 75 | <V> AtomicValueBuilder<V> atomicValueBuilder(); | 75 | <V> AtomicValueBuilder<V> atomicValueBuilder(); |
| 76 | 76 | ||
| 77 | /** | 77 | /** |
| 78 | + * Creates a new LeaderElectorBuilder. | ||
| 79 | + * | ||
| 80 | + * @return leader elector builder | ||
| 81 | + */ | ||
| 82 | + LeaderElectorBuilder leaderElectorBuilder(); | ||
| 83 | + | ||
| 84 | + /** | ||
| 78 | * Creates a new transaction context builder. | 85 | * Creates a new transaction context builder. |
| 79 | * | 86 | * |
| 80 | * @return a builder for a transaction context. | 87 | * @return a builder for a transaction context. | ... | ... |
| ... | @@ -37,8 +37,8 @@ public abstract class Synchronous<T extends DistributedPrimitive> implements Dis | ... | @@ -37,8 +37,8 @@ public abstract class Synchronous<T extends DistributedPrimitive> implements Dis |
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | @Override | 39 | @Override |
| 40 | - public Type type() { | 40 | + public Type primitiveType() { |
| 41 | - return primitive.type(); | 41 | + return primitive.primitiveType(); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | @Override | 44 | @Override | ... | ... |
| ... | @@ -36,7 +36,7 @@ import org.onosproject.store.primitives.TransactionId; | ... | @@ -36,7 +36,7 @@ import org.onosproject.store.primitives.TransactionId; |
| 36 | public interface TransactionContext extends DistributedPrimitive { | 36 | public interface TransactionContext extends DistributedPrimitive { |
| 37 | 37 | ||
| 38 | @Override | 38 | @Override |
| 39 | - default DistributedPrimitive.Type type() { | 39 | + default DistributedPrimitive.Type primitiveType() { |
| 40 | return DistributedPrimitive.Type.TRANSACTION_CONTEXT; | 40 | return DistributedPrimitive.Type.TRANSACTION_CONTEXT; |
| 41 | } | 41 | } |
| 42 | 42 | ... | ... |
| ... | @@ -19,6 +19,7 @@ package org.onosproject.store.service; | ... | @@ -19,6 +19,7 @@ package org.onosproject.store.service; |
| 19 | import java.util.function.Function; | 19 | import java.util.function.Function; |
| 20 | 20 | ||
| 21 | import org.joda.time.DateTime; | 21 | import org.joda.time.DateTime; |
| 22 | +import org.onlab.util.ByteArraySizeHashPrinter; | ||
| 22 | 23 | ||
| 23 | import com.google.common.base.MoreObjects; | 24 | import com.google.common.base.MoreObjects; |
| 24 | import com.google.common.base.Objects; | 25 | import com.google.common.base.Objects; |
| ... | @@ -140,7 +141,7 @@ public class Versioned<V> { | ... | @@ -140,7 +141,7 @@ public class Versioned<V> { |
| 140 | @Override | 141 | @Override |
| 141 | public String toString() { | 142 | public String toString() { |
| 142 | return MoreObjects.toStringHelper(this) | 143 | return MoreObjects.toStringHelper(this) |
| 143 | - .add("value", value) | 144 | + .add("value", value instanceof byte[] ? new ByteArraySizeHashPrinter((byte[]) value) : value) |
| 144 | .add("version", version) | 145 | .add("version", version) |
| 145 | .add("creationTime", new DateTime(creationTime)) | 146 | .add("creationTime", new DateTime(creationTime)) |
| 146 | .toString(); | 147 | .toString(); | ... | ... |
| ... | @@ -33,7 +33,7 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> { | ... | @@ -33,7 +33,7 @@ public class ConsistentMapAdapter<K, V> implements ConsistentMap<K, V> { |
| 33 | } | 33 | } |
| 34 | 34 | ||
| 35 | @Override | 35 | @Override |
| 36 | - public DistributedPrimitive.Type type() { | 36 | + public DistributedPrimitive.Type primitiveType() { |
| 37 | return DistributedPrimitive.Type.CONSISTENT_MAP; | 37 | return DistributedPrimitive.Type.CONSISTENT_MAP; |
| 38 | } | 38 | } |
| 39 | 39 | ... | ... |
| ... | @@ -34,7 +34,7 @@ public class EventuallyConsistentMapAdapter<K, V> implements EventuallyConsisten | ... | @@ -34,7 +34,7 @@ public class EventuallyConsistentMapAdapter<K, V> implements EventuallyConsisten |
| 34 | } | 34 | } |
| 35 | 35 | ||
| 36 | @Override | 36 | @Override |
| 37 | - public Type type() { | 37 | + public Type primitiveType() { |
| 38 | return Type.EVENTUALLY_CONSISTENT_MAP; | 38 | return Type.EVENTUALLY_CONSISTENT_MAP; |
| 39 | } | 39 | } |
| 40 | 40 | ... | ... |
| ... | @@ -53,4 +53,9 @@ public class StorageServiceAdapter implements StorageService { | ... | @@ -53,4 +53,9 @@ public class StorageServiceAdapter implements StorageService { |
| 53 | public TransactionContextBuilder transactionContextBuilder() { | 53 | public TransactionContextBuilder transactionContextBuilder() { |
| 54 | return null; | 54 | return null; |
| 55 | } | 55 | } |
| 56 | + | ||
| 57 | + @Override | ||
| 58 | + public LeaderElectorBuilder leaderElectorBuilder() { | ||
| 59 | + return null; | ||
| 60 | + } | ||
| 56 | } | 61 | } | ... | ... |
| ... | @@ -22,6 +22,7 @@ import java.io.DataInputStream; | ... | @@ -22,6 +22,7 @@ import java.io.DataInputStream; |
| 22 | import java.io.IOException; | 22 | import java.io.IOException; |
| 23 | import java.net.InetAddress; | 23 | import java.net.InetAddress; |
| 24 | import java.net.InetSocketAddress; | 24 | import java.net.InetSocketAddress; |
| 25 | +import java.net.UnknownHostException; | ||
| 25 | import java.util.Map; | 26 | import java.util.Map; |
| 26 | import java.util.concurrent.CompletableFuture; | 27 | import java.util.concurrent.CompletableFuture; |
| 27 | import java.util.concurrent.atomic.AtomicBoolean; | 28 | import java.util.concurrent.atomic.AtomicBoolean; |
| ... | @@ -32,6 +33,7 @@ import org.onlab.util.Tools; | ... | @@ -32,6 +33,7 @@ import org.onlab.util.Tools; |
| 32 | import org.onosproject.cluster.PartitionId; | 33 | import org.onosproject.cluster.PartitionId; |
| 33 | import org.onosproject.store.cluster.messaging.MessagingService; | 34 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 34 | 35 | ||
| 36 | +import com.google.common.base.Throwables; | ||
| 35 | import com.google.common.collect.Maps; | 37 | import com.google.common.collect.Maps; |
| 36 | 38 | ||
| 37 | import io.atomix.catalyst.transport.Address; | 39 | import io.atomix.catalyst.transport.Address; |
| ... | @@ -71,18 +73,23 @@ public class CopycatTransportServer implements Server { | ... | @@ -71,18 +73,23 @@ public class CopycatTransportServer implements Server { |
| 71 | messagingService.registerHandler(messageSubject, (sender, payload) -> { | 73 | messagingService.registerHandler(messageSubject, (sender, payload) -> { |
| 72 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { | 74 | try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(payload))) { |
| 73 | long connectionId = input.readLong(); | 75 | long connectionId = input.readLong(); |
| 74 | - InetAddress senderHost = InetAddress.getByAddress(sender.host().toOctets()); | ||
| 75 | - int senderPort = sender.port(); | ||
| 76 | - Address senderAddress = new Address(new InetSocketAddress(senderHost, senderPort)); | ||
| 77 | AtomicBoolean newConnection = new AtomicBoolean(false); | 76 | AtomicBoolean newConnection = new AtomicBoolean(false); |
| 78 | CopycatTransportConnection connection = connections.computeIfAbsent(connectionId, k -> { | 77 | CopycatTransportConnection connection = connections.computeIfAbsent(connectionId, k -> { |
| 79 | newConnection.set(true); | 78 | newConnection.set(true); |
| 80 | - return new CopycatTransportConnection(connectionId, | 79 | + try { |
| 81 | - CopycatTransport.Mode.SERVER, | 80 | + InetAddress senderHost = InetAddress.getByAddress(sender.host().toOctets()); |
| 82 | - partitionId, | 81 | + int senderPort = sender.port(); |
| 83 | - senderAddress, | 82 | + Address senderAddress = new Address(new InetSocketAddress(senderHost, senderPort)); |
| 84 | - messagingService, | 83 | + return new CopycatTransportConnection(connectionId, |
| 85 | - getOrCreateContext(context)); | 84 | + CopycatTransport.Mode.SERVER, |
| 85 | + partitionId, | ||
| 86 | + senderAddress, | ||
| 87 | + messagingService, | ||
| 88 | + getOrCreateContext(context)); | ||
| 89 | + } catch (UnknownHostException e) { | ||
| 90 | + Throwables.propagate(e); | ||
| 91 | + return null; | ||
| 92 | + } | ||
| 86 | }); | 93 | }); |
| 87 | byte[] request = IOUtils.toByteArray(input); | 94 | byte[] request = IOUtils.toByteArray(input); |
| 88 | return CompletableFuture.supplyAsync( | 95 | return CompletableFuture.supplyAsync( | ... | ... |
| ... | @@ -76,6 +76,7 @@ import org.onosproject.store.service.ConsistentMapException; | ... | @@ -76,6 +76,7 @@ import org.onosproject.store.service.ConsistentMapException; |
| 76 | import org.onosproject.store.service.DistributedQueueBuilder; | 76 | import org.onosproject.store.service.DistributedQueueBuilder; |
| 77 | import org.onosproject.store.service.DistributedSetBuilder; | 77 | import org.onosproject.store.service.DistributedSetBuilder; |
| 78 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; | 78 | import org.onosproject.store.service.EventuallyConsistentMapBuilder; |
| 79 | +import org.onosproject.store.service.LeaderElectorBuilder; | ||
| 79 | import org.onosproject.store.service.MapInfo; | 80 | import org.onosproject.store.service.MapInfo; |
| 80 | import org.onosproject.store.service.PartitionInfo; | 81 | import org.onosproject.store.service.PartitionInfo; |
| 81 | import org.onosproject.store.service.Serializer; | 82 | import org.onosproject.store.service.Serializer; |
| ... | @@ -360,6 +361,11 @@ public class DatabaseManager implements StorageService, StorageAdminService { | ... | @@ -360,6 +361,11 @@ public class DatabaseManager implements StorageService, StorageAdminService { |
| 360 | } | 361 | } |
| 361 | 362 | ||
| 362 | @Override | 363 | @Override |
| 364 | + public LeaderElectorBuilder leaderElectorBuilder() { | ||
| 365 | + throw new UnsupportedOperationException(); | ||
| 366 | + } | ||
| 367 | + | ||
| 368 | + @Override | ||
| 363 | public List<MapInfo> getMapInfo() { | 369 | public List<MapInfo> getMapInfo() { |
| 364 | List<MapInfo> maps = Lists.newArrayList(); | 370 | List<MapInfo> maps = Lists.newArrayList(); |
| 365 | maps.addAll(getMapInfo(inMemoryDatabase)); | 371 | maps.addAll(getMapInfo(inMemoryDatabase)); | ... | ... |
| ... | @@ -54,29 +54,29 @@ public class DefaultCatalystTypeSerializerFactory implements TypeSerializerFacto | ... | @@ -54,29 +54,29 @@ public class DefaultCatalystTypeSerializerFactory implements TypeSerializerFacto |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | @Override | 56 | @Override |
| 57 | - public T read(Class<T> clazz, BufferInput<?> input, | 57 | + public void write(T object, BufferOutput<?> buffer, |
| 58 | - io.atomix.catalyst.serializer.Serializer serializer) { | 58 | + io.atomix.catalyst.serializer.Serializer serializer) { |
| 59 | - int size = input.readInt(); | ||
| 60 | - byte[] payload = new byte[size]; | ||
| 61 | - input.read(payload); | ||
| 62 | try { | 59 | try { |
| 63 | - return this.serializer.decode(payload); | 60 | + byte[] payload = this.serializer.encode(object); |
| 61 | + buffer.writeInt(payload.length); | ||
| 62 | + buffer.write(payload); | ||
| 64 | } catch (Exception e) { | 63 | } catch (Exception e) { |
| 65 | - log.warn("Failed to deserialize as type {}", clazz, e); | 64 | + log.warn("Failed to serialize {}", object, e); |
| 66 | - Throwables.propagate(e); | ||
| 67 | - return null; | ||
| 68 | } | 65 | } |
| 69 | } | 66 | } |
| 70 | 67 | ||
| 71 | @Override | 68 | @Override |
| 72 | - public void write(T object, BufferOutput<?> output, | 69 | + public T read(Class<T> type, BufferInput<?> buffer, |
| 73 | - io.atomix.catalyst.serializer.Serializer serializer) { | 70 | + io.atomix.catalyst.serializer.Serializer serializer) { |
| 71 | + int size = buffer.readInt(); | ||
| 74 | try { | 72 | try { |
| 75 | - byte[] payload = this.serializer.encode(object); | 73 | + byte[] payload = new byte[size]; |
| 76 | - output.writeInt(payload.length); | 74 | + buffer.read(payload); |
| 77 | - output.write(payload); | 75 | + return this.serializer.decode(payload); |
| 78 | } catch (Exception e) { | 76 | } catch (Exception e) { |
| 79 | - log.warn("Failed to serialize {}", object, e); | 77 | + log.warn("Failed to deserialize as type {}. Payload size: {}", type, size, e); |
| 78 | + Throwables.propagate(e); | ||
| 79 | + return null; | ||
| 80 | } | 80 | } |
| 81 | } | 81 | } |
| 82 | } | 82 | } | ... | ... |
| ... | @@ -117,7 +117,7 @@ public class DefaultDistributedQueue<E> implements DistributedQueue<E> { | ... | @@ -117,7 +117,7 @@ public class DefaultDistributedQueue<E> implements DistributedQueue<E> { |
| 117 | } | 117 | } |
| 118 | 118 | ||
| 119 | @Override | 119 | @Override |
| 120 | - public DistributedPrimitive.Type type() { | 120 | + public DistributedPrimitive.Type primitiveType() { |
| 121 | return DistributedPrimitive.Type.QUEUE; | 121 | return DistributedPrimitive.Type.QUEUE; |
| 122 | } | 122 | } |
| 123 | 123 | ... | ... |
| 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.impl; | ||
| 17 | + | ||
| 18 | +import org.onosproject.store.primitives.DistributedPrimitiveCreator; | ||
| 19 | +import org.onosproject.store.service.AsyncLeaderElector; | ||
| 20 | +import org.onosproject.store.service.LeaderElectorBuilder; | ||
| 21 | + | ||
| 22 | +/** | ||
| 23 | + * Default implementation of {@code LeaderElectorBuilder}. | ||
| 24 | + */ | ||
| 25 | +public class DefaultLeaderElectorBuilder extends LeaderElectorBuilder { | ||
| 26 | + | ||
| 27 | + private final DistributedPrimitiveCreator base; | ||
| 28 | + private final DistributedPrimitiveCreator federated; | ||
| 29 | + | ||
| 30 | + public DefaultLeaderElectorBuilder(DistributedPrimitiveCreator base, DistributedPrimitiveCreator federated) { | ||
| 31 | + this.base = base; | ||
| 32 | + this.federated = federated; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + @Override | ||
| 36 | + public AsyncLeaderElector build() { | ||
| 37 | + DistributedPrimitiveCreator creator = partitionsDisabled() ? base : federated; | ||
| 38 | + return creator.newAsyncLeaderElector(name()); | ||
| 39 | + } | ||
| 40 | +} |
| ... | @@ -33,6 +33,7 @@ import org.onosproject.cluster.NodeId; | ... | @@ -33,6 +33,7 @@ import org.onosproject.cluster.NodeId; |
| 33 | import org.onosproject.cluster.Partition; | 33 | import org.onosproject.cluster.Partition; |
| 34 | import org.onosproject.store.cluster.messaging.MessagingService; | 34 | import org.onosproject.store.cluster.messaging.MessagingService; |
| 35 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; | 35 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
| 36 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector; | ||
| 36 | import org.onosproject.store.service.PartitionInfo; | 37 | import org.onosproject.store.service.PartitionInfo; |
| 37 | 38 | ||
| 38 | import com.google.common.collect.Collections2; | 39 | import com.google.common.collect.Collections2; |
| ... | @@ -51,6 +52,7 @@ public class StoragePartition extends DefaultPartition implements Managed<Storag | ... | @@ -51,6 +52,7 @@ public class StoragePartition extends DefaultPartition implements Managed<Storag |
| 51 | private final File logFolder; | 52 | private final File logFolder; |
| 52 | private static final Collection<ResourceType> RESOURCE_TYPES = ImmutableSet.of( | 53 | private static final Collection<ResourceType> RESOURCE_TYPES = ImmutableSet.of( |
| 53 | new ResourceType(DistributedLong.class), | 54 | new ResourceType(DistributedLong.class), |
| 55 | + new ResourceType(AtomixLeaderElector.class), | ||
| 54 | new ResourceType(AtomixConsistentMap.class)); | 56 | new ResourceType(AtomixConsistentMap.class)); |
| 55 | 57 | ||
| 56 | private NodeId localNodeId; | 58 | private NodeId localNodeId; | ... | ... |
| ... | @@ -29,6 +29,7 @@ import org.onlab.util.HexString; | ... | @@ -29,6 +29,7 @@ import org.onlab.util.HexString; |
| 29 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; | 29 | import org.onosproject.store.primitives.DistributedPrimitiveCreator; |
| 30 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; | 30 | import org.onosproject.store.primitives.resources.impl.AtomixConsistentMap; |
| 31 | import org.onosproject.store.primitives.resources.impl.AtomixCounter; | 31 | import org.onosproject.store.primitives.resources.impl.AtomixCounter; |
| 32 | +import org.onosproject.store.primitives.resources.impl.AtomixLeaderElector; | ||
| 32 | import org.onosproject.store.serializers.KryoNamespaces; | 33 | import org.onosproject.store.serializers.KryoNamespaces; |
| 33 | import org.onosproject.store.service.AsyncAtomicCounter; | 34 | import org.onosproject.store.service.AsyncAtomicCounter; |
| 34 | import org.onosproject.store.service.AsyncAtomicValue; | 35 | import org.onosproject.store.service.AsyncAtomicValue; |
| ... | @@ -133,7 +134,7 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana | ... | @@ -133,7 +134,7 @@ public class StoragePartitionClient implements DistributedPrimitiveCreator, Mana |
| 133 | 134 | ||
| 134 | @Override | 135 | @Override |
| 135 | public AsyncLeaderElector newAsyncLeaderElector(String name) { | 136 | public AsyncLeaderElector newAsyncLeaderElector(String name) { |
| 136 | - throw new UnsupportedOperationException(); | 137 | + return client.get(name, AtomixLeaderElector.class).join(); |
| 137 | } | 138 | } |
| 138 | 139 | ||
| 139 | @Override | 140 | @Override | ... | ... |
| ... | @@ -40,6 +40,7 @@ import org.onlab.util.Match; | ... | @@ -40,6 +40,7 @@ import org.onlab.util.Match; |
| 40 | import org.onosproject.app.ApplicationState; | 40 | import org.onosproject.app.ApplicationState; |
| 41 | import org.onosproject.cluster.ControllerNode; | 41 | import org.onosproject.cluster.ControllerNode; |
| 42 | import org.onosproject.cluster.DefaultControllerNode; | 42 | import org.onosproject.cluster.DefaultControllerNode; |
| 43 | +import org.onosproject.cluster.Leader; | ||
| 43 | import org.onosproject.cluster.Leadership; | 44 | import org.onosproject.cluster.Leadership; |
| 44 | import org.onosproject.cluster.LeadershipEvent; | 45 | import org.onosproject.cluster.LeadershipEvent; |
| 45 | import org.onosproject.cluster.NodeId; | 46 | import org.onosproject.cluster.NodeId; |
| ... | @@ -49,6 +50,7 @@ import org.onosproject.core.DefaultApplication; | ... | @@ -49,6 +50,7 @@ import org.onosproject.core.DefaultApplication; |
| 49 | import org.onosproject.core.DefaultApplicationId; | 50 | import org.onosproject.core.DefaultApplicationId; |
| 50 | import org.onosproject.core.DefaultGroupId; | 51 | import org.onosproject.core.DefaultGroupId; |
| 51 | import org.onosproject.core.Version; | 52 | import org.onosproject.core.Version; |
| 53 | +import org.onosproject.event.Change; | ||
| 52 | import org.onosproject.incubator.net.domain.IntentDomainId; | 54 | import org.onosproject.incubator.net.domain.IntentDomainId; |
| 53 | import org.onosproject.mastership.MastershipTerm; | 55 | import org.onosproject.mastership.MastershipTerm; |
| 54 | import org.onosproject.net.Annotations; | 56 | import org.onosproject.net.Annotations; |
| ... | @@ -204,8 +206,10 @@ import org.onosproject.net.resource.link.MplsLabelResourceAllocation; | ... | @@ -204,8 +206,10 @@ import org.onosproject.net.resource.link.MplsLabelResourceAllocation; |
| 204 | import org.onosproject.net.resource.link.MplsLabelResourceRequest; | 206 | import org.onosproject.net.resource.link.MplsLabelResourceRequest; |
| 205 | import org.onosproject.security.Permission; | 207 | import org.onosproject.security.Permission; |
| 206 | import org.onosproject.store.Timestamp; | 208 | import org.onosproject.store.Timestamp; |
| 209 | +import org.onosproject.store.primitives.MapUpdate; | ||
| 207 | import org.onosproject.store.primitives.TransactionId; | 210 | import org.onosproject.store.primitives.TransactionId; |
| 208 | import org.onosproject.store.service.MapEvent; | 211 | import org.onosproject.store.service.MapEvent; |
| 212 | +import org.onosproject.store.service.MapTransaction; | ||
| 209 | import org.onosproject.store.service.SetEvent; | 213 | import org.onosproject.store.service.SetEvent; |
| 210 | import org.onosproject.store.service.Versioned; | 214 | import org.onosproject.store.service.Versioned; |
| 211 | 215 | ||
| ... | @@ -327,6 +331,8 @@ public final class KryoNamespaces { | ... | @@ -327,6 +331,8 @@ public final class KryoNamespaces { |
| 327 | Link.Type.class, | 331 | Link.Type.class, |
| 328 | Link.State.class, | 332 | Link.State.class, |
| 329 | Timestamp.class, | 333 | Timestamp.class, |
| 334 | + Change.class, | ||
| 335 | + Leader.class, | ||
| 330 | Leadership.class, | 336 | Leadership.class, |
| 331 | LeadershipEvent.class, | 337 | LeadershipEvent.class, |
| 332 | LeadershipEvent.Type.class, | 338 | LeadershipEvent.Type.class, |
| ... | @@ -493,6 +499,9 @@ public final class KryoNamespaces { | ... | @@ -493,6 +499,9 @@ public final class KryoNamespaces { |
| 493 | .register(ExtensionSelectorType.class) | 499 | .register(ExtensionSelectorType.class) |
| 494 | .register(ExtensionTreatmentType.class) | 500 | .register(ExtensionTreatmentType.class) |
| 495 | .register(TransactionId.class) | 501 | .register(TransactionId.class) |
| 502 | + .register(MapTransaction.class) | ||
| 503 | + .register(MapUpdate.class) | ||
| 504 | + .register(MapUpdate.Type.class) | ||
| 496 | .register(Versioned.class) | 505 | .register(Versioned.class) |
| 497 | .register(MapEvent.class) | 506 | .register(MapEvent.class) |
| 498 | .register(MapEvent.Type.class) | 507 | .register(MapEvent.Type.class) | ... | ... |
-
Please register or login to post a comment