deniskuzZ commented on code in PR #5613:
URL: https://github.com/apache/hive/pull/5613#discussion_r1937584605


##########
ql/src/java/org/apache/hadoop/hive/ql/queryhistory/QueryHistoryService.java:
##########
@@ -0,0 +1,238 @@
+/*
+ * 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.hive.ql.queryhistory;
+
+import java.util.Queue;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.DriverContext;
+import org.apache.hadoop.hive.ql.ServiceContext;
+import 
org.apache.hadoop.hive.ql.queryhistory.repository.QueryHistoryRepository;
+import org.apache.hadoop.hive.ql.queryhistory.schema.Record;
+import org.apache.hadoop.hive.ql.queryhistory.schema.Schema;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hadoop.util.Time;
+import org.apache.hive.common.util.ReflectionUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+
+public class QueryHistoryService {
+  private static final Logger LOG = 
LoggerFactory.getLogger(QueryHistoryService.class);
+
+  private static QueryHistoryService INSTANCE = null;
+
+  protected final Queue<Record> queryHistoryQueue = new 
LinkedBlockingQueue<>();
+
+  private final ExecutorService flushExecutor = Executors.newFixedThreadPool(1,
+      new ThreadFactoryBuilder().setNameFormat("QueryHistoryService repository 
thread").setDaemon(true).build());
+  private final ScheduledExecutorService periodicFlushExecutor = 
Executors.newScheduledThreadPool(1,
+      new ThreadFactoryBuilder().setNameFormat("QueryHistoryService periodic 
flush check").setDaemon(true).build());
+
+  private final HiveConf conf;
+  private final ServiceContext serviceContext;
+
+  private final int batchSize;
+  private final int maxQueueSizeInMemoryBytes;
+  private final int flushInterval;
+  private final QueryHistoryRepository repository;
+
+  public QueryHistoryService(HiveConf inputConf, ServiceContext 
serviceContext) {
+    LOG.info("Creating QueryHistoryService instance");
+
+    this.conf = new HiveConf(inputConf);
+    this.serviceContext = serviceContext;
+
+    batchSize = HiveConf.getIntVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_HISTORY_BATCH_SIZE);
+    maxQueueSizeInMemoryBytes = HiveConf.getIntVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_HISTORY_MAX_MEMORY_BYTES);
+    flushInterval = HiveConf.getIntVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_HISTORY_FLUSH_INTERVAL_SECONDS);
+    repository = createRepository(conf);
+
+    printConfigInformation();
+
+    LOG.info("QueryHistoryService created [batchSize: {}, 
maxQueueSizeInMemoryBytes: {}]",
+        batchSize, maxQueueSizeInMemoryBytes);
+
+    // keeping a global, static instance is usually considered an antipattern, 
even in case of using the Singleton
+    // design, this time the constraint was the ability to hook into the query 
processing (Driver) from somewhere the
+    // outer service (HiveServer2, QTestUtil, etc.), which otherwise would 
have needed passing a service instance
+    // through many layers, making the code and method signatures more 
complicated
+    INSTANCE = this;

Review Comment:
   how about
   ````
   private static Singleton instance;
   
   private QueryHistoryService(HiveConf inputConf, ServiceContext 
serviceContext) {
     ...
   }
   public static QueryHistoryService newInstance(HiveConf inputConf, 
ServiceContext serviceContext) {
     if (instance == null) {
       instance = new QueryHistoryService(HiveConf inputConf, ServiceContext 
serviceContext);
      }
     return instance;
   }
   ````



-- 
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: gitbox-unsubscr...@hive.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: gitbox-unsubscr...@hive.apache.org
For additional commands, e-mail: gitbox-h...@hive.apache.org

Reply via email to