Skip to content
Permalink
Browse files
prefer secondary over slave referring to standby or secondary servers (
…#1070)

* The community has decided that the naming of replicas in any form
should be referred to as secondary or standby.
We have chosen to use the word secondary
This PR renames (hopefully) all of the uses of slave to secondary

* Remove slave from build.properties
Modify HostRequirements so that we will accept slave or secondary for now
slave is deprecated but we will still accept it and not document it.
  • Loading branch information
davecramer committed Jan 15, 2018
1 parent 3ba3b63 commit 32c539020db3c940bae9b2a42425b1f23e864c73
@@ -140,7 +140,7 @@ In addition to the standard connection parameters the driver supports a number o
| disableColumnSanitiser | Boolean | false | Enable optimization that disables column name sanitiser |
| assumeMinServerVersion | String | null | Assume the server is at least that version |
| currentSchema | String | null | Specify the schema to be set in the search-path |
| targetServerType | String | any | Specifies what kind of server to connect, possible values: any, master, slave, preferSlave |
| targetServerType | String | any | Specifies what kind of server to connect, possible values: any, master, slave (deprecated), secondary, preferSlave (deprecated), preferSecondary |
| hostRecheckSeconds | Integer | 10 | Specifies period (seconds) after which the host status is checked again in case it has changed |
| loadBalanceHosts | Boolean | false | If disabled hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates |
| socketFactory | String | null | Specify a socket factory for socket creation |
@@ -5,10 +5,10 @@

server=localhost
port=5432
slaveServer=localhost
slavePort=5433
slaveServer2=localhost
slavePort2=5434
secondaryServer=localhost
secondaryPort=5433
secondaryServer2=localhost
secondaryServerPort2=5434
database=test
username=test
password=test
@@ -385,9 +385,9 @@ Connection conn = DriverManager.getConnection(url);
* **targetServerType** = String

Allows opening connections to only servers with required state,
the allowed values are any, master, slave and preferSlave.
the allowed values are any, master, slave, secondary, preferSlave and preferSecondary.
The master/slave distinction is currently done by observing if the server allows writes.
The value preferSlave tries to connect to slaves if any are available,
The value preferSecondary tries to connect to secondary if any are available,
otherwise allows falls back to connecting also to master.

* **hostRecheckSeconds** = int
@@ -361,7 +361,7 @@
CURRENT_SCHEMA("currentSchema", null, "Specify the schema to be set in the search-path"),

TARGET_SERVER_TYPE("targetServerType", "any", "Specifies what kind of server to connect", false,
"any", "master", "slave", "preferSlave"),
"any", "master", "slave", "secondary", "preferSlave", "preferSecondary"),

