LZD-PratyushBhatt commented on code in PR #4880: URL: https://github.com/apache/ozone/pull/4880#discussion_r1235066802
########## hadoop-ozone/recon/src/main/java/org/apache/hadoop/ozone/recon/tasks/ScmTableCountTask.java: ########## @@ -0,0 +1,219 @@ +/* + * 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. + */ + +package org.apache.hadoop.ozone.recon.tasks; + +import org.apache.hadoop.hdds.utils.db.DBStore; +import org.apache.hadoop.hdds.utils.db.Table; +import org.apache.hadoop.hdds.utils.db.TableIterator; +import org.apache.hadoop.ozone.recon.scm.ReconScmTask; +import org.apache.hadoop.ozone.recon.scm.ReconStorageContainerManagerFacade; +import org.hadoop.ozone.recon.schema.UtilizationSchemaDefinition; +import org.hadoop.ozone.recon.schema.tables.daos.ReconTaskStatusDao; +import org.hadoop.ozone.recon.schema.tables.daos.ScmTableCountDao; +import org.hadoop.ozone.recon.schema.tables.pojos.ScmTableCount; +import org.jooq.DSLContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; + +import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Iterator; +import java.util.Collection; +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import static org.apache.hadoop.hdds.scm.metadata.SCMDBDefinition.DELETED_BLOCKS; +import static org.hadoop.ozone.recon.schema.UtilizationSchemaDefinition.SCM_TABLE_COUNT_TABLE_NAME; + + +/** + * Any background task that tracks SCM's table counts. + */ +public class ScmTableCountTask extends ReconScmTask { + + private static final Logger LOG = + LoggerFactory.getLogger(ScmTableCountTask.class); + + private final DBStore scmDBStore; + private final ScmTableCountDao scmTableCountDao; + private final DSLContext dslContext; + private final long interval; + private ReadWriteLock lock = new ReentrantReadWriteLock(true); + + public ScmTableCountTask(ReconStorageContainerManagerFacade reconSCM, + ReconTaskStatusDao reconTaskStatusDao, + ReconTaskConfig reconTaskConfig, + ScmTableCountDao scmTableCountDao, + UtilizationSchemaDefinition schemaDefinition) { + super(reconTaskStatusDao); + this.scmDBStore = reconSCM.getScmDBStore(); + this.scmTableCountDao = scmTableCountDao; + this.dslContext = schemaDefinition.getDSLContext(); + this.interval = reconTaskConfig.getScmTableCountTaskInterval().toMillis(); + } + + @Override + protected synchronized void run() { + try { + while (canRun()) { + wait(interval); + long startTime, endTime, duration, durationMilliseconds; + try { + int execute = + dslContext.truncate(SCM_TABLE_COUNT_TABLE_NAME).execute(); + LOG.info("Deleted {} records from {}", execute, + SCM_TABLE_COUNT_TABLE_NAME); + } catch (Exception e) { + LOG.error("An error occurred while truncating the table {}: {}", + SCM_TABLE_COUNT_TABLE_NAME, e.getMessage(), e); + return; + } + startTime = System.nanoTime(); + processTableCount(); + endTime = System.nanoTime(); + duration = endTime - startTime; + durationMilliseconds = duration / 1_000_000; + LOG.info("Elapsed Time in milliseconds for processTableCount() " + + "execution: {}", durationMilliseconds); + } Review Comment: Can we extract the logic into a separate method, and then call that method inside this loop? will look more cleaner. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
