|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark.sql.hudi.command.procedures |
| 21 | + |
| 22 | +import org.apache.hudi.HoodieCLIUtils |
| 23 | +import org.apache.hudi.common.config.HoodieBuildConfig |
| 24 | +import org.apache.hudi.common.table.HoodieTableMetaClient |
| 25 | +import org.apache.hudi.common.table.timeline.{HoodieActiveTimeline, HoodieTimeline} |
| 26 | +import org.apache.hudi.common.util.{BuildUtils, HoodieTimer, Option => HOption} |
| 27 | +import org.apache.spark.internal.Logging |
| 28 | +import org.apache.spark.sql.Row |
| 29 | +import org.apache.spark.sql.types.{DataTypes, Metadata, StructField, StructType} |
| 30 | + |
| 31 | +import java.util.function.Supplier |
| 32 | +import scala.collection.JavaConverters.{asScalaBufferConverter, asScalaIteratorConverter} |
| 33 | + |
| 34 | +class RunBuildProcedure extends BaseProcedure |
| 35 | + with ProcedureBuilder |
| 36 | + with ProcedurePredicateHelper |
| 37 | + with Logging { |
| 38 | + private val PARAMETERS = Array[ProcedureParameter]( |
| 39 | + ProcedureParameter.optional(0, "table", DataTypes.StringType, None), |
| 40 | + ProcedureParameter.optional(1, "path", DataTypes.StringType, None), |
| 41 | + ProcedureParameter.optional(2, "predicate", DataTypes.StringType, None), |
| 42 | + ProcedureParameter.optional(4, "show_involved_partition", DataTypes.BooleanType, false) |
| 43 | + ) |
| 44 | + |
| 45 | + private val OUTPUT_TYPE = new StructType(Array[StructField]( |
| 46 | + StructField("timestamp", DataTypes.StringType, nullable = true, Metadata.empty), |
| 47 | + StructField("task_num", DataTypes.IntegerType, nullable = true, Metadata.empty), |
| 48 | + StructField("state", DataTypes.StringType, nullable = true, Metadata.empty), |
| 49 | + StructField("involved_partitions", DataTypes.StringType, nullable = true, Metadata.empty) |
| 50 | + )) |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the input parameters of this procedure. |
| 54 | + */ |
| 55 | + override def parameters: Array[ProcedureParameter] = PARAMETERS |
| 56 | + |
| 57 | + /** |
| 58 | + * Returns the type of rows produced by this procedure. |
| 59 | + */ |
| 60 | + override def outputType: StructType = OUTPUT_TYPE |
| 61 | + |
| 62 | + /** |
| 63 | + * Executes this procedure. |
| 64 | + * <p> |
| 65 | + * Spark will align the provided arguments according to the input parameters |
| 66 | + * defined in {@link # parameters ( )} either by position or by name before execution. |
| 67 | + * <p> |
| 68 | + * Implementations may provide a summary of execution by returning one or many rows |
| 69 | + * as a result. The schema of output rows must match the defined output type |
| 70 | + * in {@link # outputType ( )}. |
| 71 | + * |
| 72 | + * @param args input arguments |
| 73 | + * @return the result of executing this procedure with the given arguments |
| 74 | + */ |
| 75 | + override def call(args: ProcedureArgs): Seq[Row] = { |
| 76 | + super.checkArgs(PARAMETERS, args) |
| 77 | + |
| 78 | + val tableName = getArgValueOrDefault(args, PARAMETERS(0)) |
| 79 | + val tablePath = getArgValueOrDefault(args, PARAMETERS(1)) |
| 80 | + val predicate = getArgValueOrDefault(args, PARAMETERS(2)) |
| 81 | + val showInvolvedPartitions = getArgValueOrDefault(args, PARAMETERS(3)).get.asInstanceOf[Boolean] |
| 82 | + |
| 83 | + val basePath: String = getBasePath(tableName, tablePath) |
| 84 | + val metaClient = HoodieTableMetaClient.builder().setConf(jsc.hadoopConfiguration()).setBasePath(basePath).build() |
| 85 | + var conf: Map[String, String] = Map.empty |
| 86 | + predicate match { |
| 87 | + case Some(p) => |
| 88 | + val prunedPartitions = prunePartition(spark, metaClient, p.asInstanceOf[String]) |
| 89 | + conf = conf ++ Map( |
| 90 | + HoodieBuildConfig.PARTITION_SELECTED.key() -> prunedPartitions.mkString(",") |
| 91 | + ) |
| 92 | + logInfo(s"Partition predicates: ${p}, partition selected: ${prunedPartitions}") |
| 93 | + case _ => |
| 94 | + logInfo("No partition predicates") |
| 95 | + } |
| 96 | + |
| 97 | + var pendingBuild = BuildUtils.getAllPendingBuildPlans(metaClient) |
| 98 | + .iterator().asScala.map(_.getLeft.getTimestamp).toSeq.sortBy(f => f) |
| 99 | + logInfo(s"Pending build instants: ${pendingBuild.mkString(",")}") |
| 100 | + |
| 101 | + val client = HoodieCLIUtils.createHoodieClientFromPath(sparkSession, basePath, conf) |
| 102 | + val instantTime = HoodieActiveTimeline.createNewInstantTime() |
| 103 | + if (client.scheduleBuildAtInstant(instantTime, HOption.empty())) { |
| 104 | + pendingBuild ++= Seq(instantTime) |
| 105 | + } |
| 106 | + logInfo(s"Build instants to run: ${pendingBuild.mkString(",")}") |
| 107 | + |
| 108 | + val timer = new HoodieTimer |
| 109 | + timer.startTimer() |
| 110 | + pendingBuild.foreach(instant => { |
| 111 | + timer.startTimer() |
| 112 | + client.build(instant, true) |
| 113 | + logInfo(s"Finish build for instant: $instant, time cost: ${timer.endTimer()}ms") |
| 114 | + }) |
| 115 | + client.close() |
| 116 | + logInfo(s"Finish build all instants: ${pendingBuild.mkString(",")}, time cost: ${timer.endTimer()}ms") |
| 117 | + |
| 118 | + val buildInstants = metaClient.reloadActiveTimeline().getInstants.iterator().asScala |
| 119 | + .filter(p => p.getAction == HoodieTimeline.BUILD_ACTION && pendingBuild.contains(p.getTimestamp)) |
| 120 | + .toSeq |
| 121 | + .sortBy(f => f.getTimestamp) |
| 122 | + .reverse |
| 123 | + |
| 124 | + val buildPlans = buildInstants.map(instant => |
| 125 | + BuildUtils.getBuildPlan(metaClient, instant) |
| 126 | + ) |
| 127 | + |
| 128 | + if (showInvolvedPartitions) { |
| 129 | + buildPlans.map { p => |
| 130 | + Row(p.get().getLeft.getTimestamp, p.get().getRight.getTasks.size(), |
| 131 | + p.get().getLeft.getState.name(), |
| 132 | + BuildUtils.extractPartitions(p.get().getRight.getTasks).asScala.mkString(",")) |
| 133 | + } |
| 134 | + } else { |
| 135 | + buildPlans.map { p => |
| 136 | + Row(p.get().getLeft.getTimestamp, p.get().getRight.getTasks.size(), p.get().getLeft.getState.name(), "*") |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + override def build: Procedure = new RunBuildProcedure |
| 142 | +} |
| 143 | + |
| 144 | + |
| 145 | +object RunBuildProcedure { |
| 146 | + val NAME = "run_build" |
| 147 | + |
| 148 | + def builder: Supplier[ProcedureBuilder] = new Supplier[ProcedureBuilder] { |
| 149 | + override def get() = new RunBuildProcedure |
| 150 | + } |
| 151 | +} |
0 commit comments