Skip to content
Closed
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>true</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>5m</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.threshold</name>
<value>10000</value>
<tag>OZONE, OM, PERFORMANCE</tag>
<description>
Compact rocksdb column families when the number of deleted keys
exceeds this number.
</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 @@ -30,6 +30,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.hadoop.hdds.utils.IOUtils;
import org.apache.hadoop.hdds.utils.MetadataKeyFilters;
import org.apache.hadoop.hdds.utils.TableCacheMetrics;
Expand Down Expand Up @@ -67,6 +68,7 @@ public class TypedTable<KEY, VALUE> implements Table<KEY, VALUE> {
private final CodecBuffer.Capacity bufferCapacity
= new CodecBuffer.Capacity(this, BUFFER_SIZE_DEFAULT);
private final TableCache<KEY, VALUE> cache;
private final AtomicLong uncompactedDeletes = new AtomicLong(0);

/**
* The same as this(rawTable, codecRegistry, keyType, valueType,
Expand Down Expand Up @@ -399,19 +401,30 @@ public void delete(KEY key) throws IOException {
} else {
rawTable.delete(encodeKey(key));
}
uncompactedDeletes.addAndGet(1);
}

@Override
public void deleteWithBatch(BatchOperation batch, KEY key)
throws IOException {
rawTable.deleteWithBatch(batch, encodeKey(key));
uncompactedDeletes.addAndGet(1);
}

@Override
public void deleteRange(KEY beginKey, KEY endKey) throws IOException {
rawTable.deleteRange(encodeKey(beginKey), encodeKey(endKey));
}

public AtomicLong getUncompactedDeletes() {
return uncompactedDeletes;
}


public void resetUncompactedDeletes() {
uncompactedDeletes.set(0);
}

@Override
public Table.KeyValueIterator<KEY, VALUE> iterator() throws IOException {
return iterator(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -622,4 +622,21 @@ private OMConfigKeys() {
public static final String OZONE_OM_MAX_BUCKET =
"ozone.om.max.buckets";
public static final int OZONE_OM_MAX_BUCKET_DEFAULT = 100000;
/**
* Configuration properties for Compaction Service.
*/
public static final String OZONE_COMPACTION_SERVICE_ENABLED = "ozone.compaction.service.enabled";
public static final boolean OZONE_COMPACTION_SERVICE_ENABLED_DEFAULT = true;
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.MINUTES.toMillis(5);

public static final String OZONE_OM_COMPACTION_SERVICE_TIMEOUT
= "ozone.om.compaction.service.timeout";
public static final String OZONE_COMPACTION_SERVICE_TIMEOUT_DEFAULT = "10m";
public static final String OZONE_OM_COMPACTION_SERVICE_THRESHOLD
= "ozone.om.compaction.service.threshold";
public static final String OZONE_COMPACTION_SERVICE_THRESHOLD_DEFAULT = "10000";
}
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 Compaction service.
* @return Background service.
*/
CompactionService getCompactionService();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@
import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SNAPSHOT_SST_FILTERING_SERVICE_TIMEOUT_DEFAULT;
import static org.apache.hadoop.ozone.OzoneConsts.ETAG;
import static org.apache.hadoop.ozone.OzoneConsts.OZONE_URI_DELIMITER;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_COMPACTION_SERVICE_ENABLED;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_COMPACTION_SERVICE_ENABLED_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_COMPACTION_SERVICE_THRESHOLD_DEFAULT;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_COMPACTION_SERVICE_TIMEOUT_DEFAULT;
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_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_THRESHOLD;
import static org.apache.hadoop.ozone.om.OMConfigKeys.OZONE_OM_COMPACTION_SERVICE_TIMEOUT;
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 @@ -150,6 +158,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 +207,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 +237,9 @@ public KeyManagerImpl(OzoneManager om, ScmClient scmClient,

@Override
public void start(OzoneConfiguration configuration) {
boolean isCompactionServiceEnabled = configuration.getBoolean(OZONE_COMPACTION_SERVICE_ENABLED,
OZONE_COMPACTION_SERVICE_ENABLED_DEFAULT);
startCompactionService(configuration, isCompactionServiceEnabled);
boolean isSnapshotDeepCleaningEnabled = configuration.getBoolean(OZONE_SNAPSHOT_DEEP_CLEANING_ENABLED,
OZONE_SNAPSHOT_DEEP_CLEANING_ENABLED_DEFAULT);
if (keyDeletingService == null) {
Expand Down Expand Up @@ -363,6 +376,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_COMPACTION_SERVICE_TIMEOUT_DEFAULT,
TimeUnit.MILLISECONDS);
long compactionThreshold = configuration.getTimeDuration(
OZONE_OM_COMPACTION_SERVICE_THRESHOLD,
OZONE_COMPACTION_SERVICE_THRESHOLD_DEFAULT,
TimeUnit.MILLISECONDS);
compactionService = new CompactionService(ozoneManager, TimeUnit.MILLISECONDS,
compactionInterval, serviceTimeout, compactionThreshold);
compactionService.start();
}
}

KeyProviderCryptoExtension getKMSProvider() {
return kmsProvider;
}
Expand Down Expand Up @@ -397,6 +431,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 @@ -806,6 +844,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,164 @@
/*
* 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 static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.DELETED_DIR_TABLE;
import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.DELETED_TABLE;
import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.DIRECTORY_TABLE;
import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.FILE_TABLE;
import static org.apache.hadoop.ozone.om.OmMetadataManagerImpl.KEY_TABLE;

import com.google.common.annotations.VisibleForTesting;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
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.TypedTable;
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;
private final long compactionThreshold;
// list of tables that can be compacted
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not compact all tables that exceed the threshold? The counter is already being tracked at for every column family.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good question. I was under the impression that snapshot related tables shouldn't be compacted. But I could be wrong. @swamirishi thoughts on this?

Also, It's based on my observation of the most active tables. I can imagine multipartInfoTable would benefit from this too.

private static final List<String> COMPACTABLE_TABLES =
Arrays.asList(DELETED_DIR_TABLE, DELETED_TABLE, DIRECTORY_TABLE, FILE_TABLE, KEY_TABLE);

public CompactionService(OzoneManager ozoneManager, TimeUnit unit, long interval, long timeout,
long compactionThreshold) {
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.compactionThreshold = compactionThreshold;
}

/**
* 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);
}

/**
* 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 : COMPACTABLE_TABLES) {
TypedTable table = (TypedTable)omMetadataManager.getTable(tableName);
if (table.getUncompactedDeletes().get() > compactionThreshold) {
queue.add(new CompactTask(tableName));
table.resetUncompactedDeletes();
}
}
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);
// Find CF Handler
RocksDatabase rocksDatabase = ((RDBStore) omMetadataManager.getStore()).getDb();
RocksDatabase.ColumnFamily columnFamily = rocksDatabase.getColumnFamily(tableName);
rocksDatabase.compactRange(columnFamily, null, null, options);
Comment on lines +129 to +132
Copy link
Member

Choose a reason for hiding this comment

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

Move this into RDBStore#compactRange(string tableName, Option opt)?

LOG.info("Compaction of column family: {} completed in {} ms",
tableName, Time.monotonicNow() - startTime);
}
}

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();
Copy link
Member

Choose a reason for hiding this comment

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

This seems only used for test, how about move this logic into test with overrided CompactionService instance.

Copy link
Member

Choose a reason for hiding this comment

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

Then replace this with some metrics.

return () -> 1;
}
}

}
Loading