Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | */ |
Thomas Vachuska | b6d3167 | 2018-07-27 17:03:46 -0700 | [diff] [blame] | 16 | package org.onosproject.store.atomix.primitives.impl; |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 17 | |
Jordan Halterman | 204c7bf | 2018-08-21 13:56:10 -0700 | [diff] [blame] | 18 | import java.time.Duration; |
| 19 | |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 20 | import io.atomix.core.Atomix; |
| 21 | import io.atomix.primitive.Recovery; |
| 22 | import io.atomix.protocols.raft.MultiRaftProtocol; |
| 23 | import org.onosproject.cluster.NodeId; |
| 24 | import org.onosproject.store.serializers.KryoNamespaces; |
| 25 | import org.onosproject.store.service.AsyncLeaderElector; |
| 26 | import org.onosproject.store.service.LeaderElectorBuilder; |
| 27 | import org.onosproject.store.service.Serializer; |
| 28 | |
| 29 | /** |
| 30 | * Default {@link org.onosproject.store.service.AsyncLeaderElector} builder. |
| 31 | */ |
| 32 | public class AtomixLeaderElectorBuilder extends LeaderElectorBuilder { |
| 33 | private static final int MAX_RETRIES = 5; |
| 34 | private final Atomix atomix; |
| 35 | private final String group; |
| 36 | private final NodeId localNodeId; |
| 37 | |
| 38 | public AtomixLeaderElectorBuilder(Atomix atomix, String group, NodeId localNodeId) { |
| 39 | this.atomix = atomix; |
| 40 | this.group = group; |
| 41 | this.localNodeId = localNodeId; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public AsyncLeaderElector build() { |
| 46 | Serializer serializer = Serializer.using(KryoNamespaces.API); |
| 47 | return new AtomixLeaderElector(atomix.<NodeId>leaderElectorBuilder(name()) |
| 48 | .withProtocol(MultiRaftProtocol.builder(group) |
| 49 | .withRecoveryStrategy(Recovery.RECOVER) |
| 50 | .withMaxRetries(MAX_RETRIES) |
Jordan Halterman | 204c7bf | 2018-08-21 13:56:10 -0700 | [diff] [blame] | 51 | .withMaxTimeout(Duration.ofMillis(electionTimeoutMillis())) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 52 | .build()) |
| 53 | .withReadOnly(readOnly()) |
Jordan Halterman | fa71cc6 | 2018-08-08 12:04:56 -0700 | [diff] [blame] | 54 | .withCacheEnabled(relaxedReadConsistency()) |
Jordan Halterman | 00e92da | 2018-05-22 23:05:52 -0700 | [diff] [blame] | 55 | .withSerializer(new AtomixSerializerAdapter(serializer)) |
| 56 | .build() |
| 57 | .async(), localNodeId); |
| 58 | } |
| 59 | } |