-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-12518. Compact RocksDB after consecutive deletions #8029
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a08f975
983471d
09037a9
7ce31f9
fd5eb1e
0e04b16
b8352bb
f58b94c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Move this into |
||
| 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then replace this with some metrics. |
||
| return () -> 1; | ||
| } | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.