Tom*_*Tom 7 hadoop hadoop-yarn hadoop2
Yarn 使用虚拟核心的概念来管理 CPU 资源。我会问使用虚拟核心有什么好处,YARN 使用 vcore 有什么原因吗?
这是文档的内容(强调我的)
节点的容量应配置与其物理核心数量相等的虚拟核心。应该向容器请求它可以饱和的核心数量,即它 期望一次可运行的平均线程数。
除非CPU核心是超线程的,否则它一次只能运行一个线程(在超线程操作系统的情况下,实际上一个物理核心有2个核心,并且可以运行两个线程——当然,这有点作弊,而且没有任何地方可以做到这一点)与拥有实际物理核心一样高效)。从本质上讲,这对最终用户意味着一个核心可以运行单个线程,因此从理论上讲,如果我想要使用 java 线程进行并行性,那么一个相当好的近似值是线程数等于核心数。因此,如果您的容器进程(即 JVM)需要 2 个线程,那么最好将其映射到 2 个 vcore - 这就是最后一行的含义。作为节点的总容量,vcore 应等于物理核心的数量。
最重要的是要记住,实际上是操作系统将调度线程在不同的内核中执行,就像在任何其他应用程序中发生的那样,并且 YARN 本身无法控制它,除了以下事实:什么是最好的可能为每个容器分配多少线程的近似值。这就是为什么考虑操作系统上运行的其他应用程序、内核使用的 CPU 周期等很重要,因为所有内核并不总是可供 YARN 应用程序使用。
编辑:进一步研究
Yarn 不会影响 CPU 的硬限制,但通过代码我可以看到它如何影响 CPU 调度或 CPU 速率。从技术上讲,Yarn 可以启动不同的容器进程 - java、python、自定义 shell 命令等。在 Yarn 中启动容器的责任属于节点管理器的 ContainerExecutor 组件,我可以看到启动容器等的代码,以及一些提示(取决于平台上)。例如,在DefaultContainerExecutor(扩展 ContainerExecutor)的情况下 - 对于 Windows,它使用“-c”参数进行 CPU 限制,而在 Linux 上,它使用进程良好度来影响它。还有另一种实现LinuxContainerExecutor(或者更好,CgroupsLCEResourcesHandler因为前者不强制使用 cgroup),它尝试使用 Linux cgroup 来限制该节点上的 Yarn CPU 资源。更多详情可在这找到。
ContainerExecutor {
.......
.......
protected String[] getRunCommand(String command, String groupId,
String userName, Path pidFile, Configuration conf, Resource resource) {
boolean containerSchedPriorityIsSet = false;
int containerSchedPriorityAdjustment =
YarnConfiguration.DEFAULT_NM_CONTAINER_EXECUTOR_SCHED_PRIORITY;
if (conf.get(YarnConfiguration.NM_CONTAINER_EXECUTOR_SCHED_PRIORITY) !=
null) {
containerSchedPriorityIsSet = true;
containerSchedPriorityAdjustment = conf
.getInt(YarnConfiguration.NM_CONTAINER_EXECUTOR_SCHED_PRIORITY,
YarnConfiguration.DEFAULT_NM_CONTAINER_EXECUTOR_SCHED_PRIORITY);
}
if (Shell.WINDOWS) {
int cpuRate = -1;
int memory = -1;
if (resource != null) {
if (conf
.getBoolean(
YarnConfiguration.NM_WINDOWS_CONTAINER_MEMORY_LIMIT_ENABLED,
YarnConfiguration.DEFAULT_NM_WINDOWS_CONTAINER_MEMORY_LIMIT_ENABLED)) {
memory = resource.getMemory();
}
if (conf.getBoolean(
YarnConfiguration.NM_WINDOWS_CONTAINER_CPU_LIMIT_ENABLED,
YarnConfiguration.DEFAULT_NM_WINDOWS_CONTAINER_CPU_LIMIT_ENABLED)) {
int containerVCores = resource.getVirtualCores();
int nodeVCores = conf.getInt(YarnConfiguration.NM_VCORES,
YarnConfiguration.DEFAULT_NM_VCORES);
// cap overall usage to the number of cores allocated to YARN
int nodeCpuPercentage = Math
.min(
conf.getInt(
YarnConfiguration.NM_RESOURCE_PERCENTAGE_PHYSICAL_CPU_LIMIT,
YarnConfiguration.DEFAULT_NM_RESOURCE_PERCENTAGE_PHYSICAL_CPU_LIMIT),
100);
nodeCpuPercentage = Math.max(0, nodeCpuPercentage);
if (nodeCpuPercentage == 0) {
String message = "Illegal value for "
+ YarnConfiguration.NM_RESOURCE_PERCENTAGE_PHYSICAL_CPU_LIMIT
+ ". Value cannot be less than or equal to 0.";
throw new IllegalArgumentException(message);
}
float yarnVCores = (nodeCpuPercentage * nodeVCores) / 100.0f;
// CPU should be set to a percentage * 100, e.g. 20% cpu rate limit
// should be set as 20 * 100. The following setting is equal to:
// 100 * (100 * (vcores / Total # of cores allocated to YARN))
cpuRate = Math.min(10000,
(int) ((containerVCores * 10000) / yarnVCores));
}
}
return new String[] { Shell.WINUTILS, "task", "create", "-m",
String.valueOf(memory), "-c", String.valueOf(cpuRate), groupId,
"cmd /c " + command };
} else {
List<String> retCommand = new ArrayList<String>();
if (containerSchedPriorityIsSet) {
retCommand.addAll(Arrays.asList("nice", "-n",
Integer.toString(containerSchedPriorityAdjustment)));
}
retCommand.addAll(Arrays.asList("bash", command));
return retCommand.toArray(new String[retCommand.size()]);
}
}
}
Run Code Online (Sandbox Code Playgroud)
对于Windows(它利用winutils.exe),它使用CPU速率对于Linux,它使用niceness作为参数来控制CPU优先级