blob: 8fd392773fb6697c3c6c9cc70cb096a1875bcefd [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
18import io.atomix.core.Atomix;
19import io.atomix.primitive.Recovery;
20import io.atomix.protocols.raft.MultiRaftProtocol;
21import org.onosproject.cluster.NodeId;
22import org.onosproject.store.serializers.KryoNamespaces;
23import org.onosproject.store.service.AsyncLeaderElector;
24import org.onosproject.store.service.LeaderElectorBuilder;
25import org.onosproject.store.service.Serializer;
26
27/**
28 * Default {@link org.onosproject.store.service.AsyncLeaderElector} builder.
29 */
30public class AtomixLeaderElectorBuilder extends LeaderElectorBuilder {
31 private static final int MAX_RETRIES = 5;
32 private final Atomix atomix;
33 private final String group;
34 private final NodeId localNodeId;
35
36 public AtomixLeaderElectorBuilder(Atomix atomix, String group, NodeId localNodeId) {
37 this.atomix = atomix;
38 this.group = group;
39 this.localNodeId = localNodeId;
40 }
41
42 @Override
43 public AsyncLeaderElector build() {
44 Serializer serializer = Serializer.using(KryoNamespaces.API);
45 return new AtomixLeaderElector(atomix.<NodeId>leaderElectorBuilder(name())
46 .withProtocol(MultiRaftProtocol.builder(group)
47 .withRecoveryStrategy(Recovery.RECOVER)
48 .withMaxRetries(MAX_RETRIES)
49 .build())
50 .withReadOnly(readOnly())
51 // TODO: Enable caching for LeaderElector in Atomix
52 .withSerializer(new AtomixSerializerAdapter(serializer))
53 .build()
54 .async(), localNodeId);
55 }
56}