Skip to content
Merged
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
37 changes: 37 additions & 0 deletions hadoop-hdds/common/src/main/resources/ozone-default.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,43 @@
</description>
</property>

<property>
<name>ozone.compaction.service.enabled</name>
<value>false</value>
<tag>OZONE, OM, PERFORMANCE</tag>
<description>
Enable or disable a background job that periodically compacts rocksdb tables flagged for compaction.
</description>
</property>
<property>
<name>ozone.om.compaction.service.run.interval</name>
<value>6h</value>
<tag>OZONE, OM, PERFORMANCE</tag>
<description>
A background job that periodically compacts rocksdb tables flagged for compaction.
Unit could be defined with postfix (ns,ms,s,m,h,d)
</description>
</property>
<property>
<name>ozone.om.compaction.service.timeout</name>
<value>10m</value>
<tag>OZONE, OM, PERFORMANCE</tag>
<description>A timeout value of compaction service. If this is set
greater than 0, the service will stop waiting for compaction
completion after this time.
Unit could be defined with postfix (ns,ms,s,m,h,d)
</description>
</property>
<property>
<name>ozone.om.compaction.service.columnfamilies</name>
<value>keyTable,fileTable,directoryTable,deletedTable,deletedDirectoryTable,multipartInfoTable</value>
<tag>OZONE, OM, PERFORMANCE</tag>
<description>A comma separated, no spaces list of all the column families
that are compacted by the compaction service.
If this is empty, no column families are compacted.
</description>
</property>

