You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hudi.apache.org by GitBox <gi...@apache.org> on 2022/06/13 11:24:42 UTC

[GitHub] [hudi] XuQianJin-Stars commented on a diff in pull request #5848: [HUDI-3499] Add Call Procedure for show rollbacks

XuQianJin-Stars commented on code in PR #5848:
URL: https://github.com/apache/hudi/pull/5848#discussion_r895606573


##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/ShowRollbacksProcedure.scala:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.hudi.command.procedures
+
+import java.io.IOException
+import java.util
+import java.util.function.Supplier
+
+import org.apache.hudi.avro.model.HoodieRollbackMetadata
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.common.table.timeline.HoodieInstant.State
+import org.apache.hudi.common.table.timeline.HoodieTimeline.ROLLBACK_ACTION
+import org.apache.hudi.common.table.timeline.{HoodieActiveTimeline, HoodieInstant, HoodieTimeline, TimelineMetadataUtils}
+import org.apache.hudi.common.util.CollectionUtils
+import org.apache.hudi.common.util.collection.Pair
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.types.{DataTypes, Metadata, StructField, StructType}
+
+import scala.collection.JavaConversions.asScalaBuffer
+import scala.collection.JavaConverters._
+
+class ShowRollbacksProcedure(showRollback: Boolean) extends BaseProcedure with ProcedureBuilder {
+  private val ROLLBACKS_PARAMETERS = Array[ProcedureParameter](
+    ProcedureParameter.required(0, "table", DataTypes.StringType, None),
+    ProcedureParameter.optional(1, "limit", DataTypes.IntegerType, 10)
+  )
+
+  private val ROLLBACK_PARAMETERS = Array[ProcedureParameter](
+    ProcedureParameter.required(0, "table", DataTypes.StringType, None),
+    ProcedureParameter.optional(1, "limit", DataTypes.IntegerType, 10),
+    ProcedureParameter.required(2, "instant_time", DataTypes.StringType, None)
+  )
+
+  private val ROLLBACKS_OUTPUT_TYPE = new StructType(Array[StructField](
+    StructField("instant", DataTypes.StringType, nullable = true, Metadata.empty),
+    StructField("rollback_instant", DataTypes.StringType, nullable = true, Metadata.empty),
+    StructField("total_files_deleted", DataTypes.IntegerType, nullable = true, Metadata.empty),
+    StructField("time_taken_in_millis", DataTypes.LongType, nullable = true, Metadata.empty),
+    StructField("total_partitions", DataTypes.IntegerType, nullable = true, Metadata.empty)
+  ))
+
+  private val ROLLBACK_OUTPUT_TYPE = new StructType(Array[StructField](
+    StructField("instant", DataTypes.StringType, nullable = true, Metadata.empty),
+    StructField("rollback_instant", DataTypes.StringType, nullable = true, Metadata.empty),
+    StructField("partition", DataTypes.StringType, nullable = true, Metadata.empty),
+    StructField("deleted_file", DataTypes.StringType, nullable = true, Metadata.empty),
+    StructField("succeeded", DataTypes.BooleanType, nullable = true, Metadata.empty)
+  ))
+
+  def parameters: Array[ProcedureParameter] = if (showRollback) ROLLBACK_PARAMETERS else ROLLBACKS_PARAMETERS
+
+  def outputType: StructType = if (showRollback) ROLLBACK_OUTPUT_TYPE else ROLLBACKS_OUTPUT_TYPE
+
+  override def call(args: ProcedureArgs): Seq[Row] = {
+    super.checkArgs(parameters, args)
+
+    val tableName = getArgValueOrDefault(args, parameters(0))
+    val limit = getArgValueOrDefault(args, parameters(1)).get.asInstanceOf[Int]
+
+    val basePath = getBasePath(tableName)
+    val metaClient = HoodieTableMetaClient.builder.setConf(jsc.hadoopConfiguration()).setBasePath(basePath).build
+    val activeTimeline = new RollbackTimeline(metaClient)
+    if (showRollback) {
+      val instantTime = getArgValueOrDefault(args, parameters(2)).get.asInstanceOf[String]
+      getRollback(activeTimeline, instantTime, limit)
+    } else {
+      getRollbacks(activeTimeline, limit)
+    }
+  }
+
+  override def build: Procedure = new ShowRollbacksProcedure(showRollback)
+
+  class RollbackTimeline(metaClient: HoodieTableMetaClient) extends HoodieActiveTimeline(metaClient,
+    CollectionUtils.createImmutableSet(HoodieTimeline.ROLLBACK_EXTENSION)) {
+  }
+
+  def getRollback(activeTimeline: RollbackTimeline,
+                  instantTime: String,
+                  limit: Int): Seq[Row] = {
+    val rows = new util.ArrayList[Row]
+    val metadata = TimelineMetadataUtils.deserializeAvroMetadata(activeTimeline.getInstantDetails(
+      new HoodieInstant(State.COMPLETED, ROLLBACK_ACTION, instantTime)).get, classOf[HoodieRollbackMetadata])
+
+    metadata.getPartitionMetadata.asScala.toMap.iterator.foreach(entry => Stream
+      .concat(entry._2.getSuccessDeleteFiles.map(f => Pair.of(f, true)),
+        entry._2.getFailedDeleteFiles.map(f => Pair.of(f, false)))
+      .foreach((fileWithDeleteStatus: Pair[String, Boolean]) => {
+        rows.add(Row(metadata.getStartRollbackTime, metadata.getCommitsRollback.toString,
+          entry._1, fileWithDeleteStatus.getLeft, fileWithDeleteStatus.getRight))
+      }))
+    rows.stream().limit(limit).toArray().map(r => r.asInstanceOf[Row]).toList
+  }
+
+  def getRollbacks(activeTimeline: RollbackTimeline,
+                   limit: Int): Seq[Row] = {
+    val rows = new util.ArrayList[Row]
+    val rollback = activeTimeline.getRollbackTimeline.filterCompletedInstants
+
+    rollback.getInstants.iterator().asScala.foreach(instant => {
+      try {
+        val metadata = TimelineMetadataUtils.deserializeAvroMetadata(activeTimeline.getInstantDetails(instant).get,
+          classOf[HoodieRollbackMetadata])
+
+        metadata.getCommitsRollback.iterator().asScala.foreach(c => {
+          rows.add(Row(metadata.getStartRollbackTime, c,
+            metadata.getTotalFilesDeleted, metadata.getTimeTakenInMillis,
+            if (metadata.getPartitionMetadata != null) metadata.getPartitionMetadata.size else 0))
+        })
+      } catch {
+        case e: IOException =>
+          e.printStackTrace()
+      }
+    })
+    rows.stream().limit(limit).toArray().map(r => r.asInstanceOf[Row]).toList
+  }
+}
+
+object ShowRollbacksProcedure {
+  val NAME = "show_rollbacks"
+
+  def builder: Supplier[ProcedureBuilder] = new Supplier[ProcedureBuilder] {
+    override def get() = new ShowRollbacksProcedure(false)
+  }
+}
+
+object ShowRollbackProcedure {
+  val NAME = "show_rollback"

Review Comment:
   show_rollback -> show_rollback_detail



##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/ShowRollbacksProcedure.scala:
##########
@@ -0,0 +1,147 @@
+/*
+ * 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.hudi.command.procedures
+
+import java.io.IOException
+import java.util
+import java.util.function.Supplier
+
+import org.apache.hudi.avro.model.HoodieRollbackMetadata
+import org.apache.hudi.common.table.HoodieTableMetaClient
+import org.apache.hudi.common.table.timeline.HoodieInstant.State
+import org.apache.hudi.common.table.timeline.HoodieTimeline.ROLLBACK_ACTION
+import org.apache.hudi.common.table.timeline.{HoodieActiveTimeline, HoodieInstant, HoodieTimeline, TimelineMetadataUtils}
+import org.apache.hudi.common.util.CollectionUtils
+import org.apache.hudi.common.util.collection.Pair
+import org.apache.spark.sql.Row
+import org.apache.spark.sql.types.{DataTypes, Metadata, StructField, StructType}
+
+import scala.collection.JavaConversions.asScalaBuffer
+import scala.collection.JavaConverters._
+
+class ShowRollbacksProcedure(showRollback: Boolean) extends BaseProcedure with ProcedureBuilder {

Review Comment:
   showRollback -> showDetails



##########
hudi-spark-datasource/hudi-spark/src/main/scala/org/apache/spark/sql/hudi/command/procedures/HoodieProcedures.scala:
##########
@@ -45,6 +45,8 @@ object HoodieProcedures {
     mapBuilder.put(ShowCommitsMetadataProcedure.NAME, ShowCommitsMetadataProcedure.builder)
     mapBuilder.put(ShowSavepointsProcedure.NAME, ShowSavepointsProcedure.builder)
     mapBuilder.put(DeleteMarkerProcedure.NAME, DeleteMarkerProcedure.builder)
+    mapBuilder.put(ShowRollbacksProcedure.NAME, ShowRollbacksProcedure.builder)

Review Comment:
   ShowRollbacksProcedure -> ShowRollbackDetailProcedure



-- 
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: commits-unsubscribe@hudi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org