errose28 commented on a change in pull request #1511: URL: https://github.com/apache/ozone/pull/1511#discussion_r552009620
########## File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OpenKeyCleanupService.java ########## @@ -1,88 +1,251 @@ /** - * 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 + * 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 * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * 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. + * 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; -import org.apache.hadoop.hdds.scm.protocol.ScmBlockLocationProtocol; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; + +import com.google.protobuf.ServiceException; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.hadoop.hdds.conf.ConfigurationSource; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OpenKey; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OpenKeyBucket; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.OMRequest; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.DeleteOpenKeysRequest; +import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos.Type; +import org.apache.hadoop.util.Time; 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.BackgroundTaskResult.EmptyTaskResult; + +import com.google.common.annotations.VisibleForTesting; + +import static org.apache.hadoop.ozone.OzoneConsts.OM_KEY_PREFIX; + +import org.apache.ratis.protocol.ClientId; +import org.apache.ratis.util.Preconditions; +import org.apache.ratis.util.TimeDuration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; -import java.util.concurrent.TimeUnit; - /** - * This is the background service to delete hanging open keys. - * Scan the metadata of om periodically to get - * the keys with prefix "#open#" and ask scm to - * delete metadata accordingly, if scm returns - * success for keys, then clean up those keys. + * Background service to move keys whose creation time is past a given + * threshold from the open key table to the deleted table, where they will + * later be purged by the {@link KeyDeletingService}. */ public class OpenKeyCleanupService extends BackgroundService { - private static final Logger LOG = - LoggerFactory.getLogger(OpenKeyCleanupService.class); + LoggerFactory.getLogger(KeyDeletingService.class); - private final static int OPEN_KEY_DELETING_CORE_POOL_SIZE = 2; + // Use only a single thread for open key deletion. Multiple threads would read + // from the same table and can send deletion requests for same key multiple + // times. + private final static int KEY_DELETING_CORE_POOL_SIZE = 1; + private final OzoneManager ozoneManager; private final KeyManager keyManager; - private final ScmBlockLocationProtocol scmClient; + private final ClientId clientId = ClientId.randomId(); + private final TimeDuration expireThreshold; + private final int cleanupLimitPerTask; + private final AtomicLong submittedOpenKeyCount; + private final AtomicLong runCount; - public OpenKeyCleanupService(ScmBlockLocationProtocol scmClient, - KeyManager keyManager, int serviceInterval, - long serviceTimeout) { - super("OpenKeyCleanupService", serviceInterval, TimeUnit.SECONDS, - OPEN_KEY_DELETING_CORE_POOL_SIZE, serviceTimeout); + OpenKeyCleanupService(OzoneManager ozoneManager, KeyManager keyManager, + long serviceInterval, ConfigurationSource conf) { + + super("OpenKeyCleanupService", serviceInterval, TimeUnit.MILLISECONDS, + KEY_DELETING_CORE_POOL_SIZE); + this.ozoneManager = ozoneManager; this.keyManager = keyManager; - this.scmClient = scmClient; + + long expireDuration = conf.getTimeDuration( + OMConfigKeys.OZONE_OPEN_KEY_EXPIRE_THRESHOLD, + OMConfigKeys.OZONE_OPEN_KEY_EXPIRE_THRESHOLD_DEFAULT.getDuration(), + TimeUnit.MILLISECONDS); + + this.expireThreshold = + TimeDuration.valueOf(expireDuration, TimeUnit.MILLISECONDS); + + this.cleanupLimitPerTask = conf.getInt( + OMConfigKeys.OZONE_OPEN_KEY_CLEANUP_LIMIT_PER_TASK, + OMConfigKeys.OZONE_OPEN_KEY_CLEANUP_LIMIT_PER_TASK_DEFAULT); + + this.submittedOpenKeyCount = new AtomicLong(0); + this.runCount = new AtomicLong(0); + } + + /** + * Returns the number of times this Background service has run. + * + * @return Long, run count. + */ + @VisibleForTesting + public AtomicLong getRunCount() { + return runCount; + } + + /** + * Returns the number of open keys that were submitted for deletion by this + * service. If these keys were committed from the open key table between + * being submitted for deletion and the actual delete operation, they will + * not be deleted. + * + * @return Long count. + */ + @VisibleForTesting + public AtomicLong getSubmittedOpenKeyCount() { + return submittedOpenKeyCount; } @Override public BackgroundTaskQueue getTasks() { BackgroundTaskQueue queue = new BackgroundTaskQueue(); - queue.add(new OpenKeyDeletingTask()); + queue.add(new OpenKeyCleanupTask()); return queue; } - private class OpenKeyDeletingTask implements BackgroundTask { + private boolean shouldRun() { + if (ozoneManager == null) { + // OzoneManager can be null for testing + return true; + } + return ozoneManager.isLeader(); Review comment: I am now using the isLeaderReady API here. Let me know if I have done so correctly. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
