Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -1291,6 +1291,21 @@ public enum ConfVars {
"metastore.partition.management.table.pattern", "*",
"Automatic partition management will look for tables using the specified table pattern"),

STATISTICS_MANAGEMENT_TASK_FREQUENCY("metastore.statistics.management.task.frequency",
"metastore.statistics.management.task.frequency",
7, TimeUnit.DAYS, "Frequency at which timer task runs to do automatic statistics \n" +
"management for tables. Statistics management include 2 configs. \n" +
"One is 'statistics.auto.deletion', and the other is 'metastore.statistics.retention.period'. \n" +
"When 'statistics.auto.deletion'='true' is set, statistics management will look for tables which their\n " +
"column statistics are over the retention period, and then delete the column stats. \n"),
STATISTICS_RETENTION_PERIOD("metastore.statistics.retention.period",
"metastore.statistics.retention.period", 365, TimeUnit.DAYS, "The retention period " +
"that we want to keep the stats for each table, which means if the stats are older than this period\n" +
"of time, the stats will be automatically deleted. \n"),

STATISTICS_AUTO_DELETION("metastore.statistics.auto.deletion", "metastore.statistics.auto.deletion", false,
"Whether table/partition column statistics will be auto deleted after retention period"),

METASTORE_METADATA_TRANSFORMER_CLASS("metastore.metadata.transformer.class", "metastore.metadata.transformer.class",
"org.apache.hadoop.hive.metastore.MetastoreDefaultTransformer",
"Fully qualified class name for the metastore metadata transformer class \n"
Expand Down Expand Up @@ -1526,7 +1541,8 @@ public enum ConfVars {
ACID_METRICS_TASK_CLASS + "," + ACID_METRICS_LOGGER_CLASS + "," +
"org.apache.hadoop.hive.metastore.HiveProtoEventsCleanerTask" + ","
+ "org.apache.hadoop.hive.metastore.ScheduledQueryExecutionsMaintTask" + ","
+ "org.apache.hadoop.hive.metastore.ReplicationMetricsMaintTask",
+ "org.apache.hadoop.hive.metastore.ReplicationMetricsMaintTask" + ","
+ "org.apache.hadoop.hive.metastore.StatisticsManagementTask",
Comment on lines +1544 to +1545
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

Adding org.apache.hadoop.hive.metastore.StatisticsManagementTask to METASTORE_TASK_THREADS_ALWAYS means it will start in all deployments. Given the new configs currently default to enabling deletion, this can change runtime behavior unexpectedly after upgrade; ensure this is gated behind an explicit opt-in (or keep it out of the always-start list until explicitly configured).

Suggested change
+ "org.apache.hadoop.hive.metastore.ReplicationMetricsMaintTask" + ","
+ "org.apache.hadoop.hive.metastore.StatisticsManagementTask",
+ "org.apache.hadoop.hive.metastore.ReplicationMetricsMaintTask",

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Acknowledged. Set to false by default.

"Comma separated list of tasks that will be started in separate threads. These will " +
"always be started, regardless of whether the metastore is running in embedded mode " +
"or in server mode. They must implement " + METASTORE_TASK_THREAD_CLASS),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
/*
* 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.hadoop.hive.metastore;

import java.util.List;
import java.util.concurrent.TimeUnit;
Comment on lines +21 to +22
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

java.util.Map is imported but not used in this class; please remove the unused import to keep the file clean (and to satisfy builds that fail on unused imports/checkstyle).

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Acknowledged.

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hive.metastore.api.DeleteColumnStatisticsRequest;
import org.apache.hadoop.hive.metastore.conf.MetastoreConf;
import org.apache.hadoop.hive.metastore.model.MTableColumnStatistics;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

/**
* Statistics management task is primarily responsible for auto deletion of table column stats based on a certain frequency

Check warning on line 37 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Line is longer than 120 characters (found 123).

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5k&open=AZ3RuCyAzXxG47XHxi5k&pullRequest=6438
*
* If some table or partition column statistics are older than the configured retention interval
* (MetastoreConf.ConfVars.STATISTICS_RETENTION_PERIOD), they are deleted when this metastore task runs periodically.
*/
public class StatisticsManagementTask extends ObjectStore implements MetastoreTaskThread {
private static final Logger LOG = LoggerFactory.getLogger(StatisticsManagementTask.class);

// The 2 configs for users to set in the conf
// this is an optional table property, if this property does not exist for a table, then it is not excluded
public static final String STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY = "statistics.auto.deletion.exclude";

Check warning on line 47 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'member def modifier' has incorrect indentation level 4, expected level should be 2.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5m&open=AZ3RuCyAzXxG47XHxi5m&pullRequest=6438

private static final Lock lock = new ReentrantLock();

private Configuration conf;

@Override
public long runFrequency(TimeUnit unit) {
return MetastoreConf.getTimeVar(conf, MetastoreConf.ConfVars.STATISTICS_MANAGEMENT_TASK_FREQUENCY, unit);
}

@Override
public void setConf(Configuration configuration) {
// we modify conf in setupConf(), so we make a copy
this.conf = new Configuration(configuration);
super.setConf(configuration);
}

Check warning on line 63 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'method def rcurly' has incorrect indentation level 4, expected level should be 2.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5v&open=AZ3RuCyAzXxG47XHxi5v&pullRequest=6438

@Override

Check warning on line 65 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'method def modifier' has incorrect indentation level 4, expected level should be 2.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5w&open=AZ3RuCyAzXxG47XHxi5w&pullRequest=6438
public Configuration getConf() {
return conf;

Check warning on line 67 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'method def' child has incorrect indentation level 8, expected level should be 4.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5x&open=AZ3RuCyAzXxG47XHxi5x&pullRequest=6438
}

// what needs to be included in this run() method:
// get the "lastAnalyzed" information from TAB_COL_STATS and find all the tables need to be deleted
// delete all column stats
@Override
public void run() {
LOG.debug("Auto statistics deletion started. Cleaning up table/partition column statistics over the retention period.");
long retentionMillis = MetastoreConf.getTimeVar(conf, MetastoreConf.ConfVars.STATISTICS_RETENTION_PERIOD, TimeUnit.MILLISECONDS);
if (retentionMillis <= 0 || !MetastoreConf.getBoolVar(conf, MetastoreConf.ConfVars.STATISTICS_AUTO_DELETION)) {
LOG.info("Statistics auto deletion is set to off currently.");

Check warning on line 78 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'if' child has incorrect indentation level 12, expected level should be 6.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi56&open=AZ3RuCyAzXxG47XHxi56&pullRequest=6438
return;

Check warning on line 79 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'if' child has incorrect indentation level 12, expected level should be 6.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi57&open=AZ3RuCyAzXxG47XHxi57&pullRequest=6438
}

Check warning on line 80 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'if rcurly' has incorrect indentation level 8, expected level should be 4.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi58&open=AZ3RuCyAzXxG47XHxi58&pullRequest=6438
if (!lock.tryLock()) {
return;

Check warning on line 82 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'if' child has incorrect indentation level 12, expected level should be 6.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5-&open=AZ3RuCyAzXxG47XHxi5-&pullRequest=6438
}
try {
long now = System.currentTimeMillis();
long lastAnalyzedThreshold = (now - retentionMillis) / 1000;

String filter = "lastAnalyzed < threshold";
String paramStr = "long threshold";

PersistenceManager pm = getPersistenceManager();
boolean committed = false;
openTransaction(); // open JDO transaction
try {
Query q = null;

Check warning on line 95 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Provide the parametrized type for this generic.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi5h&open=AZ3RuCyAzXxG47XHxi5h&pullRequest=6438
try {
q = pm.newQuery(MTableColumnStatistics.class);

Check warning on line 97 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'try' child has incorrect indentation level 20, expected level should be 10.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi6K&open=AZ3RuCyAzXxG47XHxi6K&pullRequest=6438
q.setFilter(filter);

Check warning on line 98 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'try' child has incorrect indentation level 20, expected level should be 10.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi6L&open=AZ3RuCyAzXxG47XHxi6L&pullRequest=6438
q.declareParameters(paramStr);
q.setResult(
"table.database.name, " +
"table.tableName, " +
"partitionName, " +
"table.parameters.get(\"" + STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY + "\")"
);
Comment on lines +100 to +105
Copy link

Copilot AI Apr 28, 2026

Choose a reason for hiding this comment

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

MTableColumnStatistics/TAB_COL_STATS doesn't have a partitionName field/column; this JDOQL result projection is likely to fail at runtime. If partition-level stats need handling, query MPartitionColumnStatistics and project partition.partitionName (or otherwise join to MPartition) instead of referencing partitionName directly.

Copilot uses AI. Check for mistakes.
@SuppressWarnings("unchecked")
List<Object[]> rows = (List<Object[]>) q.execute(lastAnalyzedThreshold);

try (IMetaStoreClient msc = new HiveMetaStoreClient(conf)) {

Check warning on line 109 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'try' has incorrect indentation level 20, expected level should be 10.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi6F&open=AZ3RuCyAzXxG47XHxi6F&pullRequest=6438
for (Object[] row : rows) {
String dbName = (String) row[0];
String tblName = (String) row[1];
String partName = (String) row[2];
String excludeVal = (String) row[3];

if (excludeVal != null) {
LOG.info("Skipping auto deletion of stats for table {}.{} due to STATISTICS_AUTO_DELETION_EXCLUDE_TBLPROPERTY property being set on the table.", dbName, tblName);

Check warning on line 117 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Line is longer than 120 characters (found 194).

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi6W&open=AZ3RuCyAzXxG47XHxi6W&pullRequest=6438
continue;
}
DeleteColumnStatisticsRequest request = new DeleteColumnStatisticsRequest(dbName, tblName);

Check warning on line 120 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'for' child has incorrect indentation level 28, expected level should be 14.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi6a&open=AZ3RuCyAzXxG47XHxi6a&pullRequest=6438
request.setEngine("hive");
request.setTableLevel(partName == null);
msc.deleteColumnStatistics(request);
}

Check warning on line 124 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'for rcurly' has incorrect indentation level 24, expected level should be 12.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ387xvssQMp_8th3TG-&open=AZ387xvssQMp_8th3TG-&pullRequest=6438
}
} finally {
if (q != null) {

Check warning on line 127 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'if' has incorrect indentation level 20, expected level should be 10.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ3RuCyAzXxG47XHxi6g&open=AZ3RuCyAzXxG47XHxi6g&pullRequest=6438
q.closeAll();
}
Comment thread
DanielZhu58 marked this conversation as resolved.
}

Check warning on line 130 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'finally rcurly' has incorrect indentation level 16, expected level should be 8.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ387xvssQMp_8th3TG_&open=AZ387xvssQMp_8th3TG_&pullRequest=6438
committed = commitTransaction();
} finally {

Check warning on line 132 in standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/StatisticsManagementTask.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

'try rcurly' has incorrect indentation level 12, expected level should be 6.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AZ387xvssQMp_8th3THB&open=AZ387xvssQMp_8th3THB&pullRequest=6438
if (!committed) {
rollbackTransaction();
}
}
} catch (Exception e) {
LOG.error("Error during statistics auto deletion", e);
} finally {
lock.unlock();
}
}
}
Loading
Loading