Skip to content

[SERVER] Add Kubernetes engine Pod discovery latency metric - #7722

Closed
ruanwenjun wants to merge 5 commits into
apache:masterfrom
ruanwenjun:engine-pod-discovery-latency
Closed

ruanwenjun wants to merge 5 commits into
apache:masterfrom
ruanwenjun:engine-pod-discovery-latency

Conversation

@ruanwenjun

@ruanwenjun ruanwenjun commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Why are the changes needed?

A Spark engine may not be detected by the Kyuubi server until some time after its Pod is created. This delay can contribute to an engine submission timeout, but the existing engine startup metric only covers engines that register successfully.

Add kyuubi.engine.kubernetes.pod.discovery.latency, a histogram in milliseconds from Pod creation to entry into the informer ADD callback, to help tune kyuubi.engine.kubernetes.submit.timeout. Exclude Pods created before each event handler was created so historical Pods do not inflate the metric. Informer startup behavior is unchanged.

How was this patch tested?

All 10 tests in KubernetesApplicationOperationSuite passed with JDK 17, without a Kubernetes cluster. The same compiled suite also passed on JDK 8, including timestamp parsing with a numeric timezone offset. Removing the creation-time filter causes the old-Pod regression test to fail.

Tests cover ADD latency, repeated ADD events, exclusion of older Pods and UPDATE/DELETE events, invalid timestamps, continued terminal-state handling, non-engine Pods, and disabled metrics. Reporters are disabled in metric tests to avoid binding a fixed port.

dev/reformat
build/mvn --batch-mode --no-transfer-progress test scalastyle:check spotless:check -pl kyuubi-server -am -Dtest=none -DwildcardSuites=org.apache.kyuubi.engine.KubernetesApplicationOperationSuite -Pflink-provided,hive-provided,spark-provided
git diff --check

Was this patch assisted by generative AI tooling?

Assisted-by: Codex (GPT-6)

@ruanwenjun

Copy link
Copy Markdown
Member Author

@pan3793 PTAL

@ruanwenjun

Copy link
Copy Markdown
Member Author

@wangzhigang1999 ping PTAL.

