Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset Hierarchy - Remove single partition deletion flag #859

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 20 additions & 25 deletions src/main/scala/cognite/spark/v1/AssetHierarchyBuilder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,31 +78,26 @@ class AssetHierarchyBuilder(config: RelationConfig)(val sqlContext: SQLContext)

import CdpConnector.ioRuntime

def delete(data: DataFrame): Unit = {
val partitionedData = if (config.enableSinglePartitionDeleteAssetHierarchy) {
data.repartition(numPartitions = 1)
} else {
data
}

partitionedData.foreachPartition((rows: Iterator[Row]) => {
val deletes = rows.map(r => fromRow[DeleteItemByCogniteId](r))
Stream
.fromIterator[IO](deletes, chunkSize = batchSize)
.chunks
.parEvalMapUnordered(config.parallelismPerPartition) { chunk =>
client.assets
.deleteRecursive(
chunk.toVector.map(_.toCogniteId),
recursive = true,
ignoreUnknownIds = true)
.flatTap(_ => incMetrics(itemsDeleted, chunk.size))
}
.compile
.drain
.unsafeRunSync()
})
}
def delete(data: DataFrame): Unit =
data
.repartition(numPartitions = 1)
.foreachPartition((rows: Iterator[Row]) => {
val deletes = rows.map(r => fromRow[DeleteItemByCogniteId](r))
Stream
.fromIterator[IO](deletes, chunkSize = batchSize)
.chunks
.parEvalMapUnordered(config.parallelismPerPartition) { chunk =>
client.assets
.deleteRecursive(
chunk.toVector.map(_.toCogniteId),
recursive = true,
ignoreUnknownIds = true)
.flatTap(_ => incMetrics(itemsDeleted, chunk.size))
}
.compile
.drain
.unsafeRunSync()
})

def buildFromDf(data: DataFrame): Unit =
// Do not use .collect to run the builder on one of the executors and not on the driver
Expand Down
6 changes: 1 addition & 5 deletions src/main/scala/cognite/spark/v1/DefaultSource.scala
Original file line number Diff line number Diff line change
Expand Up @@ -406,9 +406,6 @@ object DefaultSource {
val collectMetrics = toBoolean(parameters, "collectMetrics")
val collectTestMetrics = toBoolean(parameters, "collectTestMetrics")

val enableSinglePartitionDeleteAssetHierarchy =
toBoolean(parameters, "enableSinglePartitionDeleteHierarchy", defaultValue = false)

val saveMode = parseSaveMode(parameters)
val parallelismPerPartition = {
toPositiveInt(parameters, "parallelismPerPartition").getOrElse(
Expand Down Expand Up @@ -458,8 +455,7 @@ object DefaultSource {
deleteMissingAssets = toBoolean(parameters, "deleteMissingAssets"),
subtrees = subtreesOption,
ignoreNullFields = toBoolean(parameters, "ignoreNullFields", defaultValue = true),
rawEnsureParent = toBoolean(parameters, "rawEnsureParent", defaultValue = true),
enableSinglePartitionDeleteAssetHierarchy = enableSinglePartitionDeleteAssetHierarchy
rawEnsureParent = toBoolean(parameters, "rawEnsureParent", defaultValue = true)
)
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/cognite/spark/v1/RelationConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ final case class RelationConfig(
deleteMissingAssets: Boolean,
subtrees: AssetSubtreeOption,
ignoreNullFields: Boolean,
rawEnsureParent: Boolean,
enableSinglePartitionDeleteAssetHierarchy: Boolean // flag to test whether single partition helps avoid NPE in asset hierarchy builder
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we might want to keep it for potential future investigations if we see weird NPEs again, that be a way to get one of them ready

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah fair enough, I'll rename it to "useSinglePartition" or something

rawEnsureParent: Boolean
) {

/** Desired number of Spark partitions ~= partitions / parallelismPerPartition */
Expand Down
3 changes: 1 addition & 2 deletions src/test/scala/cognite/spark/v1/SparkTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,7 @@ trait SparkTest {
deleteMissingAssets = false,
subtrees = AssetSubtreeOption.Ingest,
ignoreNullFields = true,
rawEnsureParent = false,
enableSinglePartitionDeleteAssetHierarchy = false
rawEnsureParent = false
)

private def getCounterSafe(metricName: String): Option[Long] =
Expand Down