Himanshu-g81 commented on code in PR #2188:
URL: https://github.com/apache/phoenix/pull/2188#discussion_r2192487908


##########
phoenix-core-server/src/main/java/org/apache/phoenix/replication/reader/ReplicationLogProcessor.java:
##########
@@ -0,0 +1,524 @@
+/*
+ * 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.phoenix.replication.reader;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HConstants;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.AsyncConnection;
+import org.apache.hadoop.hbase.client.AsyncTable;
+import org.apache.hadoop.hbase.client.ConnectionFactory;
+import org.apache.hadoop.hbase.client.Mutation;
+import org.apache.hadoop.hbase.util.FutureUtils;
+import org.apache.phoenix.replication.log.LogFile;
+import org.apache.phoenix.replication.log.LogFileReader;
+import org.apache.phoenix.replication.log.LogFileReaderContext;
+import org.apache.phoenix.replication.metrics.MetricsReplicationLogProcessor;
+import 
org.apache.phoenix.replication.metrics.MetricsReplicationLogProcessorImpl;
+import 
org.apache.phoenix.thirdparty.com.google.common.util.concurrent.ThreadFactoryBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ReplicationLogProcessor implements Closeable {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(ReplicationLogProcessor.class);
+
+    /**
+     * The maximum count of mutations to process in single batch while reading 
replication log file
+     */
+    public static final String REPLICATION_STANDBY_LOG_REPLAY_BATCH_SIZE =
+            "phoenix.replication.log.standby.replay.batch.size";
+
+    /**
+     * The default batch size for reading the replication log file.
+     * Assuming each log record to be 10 KB (un-compressed) and allow at max 
64 MB of
+     * in-memory records to be processed
+     */
+    public static final int DEFAULT_REPLICATION_STANDBY_LOG_REPLAY_BATCH_SIZE 
= 6400;
+
+    /**
+     * The number of threads to apply mutations via async hbase client
+     */
+    public static final String REPLICATION_STANDBY_LOG_REPLAY_THREAD_POOL_SIZE 
=
+            "phoenix.replication.log.standby.replay.thread.pool.size";
+
+    /**
+     * The default number of threads for applying mutations via async hbase 
client
+     */
+    public static final int 
DEFAULT_REPLICATION_STANDBY_LOG_REPLAY_THREAD_POOL_SIZE = 5;
+
+    /**
+     * The maximum number of retries for HBase client operations while 
applying the mutations
+     */
+    public static final String REPLICATION_STANDBY_HBASE_CLIENT_RETRIES_COUNT =
+            "phoenix.replication.standby.hbase.client.retries.number";
+
+    /**
+     * The default number of retries for HBase client operations while 
applying the mutations.
+     */
+    public static final int 
DEFAULT_REPLICATION_STANDBY_HBASE_CLIENT_RETRIES_COUNT = 4;
+
+    /**
+     * The timeout for HBase client operations while applying the mutations.
+     */
+    public static final String 
REPLICATION_STANDBY_HBASE_CLIENT_OPERATION_TIMEOUT_MS =
+            "phoenix.replication.standby.hbase.client.operations.timeout";
+
+    /**
+     * The default timeout for HBase client operations while applying the 
mutations.
+     */
+    public static final long 
DEFAULT_REPLICATION_STANDBY_HBASE_CLIENT_OPERATION_TIMEOUT_MS = 8000;
+
+    /**
+     * The maximum number of retry attempts for failed batch operations.
+     */
+    public static final String REPLICATION_STANDBY_BATCH_RETRY_COUNT =
+            "phoenix.replication.standby.batch.retry.count";
+
+    /**
+     * The default number of retry attempts for failed batch operations.
+     */
+    public static final int DEFAULT_REPLICATION_STANDBY_BATCH_RETRY_COUNT = 2;
+
+    /**
+     * The maximum delay for retry attempts in milliseconds.
+     */
+    public static final String REPLICATION_STANDBY_BATCH_RETRY_MAX_DELAY_MS =
+            "phoenix.replication.standby.batch.retry.max.delay.ms";
+
+    /**
+     * The default maximum delay for retry attempts in milliseconds.
+     */
+    public static final long 
DEFAULT_REPLICATION_STANDBY_BATCH_RETRY_MAX_DELAY_MS = 10000;
+
+    private final String haGroupName;
+
+    private final Configuration conf;
+
+    private final ExecutorService executorService;
+
+    /**
+     * This {@link AsyncConnection} is used for handling mutations
+     */
+    private volatile AsyncConnection asyncConnection;
+
+    private final int batchSize;
+
+    private final int batchRetryCount;
+
+    private final long maxRetryDelayMs;
+
+    private final MetricsReplicationLogProcessor metrics;
+
+    /** Cache of ReplicationLogGroup instances by HA Group ID */
+    private static final ConcurrentHashMap<String, ReplicationLogProcessor> 
INSTANCES =
+            new ConcurrentHashMap<>();
+
+    /**
+     * Get or create a ReplicationLogProcessor instance for the given HA Group.
+     *
+     * @param conf Configuration object
+     * @param haGroupName The HA Group name
+     * @return ReplicationLogProcessor instance
+     */
+    public static ReplicationLogProcessor get(Configuration conf, String 
haGroupName) {
+        return INSTANCES.computeIfAbsent(haGroupName,
+            k -> new ReplicationLogProcessor(conf, haGroupName));
+    }
+
+    /**
+     * Creates a new ReplicationLogProcessor with the given configuration and 
executor service.
+     * @param conf The configuration to use
+     * @param haGroupName The HA group id
+     */
+    protected ReplicationLogProcessor(final Configuration conf, final String 
haGroupName) {
+        // Create a copy of configuration as some of the properties would be
+        // overridden
+        this.conf = HBaseConfiguration.create(conf);
+        this.haGroupName = haGroupName;
+        this.batchSize = 
this.conf.getInt(REPLICATION_STANDBY_LOG_REPLAY_BATCH_SIZE,
+                DEFAULT_REPLICATION_STANDBY_LOG_REPLAY_BATCH_SIZE);
+        this.batchRetryCount = 
this.conf.getInt(REPLICATION_STANDBY_BATCH_RETRY_COUNT,
+                DEFAULT_REPLICATION_STANDBY_BATCH_RETRY_COUNT);
+        this.maxRetryDelayMs = 
this.conf.getLong(REPLICATION_STANDBY_BATCH_RETRY_MAX_DELAY_MS,
+                DEFAULT_REPLICATION_STANDBY_BATCH_RETRY_MAX_DELAY_MS);
+        final int threadPoolSize = 
this.conf.getInt(REPLICATION_STANDBY_LOG_REPLAY_THREAD_POOL_SIZE,
+                DEFAULT_REPLICATION_STANDBY_LOG_REPLAY_THREAD_POOL_SIZE);
+        decorateConf();
+        this.metrics = createMetricsSource();
+        this.executorService = Executors.newFixedThreadPool(threadPoolSize,
+            new ThreadFactoryBuilder()
+                .setNameFormat("Phoenix-Replication-Log-Processor-" + 
haGroupName + "-%d")
+                .build());
+    }
+
+    /**
+     * Decorate the Configuration object to make replication more receptive to 
delays by
+     * reducing the timeout and number of retries.
+     */
+    private void decorateConf() {
+        this.conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER,
+                
this.conf.getInt(REPLICATION_STANDBY_HBASE_CLIENT_RETRIES_COUNT,
+                        
DEFAULT_REPLICATION_STANDBY_HBASE_CLIENT_RETRIES_COUNT));
+        this.conf.setLong(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
+                
this.conf.getLong(REPLICATION_STANDBY_HBASE_CLIENT_OPERATION_TIMEOUT_MS,
+                        
DEFAULT_REPLICATION_STANDBY_HBASE_CLIENT_OPERATION_TIMEOUT_MS));
+    }
+
+    public void processLogFile(FileSystem fs, Path filePath) throws 
IOException {
+
+        // Map from Table Name to List of Mutations
+        Map<TableName, List<Mutation>> tableToMutationsMap = new HashMap<>();
+
+        // Track the total number of processed records from input log file
+        long totalProcessed = 0;
+
+        // Track the current batch size as records will be processed in batch 
size of
+        // {@link REPLICATION_STANDBY_LOG_REPLAY_BATCH_SIZE}
+        long currentBatchSize = 0;
+
+        LogFileReader logFileReader = null;
+
+        long startTime = System.currentTimeMillis();
+
+        try {
+            // Create the LogFileReader for given path
+            logFileReader = createLogFileReader(fs, filePath);
+
+            for (LogFile.Record record : logFileReader) {
+                final TableName tableName = 
TableName.valueOf(record.getHBaseTableName());
+                final Mutation mutation = record.getMutation();
+
+                tableToMutationsMap.computeIfAbsent(tableName, k -> new 
ArrayList<>())
+                        .add(mutation);
+                currentBatchSize++;

Review Comment:
   Sure, added in latest commit (including test case for the same).
   Please note, have used 
[mutation.heapSize()](https://github.com/apache/hbase/blob/8ddf925daac7af48a5b624c6192bd2cdc45f7955/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Mutation.java#L412-L436)
 to get the in memory size of this mutation as I observed 
[mutation.size()](https://github.com/apache/hbase/blob/8ddf925daac7af48a5b624c6192bd2cdc45f7955/hbase-client/src/main/java/org/apache/hadoop/hbase/client/Mutation.java#L397-L403)
 returns count of key values pairs.



-- 
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: issues-unsubscr...@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to