{"record":{"id":"b99d9c07c1b89c75","repo":"apache/hadoop","slug":"percentage-percentage-must-be-greater-than","errorCode":null,"errorMessage":"Percentage \" + percentage + \" must be greater than or equal to 0  and less than or equal to 100","messagePattern":"Percentage \" \\+ percentage \\+ \" must be greater than or equal to 0  and less than or equal to 100","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java","lineNumber":383,"sourceCode":"   * Let t = percentage of max memory.\n   * Let e = round(log_2 t).\n   * Then, we choose capacity = 2^e/(size of reference),\n   * unless it is outside the close interval [1, 2^30].\n   *\n   * @param mapName mapName.\n   * @param percentage percentage.\n   * @return compute capacity.\n   */\n  public static int computeCapacity(double percentage, String mapName) {\n    return computeCapacity(Runtime.getRuntime().maxMemory(), percentage,\n        mapName);\n  }\n  \n  @VisibleForTesting\n  static int computeCapacity(long maxMemory, double percentage,\n      String mapName) {\n    if (percentage > 100.0 || percentage < 0.0) {\n      throw new HadoopIllegalArgumentException(\"Percentage \" + percentage\n          + \" must be greater than or equal to 0 \"\n          + \" and less than or equal to 100\");\n    }\n    if (maxMemory < 0) {\n      throw new HadoopIllegalArgumentException(\"Memory \" + maxMemory\n          + \" must be greater than or equal to 0\");\n    }\n    if (percentage == 0.0 || maxMemory == 0) {\n      return 0;\n    }\n    //VM detection\n    //See http://java.sun.com/docs/hotspot/HotSpotFAQ.html#64bit_detection\n    final String vmBit = System.getProperty(\"sun.arch.data.model\");\n\n    //Percentage of max memory\n    final double percentDivisor = 100.0/percentage;\n    final double percentMemory = maxMemory/percentDivisor;\n    ","sourceCodeStart":365,"sourceCodeEnd":401,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/LightWeightGSet.java#L365-L401","documentation":"LightWeightGSet.computeCapacity derives a hash-table capacity as a percentage of maxMemory and validates the input: a percentage outside [0.0, 100.0] is rejected with HadoopIllegalArgumentException printing the value and the allowed range. This is the same sizing path Hadoop services use for their GSet-backed maps.","triggerScenarios":"Passing a negative or greater-than-100 value - for example a constant typo (0.5 where 50 was meant, since the unit is percent, not a fraction), or Double.parseDouble on a config string like \"25%\" without stripping the '%' sign.","commonSituations":"Custom code copying Hadoop's sizing pattern; config keys feeding memory percentages edited to invalid values; unit tests probing boundary values.","solutions":["Validate or clamp the percentage to [0, 100] before calling computeCapacity.","When parsing percent strings from config, strip '%' and trim before parseDouble.","Use 0.0 for 'cache disabled' - it is legal and yields capacity 0 - instead of negative sentinel values."],"exampleFix":"// before\ndouble pct = Double.parseDouble(conf.get(\"cache.memory.percent\")); // \"25%\" -> 25? no: throws later\ncapacity = LightWeightGSet.computeCapacity(pct, \"cache\");\n\n// after\ndouble pct = Double.parseDouble(\n    conf.get(\"cache.memory.percent\").replace(\"%\", \"\").trim());\npct = Math.max(0.0, Math.min(100.0, pct));\ncapacity = LightWeightGSet.computeCapacity(pct, \"cache\");","handlingStrategy":"validation","validationCode":"double pct = Math.max(0.0, Math.min(100.0, rawPercent));\nint capacity = LightWeightGSet.computeCapacity(pct, mapName);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat percentages from config as untrusted input: strip units, then clamp.","Remember the unit is percent (0-100), not a fraction (0-1)."],"tags":["collections","configuration","validation","hadoop-common"],"backgroundTag":"invalid-percentage-value","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}