Author: jmaron Date: Wed May 14 20:22:42 2014 New Revision: 1594702 URL: http://svn.apache.org/r1594702 Log: SLIDER-48 reverted some recent changes and accounted for possible NPE
Modified: incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java incubator/slider/trunk/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java Modified: incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java URL: http://svn.apache.org/viewvc/incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java?rev=1594702&r1=1594701&r2=1594702&view=diff ============================================================================== --- incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java (original) +++ incubator/slider/trunk/slider-core/src/main/java/org/apache/slider/providers/agent/AgentProviderService.java Wed May 14 20:22:42 2014 @@ -23,10 +23,11 @@ import org.apache.hadoop.fs.Path; 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 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.URI; 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 @@ public class AgentProviderService extend 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 @@ public class AgentProviderService extend // 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 @@ public class AgentProviderService extend } 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 @@ public class AgentProviderService extend } 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); } } Modified: incubator/slider/trunk/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java URL: http://svn.apache.org/viewvc/incubator/slider/trunk/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java?rev=1594702&r1=1594701&r2=1594702&view=diff ============================================================================== --- incubator/slider/trunk/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java (original) +++ incubator/slider/trunk/slider-core/src/test/java/org/apache/slider/providers/agent/TestAgentProviderService.java Wed May 14 20:22:42 2014 @@ -33,6 +33,8 @@ import org.apache.hadoop.yarn.api.record 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 @@ public class TestAgentProviderService { 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); - - 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); - - 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); + 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); - return retVal; + roleMap.put("FIRST_ROLE", map1); + roleMap.put("SECOND_ROLE", map2); + + cd.status.put(ClusterDescriptionKeys.KEY_CLUSTER_LIVE, roleMap); + + 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 @@ public class TestAgentProviderService { 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");