{"record":{"id":"e0d8fd1950c48786","repo":"apache/flink","slug":"tasknumber-numtasks","errorCode":null,"errorMessage":"TaskNumber: {}, numTasks: {}","messagePattern":"TaskNumber: (.+?), numTasks: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-core/src/main/java/org/apache/flink/api/common/io/FileOutputFormat.java","lineNumber":188,"sourceCode":"        }\n\n        // check if have not been set and use the defaults in that case\n        if (this.writeMode == null) {\n            this.writeMode = DEFAULT_WRITE_MODE;\n        }\n\n        if (this.outputDirectoryMode == null) {\n            this.outputDirectoryMode = DEFAULT_OUTPUT_DIRECTORY_MODE;\n        }\n    }\n\n    @Override\n    public void open(InitializationContext context) throws IOException {\n        int numTasks = context.getNumTasks();\n        int taskNumber = context.getTaskNumber();\n\n        if (taskNumber < 0 || numTasks < 1) {\n            throw new IllegalArgumentException(\n                    \"TaskNumber: \" + taskNumber + \", numTasks: \" + numTasks);\n        }\n\n        if (LOG.isDebugEnabled()) {\n            LOG.debug(\n                    \"Opening stream for output (\"\n                            + (taskNumber + 1)\n                            + \"/\"\n                            + numTasks\n                            + \"). WriteMode=\"\n                            + writeMode\n                            + \", OutputDirectoryMode=\"\n                            + outputDirectoryMode);\n        }\n\n        Path p = this.outputFilePath;\n        if (p == null) {\n            throw new IOException(\"The file path is null.\");","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-core/src/main/java/org/apache/flink/api/common/io/FileOutputFormat.java#L170-L206","documentation":"Thrown by FileOutputFormat.open(InitializationContext) when the context reports an invalid parallel execution layout: taskNumber < 0 or numTasks < 1. These values come from the runtime's InitializationContext and describe this operator's position within the parallel subtask set; they must be internally consistent.","triggerScenarios":"The InitializationContext supplied to open has a negative taskNumber or a numTasks of 0; a custom ExecutionEnvironment or test harness that builds an InitializationContext with bad values; a framework bug or edge case where parallelism resolves to 0 for the sink.","commonSituations":"Unit tests that construct an InitializationContext manually and pass 0 for numTasks; a misconfigured local execution with parallelism 0; embedding Flink in a custom runtime that fabricates the context.","solutions":["Ensure the sink runs with parallelism >= 1 (env.setParallelism(n>=1)).","In tests, build the InitializationContext with valid taskNumber in [0, numTasks) and numTasks >= 1.","If using a custom ExecutionEnvironment, verify it emits a correct subtask context."],"exampleFix":"// before (test harness)\nInitializationContext ctx = new InitializationContext() {\n  public int getTaskNumber() { return 0; }\n  public int getNumTasks() { return 0; } // invalid\n};\nformat.open(ctx);\n\n// after\nInitializationContext ctx = ...getTaskNumber()==0, getNumTasks()==1;\nformat.open(ctx);","handlingStrategy":"validation","validationCode":"// Ensure parallelism >= 1 for the sink\nint p = env.getParallelism();\nif (p < 1) {\n    throw new IllegalStateException(\"Sink parallelism must be >= 1, got \" + p);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never set sink/env parallelism to 0.","In tests, build InitializationContext with valid taskNumber in [0,numTasks) and numTasks >= 1.","Validate the context in a custom ExecutionEnvironment before open."],"tags":["flink","file-output","parallelism","validation","testing"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}