<property>
<name>ozone.om.snapshot.rocksdb.metrics.enabled</name>
<value>false</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,22 @@ private OMConfigKeys() {
public static final String OZONE_OM_EDEKCACHELOADER_MAX_RETRIES_KEY =
"ozone.om.edekcacheloader.max-retries";
public static final int OZONE_OM_EDEKCACHELOADER_MAX_RETRIES_DEFAULT = 10;

/**
* Configuration properties for Compaction Service.
*/
public static final String OZONE_OM_COMPACTION_SERVICE_ENABLED = "ozone.compaction.service.enabled";
Copy link
Member

Choose a reason for hiding this comment

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

@Tejaskriya
Noticed that there is a typo here.

- public static final String OZONE_OM_COMPACTION_SERVICE_ENABLED = "ozone.compaction.service.enabled";
+ public static final String OZONE_OM_COMPACTION_SERVICE_ENABLED = "ozone.om.compaction.service.enabled";

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although it is consistent with what the config is named in ozone-default.xml, I will correct it in a followup jira. Thanks for catching this!

@jojochuang I am not sure if someone using this version of the patch would face any issues. Do you happen to know if not following the config naming convention would cause any breakage?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

HDDS-13525 fixes this

public static final boolean OZONE_OM_COMPACTION_SERVICE_ENABLED_DEFAULT = false;
public static final String OZONE_OM_COMPACTION_SERVICE_RUN_INTERVAL =
"ozone.om.compaction.service.run.interval";
public static final long OZONE_OM_COMPACTION_SERVICE_RUN_INTERVAL_DEFAULT
= TimeUnit.HOURS.toMillis(6);

public static final String OZONE_OM_COMPACTION_SERVICE_TIMEOUT
= "ozone.om.compaction.service.timeout";
public static final String OZONE_OM_COMPACTION_SERVICE_TIMEOUT_DEFAULT = "10m";
public static final String OZONE_OM_COMPACTION_SERVICE_COLUMNFAMILIES
= "ozone.om.compaction.service.columnfamilies";
public static final String OZONE_OM_COMPACTION_SERVICE_COLUMNFAMILIES_DEFAULT =
"keyTable,fileTable,directoryTable,deletedTable,deletedDirectoryTable,multipartInfoTable";
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.apache.hadoop.ozone.om.helpers.OmKeyInfo;
import org.apache.hadoop.ozone.om.helpers.OmMultipartUploadList;
import org.apache.hadoop.ozone.om.helpers.OmMultipartUploadListParts;
import org.apache.hadoop.ozone.om.service.CompactionService;
import org.apache.hadoop.ozone.om.service.DirectoryDeletingService;
import org.apache.hadoop.ozone.om.service.KeyDeletingService;
import org.apache.hadoop.ozone.om.service.SnapshotDeletingService;
Expand Down Expand Up @@ -302,4 +303,10 @@ DeleteKeysResult getPendingDeletionSubFiles(long volumeId,
* @return Background service.
*/
SnapshotDirectoryCleaningService getSnapshotDirectoryService();

/**
* Returns the instance of CompactionService.
* @return BackgroundService
*/
CompactionService getCompactionService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_DIR_DELETING_SERVICE_INTERVAL;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_DIR_DELETING_SERVICE_INTERVAL_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_COLUMNFAMILIES;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_COLUMNFAMILIES_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_ENABLED;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_ENABLED_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_RUN_INTERVAL;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_RUN_INTERVAL_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_TIMEOUT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_TIMEOUT_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_MPU_CLEANUP_SERVICE_INTERVAL;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_MPU_CLEANUP_SERVICE_INTERVAL_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_MPU_CLEANUP_SERVICE_TIMEOUT;
Expand Down Expand Up @@ -80,6 +88,7 @@
import java.security.PrivilegedExceptionAction;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
Expand Down Expand Up @@ -150,6 +159,7 @@
import org.apache.hadoop.ozone.om.request.OMClientRequest;
import org.apache.hadoop.ozone.om.request.file.OMFileRequest;
import org.apache.hadoop.ozone.om.request.util.OMMultipartUploadUtils;
import org.apache.hadoop.ozone.om.service.CompactionService;
import org.apache.hadoop.ozone.om.service.DirectoryDeletingService;
import org.apache.hadoop.ozone.om.service.KeyDeletingService;
import org.apache.hadoop.ozone.om.service.MultipartUploadCleanupService;
Expand Down Expand Up @@ -198,6 +208,7 @@ public class KeyManagerImpl implements KeyManager {
private BackgroundService multipartUploadCleanupService;
private SnapshotDirectoryCleaningService snapshotDirectoryCleaningService;
private DNSToSwitchMapping dnsToSwitchMapping;
private CompactionService compactionService;

public KeyManagerImpl(OzoneManager om, ScmClient scmClient,
OzoneConfiguration conf, OMPerformanceMetrics metrics) {
Expand Down Expand Up @@ -227,6 +238,10 @@ public KeyManagerImpl(OzoneManager om, ScmClient scmClient,

@Override
public void start(OzoneConfiguration configuration) {
boolean isCompactionEnabled = configuration.getBoolean(OZONE_OM_COMPACTION_SERVICE_ENABLED,
OZONE_OM_COMPACTION_SERVICE_ENABLED_DEFAULT);
startCompactionService(configuration, isCompactionEnabled);

boolean isSnapshotDeepCleaningEnabled = configuration.getBoolean(OZONE_SNAPSHOT_DEEP_CLEANING_ENABLED,
OZONE_SNAPSHOT_DEEP_CLEANING_ENABLED_DEFAULT);
if (keyDeletingService == null) {
Expand Down Expand Up @@ -363,6 +378,27 @@ public void start(OzoneConfiguration configuration) {
: new CachedDNSToSwitchMapping(newInstance));
}

private void startCompactionService(OzoneConfiguration configuration,
boolean isCompactionServiceEnabled) {
if (compactionService == null && isCompactionServiceEnabled) {
long compactionInterval = configuration.getTimeDuration(
OZONE_OM_COMPACTION_SERVICE_RUN_INTERVAL,
OZONE_OM_COMPACTION_SERVICE_RUN_INTERVAL_DEFAULT,
TimeUnit.MILLISECONDS);
long serviceTimeout = configuration.getTimeDuration(
OZONE_OM_COMPACTION_SERVICE_TIMEOUT,
OZONE_OM_COMPACTION_SERVICE_TIMEOUT_DEFAULT,
TimeUnit.MILLISECONDS);
String compactionColumnFamilies = configuration.get(
OZONE_OM_COMPACTION_SERVICE_COLUMNFAMILIES,
OZONE_OM_COMPACTION_SERVICE_COLUMNFAMILIES_DEFAULT);
String[] tables = compactionColumnFamilies.split(",");
compactionService = new CompactionService(ozoneManager, TimeUnit.MILLISECONDS,
compactionInterval, serviceTimeout, Arrays.asList(tables));
compactionService.start();
}
}

KeyProviderCryptoExtension getKMSProvider() {
return kmsProvider;
}
Expand Down Expand Up @@ -397,6 +433,10 @@ public void stop() throws IOException {
snapshotDirectoryCleaningService.shutdown();
snapshotDirectoryCleaningService = null;
}
if (compactionService != null) {
compactionService.shutdown();
compactionService = null;
}
}

private OmBucketInfo getBucketInfo(String volumeName, String bucketName)
Expand Down Expand Up @@ -807,6 +847,11 @@ public SnapshotDirectoryCleaningService getSnapshotDirectoryService() {
return snapshotDirectoryCleaningService;
}

@Override
public CompactionService getCompactionService() {
return compactionService;
}

public boolean isSstFilteringSvcEnabled() {
long serviceInterval = ozoneManager.getConfiguration()
.getTimeDuration(OZONE_SNAPSHOT_SST_FILTERING_SERVICE_INTERVAL,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/*
* 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.ozone.om.service;

import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.hadoop.hdds.utils.BackgroundService;
import org.apache.hadoop.hdds.utils.BackgroundTask;
import org.apache.hadoop.hdds.utils.BackgroundTaskQueue;
import org.apache.hadoop.hdds.utils.BackgroundTaskResult;
import org.apache.hadoop.hdds.utils.db.RDBStore;
import org.apache.hadoop.hdds.utils.db.RocksDatabase;
import org.apache.hadoop.hdds.utils.db.managed.ManagedCompactRangeOptions;
import org.apache.hadoop.ozone.om.OMMetadataManager;
import org.apache.hadoop.ozone.om.OzoneManager;
import org.apache.hadoop.util.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This is the background service to compact OM rocksdb tables.
*/
public class CompactionService extends BackgroundService {
private static final Logger LOG =
LoggerFactory.getLogger(CompactionService.class);

// Use only a single thread for Compaction.
private static final int COMPACTOR_THREAD_POOL_SIZE = 1;

private final OzoneManager ozoneManager;
private final OMMetadataManager omMetadataManager;
private final AtomicLong numCompactions;
private final AtomicBoolean suspended;
// list of tables that can be compacted
private final List<String> compactableTables;

public CompactionService(OzoneManager ozoneManager, TimeUnit unit, long interval, long timeout,
List<String> tables) {
super("CompactionService", interval, unit,
COMPACTOR_THREAD_POOL_SIZE, timeout,
ozoneManager.getThreadNamePrefix());
this.ozoneManager = ozoneManager;
this.omMetadataManager = this.ozoneManager.getMetadataManager();

this.numCompactions = new AtomicLong(0);
this.suspended = new AtomicBoolean(false);
this.compactableTables = validateTables(tables);
}

private List<String> validateTables(List<String> tables) {
if (tables == null || tables.isEmpty()) {
return Collections.emptyList();
}
List<String> validTables = new ArrayList<>();
Set<String> allTableNames = new HashSet<>(omMetadataManager.listTableNames());
for (String table : tables) {
if (allTableNames.contains(table)) {
validTables.add(table);
} else {
LOG.warn("CompactionService: Table \"{}\" not found in OM metadata. Skipping this table.", table);
}
}
if (validTables.isEmpty()) {
LOG.error("CompactionService: No valid compaction tables found. Failing initialization.");
throw new IllegalArgumentException("CompactionService: None of the provided tables are valid.");
}
return Collections.unmodifiableList(validTables);
}

/**
* Suspend the service (for testing).
*/
@VisibleForTesting
public void suspend() {
suspended.set(true);
}

/**
* Resume the service if suspended (for testing).
*/
@VisibleForTesting
public void resume() {
suspended.set(false);
}

@VisibleForTesting
public List<String> getCompactableTables() {
return compactableTables;
}

/**
* Returns the number of manual compactions performed.
*
* @return long count.
*/
@VisibleForTesting
public long getNumCompactions() {
return numCompactions.get();
}

@Override
public synchronized BackgroundTaskQueue getTasks() {
BackgroundTaskQueue queue = new BackgroundTaskQueue();
for (String tableName : compactableTables) {
queue.add(new CompactTask(tableName));
}
return queue;
}

private boolean shouldRun() {
return !suspended.get();
}

protected void compactFully(String tableName) throws IOException {
long startTime = Time.monotonicNow();
LOG.info("Compacting column family: {}", tableName);
try (ManagedCompactRangeOptions options = new ManagedCompactRangeOptions()) {
options.setBottommostLevelCompaction(ManagedCompactRangeOptions.BottommostLevelCompaction.kForce);
options.setExclusiveManualCompaction(true);
RocksDatabase rocksDatabase = ((RDBStore) omMetadataManager.getStore()).getDb();

try {
// Find CF Handler
RocksDatabase.ColumnFamily columnFamily = rocksDatabase.getColumnFamily(tableName);
rocksDatabase.compactRange(columnFamily, null, null, options);
LOG.info("Compaction of column family: {} completed in {} ms",
tableName, Time.monotonicNow() - startTime);
} catch (NullPointerException ex) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should check null instead of catching NullPointerException.

BTW, the commit message somehow used HDDS-12518 instead of HDDS-12819.

commit b91e8e732f07e220b1be6b64fb74a8d3f67dbf3c
Author: Tejaskriya <[email protected]>
Date:   Wed Apr 16 13:19:49 2025 +0530

    HDDS-12518. Auto-compact tables which can tend to be large in size at intervals (#8260)

LOG.error("Unable to trigger compaction for \"{}\". Column family not found ", tableName);
throw new IOException("Column family \"" + tableName + "\" not found.");
}
}
}

private class CompactTask implements BackgroundTask {
private final String tableName;

CompactTask(String tableName) {
this.tableName = tableName;
}

@Override
public int getPriority() {
return 0;
}

@Override
public BackgroundTaskResult call() throws Exception {
// trigger full compaction for the specified table.
if (!shouldRun()) {
return BackgroundTaskResult.EmptyTaskResult.newResult();
}
LOG.debug("Running CompactTask");

compactFully(tableName);
numCompactions.incrementAndGet();
return () -> 1;
}
}

}
Loading