2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.raft;
10 import org.opendaylight.controller.cluster.DataPersistenceProvider;
11 import org.opendaylight.controller.cluster.raft.base.messages.UpdateElectionTerm;
12 import org.slf4j.Logger;
15 * Implementation of ElectionTerm for the RaftActor.
17 class ElectionTermImpl implements ElectionTerm {
19 * Identifier of the actor whose election term information this is
21 private long currentTerm = 0;
22 private String votedFor = null;
24 private final DataPersistenceProvider persistence;
26 private final Logger log;
27 private final String logId;
29 ElectionTermImpl(DataPersistenceProvider persistence, String logId, Logger log) {
30 this.persistence = persistence;
36 public long getCurrentTerm() {
41 public String getVotedFor() {
45 @Override public void update(long currentTerm, String votedFor) {
46 if(log.isDebugEnabled()) {
47 log.debug("{}: Set currentTerm={}, votedFor={}", logId, currentTerm, votedFor);
49 this.currentTerm = currentTerm;
50 this.votedFor = votedFor;
54 public void updateAndPersist(long currentTerm, String votedFor){
55 update(currentTerm, votedFor);
56 // FIXME : Maybe first persist then update the state
57 persistence.persist(new UpdateElectionTerm(this.currentTerm, this.votedFor), NoopProcedure.instance());