blob: ef19b702c48e6b91bdde0663b67308edc8649be2 [file] [log] [blame]
Jordan Halterman00e92da2018-05-22 23:05:52 -07001/*
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 Vachuskab6d31672018-07-27 17:03:46 -070016package org.onosproject.store.atomix.primitives.impl;
Jordan Halterman00e92da2018-05-22 23:05:52 -070017
Jordan Halterman204c7bf2018-08-21 13:56:10 -070018import java.time.Duration;
19
Jordan Halterman00e92da2018-05-22 23:05:52 -070020import io.atomix.core.Atomix;
21import io.atomix.primitive.Recovery;
22import io.atomix.protocols.raft.MultiRaftProtocol;
23import org.onosproject.cluster.NodeId;
24import org.onosproject.store.serializers.KryoNamespaces;
25import org.onosproject.store.service.AsyncLeaderElector;
26import org.onosproject.store.service.LeaderElectorBuilder;
27import org.onosproject.store.service.Serializer;
28
29/**
30 * Default {@link org.onosproject.store.service.AsyncLeaderElector} builder.
31 */
32public 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 Halterman204c7bf2018-08-21 13:56:10 -070051 .withMaxTimeout(Duration.ofMillis(electionTimeoutMillis()))
Jordan Halterman00e92da2018-05-22 23:05:52 -070052 .build())
53 .withReadOnly(readOnly())
Jordan Haltermanfa71cc62018-08-08 12:04:56 -070054 .withCacheEnabled(relaxedReadConsistency())
Jordan Halterman00e92da2018-05-22 23:05:52 -070055 .withSerializer(new AtomixSerializerAdapter(serializer))
56 .build()
57 .async(), localNodeId);
58 }
59}