aokolnychyi commented on code in PR #7447:
URL: https://github.com/apache/iceberg/pull/7447#discussion_r1220722569


##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/SparkBatchQueryScan.java:
##########
@@ -256,4 +275,56 @@ public String toString() {
         runtimeFilterExpressions,
         caseSensitive());
   }
+
+  @Override
+  public CustomTaskMetric[] reportDriverMetrics() {

Review Comment:
   Why implement this only in `SparkBatchQueryScan`? Can we do this in 
`SparkScan`?



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/SparkBatchQueryScan.java:
##########
@@ -65,6 +82,7 @@ class SparkBatchQueryScan extends 
SparkPartitioningAwareScan<PartitionScanTask>
   private final Long endSnapshotId;
   private final Long asOfTimestamp;
   private final String tag;
+  private final SparkReadMetricReporter sparkReadMetricReporter;

Review Comment:
   What about using `Supplier<ScanReport> scanReportSupplier` to make this 
independent from a particular metrics reporter? We can use 
`metricsReporter::scanReport` closure to construct it.
   
   ```
   InMemoryMetricsReporter metricsReporter = new InMemoryMetricsReporter();
   ...
   scan.metricsReporter(metricsReporter)
   ...
   return new SparkBatchQueryScan(..., metricsReporter::scanReport);
   ```



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/ResultDataFiles.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import java.util.Arrays;
+import org.apache.spark.sql.connector.metric.CustomMetric;
+
+public class ResultDataFiles implements CustomMetric {
+
+  static final String name = "resultDataFiles";

Review Comment:
   Static constant naming: `name` -> `NAME`



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/SparkReadMetricReporter.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import java.util.Optional;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.MetricsReporter;
+import org.apache.iceberg.metrics.ScanReport;
+
+public class SparkReadMetricReporter implements MetricsReporter {
+
+  private MetricsReport metricsReport;
+
+  @Override
+  public void report(MetricsReport report) {
+    this.metricsReport = report;
+  }
+
+  public Optional<ScanReport> getScanReport() {

Review Comment:
   Let's not use `Optional` and `getXXX` prefix.
   
   ```
   public ScanReport scanReport() {
     return (ScanReport) metricsReport;
   }
   ```
   
   We can check if the value is null later.



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/ResultDataFiles.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import java.util.Arrays;
+import org.apache.spark.sql.connector.metric.CustomMetric;
+
+public class ResultDataFiles implements CustomMetric {
+
+  static final String name = "resultDataFiles";
+
+  @Override
+  public String name() {
+    return name;
+  }
+
+  @Override
+  public String description() {
+    return "Result data files";
+  }
+
+  @Override
+  public String aggregateTaskMetrics(long[] taskMetrics) {

Review Comment:
   Can we extend `CustomSumMetric` from Spark instead of overriding this?



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/TaskResultDataFiles.java:
##########
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import org.apache.spark.sql.connector.metric.CustomTaskMetric;
+
+public class TaskResultDataFiles implements CustomTaskMetric {

Review Comment:
   I'd add a static `from(ScanReport scanReport)` to each `TaskXXX` metric like 
suggested above and make the constructor private.



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/SparkBatchQueryScan.java:
##########
@@ -256,4 +275,56 @@ public String toString() {
         runtimeFilterExpressions,
         caseSensitive());
   }
+
+  @Override
+  public CustomTaskMetric[] reportDriverMetrics() {
+    List<CustomTaskMetric> customTaskMetrics = Lists.newArrayList();
+    Optional<ScanReport> scanReportOptional = 
sparkReadMetricReporter.getScanReport();
+
+    scanReportOptional.ifPresent(
+        scanReport -> {
+          Optional.ofNullable(scanReport.scanMetrics().totalFileSizeInBytes())
+              .ifPresent(
+                  counterResult ->
+                      customTaskMetrics.add(new 
TaskTotalFileSize(counterResult.value())));
+          Optional.ofNullable(scanReport.scanMetrics().totalPlanningDuration())
+              .ifPresent(
+                  timerResult ->
+                      customTaskMetrics.add(new 
TaskTotalPlanningDuration(timerResult.count())));
+
+          Optional.ofNullable(scanReport.scanMetrics().skippedDataFiles())
+              .ifPresent(
+                  skippedDataFilesResult ->
+                      customTaskMetrics.add(
+                          new 
TaskSkippedDataFiles(skippedDataFilesResult.value())));
+          Optional.ofNullable(scanReport.scanMetrics().resultDataFiles())
+              .ifPresent(
+                  resultDataFilesResult -> new 
TaskResultDataFiles(resultDataFilesResult.value()));
+
+          Optional.ofNullable(scanReport.scanMetrics().skippedDataManifests())
+              .ifPresent(
+                  skippedDataManifestsResult ->
+                      customTaskMetrics.add(
+                          new 
TaskSkippedDataManifests(skippedDataManifestsResult.value())));
+          Optional.ofNullable(scanReport.scanMetrics().scannedDataManifests())
+              .ifPresent(
+                  scannedDataManifestResult ->
+                      customTaskMetrics.add(
+                          new 
TaskScannedDataManifests(scannedDataManifestResult.value())));
+        });
+
+    return customTaskMetrics.toArray(new CustomTaskMetric[0]);
+  }
+
+  @Override
+  public CustomMetric[] supportedCustomMetrics() {

Review Comment:
   This overrides `supportedCustomMetrics()` in `SparkScan` and breaks it. Can 
we move this logic there and combine with existing metrics?



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/SparkScanBuilder.java:
##########
@@ -420,12 +423,15 @@ private Scan buildBatchScan() {
   private Scan buildBatchScan(Long snapshotId, Long asOfTimestamp, String 
branch, String tag) {
     Schema expectedSchema = schemaWithMetadataColumns();
 
+    sparkReadMetricReporter = new SparkReadMetricReporter();

Review Comment:
   Why init it here again? We should either use local vars or not init it here.



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/ScannedDataManifests.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import java.util.Arrays;
+import org.apache.spark.sql.connector.metric.CustomMetric;
+
+public class ScannedDataManifests implements CustomMetric {

Review Comment:
   Comments above apply to all metrics below.



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/SparkBatchQueryScan.java:
##########
@@ -256,4 +275,56 @@ public String toString() {
         runtimeFilterExpressions,
         caseSensitive());
   }
+
+  @Override
+  public CustomTaskMetric[] reportDriverMetrics() {
+    List<CustomTaskMetric> customTaskMetrics = Lists.newArrayList();
+    Optional<ScanReport> scanReportOptional = 
sparkReadMetricReporter.getScanReport();

Review Comment:
   Iceberg historically uses null instead of `Optional` and I think we should 
continue to follow that. Also, Spotless makes these closures really hard to 
heard.
   
   What about something like this?
   
   ```
   @Override
   public CustomTaskMetric[] reportDriverMetrics() {
     ScanReport scanReport = scanReportSupplier.get();
   
     if (scanReport == null) {
       return new CustomTaskMetric[0];
     }
   
     List<CustomTaskMetric> driverMetrics = Lists.newArrayList();
     driverMetrics.add(TaskTotalFileSize.from(scanReport));
     ...
     return driverMetrics.toArray(new CustomTaskMetric[0]);
   }
   ```
   
   Where `TaskTotalFileSize` would be defined as follows:
   
   ```
   public class TaskTotalFileSize implements CustomTaskMetric {
   
     private final long value;
   
     public static TaskTotalFileSize from(ScanReport scanReport) {
       CounterResult counter = scanReport.scanMetrics().totalFileSizeInBytes();
       long value = counter != null ? counter.value() : -1;
       return new TotalFileSizeTaskMetric(value);
     }
   
     private TaskTotalFileSize(long value) {
       this.value = value;
     }
   
     @Override
     public String name() {
       return TotalFileSize.NAME;
     }
   
     @Override
     public long value() {
       return value;
     }
   }
   ```



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/ResultDataFiles.java:
##########
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import java.util.Arrays;
+import org.apache.spark.sql.connector.metric.CustomMetric;
+
+public class ResultDataFiles implements CustomMetric {
+
+  static final String name = "resultDataFiles";
+
+  @Override
+  public String name() {
+    return name;
+  }
+
+  @Override
+  public String description() {
+    return "Result data files";

Review Comment:
   We should follow what Spark does in other places. Specifically, its name 
does not with capital letters.



##########
spark/v3.4/spark/src/main/java/org/apache/iceberg/spark/source/metrics/SparkReadMetricReporter.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.spark.source.metrics;
+
+import java.util.Optional;
+import org.apache.iceberg.metrics.MetricsReport;
+import org.apache.iceberg.metrics.MetricsReporter;
+import org.apache.iceberg.metrics.ScanReport;
+
+public class SparkReadMetricReporter implements MetricsReporter {

Review Comment:
   Is there a better name? Shall we call it `InMemoryMetricsReporter` and more 
to `core`?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to