{"record":{"id":"66c509a5401993bb","repo":"apache/pulsar","slug":"invalid-number-of-bundles-number-of-bundles-has-t-66c509","errorCode":null,"errorMessage":"Invalid number of bundles. Number of bundles has to be in the range of (0, 2^32].","messagePattern":"Invalid number of bundles\\. Number of bundles has to be in the range of \\(0, 2\\^32\\]\\.","errorType":"http","errorClass":"RestException","httpStatus":400,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PoliciesUtil.java","lineNumber":54,"sourceCode":"        boundaries.add(LAST_BOUNDARY);\n        return BundlesData.builder()\n                .numBundles(1)\n                .boundaries(boundaries)\n                .build();\n    }\n\n    public static void setStorageQuota(Policies polices, BacklogQuota quota) {\n        if (polices == null) {\n            return;\n        }\n        polices.backlog_quota_map.put(BacklogQuota.BacklogQuotaType.destination_storage, quota);\n    }\n\n    private static final long MAX_BUNDLES = ((long) 1) << 32;\n\n    public static BundlesData getBundles(int numBundles) {\n        if (numBundles <= 0) {\n            throw new RestException(Response.Status.BAD_REQUEST,\n                    \"Invalid number of bundles. Number of bundles has to be in the range of (0, 2^32].\");\n        }\n        Long maxVal = MAX_BUNDLES;\n        Long segSize = maxVal / numBundles;\n        List<String> partitions = new ArrayList<>();\n        partitions.add(String.format(\"0x%08x\", 0L));\n        Long curPartition = segSize;\n        for (int i = 0; i < numBundles; i++) {\n            if (i != numBundles - 1) {\n                partitions.add(String.format(\"0x%08x\", curPartition));\n            } else {\n                partitions.add(String.format(\"0x%08x\", maxVal - 1));\n            }\n            curPartition += segSize;\n        }\n        return BundlesData.builder()\n                .boundaries(partitions)\n                .numBundles(numBundles)","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/policies/data/PoliciesUtil.java#L36-L72","documentation":"PoliciesUtil.getBundles(numBundles) computes the namespace bundle boundaries by dividing the full 32-bit hash range by numBundles. A count of zero or negative makes that division meaningless (and would divide by zero), so the method rejects it with a 400-style RestException before any bundle data is built.","triggerScenarios":"Any admin API path that sets bundle count for a namespace (e.g. setNamespaceBundlesNum, namespaces.createNamespace with bundles) with numBundles <= 0, or with a value that overflowed int.","commonSituations":"Passing an unset/0 default from tooling or scripts, parsing the bundles count from config where the key is missing and defaults to 0, or integer overflow when users specify > 2^31-1 bundles.","solutions":["Pass a positive bundle count (1 is the minimum) to the namespace creation/update API","Fix the calling script/config so the bundles value is not defaulted to 0","Clamp or validate the value client-side before calling the admin API","If you need more than 2^31 bundles, restructure the namespace instead"],"exampleFix":"// before\nadmin.namespaces().createNamespace(namespace, 0); // throws\n// after\nint numBundles = Math.max(1, configuredBundles);\nadmin.namespaces().createNamespace(namespace, numBundles);","handlingStrategy":"validation","validationCode":"// Validate before calling any bundles API\nif (numBundles <= 0 || numBundles > (1L << 32)) {\n    throw new IllegalArgumentException(\"numBundles must be in (0, 2^32], got \" + numBundles);\n}","typeGuard":"static boolean isValidBundleCount(Integer n) {\n    return n != null && n > 0 && n <= (1L << 32);\n}","tryCatchPattern":"try {\n    admin.namespaces().createNamespace(ns, numBundles);\n} catch (PulsarAdminException e) {\n    if (e.getStatusCode() == 400) {\n        log.warn(\"Invalid bundle count {} — falling back to default\", numBundles);\n        admin.namespaces().createNamespace(ns, 4);\n    } else throw e;\n}","preventionTips":["Never let bundle count default to 0; initialize config keys explicitly","Validate numeric inputs from scripts/UI before admin calls","Remember the practical limit is 2^32; typical values are small (e.g. 4–256)","Clamp values read from external config with Math.max(1, v)"],"tags":["validation","namespaces","bundles"],"backgroundTag":"invalid-parameter-range","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}