+
+{merge} = require 'mecano/lib/misc'
 
 memory = (ctx) ->
   {yarn, hadoop_conf_dir} = ctx.config.hdp

      'maxNumberOfcontainers': maxNumberOfcontainers
      'memoryPerContainer': memoryPerContainer
      'virtualMemoryRatio': virtualMemoryRatio
    'yarn_site':
      'yarn.nodemanager.resource.memory-mb': "#{maxNumberOfcontainers * memoryPerContainer}" # containers * RAM-per-container
      'yarn.scheduler.minimum-allocation-mb': "#{memoryPerContainer}" # RAM-per-container
      'yarn.scheduler.maximum-allocation-mb': "#{maxNumberOfcontainers * memoryPerContainer}" # containers * RAM-per-container
      'yarn.app.mapreduce.am.resource.mb': "#{2 * memoryPerContainer}" # 2 * RAM-per-container
      'yarn.app.mapreduce.am.command-opts': "-Xmx#{Math.floor(.8 * 2 * memoryPerContainer)}m" # 0.8 * 2 * RAM-per-container
      'yarn.nodemanager.vmem-pmem-ratio': "#{virtualMemoryRatio}"
    'mapred_site':
      'mapreduce.map.memory.mb': "#{memoryPerContainer}" # RAM-per-container
      'mapreduce.reduce.memory.mb': "#{2 * memoryPerContainer}" # 2 * RAM-per-container
      'mapreduce.map.java.opts': "-Xmx#{Math.floor .8 * memoryPerContainer}m" # 0.8 * RAM-per-container
      'mapreduce.reduce.java.opts': "-Xmx#{Math.floor .8 * 2 * memoryPerContainer}m" # 0.8 * 2 * RAM-per-container
      'mapreduce.task.io.sort.mb': "#{Math.floor .4 * memoryPerContainer}"
  # REDUCE capability required is more than the supported max container capability in the cluster. Killing the Job. reduceResourceReqt: 3844 maxContainerCapability:1922
  result.mapred_site['mapreduce.reduce.memory.mb'] = Math.min result.yarn_site['yarn.scheduler.maximum-allocation-mb'], result.mapred_site['mapreduce.reduce.memory.mb']
  result

memory.reservedStack = 4:1, 8:2, 16:2, 24:4, 48:6, 64:8, 72:8, 96:12, 128:24, 256:32, 512:64

  memoryPerContainer = Math.floor Math.max mininumContainerSize, memoryAvailableMb / maxNumberOfcontainers
  # Virtual memory (physical + paged memory) upper limit for each Map and Reduce task
  virtualMemoryRatio = 2.1
  # Compute
  yarnSchedulerMaximumAllocationMb = maxNumberOfcontainers * memoryPerContainer # containers * RAM-per-container
  yarnAppMapreduceAmResourceMb = 2 * memoryPerContainer # 2 * RAM-per-container
  yarnAppMapreduceAmCommmandOpts = Math.floor(.8 * 2 * memoryPerContainer) # 0.8 * 2 * RAM-per-container
  mapreduceReduceMemoryMb = 2 * memoryPerContainer # 2 * RAM-per-container
  # Constrain
  yarnAppMapreduceAmResourceMb = Math.min yarnAppMapreduceAmResourceMb, maxNumberOfcontainers * memoryPerContainer
  yarnAppMapreduceAmCommmandOpts = Math.min yarnAppMapreduceAmCommmandOpts, maxNumberOfcontainers * memoryPerContainer
  mapreduceReduceMemoryMb = Math.min yarnSchedulerMaximumAllocationMb, mapreduceReduceMemoryMb # REDUCE capability required is more than the supported max container capability in the cluster. Killing the Job. reduceResourceReqt: 3844 maxContainerCapability:1922
  result =
    info:
      'coreNumber': coreNumber
      'diskNumber': diskNumber
      'memoryTotalMb': memoryTotalMb