Comment on lines +366 to +376
val observedAt = System.currentTimeMillis()
if (isSparkEnginePod(pod)) {
Option(pod.getMetadata.getCreationTimestamp) match {
case Some(creationTimestamp) =>
try {
val latency = observedAt - Instant.parse(creationTimestamp).toEpochMilli
if (latency >= 0) {
MetricsSystem.tracing(_.updateHistogram(
ENGINE_KUBERNETES_POD_DISCOVERY_LATENCY,
latency))
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inform(...) performs an initial LIST on startup (and on lazy (context, namespace) client initialization) and delivers every pre-existing engine Pod to onAdd. Those Pods can be hours or days old, so observedAt - creationTimestamp records Pod age rather than discovery latency. With Dropwizard's ExponentiallyDecayingReservoir (size 1028), those outliers stay in max and skew high percentiles until enough new Pods arrive, which makes the metric misleading for tuning kyuubi.engine.kubernetes.submit.timeout.

Please exclude initial-list Pods from this metric (for example, enable latency recording only after inform(...) finishes its blocking initial sync, or skip Pods whose creationTimestamp predates informer/handler creation). Note that informer.hasSynced() is not safe for this in Fabric8 6.14.0 because lastSyncResourceVersion is set before queued initial-list onAdd callbacks finish executing.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metric now excludes Pods created before each event handler was created, covering lazy client initialization without changing informer startup behavior. Added a regression test to verify that excluded Pods still update application state.

override def onAdd(pod: Pod): Unit = {
val observedAt = System.currentTimeMillis()
if (isSparkEnginePod(pod)) {
Option(pod.getMetadata.getCreationTimestamp) match {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move the timestamp lookup, parsing, and warnings inside MetricsSystem.tracing { ms => ... } so we avoid unnecessary parsing and warning logs when kyuubi.metrics.enabled=false (or in unit tests that do not start MetricsSystem).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved the Pod timestamp lookup, parsing, and warnings inside MetricsSystem.tracing. Added a test confirming that disabled metrics do not produce timestamp warnings.

Comment on lines +371 to +384
val latency = observedAt - Instant.parse(creationTimestamp).toEpochMilli
if (latency >= 0) {
MetricsSystem.tracing(_.updateHistogram(
ENGINE_KUBERNETES_POD_DISCOVERY_LATENCY,
latency))
}
} catch {
case e: DateTimeParseException =>
warn(s"Invalid creation timestamp for engine pod ${pod.getMetadata.getName}", e)
}
case None =>
warn(s"[$kubernetesInfo] Missing creation timestamp for engine pod " +
s"${pod.getMetadata.getNamespace}/${pod.getMetadata.getName}, " +
"skipping pod discovery latency metric")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two small nits here:

  1. Use OffsetDateTime.parse(creationTimestamp).toInstant.toEpochMilli instead of Instant.parse, since Instant.parse rejects RFC 3339 numeric offsets on JDK 8.
  2. Align the two warn messages to include [$kubernetesInfo] and namespace/name consistently, and handle Option(pod.getMetadata.getNamespace) so an unset namespace does not log as null/....

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to OffsetDateTime.parse(...).toInstant and verified numeric timezone offsets on JDK 8. Both warnings now include kubernetesInfo and the Pod name, with the namespace included when available.

Comment on lines +36 to +39
val conf = KyuubiConf()
operation.initialize(conf, None)
metricsSystem.initialize(conf)
metricsSystem.start()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please set .set(MetricsConf.METRICS_REPORTERS, Set.empty[String]) in both new tests (matching KyuubiOperationSuite). With default KyuubiConf(), MetricsSystem starts PrometheusReporterService on fixed port 10019, which can fail with BindException when port 10019 is occupied.

Also move operation.initialize, metricsSystem.initialize, and metricsSystem.start inside try ... finally (or into beforeEach/afterEach) so a failure during startup does not skip operation.stop() and leak executors.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Disabled reporters in the metric tests and moved initialization and startup inside try/finally, ensuring cleanup also runs when startup fails.

Comment thread docs/monitor/metrics.md Outdated
| `kyuubi.operation.exec_time` | `${operationType}` | histogram | 1.7.0 | <div style='width: 150pt;word-wrap: break-word;white-space: normal'> execution time histogram for the operation `${operationType}`, now only `ExecuteStatement` is enabled. </div> |
| `kyuubi.engine.total` | | counter | 1.2.0 | <div style='width: 150pt;word-wrap: break-word;white-space: normal'> cumulative created engines</div> |
| `kyuubi.engine.startup.time` | | histogram | 1.12.0 | <div style='width: 150pt;word-wrap: break-word;white-space: normal'> startup time histogram for successfully created engines, from submitting a new engine to the engine being registered and discovered by Kyuubi server </div> |
| `kyuubi.engine.kubernetes.pod.discovery.latency` | | histogram | 1.13.0 | Time in milliseconds from Spark engine Pod creation to the informer ADD callback. Recorded for each ADD event, including existing Pods discovered during initial listing. UPDATE and DELETE events do not contribute samples. |

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please wrap the description in <div style='width: 150pt;word-wrap: break-word;white-space: normal'> ... </div> to match all other rows in this table (and update the wording if initial-list Pods are excluded).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped the description in the same <div> style as the other rows and clarified that Pods created before the event handler was created are excluded.

@pan3793 pan3793 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with one non-blocking test nit.

val deletedPod = new PodBuilder()
.withNewMetadata()
.withName("deleted-driver")
.withCreationTimestamp(Instant.now().minusSeconds(180).toString)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking nit: Instant.now().minusSeconds(180) predates handlerCreatedAt, so the non-engine Pod onAdd check below would pass due to the creation-time filter even without isSparkEnginePod. Using Instant.now().toString here would exercise isSparkEnginePod directly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@pan3793 pan3793 added this to the v1.13.0 milestone Nov 27, 2026
@pan3793 pan3793 closed this in 083bf23 Nov 27, 2026
@pan3793

pan3793 commented Nov 27, 2026

Copy link
Copy Markdown
Member

Merge Summary:

Posted by merge_kyuubi_pr.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants