{"record":{"id":"381b4281f03017ab","repo":"apache/cassandra","slug":"error-can-not-sort-by-token-when-there-is-not-tok","errorCode":null,"errorMessage":"Error: Can not sort by token when there is not token per node.","messagePattern":"Error: Can not sort by token when there is not token per node\\.","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/tools/nodetool/Status.java","lineNumber":149,"sourceCode":"        SortedMap<String, SetHostStatWithPort> dcs = CommandUtils.getOwnershipByDcWithPort(probe, resolveIp, tokensToEndpoints, ownerships);\n\n        int nodesOfTokens = tokensToEndpoints.values().size();\n\n        // More tokens than nodes (aka vnodes)?\n        if (hostIDMap.size() < nodesOfTokens)\n            isTokenPerNode = false;\n\n        if (sortBy == null)\n        {\n            if (isTokenPerNode)\n                sortBy = SortBy.token;\n            else\n                sortBy = SortBy.id;\n        }\n        else if (!isTokenPerNode && sortBy == SortBy.token)\n        {\n            errOut.printf(\"%nError: Can not sort by token when there is not token per node.%n\");\n            throw new IllegalArgumentException(\"Error: Can not sort by token when there is not token per node.\");\n        }\n        else if (!resolveIp && sortBy == SortBy.host)\n        {\n            errOut.printf(\"%nError: Can not sort by host when there is not -r/--resolve-ip flag used.%n\");\n            throw new IllegalArgumentException(\"Error: Can not sort by host when there is not -r/--resolve-ip flag used.\");\n        }\n\n        // Datacenters\n        for (Map.Entry<String, SetHostStatWithPort> dc : dcs.entrySet())\n        {\n            TableBuilder tableBuilder = sharedTable.next();\n            addNodesHeader(hasEffectiveOwns, tableBuilder);\n\n            ArrayListMultimap<InetAddressAndPort, HostStatWithPort> hostToTokens = ArrayListMultimap.create();\n            for (HostStatWithPort stat : dc.getValue())\n                hostToTokens.put(stat.endpointWithPort, stat);\n\n            Map<String, List<Object>> data = new HashMap<>();","sourceCodeStart":131,"sourceCodeEnd":167,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/tools/nodetool/Status.java#L131-L167","documentation":"nodetool status supports sorting by token only when the cluster has exactly one token per node (vnodes disabled). With virtual nodes a node owns many tokens, so token ordering is undefined; Status.execute() prints the message to stderr and throws IllegalArgumentException.","triggerScenarios":"Running `nodetool status -t token` (or `nodetool status <keyspace> -t`) on a cluster configured with num_tokens > 1.","commonSituations":"Running the sort-by-token option against modern default configurations where num_tokens defaults to 16, i.e. multi-token nodes.","solutions":["Drop the -t/--sort token option and sort by another field (id, address, load, own).","Set num_tokens=1 per node if single-token-per-node is truly required (requires migration planning).","Use a tool that can represent vnodes if token-ordered output is essential."],"exampleFix":"// before\nnodetool status -t\n// after\nnodetool status -l","handlingStrategy":"validation","validationCode":"// Pre-check before requesting token sort\nif (numTokensPerNode > 1 && requestedSort == SortBy.token) throw new IllegalArgumentException('Cannot sort by token with vnodes');","typeGuard":null,"tryCatchPattern":"try { runStatus(args); } catch (IllegalArgumentException e) { if (e.getMessage().contains(\"Can not sort by token\")) { retryWithoutTokenSort(); } else throw e; }","preventionTips":["Only script -t token sort on num_tokens=1 clusters","Fall back to sorting by id or load on vnode clusters"],"tags":["nodetool","status","vnodes"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}