001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.oozie.action.hadoop;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.regex.Matcher;
027import java.util.regex.Pattern;
028
029import org.apache.hadoop.yarn.api.records.Priority;
030import org.apache.hadoop.yarn.api.records.Resource;
031import org.apache.hadoop.yarn.api.records.ResourceRequest;
032import org.apache.hadoop.yarn.factories.RecordFactory;
033import org.apache.hadoop.yarn.factory.providers.RecordFactoryProvider;
034import org.apache.oozie.util.XLog;
035
036import com.google.common.collect.Sets;
037
038public final class AMLocalityHelper {
039    private AMLocalityHelper() {}  // no instances
040
041    private static final RecordFactory recordFactory = RecordFactoryProvider
042            .getRecordFactory(null);
043    private static final XLog LOG = XLog.getLog(AMLocalityHelper.class);
044
045    private static final String DEFAULT_RACK = "/default-rack";
046    private static final String RACK_GROUP = "rack";
047    private static final String NODE_IF_RACK_GROUP = "node1";
048    private static final String NODE_IF_NO_RACK_GROUP = "node2";
049
050    // Matches patterns like "node", "/rack", or "/rack/node"
051    private static final Pattern RACK_NODE_PATTERN =
052            Pattern.compile(
053                String.format("(?<%s>[^/]+?)|(?<%s>/[^/]+?)(?:/(?<%s>[^/]+?))?",
054                NODE_IF_NO_RACK_GROUP, RACK_GROUP, NODE_IF_RACK_GROUP));
055
056    // Based on MAPREDUCE-6871
057    public static List<ResourceRequest> generateResourceRequests(int memory, int vcores, int priority,
058            Collection<String> locality) {
059        Resource capability = recordFactory.newRecordInstance(Resource.class);
060        capability.setMemorySize(memory);
061        capability.setVirtualCores(vcores);
062        LOG.debug("AppMaster capability = {0}", capability);
063
064        List<ResourceRequest> amResourceRequests = new ArrayList<>();
065        // Always have an ANY request
066        ResourceRequest amAnyResourceRequest =
067                createSingleContainerRelaxedLocalityResourceRequest(ResourceRequest.ANY, capability, priority);
068        Map<String, ResourceRequest> rackRequests = new HashMap<>();
069        amResourceRequests.add(amAnyResourceRequest);
070
071        for (String amStrictResource : locality) {
072            amAnyResourceRequest.setRelaxLocality(false);
073            Matcher matcher = RACK_NODE_PATTERN.matcher(amStrictResource);
074            if (matcher.matches()) {
075                String nodeName;
076                String rackName = matcher.group(RACK_GROUP);
077                if (rackName == null) {
078                    rackName = DEFAULT_RACK;
079                    nodeName = matcher.group(NODE_IF_NO_RACK_GROUP);
080                } else {
081                    nodeName = matcher.group(NODE_IF_RACK_GROUP);
082                }
083                ResourceRequest amRackResourceRequest = rackRequests.get(rackName);
084                if (amRackResourceRequest == null) {
085                    amRackResourceRequest = createSingleContainerRelaxedLocalityResourceRequest(rackName, capability, priority);
086                    amResourceRequests.add(amRackResourceRequest);
087                    rackRequests.put(rackName, amRackResourceRequest);
088                }
089                if (nodeName != null) {
090                    amRackResourceRequest.setRelaxLocality(false);
091                    ResourceRequest amNodeResourceRequest =
092                            createSingleContainerRelaxedLocalityResourceRequest(nodeName, capability, priority);
093                    amResourceRequests.add(amNodeResourceRequest);
094                }
095            } else {
096                String errMsg =
097                        "Invalid resource name: " + amStrictResource + " specified.";
098                LOG.error(errMsg);
099                throw new IllegalArgumentException(errMsg);
100            }
101        }
102
103        for (ResourceRequest amResourceRequest : amResourceRequests) {
104            LOG.debug("ResourceRequest: resource = {0}, locality = {1}",
105                    amResourceRequest.getResourceName(),
106                    amResourceRequest.getRelaxLocality());
107        }
108
109        return amResourceRequests;
110    }
111
112    private static ResourceRequest createSingleContainerRelaxedLocalityResourceRequest(String resource,
113            Resource capability, int priority) {
114        ResourceRequest resourceRequest =
115                recordFactory.newRecordInstance(ResourceRequest.class);
116        resourceRequest.setPriority(Priority.newInstance(priority));
117        resourceRequest.setResourceName(resource);
118        resourceRequest.setCapability(capability);
119        resourceRequest.setNumContainers(1);
120        resourceRequest.setRelaxLocality(true);
121        return resourceRequest;
122    }
123}