[FLINK-39546][s3] Improve observability in flink-s3-fs-native by exposing operation-level S3 metrics - #28427
[FLINK-39546][s3] Improve observability in flink-s3-fs-native by exposing operation-level S3 metrics#28427Samrat002 wants to merge 2 commits into
Conversation
gaborgsomogyi
left a comment
There was a problem hiding this comment.
Thanks for the efforts! Left some high level questions
| .defaultValues( | ||
| "api_call_count", | ||
| "api_call_duration_ms", | ||
| "throttle_count", | ||
| "retry_count", | ||
| "iops") |
There was a problem hiding this comment.
Just for my own understanding. I want to use a new metric called foo. Do I need to go to s3.metrics.allowlist definition, check the default value and then I need to set: s3.metrics.allowlist=ORIGINAL_DEFAULT,foo?
There was a problem hiding this comment.
Yes. The allowlist replaces the default list, so retaining the defaults while adding foo requires ORIGINAL_DEFAULT,foo; * enables every metric emitted by the plugin. The shared option description now states the replacement behavior explicitly.
| AwsSdkMetricBridge bridge = this.metricBridge; | ||
| if (bridge == null) { | ||
| synchronized (this) { | ||
| bridge = this.metricBridge; | ||
| if (bridge == null) { | ||
| bridge = | ||
| new AwsSdkMetricBridge( | ||
| metrics, | ||
| config.get(METRICS_ALLOWLIST), | ||
| config.get(METRICS_HISTOGRAM_WINDOW_SIZE)); | ||
| this.metricBridge = bridge; | ||
| } | ||
| } | ||
| } | ||
| return bridge; |
There was a problem hiding this comment.
metricBridge write is inside the the sync block but read is outside + not transient. I've a feeling that we have random read here without super well founded consideration. Maybe some guardedby would be good as always. Not if that fits for synchronized but end-to-end what protects what and when would be good.
There was a problem hiding this comment.
Good catch. I removed the lock-free double-checked path in 47babe4. setMetricGroup(...) and bridge resolution now synchronize on the same monitor, and pluginMetrics, attachedMetricGroup, and metricBridge are all @GuardedBy("this"). transient is unnecessary because the factory is not serializable.
| metricRegistry = createMetricRegistry(configuration, pluginManager, rpcSystem); | ||
|
|
||
| final RpcService metricQueryServiceRpcService = | ||
| MetricUtils.startRemoteMetricsRpcService( | ||
| configuration, | ||
| commonRpcService.getAddress(), | ||
| configuration.get(JobManagerOptions.BIND_HOST), | ||
| rpcSystem); | ||
| metricRegistry.startQueryService(metricQueryServiceRpcService, null); | ||
|
|
||
| final String hostname = RpcUtils.getHostname(commonRpcService); | ||
|
|
||
| processMetricGroup = | ||
| MetricUtils.instantiateProcessMetricGroup( | ||
| metricRegistry, | ||
| hostname, | ||
| ConfigurationUtils.getSystemResourceMetricsProbingInterval( | ||
| configuration)); | ||
|
|
||
| // Second-phase init for file system plugins that opt into metrics (e.g. | ||
| // flink-s3-fs-native): hand them the process-level metric group before any file system | ||
| // is used. This must run ahead of the HA services and BlobServer below, because those | ||
| // may open external file systems (e.g. S3 HA/blob storage), creating them first would | ||
| // cache metric-less file system clients for the rest of the process lifetime. See | ||
| // FileSystem#attachMetrics and MetricsAware. |
There was a problem hiding this comment.
Not yet, checked in-depth. Is this just a move as-is or changed too?
There was a problem hiding this comment.
The metric-registry, query-service, and process-group block is moved as-is. The functional addition is FileSystem.attachMetrics(processMetricGroup) immediately after the process group exists, so filesystem clients created by later startup services receive the metric scope.
| // Second-phase init for file system plugins that opt into metrics (e.g. | ||
| // flink-s3-fs-native): hand them the process-level metric group now that the | ||
| // MetricRegistry exists. See FileSystem#attachMetrics and MetricsAware. | ||
| FileSystem.attachMetrics(taskManagerMetricGroup.f0); |
There was a problem hiding this comment.
Now we're solving that for filesystems which is good. How do we think that a non-FS plugin can be used in metrics system. We don't need a full featuring plan but knowing this is a good measure how this solution fits here.
There was a problem hiding this comment.
MetricsAware is intentionally only the opt-in contract; it does not add global plugin discovery. A non-filesystem plugin family would use the same pattern by forwarding setMetricGroup(...) through its wrappers and invoking it at the runtime point where that family already owns an appropriate metric group. The filesystem attachment is the first concrete integration of that pattern.
Izeren
left a comment
There was a problem hiding this comment.
Hi @Samrat002, thank you for the change. I have left clarification questions.
Overall, it looks good, but I would like you to extract more cloud agnostic code from native FS. This way, the shims we will need to do to repeat this in Azure, GCS will be really thin. Another concern I have is regrading duplicating non-trivial code to avoid dependency footprint. It seems to me that would be worth extracting lightweight dependency rather than duplicating the code.
| // Second-phase init for file system plugins that opt into metrics (e.g. | ||
| // flink-s3-fs-native): hand them the process-level metric group now that the | ||
| // MetricRegistry exists. See FileSystem#attachMetrics and MetricsAware. | ||
| FileSystem.attachMetrics(taskManagerMetricGroup.f0); |
ff9f59f to
f027133
Compare
Izeren
left a comment
There was a problem hiding this comment.
Hi @Samrat002, I went through some of it today, and I can see that there are many comments that I have left before that were not replied. Could you please provide replies in threads before I take a second pass.
f027133 to
7bc4048
Compare
288c21d to
540295f
Compare
Izeren
left a comment
There was a problem hiding this comment.
Thank you @Samrat002, high level LGTM. But I would like some brief explanation regarding backwards compatibility of the change:
-import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
+import org.apache.flink.metrics.SlidingWindowHistogram;
My original suggestion was rather "move" DescriptiveStatistics to metrics-core and make both runtime-core and native-fs import it from metrics-core (which is supposedly lighter dependency to take).
In current implementation, there is a behavioural change of DescriptiveStatisticsHistogram + we still bring in custom implementation which is otherwise unjustified.
|
Thanks @Izeren . I've decoupled this. DescriptiveStatisticsHistogram is now reverted to its original commons-math3 implementation, so there's no behavioural or backwards-compat change to the runtime histogram. On reusing it from metrics-core, the blocker is that DescriptiveStatisticsHistogramStatistics pulls in commons-math3 (Percentile, StandardDeviation, …). Moving it to flink-metrics-core would force commons-math3 onto the leanest, most widely-depended module (every reporter/connector), which felt like a worse trade than a small dependency-free histogram. And since the S3 plugin only depends on flink-runtime in test scope, its main code can't reference DescriptiveStatisticsHistogram anyway. I'd propose doing that as its own JIRA with proper percentile-compat testing rather than as a side effect here. WDUT? |
540295f to
998b32b
Compare
Thank you @Samrat002, I agree, lets do improvement of histogram implementation separately. This PR is big enough |
…sing operation-level S3 metrics
998b32b to
e5c8bce
Compare
|
@gaborgsomogyi PTAL whenever time |
What is the purpose of the change
flink-s3-fs-nativecurrently emits no metrics. When a job's checkpoints, savepoints, or sinks go through it, operators have no visibility into how Flink is actually talking to S3: request volume, latency, throttling, or retries, which makes diagnosing slow or failing checkpoints largely guesswork.This change makes the native S3 filesystem report operation-level S3 metrics into Flink's metric system. It does so by bridging the AWS SDK's built-in metrics SPI into Flink
Counter/Histograminstruments, so every completed S3 API call is counted, timed, and classified.More details on : FLIP-576
Brief change log
flink-core
MetricsAware(@PublicEvolving,org.apache.flink.core.plugin): aFileSystemFactoryimplements it to be handed aMetricGroup.FileSystem.attachMetrics(MetricGroup)(@Internal): creates afilesystemchild group and forwards it to every registeredMetricsAwarefactory; resilient to a misbehaving factory and idempotent.PluginFileSystemFactorynow implementsMetricsAwareand forwardssetMetricGroupto the wrapped inner factory under the plugin classloader. Without this, plugin-loaded filesystems (the normal deployment mode) would silently never receive the group.flink-runtime
ClusterEntrypoint(JobManager) andTaskManagerRunner(TaskManager) callFileSystem.attachMetrics(processMetricGroup)during startup. TheClusterEntrypointservice-init order was adjusted so this runs before HA/blob services cache filesystem clients, otherwise those early clients would be created without a metric group.flink-s3-fs-native
NativeS3FileSystemFactory/NativeS3AFileSystemFactoryimplementMetricsAwareand tag metrics with afilesystem_typelabel set to the scheme (s3vss3a), so the two stay distinguishable.AwsSdkMetricBridgeimplementssoftware.amazon.awssdk.metrics.MetricPublisherand translates eachMetricCollectioninto Flink metrics:api_call_count(labels:op,status_class),api_call_duration_ms(histogram, labelop),throttle_count(labelop),retry_count(labels:op,reason).S3MetricHistogram: a bounded sliding-window histogram backing the duration metric.S3ClientProviderregisters the publisher on the sync/async clients.s3.metrics.enabled(off by default),s3.metrics.allowlist,s3.metrics.histogram.window-size.Verifying this change
This change added tests and can be verified as follows.
Automated tests
AwsSdkMetricBridgeTest— translation of SDK records to Flink metrics;status_classclassification (2xx/4xx/5xx/throttled); retry attribution; allowlist behavior (explicit list,*wildcard, empty → defaults).S3MetricHistogramTest— sliding-window statistics.NativeS3FileSystemFactoryMetricsTest— thefilesystem_typelabel resolves tos3/s3aper factory.FileSystemAttachMetricsTest(flink-core) —attachMetricsunwrapsPluginFileSystemFactoryto reach the real factory, skips non-MetricsAwarefactories, survives a throwing factory, and is idempotent.NativeS3MetricsEmissionITCase— MinIO via Testcontainers; real GET/HEAD/LIST round trips, asserting the counters/histograms are readable back through a realMetricRegistry(MetricListener). Auto-skips without Docker.Manual end-to-end against real AWS S3
I ran a standalone cluster built from this branch with
s3.metrics.enabled: trueand the SLF4J reporter, and submitted a large-state streaming job checkpointing tos3://<bucket>/checkpoints(HashMap backend, filesystem checkpoint storage, 10 s interval). The native plugin loaded (Plugin loader ... s3-fs-native), built its client via the SDK default credential chain, and wrote real checkpoint objects to S3. The reporter then showed the metrics on both the TaskManager (data-plane writes) and the JobManager (checkpoint coordination / multipart):Does this pull request potentially affect one of the following parts:
@Public(Evolving): noDocumentation
this change introduces the feature, followup documentation is up next.
Was generative AI tooling used to co-author this PR?