|
@@ -20,6 +20,7 @@ package org.apache.hadoop.yarn.conf;
|
|
|
|
|
|
import org.apache.hadoop.conf.Configuration;
|
|
import org.apache.hadoop.conf.Configuration;
|
|
|
|
|
|
|
|
+import org.apache.hadoop.util.StringUtils;
|
|
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
|
|
import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
@@ -27,53 +28,134 @@ import org.junit.Test;
|
|
import java.util.Collection;
|
|
import java.util.Collection;
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
+import static org.junit.Assert.assertNull;
|
|
import static org.junit.Assert.fail;
|
|
import static org.junit.Assert.fail;
|
|
|
|
|
|
public class TestHAUtil {
|
|
public class TestHAUtil {
|
|
private Configuration conf;
|
|
private Configuration conf;
|
|
|
|
|
|
- private static final String RM1_ADDRESS = "1.2.3.4:8021";
|
|
|
|
|
|
+ private static final String RM1_ADDRESS_UNTRIMMED = " \t\t\n 1.2.3.4:8021 \n\t ";
|
|
|
|
+ private static final String RM1_ADDRESS = RM1_ADDRESS_UNTRIMMED.trim();
|
|
private static final String RM2_ADDRESS = "localhost:8022";
|
|
private static final String RM2_ADDRESS = "localhost:8022";
|
|
- private static final String RM1_NODE_ID = "rm1";
|
|
|
|
|
|
+ private static final String RM1_NODE_ID_UNTRIMMED = "rm1 ";
|
|
|
|
+ private static final String RM1_NODE_ID = RM1_NODE_ID_UNTRIMMED.trim();
|
|
private static final String RM2_NODE_ID = "rm2";
|
|
private static final String RM2_NODE_ID = "rm2";
|
|
|
|
+ private static final String RM3_NODE_ID = "rm3";
|
|
|
|
+ private static final String RM_INVALID_NODE_ID = ".rm";
|
|
|
|
+ private static final String RM_NODE_IDS_UNTRIMMED = RM1_NODE_ID_UNTRIMMED + "," + RM2_NODE_ID;
|
|
|
|
+ private static final String RM_NODE_IDS = RM1_NODE_ID + "," + RM2_NODE_ID;
|
|
|
|
|
|
@Before
|
|
@Before
|
|
public void setUp() {
|
|
public void setUp() {
|
|
conf = new Configuration();
|
|
conf = new Configuration();
|
|
- conf.set(YarnConfiguration.RM_HA_IDS, RM1_NODE_ID + "," + RM2_NODE_ID);
|
|
|
|
- conf.set(YarnConfiguration.RM_HA_ID, RM1_NODE_ID);
|
|
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_IDS, RM_NODE_IDS_UNTRIMMED);
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_ID, RM1_NODE_ID_UNTRIMMED);
|
|
|
|
|
|
for (String confKey : HAUtil.RPC_ADDRESS_CONF_KEYS) {
|
|
for (String confKey : HAUtil.RPC_ADDRESS_CONF_KEYS) {
|
|
- conf.set(HAUtil.addSuffix(confKey, RM1_NODE_ID), RM1_ADDRESS);
|
|
|
|
|
|
+ // configuration key itself cannot contains space/tab/return chars.
|
|
|
|
+ conf.set(HAUtil.addSuffix(confKey, RM1_NODE_ID), RM1_ADDRESS_UNTRIMMED);
|
|
conf.set(HAUtil.addSuffix(confKey, RM2_NODE_ID), RM2_ADDRESS);
|
|
conf.set(HAUtil.addSuffix(confKey, RM2_NODE_ID), RM2_ADDRESS);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testGetRMServiceId() throws Exception {
|
|
public void testGetRMServiceId() throws Exception {
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_IDS, RM1_NODE_ID + "," + RM2_NODE_ID);
|
|
Collection<String> rmhaIds = HAUtil.getRMHAIds(conf);
|
|
Collection<String> rmhaIds = HAUtil.getRMHAIds(conf);
|
|
assertEquals(2, rmhaIds.size());
|
|
assertEquals(2, rmhaIds.size());
|
|
|
|
+
|
|
|
|
+ String[] ids = rmhaIds.toArray(new String[0]);
|
|
|
|
+ assertEquals(RM1_NODE_ID, ids[0]);
|
|
|
|
+ assertEquals(RM2_NODE_ID, ids[1]);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
public void testGetRMId() throws Exception {
|
|
public void testGetRMId() throws Exception {
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_ID, RM1_NODE_ID);
|
|
assertEquals("Does not honor " + YarnConfiguration.RM_HA_ID,
|
|
assertEquals("Does not honor " + YarnConfiguration.RM_HA_ID,
|
|
- RM1_NODE_ID, HAUtil.getRMHAId(conf));
|
|
|
|
- conf = new YarnConfiguration();
|
|
|
|
- try {
|
|
|
|
- HAUtil.getRMHAId(conf);
|
|
|
|
- fail("getRMHAId() fails to throw an exception when RM_HA_ID is not set");
|
|
|
|
- } catch (YarnRuntimeException yre) {
|
|
|
|
- // do nothing
|
|
|
|
- }
|
|
|
|
|
|
+ RM1_NODE_ID, HAUtil.getRMHAId(conf));
|
|
|
|
+
|
|
|
|
+ conf.clear();
|
|
|
|
+ assertNull("Return null when " + YarnConfiguration.RM_HA_ID
|
|
|
|
+ + " is not set", HAUtil.getRMHAId(conf));
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void testSetGetRpcAddresses() throws Exception {
|
|
|
|
- HAUtil.setAllRpcAddresses(conf);
|
|
|
|
|
|
+ public void testVerifyAndSetConfiguration() throws Exception {
|
|
|
|
+ try {
|
|
|
|
+ HAUtil.verifyAndSetConfiguration(conf);
|
|
|
|
+ } catch (YarnRuntimeException e) {
|
|
|
|
+ fail("Should not throw any exceptions.");
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ assertEquals("Should be saved as Trimmed collection",
|
|
|
|
+ StringUtils.getStringCollection(RM_NODE_IDS), HAUtil.getRMHAIds(conf));
|
|
|
|
+ assertEquals("Should be saved as Trimmed string",
|
|
|
|
+ RM1_NODE_ID, HAUtil.getRMHAId(conf));
|
|
for (String confKey : HAUtil.RPC_ADDRESS_CONF_KEYS) {
|
|
for (String confKey : HAUtil.RPC_ADDRESS_CONF_KEYS) {
|
|
assertEquals("RPC address not set for " + confKey,
|
|
assertEquals("RPC address not set for " + confKey,
|
|
- RM1_ADDRESS, conf.get(confKey));
|
|
|
|
|
|
+ RM1_ADDRESS, conf.get(confKey));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ conf.clear();
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_IDS, RM_INVALID_NODE_ID);
|
|
|
|
+ try {
|
|
|
|
+ HAUtil.verifyAndSetConfiguration(conf);
|
|
|
|
+ } catch (YarnRuntimeException e) {
|
|
|
|
+ assertEquals("YarnRuntimeException by getRMId()",
|
|
|
|
+ HAUtil.BAD_CONFIG_MESSAGE_PREFIX +
|
|
|
|
+ HAUtil.getNeedToSetValueMessage(YarnConfiguration.RM_HA_ID),
|
|
|
|
+ e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ conf.clear();
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_ID, RM_INVALID_NODE_ID);
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_IDS, RM_INVALID_NODE_ID);
|
|
|
|
+ for (String confKey : HAUtil.RPC_ADDRESS_CONF_KEYS) {
|
|
|
|
+ // simulate xml with invalid node id
|
|
|
|
+ conf.set(confKey + RM_INVALID_NODE_ID, RM_INVALID_NODE_ID);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ HAUtil.verifyAndSetConfiguration(conf);
|
|
|
|
+ } catch (YarnRuntimeException e) {
|
|
|
|
+ assertEquals("YarnRuntimeException by addSuffix()",
|
|
|
|
+ HAUtil.BAD_CONFIG_MESSAGE_PREFIX +
|
|
|
|
+ HAUtil.getInvalidValueMessage(YarnConfiguration.RM_HA_ID,
|
|
|
|
+ RM_INVALID_NODE_ID),
|
|
|
|
+ e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ conf.clear();
|
|
|
|
+ // simulate the case HAUtil.RPC_ADDRESS_CONF_KEYS are not set
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_ID, RM1_NODE_ID);
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_IDS, RM1_NODE_ID);
|
|
|
|
+ try {
|
|
|
|
+ HAUtil.verifyAndSetConfiguration(conf);
|
|
|
|
+ fail("Should throw YarnRuntimeException. by Configuration#set()");
|
|
|
|
+ } catch (YarnRuntimeException e) {
|
|
|
|
+ String confKey =
|
|
|
|
+ HAUtil.addSuffix(YarnConfiguration.RM_ADDRESS, RM1_NODE_ID);
|
|
|
|
+ assertEquals("YarnRuntimeException by Configuration#set()",
|
|
|
|
+ HAUtil.BAD_CONFIG_MESSAGE_PREFIX + HAUtil.getNeedToSetValueMessage(confKey),
|
|
|
|
+ e.getMessage());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // simulate the case YarnConfiguration.RM_HA_IDS doesn't contain
|
|
|
|
+ // the value of YarnConfiguration.RM_HA_ID
|
|
|
|
+ conf.clear();
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_IDS, RM2_NODE_ID + "," + RM3_NODE_ID);
|
|
|
|
+ conf.set(YarnConfiguration.RM_HA_ID, RM1_NODE_ID_UNTRIMMED);
|
|
|
|
+ for (String confKey : HAUtil.RPC_ADDRESS_CONF_KEYS) {
|
|
|
|
+ conf.set(HAUtil.addSuffix(confKey, RM1_NODE_ID), RM1_ADDRESS_UNTRIMMED);
|
|
|
|
+ conf.set(HAUtil.addSuffix(confKey, RM2_NODE_ID), RM2_ADDRESS);
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ HAUtil.verifyAndSetConfiguration(conf);
|
|
|
|
+ } catch (YarnRuntimeException e) {
|
|
|
|
+ assertEquals("YarnRuntimeException by getRMId()'s validation",
|
|
|
|
+ HAUtil.BAD_CONFIG_MESSAGE_PREFIX +
|
|
|
|
+ HAUtil.getRMHAIdNeedToBeIncludedMessage("[rm2, rm3]", RM1_NODE_ID),
|
|
|
|
+ e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|