LOAD_BALANCE_HOSTS("loadBalanceHosts", "false",
"If disabled hosts are connected in the given order. If enabled hosts are chosen randomly from the set of suitable candidates"),
@@ -118,7 +118,7 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, String user, Strin
HostRequirement targetServerType;
String targetServerTypeStr = PGProperty.TARGET_SERVER_TYPE.get(info);
try {
targetServerType = HostRequirement.valueOf(targetServerTypeStr);
targetServerType = HostRequirement.getTargetServerType(targetServerTypeStr);
} catch (IllegalArgumentException ex) {
throw new PSQLException(
GT.tr("Invalid targetServerType value: {0}", targetServerTypeStr),
@@ -212,10 +212,10 @@ public QueryExecutor openConnectionImpl(HostSpec[] hostSpecs, String user, Strin
QueryExecutor queryExecutor = new QueryExecutorImpl(newStream, user, database,
cancelSignalTimeout, info);

// Check Master or Slave
// Check Master or Secondary
HostStatus hostStatus = HostStatus.ConnectOK;
if (candidateHost.targetServerType != HostRequirement.any) {
hostStatus = isMaster(queryExecutor) ? HostStatus.Master : HostStatus.Slave;
hostStatus = isMaster(queryExecutor) ? HostStatus.Master : HostStatus.Secondary;
}
GlobalHostStatusTracker.reportHostStatus(hostSpec, hostStatus);
knownStates.put(hostSpec, hostStatus);
@@ -19,16 +19,35 @@ public boolean allowConnectingTo(HostStatus status) {
return status == HostStatus.Master || status == HostStatus.ConnectOK;
}
},
slave {
secondary {
public boolean allowConnectingTo(HostStatus status) {
return status == HostStatus.Slave || status == HostStatus.ConnectOK;
return status == HostStatus.Secondary || status == HostStatus.ConnectOK;
}
},
preferSlave {
preferSecondary {
public boolean allowConnectingTo(HostStatus status) {
return status != HostStatus.ConnectFail;
}
};

public abstract boolean allowConnectingTo(HostStatus status);

/**
*
* The postgreSQL project has decided not to use the term slave to refer to alternate servers.
* secondary or standby is preferred. We have arbitrarily chosen secondary.
* As of Jan 2018 in order not to break existint code we are going to accept both slave or
* secondary for names of alternate servers.
*
* The current policy is to keep accepting this silently but not document slave, or slave preferSlave
*
* @param targetServerType the value of {@code targetServerType} connection property
* @return HostRequirement
*/

public static HostRequirement getTargetServerType(String targetServerType) {
String allowSlave = targetServerType.replace("lave", "econdary");
return valueOf(allowSlave);
}

}
@@ -12,5 +12,5 @@
ConnectFail,
ConnectOK,
Master,
Slave
Secondary
}
@@ -56,32 +56,32 @@
}

private Iterator<CandidateHost> candidateIterator() {
if (targetServerType != HostRequirement.preferSlave) {
if (targetServerType != HostRequirement.preferSecondary) {
return getCandidateHosts(targetServerType).iterator();
}

// preferSlave tries to find slave hosts first
// preferSecondary tries to find secondary hosts first
// Note: sort does not work here since there are "unknown" hosts,
// and that "unkown" might turn out to be master, so we should discard that
// if other slaves exist
List<CandidateHost> slaves = getCandidateHosts(HostRequirement.slave);
// and that "unknown" might turn out to be master, so we should discard that
// if other secondaries exist
List<CandidateHost> secondaries = getCandidateHosts(HostRequirement.secondary);
List<CandidateHost> any = getCandidateHosts(HostRequirement.any);

if (slaves.isEmpty()) {
if (secondaries.isEmpty()) {
return any.iterator();
}

if (any.isEmpty()) {
return slaves.iterator();
return secondaries.iterator();
}

if (slaves.get(slaves.size() - 1).equals(any.get(0))) {
// When the last slave's hostspec is the same as the first in "any" list, there's no need
// to attempt to connect it as "slave"
if (secondaries.get(secondaries.size() - 1).equals(any.get(0))) {
// When the last secondary's hostspec is the same as the first in "any" list, there's no need
// to attempt to connect it as "secondary"
// Note: this is only an optimization
slaves = rtrim(1, slaves);
secondaries = rtrim(1, secondaries);
}
return append(slaves, any).iterator();
return append(secondaries, any).iterator();
}

private List<CandidateHost> getCandidateHosts(HostRequirement hostRequirement) {
@@ -20,56 +20,56 @@
*/
public class MultiHostTestSuite extends TestSuite {

public static java.sql.Connection openSlaveDB() throws Exception {
public static java.sql.Connection openSecondaryDB() throws Exception {
TestUtil.initDriver();

Properties props = new Properties();

props.setProperty("user", TestUtil.getUser());
props.setProperty("password", TestUtil.getPassword());

return DriverManager.getConnection(TestUtil.getURL(getSlaveServer(), getSlavePort()), props);
return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer(), getSecondaryPort()), props);
}

public static java.sql.Connection openSlaveDB2() throws Exception {
public static java.sql.Connection openSecondaryDB2() throws Exception {
TestUtil.initDriver();

Properties props = new Properties();

props.setProperty("user", TestUtil.getUser());
props.setProperty("password", TestUtil.getPassword());

return DriverManager.getConnection(TestUtil.getURL(getSlaveServer2(), getSlavePort2()), props);
return DriverManager.getConnection(TestUtil.getURL(getSecondaryServer2(), getSecondaryPort2()), props);
}

/*
* Returns the Test server
*/
public static String getSlaveServer() {
return System.getProperty("slaveServer", TestUtil.getServer());
public static String getSecondaryServer() {
return System.getProperty("secondaryServer", TestUtil.getServer());
}

/*
* Returns the Test port
*/
public static int getSlavePort() {
public static int getSecondaryPort() {
return Integer
.parseInt(System.getProperty("slavePort", String.valueOf(TestUtil.getPort() + 1)));
.parseInt(System.getProperty("secondaryPort", String.valueOf(TestUtil.getPort() + 1)));
}

/*
* Returns the Test server
*/
public static String getSlaveServer2() {
return System.getProperty("slaveServer2", TestUtil.getServer());
public static String getSecondaryServer2() {
return System.getProperty("secondaryServer2", TestUtil.getServer());
}

/*
* Returns the Test port
*/
public static int getSlavePort2() {
public static int getSecondaryPort2() {
return Integer
.parseInt(System.getProperty("slavePort2", String.valueOf(TestUtil.getPort() + 2)));
.parseInt(System.getProperty("secondaryPort2", String.valueOf(TestUtil.getPort() + 2)));
}

/*
@@ -79,10 +79,10 @@ public static TestSuite suite() throws Exception {
TestSuite suite = new TestSuite();

try {
Connection connection = openSlaveDB();
Connection connection = openSecondaryDB();
TestUtil.closeDB(connection);

connection = openSlaveDB2();
connection = openSecondaryDB2();
TestUtil.closeDB(connection);
} catch (PSQLException ex) {
// replication instance is not available, but suite must have at lest one test case
Loading

0 comments on commit 32c5390

Please sign in to comment.