Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.spark.sql.connector.read;

import org.apache.spark.annotation.Evolving;

/**
* A custom metric for {@link Scan}.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this should note that the metrics is a long. What about calling it a LongMetric? Any plans to generalize it?

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.

LongMetric sounds okay to me. This basically follows SQLMetric to use long value.

*
* @since 3.2.0
*/
@Evolving
public interface CustomMetric {
/**
* Returns the name of custom metric.
*/
String getName();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I find that get is not very useful in method names. This doesn't fit with the conventions used in Scala (which omits get) and using get in an interface means that although a quick field retrieval is expected by Java convention, the actual implementation can do anything.

I think it would be better to name these name(), description(), and value().

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.

Okay. Let me update it in next commit.


/**
* Returns the description of custom metric.
*/
String getDescription();

/**
* Returns the value of custom metric.
*/
Long getValue();
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,12 @@ public interface PartitionReader<T> extends Closeable {
* Return the current record. This method should return same value until `next` is called.
*/
T get();

/**
* Returns an array of custom metrics. By default it returns empty array.
*/
default CustomMetric[] getCustomMetrics() {
CustomMetric[] NO_METRICS = {};
return NO_METRICS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,13 @@ default MicroBatchStream toMicroBatchStream(String checkpointLocation) {
default ContinuousStream toContinuousStream(String checkpointLocation) {
throw new UnsupportedOperationException(description() + ": Continuous scan are not supported");
}

/**
* Returns an array of supported custom metrics with name and description.
* By default it returns empty array.
*/
default CustomMetric[] supportedCustomMetrics() {
CustomMetric[] NO_METRICS = {};
return NO_METRICS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.plans.QueryPlan
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReaderFactory, Scan}
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReader, PartitionReaderFactory, Scan}

/**
* Physical plan node for scanning a batch of data from a data source v2.
Expand All @@ -44,8 +44,19 @@ case class BatchScanExec(

override lazy val readerFactory: PartitionReaderFactory = batch.createReaderFactory()

/**
* The callback function which is called when the output iterator of input RDD is consumed
* completely.
*/
private def onOutputCompletion(reader: PartitionReader[_]) = {

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.

Do we have the plan to do other things on completion? Otherwise, I'd like to suggest naming collectMetericsOnCompletion for now.

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.

sounds good to me.

reader.getCustomMetrics.foreach { metric =>
longMetric(metric.getName) += metric.getValue

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.

What if the metric doesn't exist in the SQLMetrics in case of users implemented mistakenly? Would it be better to throw an explicit error?

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.

Good idea. I will do it.

}
}

override lazy val inputRDD: RDD[InternalRow] = {
new DataSourceRDD(sparkContext, partitions, readerFactory, supportsColumnar)
new DataSourceRDD(sparkContext, partitions, readerFactory, supportsColumnar,
onOutputCompletion)
}

override def doCanonicalize(): BatchScanExec = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.execution.datasources.v2
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.connector.read.{InputPartition, Scan}
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReader, Scan}
import org.apache.spark.sql.connector.read.streaming.{ContinuousPartitionReaderFactory, ContinuousStream, Offset}
import org.apache.spark.sql.execution.streaming.continuous._

Expand All @@ -47,6 +47,16 @@ case class ContinuousScanExec(
stream.createContinuousReaderFactory()
}

/**
* The callback function which is called when the output iterator of input RDD is consumed
* completely.
*/
private def onOutputCompletion(reader: PartitionReader[_]) = {
reader.getCustomMetrics.foreach { metric =>
longMetric(metric.getName) += metric.getValue
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we also have a way to set task input metrics? Maybe a reserved metric name like input-bytes that will set TaskContext.get().taskMetrics().inputMetrics.bytesRead. Records read would be really useful as well.

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.

+1 will add in later implementation PRs.

}

override lazy val inputRDD: RDD[InternalRow] = {
EpochCoordinatorRef.get(
sparkContext.getLocalProperty(ContinuousExecution.EPOCH_COORDINATOR_ID_KEY),
Expand All @@ -58,6 +68,7 @@ case class ContinuousScanExec(
sqlContext.conf.continuousStreamingExecutorPollIntervalMs,
partitions,
schema,
readerFactory.asInstanceOf[ContinuousPartitionReaderFactory])
readerFactory.asInstanceOf[ContinuousPartitionReaderFactory],
onOutputCompletion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReader, PartitionReaderFactory}
import org.apache.spark.sql.vectorized.ColumnarBatch
import org.apache.spark.util.CompletionIterator

class DataSourceRDDPartition(val index: Int, val inputPartition: InputPartition)
extends Partition with Serializable
Expand All @@ -36,7 +37,8 @@ class DataSourceRDD(
sc: SparkContext,
@transient private val inputPartitions: Seq[InputPartition],
partitionReaderFactory: PartitionReaderFactory,
columnarReads: Boolean)
columnarReads: Boolean,
onCompletion: PartitionReader[_] => Unit = _ => {})
extends RDD[InternalRow](sc, Nil) {

override protected def getPartitions: Array[Partition] = {
Expand All @@ -55,11 +57,21 @@ class DataSourceRDD(
val (iter, reader) = if (columnarReads) {
val batchReader = partitionReaderFactory.createColumnarReader(inputPartition)
val iter = new MetricsBatchIterator(new PartitionIterator[ColumnarBatch](batchReader))
(iter, batchReader)
def completionFunction = {
onCompletion(batchReader)
}
val completionIterator = CompletionIterator[ColumnarBatch, Iterator[ColumnarBatch]](
iter, completionFunction)
(completionIterator, batchReader)
} else {
val rowReader = partitionReaderFactory.createReader(inputPartition)
val iter = new MetricsRowIterator(new PartitionIterator[InternalRow](rowReader))
(iter, rowReader)
def completionFunction = {
onCompletion(rowReader)
}
val completionIterator = CompletionIterator[InternalRow, Iterator[InternalRow]](
iter, completionFunction)
(completionIterator, rowReader)
}
context.addTaskCompletionListener[Unit](_ => reader.close())
// TODO: SPARK-25083 remove the type erasure hack in data source scan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ import org.apache.spark.util.Utils

trait DataSourceV2ScanExecBase extends LeafExecNode {

override lazy val metrics = Map(
"numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows"))
override lazy val metrics = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Any plans to similarly support metrics in writes?

@viirya viirya Feb 4, 2021

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.

I looked at a few V2 write nodes, but seems we don't have any SQL metrics there (even number of output rows). I guess we don't provide metrics for writes generally now?

If there is interest to see metrics in writes, I think it is okay to work on it later.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks like updating context.taskMetrics().outputMetrics is just in our branch. That just uses the Hadoop FS metrics collection that we use elsewhere, so it isn't metrics from the source as we want to support in this PR.

I think it would be good to follow up and support metrics on the output side. It doesn't need to be done here, but metrics are really useful.

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.

Sounds good to me. I can work on it in follow up PRs.

val customMetrics = scan.supportedCustomMetrics().map { customMetric =>
customMetric.getName -> SQLMetrics.createMetric(sparkContext, customMetric.getDescription)
}
Map("numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows")) ++
customMetrics
}

def scan: Scan

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package org.apache.spark.sql.execution.datasources.v2
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.Attribute
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReaderFactory, Scan}
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReader, PartitionReaderFactory, Scan}
import org.apache.spark.sql.connector.read.streaming.{MicroBatchStream, Offset}

/**
Expand All @@ -45,7 +45,18 @@ case class MicroBatchScanExec(

override lazy val readerFactory: PartitionReaderFactory = stream.createReaderFactory()

/**
* The callback function which is called when the output iterator of input RDD is consumed
* completely.
*/
private def onOutputCompletion(reader: PartitionReader[_]) = {
reader.getCustomMetrics.foreach { metric =>
longMetric(metric.getName) += metric.getValue
}
}

override lazy val inputRDD: RDD[InternalRow] = {
new DataSourceRDD(sparkContext, partitions, readerFactory, supportsColumnar)
new DataSourceRDD(sparkContext, partitions, readerFactory, supportsColumnar,
onOutputCompletion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ package org.apache.spark.sql.execution.streaming.continuous
import org.apache.spark._
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.connector.read.InputPartition
import org.apache.spark.sql.connector.read.{InputPartition, PartitionReader}
import org.apache.spark.sql.connector.read.streaming.ContinuousPartitionReaderFactory
import org.apache.spark.sql.types.StructType
import org.apache.spark.util.NextIterator
import org.apache.spark.util.{CompletionIterator, NextIterator}

class ContinuousDataSourceRDDPartition(
val index: Int,
Expand Down Expand Up @@ -52,7 +52,8 @@ class ContinuousDataSourceRDD(
epochPollIntervalMs: Long,
private val inputPartitions: Seq[InputPartition],
schema: StructType,
partitionReaderFactory: ContinuousPartitionReaderFactory)
partitionReaderFactory: ContinuousPartitionReaderFactory,
onCompletion: PartitionReader[_] => Unit = _ => {})
extends RDD[InternalRow](sc, Nil) {

override protected def getPartitions: Array[Partition] = {
Expand Down Expand Up @@ -88,7 +89,7 @@ class ContinuousDataSourceRDD(
partition.queueReader
}

new NextIterator[InternalRow] {
val nextIter = new NextIterator[InternalRow] {
override def getNext(): InternalRow = {
readerForPartition.next() match {
case null =>
Expand All @@ -100,6 +101,12 @@ class ContinuousDataSourceRDD(

override def close(): Unit = {}
}

def completionFunction = {
onCompletion(readerForPartition.getPartitionReader())
}
CompletionIterator[InternalRow, Iterator[InternalRow]](
nextIter, completionFunction)
}

override def getPreferredLocations(split: Partition): Seq[String] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import org.apache.spark.{SparkEnv, SparkException, TaskContext}
import org.apache.spark.internal.Logging
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions.UnsafeProjection
import org.apache.spark.sql.connector.read.PartitionReader
import org.apache.spark.sql.connector.read.streaming.{ContinuousPartitionReader, PartitionOffset}
import org.apache.spark.sql.types.StructType
import org.apache.spark.util.ThreadUtils
Expand All @@ -47,6 +48,8 @@ class ContinuousQueuedDataReader(
// Important sequencing - we must get our starting point before the provider threads start running
private var currentOffset: PartitionOffset = reader.getOffset

def getPartitionReader(): PartitionReader[InternalRow] = reader

/**
* The record types in the read buffer.
*/
Expand Down