anuengineer commented on a change in pull request #810: HDDS-1512. Implement DoubleBuffer in OzoneManager. URL: https://github.com/apache/hadoop/pull/810#discussion_r285337258
########## File path: hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/ratis/OzoneManagerDoubleBuffer.java ########## @@ -0,0 +1,212 @@ +/** + * 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.om.ratis; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.concurrent.atomic.AtomicLong; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import org.apache.hadoop.ozone.om.OMMetadataManager; +import org.apache.hadoop.ozone.om.ratis.helpers.DoubleBufferEntry; +import org.apache.hadoop.ozone.om.response.OMClientResponse; +import org.apache.hadoop.util.Daemon; +import org.apache.hadoop.util.ExitUtil; +import org.apache.hadoop.utils.db.BatchOperation; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A double-buffer for OM requests. + */ +public class OzoneManagerDoubleBuffer { + + private static final Logger LOG = + LoggerFactory.getLogger(OzoneManagerDoubleBuffer.class.getName()); + + private TransactionBuffer currentBuffer; + private TransactionBuffer readyBuffer; + private Daemon daemon; + private volatile boolean syncToDB; + private final OMMetadataManager omMetadataManager; + private AtomicLong flushedTransactionCount = new AtomicLong(0); + private AtomicLong flushIterations = new AtomicLong(0); + + public OzoneManagerDoubleBuffer(OMMetadataManager omMetadataManager) { + this.currentBuffer = new TransactionBuffer(); + this.readyBuffer = new TransactionBuffer(); + this.omMetadataManager = omMetadataManager; + + // Daemon thread which runs in back ground and flushes transactions to DB. + daemon = new Daemon(this::flushTransactions); + daemon.start(); + + } + + /** + * Runs in a background thread and batches the transaction in currentBuffer + * and commit to DB. + */ + private void flushTransactions() { + while(true) { + if (canFlush()) { + syncToDB = true; + setReadyBuffer(); + final BatchOperation batchOperation = omMetadataManager.getStore() + .initBatchOperation(); + + readyBuffer.iterator().forEachRemaining((entry) -> { + try { + entry.getResponse().addToRocksDBBatch(omMetadataManager, + batchOperation); + } catch (IOException ex) { + // During Adding to RocksDB batch entry got an exception. + // We should terminate the OM. + String message = "During flush to DB encountered error " + + ex.getMessage(); + ExitUtil.terminate(1, message); + } + }); + + try { + omMetadataManager.getStore().commitBatchOperation(batchOperation); + } catch (IOException ex) { + // During flush to rocksdb got an exception. + // We should terminate the OM. + String message = "During flush to DB encountered error " + + ex.getMessage(); + ExitUtil.terminate(1, message); + } + + int flushedTransactionsSize = readyBuffer.size(); + flushedTransactionCount.addAndGet(flushedTransactionsSize); + flushIterations.incrementAndGet(); + + LOG.info("Sync Iteration {} flushed transactions in this iteration{}", Review comment: Nit: Should we make this a Debug instead of info. There will be many in OM. But on the other hand, this might be ok, not sure. @arp7 What do think ? will this flood the logs and slow us down? Based on Namenode experience what would be your call? ---------------------------------------------------------------- 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] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
