1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 2010, 2013 Oracle and/or its affiliates. All rights reserved.
*
*/
package repmgrtests;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import org.junit.Test;
import org.junit.Before;
import com.sleepycat.db.Environment;
import com.sleepycat.db.EnvironmentConfig;
import com.sleepycat.db.ReplicationConfig;
import com.sleepycat.db.ReplicationManagerSiteConfig;
import com.sleepycat.db.ReplicationManagerStartPolicy;
import com.sleepycat.db.ReplicationStats;
import com.sleepycat.db.ReplicationTimeoutType;
import com.sleepycat.db.StatsConfig;
import com.sleepycat.db.VerboseConfig;
/**
* Tests for repmgr's handling of elections, and the "strict 2-site"
* config flag.
*/
public class TestStrictElect {
private int[] testPorts;
private int masterPort;
private int clientPort;
private int client2Port;
@Before public void setUp() throws Exception {
testPorts = Util.findAvailablePorts(3);
masterPort = testPorts[0];
clientPort = testPorts[1];
client2Port = testPorts[2];
}
/**
* Verifies that by default in a 2-site group, client takes over
* when master seems to have failed.
*/
@Test public void liberal() throws Exception {
EnvironmentConfig ec = makeBasicConfig();
ReplicationManagerSiteConfig local = new ReplicationManagerSiteConfig("localhost", masterPort);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
File masterDir = Util.mkdir("master");
Environment master = new Environment(masterDir, ec);
master.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
master.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_MASTER);
ec = makeBasicConfig();
local = new ReplicationManagerSiteConfig("localhost", clientPort);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
ReplicationManagerSiteConfig remote = new ReplicationManagerSiteConfig("localhost", masterPort);
remote.setBootstrapHelper(true);
ec.addReplicationManagerSite(remote);
EventHandler mon = new EventHandler();
ec.setEventHandler(mon);
File clientDir = Util.mkdir("client");
Environment client = new Environment(clientDir, ec);
client.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
client.setReplicationTimeout(ReplicationTimeoutType.ELECTION_RETRY, 500000);
client.setReplicationTimeout(ReplicationTimeoutType.ELECTION_TIMEOUT, 500000);
client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);
mon.await();
// check stats, at this point there should be 0 elections
ReplicationStats stats = client.getReplicationStats(StatsConfig.DEFAULT);
assertTrue(stats.getElections() == 0);
master.close();
// wait a little while
// there should be a couple of failed elections, and we should
// not be master
Thread.sleep(5000);
stats = client.getReplicationStats(StatsConfig.DEFAULT);
assertTrue(stats.getElections() > 2);
assertTrue(stats.getEnvId() != stats.getMaster());
client.close();
}
/**
* Verifies that when the "strict" setting is on, failures
* of a master leaves the group with no master: the client does
* not take over.
*/
@Test public void strict() throws Exception {
EnvironmentConfig ec = makeBasicConfig();
ReplicationManagerSiteConfig local = new ReplicationManagerSiteConfig("localhost", masterPort);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
File masterDir = Util.mkdir("master");
Environment master = new Environment(masterDir, ec);
master.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_MASTER);
ec = makeBasicConfig();
local = new ReplicationManagerSiteConfig("localhost", clientPort);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
ReplicationManagerSiteConfig remote = new ReplicationManagerSiteConfig("localhost", masterPort);
remote.setBootstrapHelper(true);
ec.addReplicationManagerSite(remote);
EventHandler mon = new EventHandler();
ec.setEventHandler(mon);
File clientDir = Util.mkdir("client");
Environment client = new Environment(clientDir, ec);
client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);
mon.await();
// check stats, at this point there should be 0 elections
ReplicationStats stats = client.getReplicationStats(StatsConfig.DEFAULT);
assertTrue(stats.getElections() == 0);
master.close();
Thread.sleep(3000);
stats = client.getReplicationStats(StatsConfig.DEFAULT);
assertEquals(1, stats.getElections());
assertEquals(ReplicationStats.REP_CLIENT, stats.getStatus());
client.close();
}
/**
* Verifies that the usual strict majority rule is observed in a
* group with more than two sites, regardless of the config setting.
*/
@Test public void threeSite() throws Exception {
EnvironmentConfig ec = makeBasicConfig();
ReplicationManagerSiteConfig local = new ReplicationManagerSiteConfig("localhost", masterPort);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
File masterDir = Util.mkdir("master");
Environment master = new Environment(masterDir, ec);
master.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
master.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_MASTER);
ec = makeBasicConfig();
local = new ReplicationManagerSiteConfig("localhost", clientPort);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
ReplicationManagerSiteConfig remote = new ReplicationManagerSiteConfig("localhost", masterPort);
remote.setBootstrapHelper(true);
ec.addReplicationManagerSite(remote);
EventHandler mon = new EventHandler();
ec.setEventHandler(mon);
File clientDir = Util.mkdir("client");
Environment client = new Environment(clientDir, ec);
client.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
client.setReplicationTimeout(ReplicationTimeoutType.ELECTION_RETRY, 500000);
client.setReplicationTimeout(ReplicationTimeoutType.ELECTION_TIMEOUT, 500000);
client.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);
mon.await();
// Create, start, and sync the 3rd site, just to establish its
// existence (so that the rest of the group recognizes that
// the group size is 3); then shut it down so that the later
// election will fail for insufficient sites.
//
ec = makeBasicConfig();
local = new ReplicationManagerSiteConfig("localhost", client2Port);
local.setLocalSite(true);
ec.addReplicationManagerSite(local);
remote = new ReplicationManagerSiteConfig("localhost", masterPort);
remote.setBootstrapHelper(true);
ec.addReplicationManagerSite(remote);
mon = new EventHandler();
ec.setEventHandler(mon);
clientDir = Util.mkdir("client2");
Environment client2 = new Environment(clientDir, ec);
client2.setReplicationConfig(ReplicationConfig.STRICT_2SITE, true);
client2.setReplicationTimeout(ReplicationTimeoutType.ELECTION_RETRY, 500000);
client2.setReplicationTimeout(ReplicationTimeoutType.ELECTION_TIMEOUT, 500000);
client2.replicationManagerStart(1, ReplicationManagerStartPolicy.REP_CLIENT);
mon.await();
client2.close();
// check stats, at this point there should be 0 elections
ReplicationStats stats = client.getReplicationStats(StatsConfig.DEFAULT);
assertTrue(stats.getElections() == 0);
master.close();
// wait a little while
// there should be a couple of failed elections, and we should
// not be master
Thread.sleep(5000);
stats = client.getReplicationStats(StatsConfig.DEFAULT);
assertTrue(stats.getElections() > 2);
assertTrue(stats.getEnvId() != stats.getMaster());
client.close();
}
private EnvironmentConfig makeBasicConfig() throws Exception {
EnvironmentConfig ec = new EnvironmentConfig();
ec.setAllowCreate(true);
ec.setInitializeCache(true);
ec.setInitializeLocking(true);
ec.setInitializeLogging(true);
ec.setInitializeReplication(true);
ec.setTransactional(true);
ec.setThreaded(true);
if (Boolean.getBoolean("VERB_REPLICATION"))
ec.setVerbose(VerboseConfig.REPLICATION, true);
return (ec);
}
}
|