SLIDER-48 reverted some recent changes and accounted for possible NPE
git-svn-id: https://svn.apache.org/repos/asf/incubator/slider/trunk@1594702 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java b/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
index 4dbd7a6..f5ebe2e 100644
--- a/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
+++ b/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java
@@ -23,10 +23,11 @@
import org.apache.hadoop.util.StringUtils;
import org.apache.hadoop.yarn.api.ApplicationConstants;
import org.apache.hadoop.yarn.api.records.Container;
-import org.apache.hadoop.yarn.api.records.ContainerId;
import org.apache.hadoop.yarn.api.records.LocalResource;
import org.apache.hadoop.yarn.api.records.LocalResourceType;
import org.apache.slider.api.ClusterDescription;
+import org.apache.slider.api.ClusterDescriptionKeys;
+import org.apache.slider.api.ClusterNode;
import org.apache.slider.api.OptionKeys;
import org.apache.slider.api.StatusKeys;
import org.apache.slider.common.SliderKeys;
@@ -50,7 +51,6 @@
import org.apache.slider.providers.agent.application.metadata.Metainfo;
import org.apache.slider.providers.agent.application.metadata.MetainfoParser;
import org.apache.slider.providers.agent.application.metadata.Service;
-import org.apache.slider.server.appmaster.state.RoleInstance;
import org.apache.slider.server.appmaster.state.StateAccessForProviders;
import org.apache.slider.server.appmaster.web.rest.agent.AgentCommandType;
import org.apache.slider.server.appmaster.web.rest.agent.AgentRestOperations;
@@ -74,6 +74,7 @@
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
@@ -260,22 +261,11 @@
getStateAccessor().getPublishedConfigurations().put(name, pubconf);
}
- protected Map<String, Map<String, String>> getRoleClusterNodeMapping() {
- StateAccessForProviders accessor = getStateAccessor();
- assert accessor.isApplicationLive();
- Map<ContainerId, RoleInstance> liveNodes = accessor.getLiveNodes();
- Map<String, Map<String, String>> retVal = new HashMap<>();
- for (ContainerId cid : liveNodes.keySet()) {
- RoleInstance ri = liveNodes.get(cid);
- Map<String, String> containerMap = retVal.get(ri.role);
- if (containerMap == null) {
- containerMap = new HashMap<>();
- retVal.put(ri.role, containerMap);
- }
- containerMap.put(cid.toString(), ri.host);
- }
-
- return retVal;
+ protected Map<String, Map<String, ClusterNode>> getRoleClusterNodeMapping() {
+ stateAccessor.refreshClusterStatus();
+ return (Map<String, Map<String, ClusterNode>>)
+ stateAccessor.getClusterStatus().status.get(
+ ClusterDescriptionKeys.KEY_CLUSTER_LIVE);
}
private String getContainerLabel(Container container, String role) {
@@ -459,8 +449,8 @@
// publish export groups if any
Map<String, String> replaceTokens = new HashMap<>();
- for (Map.Entry<String, Map<String, String>> entry : getRoleClusterNodeMapping().entrySet()) {
- String hostName = getHostsList(entry.getValue(), true).iterator().next();
+ for (Map.Entry<String, Map<String, ClusterNode>> entry : getRoleClusterNodeMapping().entrySet()) {
+ String hostName = getHostsList(entry.getValue().values(), true).iterator().next();
replaceTokens.put(String.format(hostKeyFormat, entry.getKey().toUpperCase(Locale.ENGLISH)), hostName);
}
@@ -730,17 +720,18 @@
}
protected void addRoleRelatedTokens(Map<String, String> tokens) {
- for (Map.Entry<String, Map<String, String>> entry : getRoleClusterNodeMapping().entrySet()) {
+ for (Map.Entry<String, Map<String, ClusterNode>> entry : getRoleClusterNodeMapping().entrySet()) {
String tokenName = entry.getKey().toUpperCase(Locale.ENGLISH) + "_HOST";
- String hosts = StringUtils.join(",", getHostsList(entry.getValue(), true));
+ String hosts = StringUtils.join(",", getHostsList(entry.getValue().values(), true));
tokens.put("${" + tokenName + "}", hosts);
}
}
- private Iterable<String> getHostsList(Map<String, String> values, boolean hostOnly) {
+ private Iterable<String> getHostsList(Collection<ClusterNode> values,
+ boolean hostOnly) {
List<String> hosts = new ArrayList<>();
- for (Map.Entry<String, String> entry : values.entrySet()) {
- hosts.add(hostOnly ? entry.getValue() : entry.getValue() + "/" + entry.getKey());
+ for (ClusterNode cn : values) {
+ hosts.add(hostOnly ? cn.host : cn.host + "/" + cn.name);
}
return hosts;
@@ -761,10 +752,10 @@
}
private void buildRoleHostDetails(Map<String, URL> details) {
- for (Map.Entry<String, Map<String, String>> entry :
+ for (Map.Entry<String, Map<String, ClusterNode>> entry :
getRoleClusterNodeMapping().entrySet()) {
details.put(entry.getKey() + " Host(s)/Container(s): " +
- getHostsList(entry.getValue(), false),
+ getHostsList(entry.getValue().values(), false),
null);
}
}
diff --git a/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java b/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java
index e017c02..bf792f9 100644
--- a/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java
+++ b/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java
@@ -33,6 +33,8 @@
import org.apache.hadoop.yarn.api.records.Resource;
import org.apache.hadoop.yarn.api.records.Token;
import org.apache.slider.api.ClusterDescription;
+import org.apache.slider.api.ClusterDescriptionKeys;
+import org.apache.slider.api.ClusterNode;
import org.apache.slider.api.OptionKeys;
import org.apache.slider.api.StatusKeys;
import org.apache.slider.common.tools.SliderFileSystem;
@@ -253,47 +255,47 @@
AgentProviderService aps = new AgentProviderService();
StateAccessForProviders appState = new AppState(null) {
@Override
- public Map<ContainerId, RoleInstance> getLiveNodes() {
- Map<ContainerId, RoleInstance> retVal = new HashMap<>();
- ContainerId cid = new MyContainerId(1);
- Container container = new MyContainer();
- container.setId(cid);
- RoleInstance ri = new RoleInstance(container);
- ri.host = "FIRST_HOST";
- ri.role = "FIRST_ROLE";
- retVal.put(cid, ri);
+ public ClusterDescription getClusterStatus() {
+ ClusterDescription cd = new ClusterDescription();
+ cd.status = new HashMap<String,Object>();
+ Map<String, Map<String,ClusterNode>> roleMap = new HashMap<>();
+ ClusterNode cn1 = new ClusterNode(new MyContainerId(1));
+ cn1.host = "FIRST_HOST";
+ Map<String, ClusterNode> map1 = new HashMap<>();
+ map1.put("FIRST_CONTAINER", cn1);
+ ClusterNode cn2 = new ClusterNode(new MyContainerId(2));
+ cn2.host = "SECOND_HOST";
+ Map<String, ClusterNode> map2 = new HashMap<>();
+ map2.put("SECOND_CONTAINER", cn2);
+ ClusterNode cn3 = new ClusterNode(new MyContainerId(3));
+ cn3.host = "THIRD_HOST";
+ map2.put("THIRD_CONTAINER", cn3);
- cid = new MyContainerId(2);
- container = new MyContainer();
- container.setId(cid);
- ri = new RoleInstance(container);
- ri.host = "SECOND_HOST";
- ri.role = "SECOND_ROLE";
- retVal.put(cid, ri);
+ roleMap.put("FIRST_ROLE", map1);
+ roleMap.put("SECOND_ROLE", map2);
- cid = new MyContainerId(3);
- container = new MyContainer();
- container.setId(cid);
- ri = new RoleInstance(container);
- ri.host = "THIRD_HOST";
- ri.role = "SECOND_ROLE";
- retVal.put(cid, ri);
+ cd.status.put(ClusterDescriptionKeys.KEY_CLUSTER_LIVE, roleMap);
- return retVal;
+ return cd;
}
@Override
public boolean isApplicationLive() {
return true;
}
+
+ @Override
+ public void refreshClusterStatus() {
+ // do nothing
+ }
};
aps.setStateAccessor(appState);
- Map<String, String> tokens = new HashMap<>();
+ Map<String, String> tokens = new HashMap<String, String>();
aps.addRoleRelatedTokens(tokens);
TestCase.assertEquals(2, tokens.size());
TestCase.assertEquals("FIRST_HOST", tokens.get("${FIRST_ROLE_HOST}"));
- TestCase.assertEquals("SECOND_HOST,THIRD_HOST", tokens.get("${SECOND_ROLE_HOST}"));
+ TestCase.assertEquals("THIRD_HOST,SECOND_HOST", tokens.get("${SECOND_ROLE_HOST}"));
aps.close();
}
@@ -317,9 +319,11 @@
status.setConfigs(configs);
hb.setComponentStatus(new ArrayList<>(Arrays.asList(status)));
- Map<String, Map<String, String>> roleClusterNodeMap = new HashMap<>();
- Map<String, String> container = new HashMap<>();
- container.put("cid1", "HOST1");
+ Map<String, Map<String, ClusterNode>> roleClusterNodeMap = new HashMap<>();
+ Map<String, ClusterNode> container = new HashMap<>();
+ ClusterNode cn1 = new ClusterNode(new MyContainerId(1));
+ cn1.host = "HOST1";
+ container.put("cid1", cn1);
roleClusterNodeMap.put("HBASE_MASTER", container);
ComponentInstanceState componentStatus = new ComponentInstanceState("HBASE_MASTER", "aid", "cid");