From 1a4eca04592ffb56bf81110e24e13635c5a84a08 Mon Sep 17 00:00:00 2001 From: Madan Jampani Date: Thu, 2 Apr 2015 15:29:26 -0700 Subject: [PATCH] Use a low raft leader election timeout when cluster size is one Change-Id: I0755411f0b20b8e4cd8f8f2fa58e173add4f32dc --- .../consistent/impl/DatabaseManager.java | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java index f0ff5c2995..010276aca4 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java +++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/DatabaseManager.java @@ -85,8 +85,7 @@ public class DatabaseManager implements StorageService, StorageAdminService { public static final String BASE_PARTITION_NAME = "p0"; private static final int DATABASE_STARTUP_TIMEOUT_SEC = 60; - private static final int RAFT_ELECTION_TIMEOUT = 3000; - private static final int RAFT_HEARTBEAT_TIMEOUT = 1500; + private static final int RAFT_ELECTION_TIMEOUT_MILLIS = 3000; private static final int DATABASE_OPERATION_TIMEOUT_MILLIS = 5000; private ClusterCoordinator coordinator; @@ -132,8 +131,8 @@ public class DatabaseManager implements StorageService, StorageAdminService { ClusterConfig clusterConfig = new ClusterConfig() .withProtocol(newNettyProtocol()) - .withElectionTimeout(RAFT_ELECTION_TIMEOUT) - .withHeartbeatInterval(RAFT_HEARTBEAT_TIMEOUT) + .withElectionTimeout(electionTimeoutMillis(activeNodeUris)) + .withHeartbeatInterval(heartbeatTimeoutMillis(activeNodeUris)) .withMembers(activeNodeUris) .withLocalMember(localNodeUri); @@ -264,14 +263,22 @@ public class DatabaseManager implements StorageService, StorageAdminService { private DatabaseConfig newDatabaseConfig(String name, Log log, String[] replicas) { return new DatabaseConfig() .withName(name) - .withElectionTimeout(RAFT_ELECTION_TIMEOUT) - .withHeartbeatInterval(RAFT_HEARTBEAT_TIMEOUT) + .withElectionTimeout(electionTimeoutMillis(replicas)) + .withHeartbeatInterval(heartbeatTimeoutMillis(replicas)) .withConsistency(Consistency.STRONG) .withLog(log) .withDefaultSerializer(new DatabaseSerializer()) .withReplicas(replicas); } + private long electionTimeoutMillis(String[] replicas) { + return replicas.length == 1 ? 10L : RAFT_ELECTION_TIMEOUT_MILLIS; + } + + private long heartbeatTimeoutMillis(String[] replicas) { + return electionTimeoutMillis(replicas) / 2; + } + /** * Maps a Raft Database object to a PartitionInfo object. *