Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -1089,7 +1089,8 @@ private[spark] object RandomForest extends Logging {
var numNodesInGroup = 0
// If maxMemoryInMB is set very small, we want to still try to split 1 node,
// so we allow one iteration if memUsage == 0.
while (nodeStack.nonEmpty && (memUsage < maxMemoryUsage || memUsage == 0)) {
var groupDone = false
while (nodeStack.nonEmpty && !groupDone) {
val (treeIndex, node) = nodeStack.top
// Choose subset of features for node (if subsampling).
val featureSubset: Option[Array[Int]] = if (metadata.subsamplingFeatures) {
Expand All @@ -1107,9 +1108,11 @@ private[spark] object RandomForest extends Logging {
mutableTreeToNodeToIndexInfo
.getOrElseUpdate(treeIndex, new mutable.HashMap[Int, NodeIndexInfo]())(node.id)
= new NodeIndexInfo(numNodesInGroup, featureSubset)
numNodesInGroup += 1
memUsage += nodeMemUsage
} else {
groupDone = true
}
numNodesInGroup += 1
memUsage += nodeMemUsage
}
if (memUsage > maxMemoryUsage) {
// If maxMemoryUsage is 0, we should still allow splitting 1 node.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ abstract class ExternalCatalog

def getTable(db: String, table: String): CatalogTable

def getTableOption(db: String, table: String): Option[CatalogTable]

def tableExists(db: String, table: String): Boolean

def listTables(db: String): Seq[String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,6 @@ class InMemoryCatalog(
catalog(db).tables(table).table
}

override def getTableOption(db: String, table: String): Option[CatalogTable] = synchronized {
if (!tableExists(db, table)) None else Option(catalog(db).tables(table).table)
}

override def tableExists(db: String, table: String): Boolean = synchronized {
requireDbExists(db)
catalog(db).tables.contains(table)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,9 +387,10 @@ class SessionCatalog(

/**
* Retrieve the metadata of an existing permanent table/view. If no database is specified,
* assume the table/view is in the current database. If the specified table/view is not found
* in the database then a [[NoSuchTableException]] is thrown.
* assume the table/view is in the current database.
*/
@throws[NoSuchDatabaseException]
@throws[NoSuchTableException]
def getTableMetadata(name: TableIdentifier): CatalogTable = {
val db = formatDatabaseName(name.database.getOrElse(getCurrentDatabase))
val table = formatTableName(name.table)
Expand All @@ -398,18 +399,6 @@ class SessionCatalog(
externalCatalog.getTable(db, table)
}

/**
* Retrieve the metadata of an existing metastore table.
* If no database is specified, assume the table is in the current database.
* If the specified table is not found in the database then return None if it doesn't exist.
*/
def getTableMetadataOption(name: TableIdentifier): Option[CatalogTable] = {
val db = formatDatabaseName(name.database.getOrElse(getCurrentDatabase))
val table = formatTableName(name.table)
requireDbExists(db)
externalCatalog.getTableOption(db, table)
}

/**
* Load files stored in given path into an existing metastore table.
* If no database is specified, assume the table is in the current database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,17 +510,6 @@ abstract class SessionCatalogSuite extends AnalysisTest {
}
}

test("get option of table metadata") {
withBasicCatalog { catalog =>
assert(catalog.getTableMetadataOption(TableIdentifier("tbl1", Some("db2")))
== Option(catalog.externalCatalog.getTable("db2", "tbl1")))
assert(catalog.getTableMetadataOption(TableIdentifier("unknown_table", Some("db2"))).isEmpty)
intercept[NoSuchDatabaseException] {
catalog.getTableMetadataOption(TableIdentifier("tbl1", Some("unknown_db")))
}
}
}

test("lookup table relation") {
withBasicCatalog { catalog =>
val tempTable1 = Range(1, 10, 1, 10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@

package org.apache.spark.sql.execution.command

import org.apache.hadoop.conf.Configuration

import org.apache.spark.SparkContext
import org.apache.spark.sql.execution.SQLExecution
import org.apache.spark.sql.execution.datasources.ExecutedWriteSummary
import org.apache.spark.sql.execution.datasources.BasicWriteJobStatsTracker
import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
import org.apache.spark.util.SerializableConfiguration


/**
* A special `RunnableCommand` which writes data out and updates metrics.
Expand All @@ -37,29 +40,8 @@ trait DataWritingCommand extends RunnableCommand {
)
}

/**
* Callback function that update metrics collected from the writing operation.
*/
protected def updateWritingMetrics(writeSummaries: Seq[ExecutedWriteSummary]): Unit = {
val sparkContext = SparkContext.getActive.get
var numPartitions = 0
var numFiles = 0
var totalNumBytes: Long = 0L
var totalNumOutput: Long = 0L

writeSummaries.foreach { summary =>
numPartitions += summary.updatedPartitions.size
numFiles += summary.numOutputFile
totalNumBytes += summary.numOutputBytes
totalNumOutput += summary.numOutputRows
}

metrics("numFiles").add(numFiles)
metrics("numOutputBytes").add(totalNumBytes)
metrics("numOutputRows").add(totalNumOutput)
metrics("numParts").add(numPartitions)

val executionId = sparkContext.getLocalProperty(SQLExecution.EXECUTION_ID_KEY)
SQLMetrics.postDriverMetricUpdates(sparkContext, executionId, metrics.values.toList)
def basicWriteJobStatsTracker(hadoopConf: Configuration): BasicWriteJobStatsTracker = {
val serializableHadoopConf = new SerializableConfiguration(hadoopConf)
new BasicWriteJobStatsTracker(serializableHadoopConf, metrics)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.execution.datasources

import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.Path

import org.apache.spark.SparkContext
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.execution.SQLExecution
import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
import org.apache.spark.util.SerializableConfiguration


/**
* Simple metrics collected during an instance of [[FileFormatWriter.ExecuteWriteTask]].
* These were first introduced in https://github.com/apache/spark/pull/18159 (SPARK-20703).
*/
case class BasicWriteTaskStats(
numPartitions: Int,
numFiles: Int,
numBytes: Long,
numRows: Long)
extends WriteTaskStats


/**
* Simple [[WriteTaskStatsTracker]] implementation that produces [[BasicWriteTaskStats]].
* @param hadoopConf
*/
class BasicWriteTaskStatsTracker(hadoopConf: Configuration)
extends WriteTaskStatsTracker {

private[this] var numPartitions: Int = 0
private[this] var numFiles: Int = 0
private[this] var numBytes: Long = 0L
private[this] var numRows: Long = 0L

private[this] var curFile: String = null


private def getFileSize(filePath: String): Long = {
val path = new Path(filePath)
val fs = path.getFileSystem(hadoopConf)
fs.getFileStatus(path).getLen()
}


override def newPartition(partitionValues: InternalRow): Unit = {
numPartitions += 1
}

override def newBucket(bucketId: Int): Unit = {
// currently unhandled
}

override def newFile(filePath: String): Unit = {
if (numFiles > 0) {
// we assume here that we've finished writing to disk the previous file by now
numBytes += getFileSize(curFile)
}
curFile = filePath
numFiles += 1
}

override def newRow(row: InternalRow): Unit = {
numRows += 1
}

override def getFinalStats(): WriteTaskStats = {
if (numFiles > 0) {
numBytes += getFileSize(curFile)
}
BasicWriteTaskStats(numPartitions, numFiles, numBytes, numRows)
}
}


/**
* Simple [[WriteJobStatsTracker]] implementation that's serializable, capable of
* instantiating [[BasicWriteTaskStatsTracker]] on executors and processing the
* [[BasicWriteTaskStats]] they produce by aggregating the metrics and posting them
* as DriverMetricUpdates.
*/
class BasicWriteJobStatsTracker(
serializableHadoopConf: SerializableConfiguration,
@transient val metrics: Map[String, SQLMetric])
extends WriteJobStatsTracker {

override def newTaskInstance(): WriteTaskStatsTracker = {
new BasicWriteTaskStatsTracker(serializableHadoopConf.value)
}

override def processStats(stats: Seq[WriteTaskStats]): Unit = {
val sparkContext = SparkContext.getActive.get
var numPartitions: Long = 0L
var numFiles: Long = 0L
var totalNumBytes: Long = 0L
var totalNumOutput: Long = 0L

val basicStats = stats.map(_.asInstanceOf[BasicWriteTaskStats])

basicStats.foreach { summary =>
numPartitions += summary.numPartitions
numFiles += summary.numFiles
totalNumBytes += summary.numBytes
totalNumOutput += summary.numRows
}

metrics("numFiles").add(numFiles)
metrics("numOutputBytes").add(totalNumBytes)
metrics("numOutputRows").add(totalNumOutput)
metrics("numParts").add(numPartitions)

val executionId = sparkContext.getLocalProperty(SQLExecution.EXECUTION_ID_KEY)
SQLMetrics.postDriverMetricUpdates(sparkContext, executionId, metrics.values.toList)
}
}
Loading