eskabetxe opened a new pull request, #30153:
URL: https://github.com/apache/flink/pull/30153

   ## What is the purpose of the change
   
   `kubernetes.{jobmanager,taskmanager}.cpu.limit-factor` raises the container 
CPU limit above
   the CPU request to allow bursting. The container-aware JVM derives
   `Runtime.availableProcessors()` from the cgroup CPU quota, i.e. from the 
*inflated* limit,
   while the memory budget (including `MaxDirectMemorySize`) stays sized for 
the *request*.
   GC/JIT threads, `ForkJoinPool.commonPool()`, Flink thread pools and Netty 
allocators sized
   from the processor count therefore grow against an unchanged memory ceiling 
and can fail with
   `OutOfMemoryError: Direct buffer memory`.
   
   This change appends `-XX:ActiveProcessorCount=<CPU request, rounded up>` to 
the `JVM_ARGS`
   environment variable of the JobManager/TaskManager container, keeping the 
JVM sized
   consistently with the memory model while the CPU limit still allows bursting.
   
   It ports the operator-side fix from apache/flink-kubernetes-operator#1220 
into
   `flink-kubernetes`, so native Kubernetes deployments get it without the 
operator. The memory
   side of the same problem is addressed separately by
   `jobmanager.memory.enable-jvm-direct-memory-limit`; this PR covers the 
processor-count side.
   
   ## Brief change log
   
     - Add `@Internal ActiveProcessorCountLimiter` (with a `JvmComponent` enum) 
and wire it into
       `CmdJobManagerDecorator` / `CmdTaskManagerDecorator`, after the start 
command is set.
     - Add 
`kubernetes.{jobmanager,taskmanager}.cpu.limit-active-processor-count.enabled`
       (`@PublicEvolving`, default `true`) as the per-component escape hatch.
     - The flag goes into `JVM_ARGS` because that variable precedes all options 
derived from the
       configuration on the `java` command line (`flink-console.sh`:
       `exec java $JVM_ARGS ${FLINK_ENV_JAVA_OPTS} …`), so an explicit user 
setting still wins.
     - Do not inject when the flag is already effective via `env.java.opts.*`,
       `env.java.default-opts.*`, `FLINK_ENV_JAVA_OPTS*`, `JVM_ARGS`, 
`JAVA_TOOL_OPTIONS`,
       `JDK_JAVA_OPTIONS` or `_JAVA_OPTIONS`. `env.java.*` options are ignored 
when the matching
       `FLINK_ENV_JAVA_OPTS*` env var is set, mirroring `config.sh`.
     - Skip insertion, with the reason at INFO, when the container uses 
`envFrom` or declares
       `JVM_ARGS`, `JAVA_TOOL_OPTIONS` or `JDK_JAVA_OPTIONS` via `valueFrom`: 
those values cannot
       be inspected at pod creation time and the injected flag would override 
or shadow them.
     - Detect the flag as a standalone JVM argument, so a value merely 
mentioning it (e.g.
       `-Dfoo=-XX:ActiveProcessorCount=4`) does not disable the limit.
     - Resolve duplicate env vars with Kubernetes semantics (last entry wins), 
for both detection
       and appending.
     - Document the behaviour in a new *CPU Limit Factor* section of the native 
Kubernetes page
       (English and Chinese), update the pod-template `env` row, and regenerate 
the config
       reference.
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   
     - New `ActiveProcessorCountLimiterTest` covers: rounding (`ceil`) for 
fractional requests,
       factor boundaries, invalid CPU requests, the enable option, every 
watched config option and
       env var (for both components, from an explicit per-component table 
rather than the enum
       itself), the `config.sh` override narrowing, duplicate-env last-wins, 
`envFrom`/`valueFrom`,
       idempotency, empty `JVM_ARGS`, and that the input `Container` is never 
mutated.
     - Extended `CmdJobManagerDecoratorTest` / `CmdTaskManagerDecoratorTest` 
for end-to-end wiring,
       the disable option, cross-component isolation, and user-configured 
precedence.
     - Extended `KubernetesJobManagerFactoryTest` / 
`KubernetesTaskManagerFactoryTest` for the
       resulting pod env.
     - Each guard was mutation-checked: reverting it (dropping a watched env 
var, swapping the
       jobmanager/taskmanager mapping, first-vs-last duplicate, removing the 
`config.sh`
       narrowing, substring instead of token matching) makes at least one test 
fail.
     - `flink-kubernetes` module: 401 tests pass; `spotless:check` and 
`checkstyle:check` are
       clean; `flink-docs verify` passes.
   
   **Not covered:** there is no live-cluster integration test in this PR. 
Manual verification on
   `kind`/`minikube` with `limit-factor: 2` (checking `availableProcessors()` 
in the TM and that a
   `valueFrom` `JVM_ARGS` is untouched) is still recommended before merge.
   
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): **no**
     - The public API, i.e., is any changed class annotated with 
`@Public(Evolving)`: **yes**
       (two new config options in `@PublicEvolving KubernetesConfigOptions`; no 
change to existing
       API)
     - The serializers: **no**
     - The runtime per-record code paths (performance sensitive): **no**
     - Anything that affects deployment or recovery: JobManager (and its 
components),
       Checkpointing, Kubernetes/Yarn, ZooKeeper: **yes** (Kubernetes pod 
construction; no effect
       at the default `limit-factor = 1.0`)
     - The S3 file system connector: **no**
   
   ## Documentation
   
     - Does this pull request introduce a new feature? **yes**
     - If yes, how is the feature documented? **docs** (new *CPU Limit Factor* 
section in the
       native Kubernetes page, English and Chinese; the config options are 
covered by the
       generated configuration reference)
   
   ## Reviewer notes / trade-offs
   
     - **Default `true`.** The feature is inert at the default `limit-factor = 
1.0`; only users
       who already opted into CPU bursting are affected, and they are exactly 
the users hitting
       the OOM. Default-off would leave the problem in place. The release note 
should still call
       out that `availableProcessors()` drops to the request (a throughput, not 
only memory,
       change).
     - **Image-provided state is not inspectable.** When the flag is added, the 
pod-spec
       `JVM_ARGS` replaces an `ENV JVM_ARGS=…` from the image; likewise a
       `JAVA_TOOL_OPTIONS`/`JDK_JAVA_OPTIONS` baked into the image or injected 
by an admission
       webhook, and JVM argument files (`@file`), are invisible here. 
Documented, with
       `env.java.opts.*` as the mitigation.
     - **`envFrom` disables the feature conservatively.** Whether an `envFrom` 
source defines
       `JVM_ARGS` cannot be known without reading the ConfigMap/Secret, which 
the client should
       not do. The skip is logged with the reason and the kill switch.
     - **`_JAVA_OPTIONS` is not treated as overridden** because the JVM applies 
it *after* the
       command line, so a user value there already wins; it is only inspected 
to avoid a
       misleading "limiting …" log line.
     - **Operator native mode is affected too.** The Flink Kubernetes Operator 
derives
       `kubernetes.*.cpu.limit-factor` from the pod `limits`/`requests`, so 
Operator deployments
       in native mode get this automatically; standalone mode is unaffected. 
Verified against the
       operator's `FlinkConfigBuilder`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to