jackylk commented on a change in pull request #3602: [CARBONDATA-3676] Support 
clean carbon data files of stages.
URL: https://github.com/apache/carbondata/pull/3602#discussion_r376186956
 
 

 ##########
 File path: 
integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/management/CarbonDeleteStageFilesCommand.scala
 ##########
 @@ -0,0 +1,175 @@
+/*
+ * 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.command.management
+
+import java.io.InputStreamReader
+import java.util
+import java.util.Collections
+
+import scala.collection.JavaConverters._
+
+import com.google.gson.Gson
+import org.apache.hadoop.conf.Configuration
+import org.apache.log4j.Logger
+import org.apache.spark.sql.{CarbonEnv, Row, SparkSession}
+import org.apache.spark.sql.execution.command.{Checker, DataCommand}
+
+import 
org.apache.carbondata.common.exceptions.sql.MalformedCarbonCommandException
+import org.apache.carbondata.common.logging.LogServiceFactory
+import org.apache.carbondata.core.constants.CarbonCommonConstants
+import org.apache.carbondata.core.datastore.filesystem.CarbonFile
+import org.apache.carbondata.core.datastore.impl.FileFactory
+import org.apache.carbondata.core.statusmanager.StageInput
+import org.apache.carbondata.core.util.path.CarbonTablePath
+
+/**
+ * Delete carbon data files of table stages.
+ *
+ * @param databaseNameOp database name
+ * @param tableName      table name
+ */
+case class CarbonDeleteStageFilesCommand(
+    databaseNameOp: Option[String],
+    tableName: String,
+    options: Map[String, String]
+) extends DataCommand {
+
+  @transient val LOGGER: Logger = 
LogServiceFactory.getLogService(this.getClass.getCanonicalName)
+
+  override def processData(spark: SparkSession): Seq[Row] = {
+    Checker.validateTableExists(databaseNameOp, tableName, spark)
+    val table = CarbonEnv.getCarbonTable(databaseNameOp, tableName)(spark)
+    val configuration = spark.sessionState.newHadoopConf()
+    setAuditTable(table)
+    if (!table.getTableInfo.isTransactionalTable) {
+      throw new MalformedCarbonCommandException("Unsupported operation on non 
transactional table")
+    }
+    if (table.isChildTableForMV) {
+      throw new MalformedCarbonCommandException("Unsupported operation on MV 
table")
+    }
+    val tablePath = table.getTablePath
+    val startTime = System.currentTimeMillis()
+    val stageDataFileLocation = CarbonTablePath.getStageDataDir(tablePath)
+    val stageDataFileActiveTime = try {
+      Integer.valueOf(options.getOrElse("retain_hour", "0")) * 3600000
+    } catch {
+      case _: NumberFormatException =>
+        throw new MalformedCarbonCommandException(
+          "Option [retain_hour] is not a number.")
+    }
+    if (stageDataFileActiveTime < 0) {
+      throw new MalformedCarbonCommandException(
+        "Option [retain_hour] is negative.")
+    }
+    val stageDataFilesReferenced =
+      listStageDataFilesReferenced(listStageMetadataFiles(tablePath, 
configuration), configuration)
+    val stageDataFiles = listStageDataFiles(stageDataFileLocation, 
configuration)
+    stageDataFiles.collect {
+      case stageDataFile: CarbonFile =>
+        // Which file will be deleted:
+        // 1. Not referenced by any stage file;
+        // 2. Has passed retain time.
+        if (!stageDataFilesReferenced.contains(stageDataFile.getCanonicalPath) 
&&
+            (startTime - stageDataFile.getLastModifiedTime) >= 
stageDataFileActiveTime) {
+          stageDataFile.delete()
+        }
+    }
+    Seq.empty
+  }
+
+  private def listStageMetadataFiles(
+      tablePath: String,
+      configuration: Configuration
+  ): Seq[CarbonFile] = {
+    val stagePath = CarbonTablePath.getStageDir(tablePath)
+    val stageDirectory = FileFactory.getCarbonFile(stagePath, configuration)
+    if (stageDirectory.exists()) {
+      stageDirectory.listFiles().filter { file =>
+        !file.getName.endsWith(CarbonTablePath.SUCCESS_FILE_SUBFIX)
+      }
+    } else {
+      Seq.empty
+    }
+  }
+
+  private def listStageDataFiles(
+      location: String,
+      configuration: Configuration
+  ): Seq[CarbonFile] = {
+    val stageDataFileLocation = FileFactory.getCarbonFile(location, 
configuration)
+    if (!stageDataFileLocation.exists()) {
+      LOGGER.warn("Stage data file location is not exists. " + location)
+      Seq.empty
+    } else {
+      stageDataFileLocation.listFiles(true).asScala
+    }
+  }
+
+  private def listStageDataFilesReferenced(
 
 Review comment:
   can you add comment to describe the functionality of this function

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to