|
| 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, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hive.metastore; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import java.util.concurrent.CountDownLatch; |
| 28 | +import java.util.concurrent.ExecutorService; |
| 29 | +import java.util.concurrent.Executors; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import java.util.concurrent.locks.Lock; |
| 32 | +import java.util.concurrent.locks.ReentrantLock; |
| 33 | + |
| 34 | +import org.apache.hadoop.conf.Configuration; |
| 35 | +import org.apache.hadoop.hive.common.TableName; |
| 36 | +import org.apache.hadoop.hive.metastore.api.Database; |
| 37 | +import org.apache.hadoop.hive.metastore.api.DeleteColumnStatisticsRequest; |
| 38 | +import org.apache.hadoop.hive.metastore.api.Partition; |
| 39 | +import org.apache.hadoop.hive.metastore.api.Table; |
| 40 | +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; |
| 41 | +import org.apache.hadoop.hive.metastore.conf.MetastoreConf; |
| 42 | +import org.apache.hadoop.hive.metastore.conf.TimeValidator; |
| 43 | +import org.apache.hadoop.hive.metastore.model.MPartitionColumnStatistics; |
| 44 | +import org.apache.hadoop.hive.metastore.model.MTable; |
| 45 | +import org.apache.hadoop.hive.metastore.model.MTableColumnStatistics; |
| 46 | +import org.apache.hadoop.hive.metastore.utils.MetaStoreUtils; |
| 47 | +import org.slf4j.Logger; |
| 48 | +import org.slf4j.LoggerFactory; |
| 49 | + |
| 50 | +import com.google.common.annotations.VisibleForTesting; |
| 51 | +import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 52 | + |
| 53 | +import javax.jdo.PersistenceManager; |
| 54 | +import javax.jdo.Query; |
| 55 | + |
| 56 | +/** |
| 57 | + * Statistics management task is primarily responsible for auto deletion of table column stats based on a certain frequency |
| 58 | + * |
| 59 | + * If some table column statistics are older than the period time, they should be deleted automatically |
| 60 | + * Statistics Retention - If "partition.retention.period" table property is set with retention interval, when this |
| 61 | + * metastore task runs periodically, it will drop partitions with age (creation time) greater than retention period. |
| 62 | + * Dropping partitions after retention period will also delete the data in that partition. |
| 63 | + * |
| 64 | + */ |
| 65 | +public class StatisticsManagementTask implements MetastoreTaskThread { |
| 66 | + private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagementTask.class); |
| 67 | + |
| 68 | + // global |
| 69 | + public static final String STATISTICS_AUTO_DELETION = "statistics.auto.deletion"; |
| 70 | + public static final String STATISTICS_RETENTION_PERIOD = "statistics.retention.period"; |
| 71 | + |
| 72 | + // The 2 configs for users to set in the conf |
| 73 | + // this is an optional table property, if this property does not exist for a table, then it is not excluded |
| 74 | + public static final String STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY = "statistics.auto.deletion.exclude"; |
| 75 | + |
| 76 | + private static final Lock lock = new ReentrantLock(); |
| 77 | + |
| 78 | + // these are just for testing |
| 79 | + private static int completedAttempts; |
| 80 | + private static int skippedAttempts; |
| 81 | + |
| 82 | + private Configuration conf; |
| 83 | + |
| 84 | + @Override |
| 85 | + public long runFrequency(TimeUnit unit) { |
| 86 | + return MetastoreConf.getTimeVar(conf, MetastoreConf.ConfVars.STATISTICS_MANAGEMENT_TASK_FREQUENCY, unit); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void setConf(Configuration configuration) { |
| 91 | + // we modify conf in setupConf(), so we make a copy |
| 92 | + conf = new Configuration(configuration); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public Configuration getConf() { |
| 97 | + return conf; |
| 98 | + } |
| 99 | + |
| 100 | + // what needs to be included in this run() method: |
| 101 | + // get the "lastAnalyzed" information from TAB_COL_STATS and find all the tables need to be deleted |
| 102 | + // delete all column stats |
| 103 | + @Override |
| 104 | + public void run() { |
| 105 | + if (LOG.isDebugEnabled()) { |
| 106 | + LOG.debug("Auto statistics deletion started. Cleaning up table/partition column statistics over the retention period."); |
| 107 | + } |
| 108 | + long retentionMillis = MetastoreConf.getTimeVar(conf, MetastoreConf.ConfVars. STATISTICS_RETENTION_PERIOD, TimeUnit.MILLISECONDS); |
| 109 | + if (retentionMillis <= 0 || !MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.STATISTICS_AUTO_DELETION)) { |
| 110 | + LOG.info("Statistics auto deletion is set to off currently."); |
| 111 | + return; |
| 112 | + } |
| 113 | + if (lock.tryLock()) { |
| 114 | + skippedAttempts = 0; |
| 115 | + String qualifiedTableName = null; |
| 116 | + IMetaStoreClient msc = null; |
| 117 | + try { |
| 118 | + // Get retention period in conf in milliseconds; default is 365 days. |
| 119 | + long now = System.currentTimeMillis(); |
| 120 | + long lastAnalyzedThreshold = (now - retentionMillis) / 1000; |
| 121 | + |
| 122 | + // Get all databases from metastore |
| 123 | + List<String> databases = msc.getAllDatabases(); |
| 124 | + RawStore ms = HMSHandler.getMSForConf(conf); |
| 125 | + PersistenceManager pm = ((ObjectStore) ms).getPersistenceManager(); |
| 126 | + Query q = pm.newQuery("SELECT FROM org.apache.hadoop.hive.metastore.model.MTableColumnStatistics"); |
| 127 | + q.setFilter("lastAnalyzed < " + lastAnalyzedThreshold); |
| 128 | + List<MTableColumnStatistics> results = (List<MTableColumnStatistics>) q.execute(); |
| 129 | + |
| 130 | + for (MTableColumnStatistics stat : results) { |
| 131 | + String dbName = stat.getTable().getDatabase().getName(); |
| 132 | + String tblName = stat.getTable().getTableName(); |
| 133 | + Map<String, String> tblParams = stat.getTable().getParameters(); |
| 134 | + if (tblParams != null && tblParams.getOrDefault(STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY, null) != null) { |
| 135 | + LOG.info("Skipping table {}.{} due to exclude property.", dbName, tblName); |
| 136 | + continue; |
| 137 | + } |
| 138 | + /** |
| 139 | + // if this table contains "lastAnalyzed" in table property, we process the auto stats deletion |
| 140 | + long lastAnalyzed = stat.getLastAnalyzed(); |
| 141 | + // lastAnalyzed is in unit seconds, switch it to milliseconds |
| 142 | + lastAnalyzed *= 1000; |
| 143 | + **/ |
| 144 | + |
| 145 | + DeleteColumnStatisticsRequest request = new DeleteColumnStatisticsRequest(dbName, tblName); |
| 146 | + request.setEngine("hive"); |
| 147 | + boolean isPartitioned = stat.getTable().getPartitionKeys() != null && !stat.getTable().getPartitionKeys().isEmpty(); |
| 148 | + // Delete table-level column statistics |
| 149 | + if (!isPartitioned) { |
| 150 | + request.setTableLevel(true); |
| 151 | + } else { |
| 152 | + request.setTableLevel(false); |
| 153 | + } |
| 154 | + msc.deleteColumnStatistics(request); |
| 155 | + } |
| 156 | + } catch (Exception e) { |
| 157 | + LOG.error("Error during statistics auto deletion", e); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments