You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@hive.apache.org by "ASF GitHub Bot (Jira)" <ji...@apache.org> on 2021/05/10 14:24:00 UTC

[jira] [Work logged] (HIVE-24802) Show operation log at webui

     [ https://issues.apache.org/jira/browse/HIVE-24802?focusedWorklogId=594009&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-594009 ]

ASF GitHub Bot logged work on HIVE-24802:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 10/May/21 14:23
            Start Date: 10/May/21 14:23
    Worklog Time Spent: 10m 
      Work Description: zchovan commented on a change in pull request #1998:
URL: https://github.com/apache/hive/pull/1998#discussion_r629403542



##########
File path: service/src/java/org/apache/hive/service/cli/operation/OperationLogManager.java
##########
@@ -0,0 +1,372 @@
+/*
+ * 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.hive.service.cli.operation;
+
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStreamReader;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.TimeUnit;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.QueryInfo;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.session.OperationLog;
+import org.apache.hadoop.util.StringUtils;
+import org.apache.hive.service.cli.OperationHandle;
+import org.apache.hive.service.cli.session.HiveSession;
+import org.apache.hive.service.cli.session.HiveSessionImpl;
+import org.apache.hive.service.cli.session.SessionManager;
+
+/**
+ * Move the operation log into another log location that different from the dir created by
+ * {@link HiveSessionImpl#setOperationLogSessionDir(File)},
+ * this will avoid the operation log being cleaned when session/operation is closed, refer to
+ * {@link HiveSessionImpl#close()}, so we can get the operation log for the optimization
+ * and investigating the problem of the operation handily for users or administrators.
+ * The tree under the log location looks like:
+ * - ${@link SessionManager#operationLogRootDir}_historic
+ *    - sessionId
+ *        - queryId (the operation log file)
+ * <p>
+ * while the origin tree would like:
+ * - ${@link SessionManager#operationLogRootDir}
+ *    - sessionId
+ *        - queryId (the operation log file)
+ * <p>
+ * The lifecycle of the log is managed by a daemon called {@link OperationLogDirCleaner},
+ * it gets all query info stored in {@link QueryInfoCache}, searches for the query info that can not be reached on the webui,
+ * and removes the log. If the operation log session directory has no operation log under it and the session is dead,
+ * then the OperationLogDirCleaner will try to cleanup the session log directory.
+ */
+
+public class OperationLogManager {
+  private static final Logger LOG = LoggerFactory.getLogger(OperationLogManager.class);
+  private static final String HISTORIC_DIR_SUFFIX = "_historic";
+  private static String HISTORIC_OPERATION_LOG_ROOT_DIR;
+  private static long MAX_BYTES_TO_FETCH;
+
+  private final HiveConf hiveConf;
+  private final SessionManager sessionManager;
+  private final OperationManager operationManager;
+  private OperationLogDirCleaner cleaner;
+
+  public OperationLogManager(SessionManager sessionManager) {
+    this.operationManager = sessionManager.getOperationManager();
+    this.hiveConf = sessionManager.getHiveConf();
+    this.sessionManager = sessionManager;
+    if (HiveConf.getBoolVar(hiveConf, HiveConf.ConfVars.HIVE_SERVER2_HISTORIC_OPERATION_LOG_ENABLED)
+        && hiveConf.getBoolVar(HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_ENABLED)
+        && hiveConf.isWebUiQueryInfoCacheEnabled()) {
+      initHistoricOperationLogRootDir();
+      MAX_BYTES_TO_FETCH = HiveConf.getSizeVar(hiveConf,
+          HiveConf.ConfVars.HIVE_SERVER2_HISTORIC_OPERATION_LOG_FETCH_MAXBYTES);
+      if (HISTORIC_OPERATION_LOG_ROOT_DIR != null
+          && !HiveConf.getBoolVar(hiveConf, HiveConf.ConfVars.HIVE_IN_TEST)) {
+        cleaner = new OperationLogDirCleaner();
+        cleaner.start();
+      }
+    }
+  }
+
+  private void initHistoricOperationLogRootDir() {
+    String originalLogLoc = hiveConf.getVar(HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_LOG_LOCATION);
+    String historicLogLoc = originalLogLoc + HISTORIC_DIR_SUFFIX;
+    File operationLogRootDir = new File(historicLogLoc);
+
+    if (operationLogRootDir.exists() && !operationLogRootDir.isDirectory()) {
+      LOG.warn("The historic operation log root directory exists, but it is not a directory: " +
+          operationLogRootDir.getAbsolutePath());
+      return;
+    }
+
+    if (!operationLogRootDir.exists()) {
+      if (!operationLogRootDir.mkdirs()) {
+        LOG.warn("Unable to create historic operation log root directory: " +
+            operationLogRootDir.getAbsolutePath());
+        return;
+      }
+    }
+    HISTORIC_OPERATION_LOG_ROOT_DIR = historicLogLoc;
+  }
+
+  public static OperationLog createOperationLog(Operation operation, QueryState queryState) {
+    HiveSession session = operation.getParentSession();
+    File parentFile = session.getOperationLogSessionDir();
+    boolean isHistoricLogEnabled = HISTORIC_OPERATION_LOG_ROOT_DIR != null;
+    if (isHistoricLogEnabled && operation instanceof SQLOperation) {
+      String sessionId = session.getSessionHandle().getHandleIdentifier().toString();
+      parentFile = new File(HISTORIC_OPERATION_LOG_ROOT_DIR + "/" + sessionId);
+      if (!parentFile.exists()) {
+        if (!parentFile.mkdirs()) {
+          LOG.warn("Unable to create the historic operation log session dir: {}, " +
+              "fall back to the original operation log session dir.", parentFile);
+          parentFile = session.getOperationLogSessionDir();
+          isHistoricLogEnabled = false;
+        }
+      } else if (!parentFile.isDirectory()) {
+        LOG.warn("The historic operation log session dir: {} is exist, but it's not a directory, " +
+            "fall back to the original operation log session dir.", parentFile);
+        parentFile = session.getOperationLogSessionDir();
+        isHistoricLogEnabled = false;
+      }
+    }
+
+    OperationHandle opHandle = operation.getHandle();
+    File operationLogFile = new File(parentFile, queryState.getQueryId());
+    OperationLog operationLog;
+    if (isHistoricLogEnabled) {
+      // dynamically setting the log location to route the operation log
+      HiveConf.setVar(queryState.getConf(),
+          HiveConf.ConfVars.HIVE_SERVER2_LOGGING_OPERATION_LOG_LOCATION, HISTORIC_OPERATION_LOG_ROOT_DIR);
+      HiveConf.setBoolVar(queryState.getConf(), HiveConf.ConfVars.HIVE_TESTING_REMOVE_LOGS, false);
+      LOG.info("The operation log location changes from {} to {}.", new File(session.getOperationLogSessionDir(),
+          queryState.getQueryId()), operationLogFile);
+    }
+    operationLog = new OperationLog(opHandle.toString(), operationLogFile, queryState.getConf());
+    return operationLog;
+  }
+
+  private Set<String> getLiveSessions() {
+    Collection<HiveSession> hiveSessions = sessionManager.getSessions();
+    Set<String> liveSessions = new HashSet<>();
+    for (HiveSession session : hiveSessions) {
+      liveSessions.add(session.getSessionHandle().getHandleIdentifier().toString());
+    }
+    return liveSessions;
+  }
+
+  private Set<String> getHistoricSessions(String logRootDir) {
+    File logDir = new File(logRootDir);

Review comment:
       what happens if the logRootDir was deleted before this function is called? wouldn't this throw an NPE?




-- 
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:
users@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 594009)
    Time Spent: 1h 10m  (was: 1h)

> Show operation log at webui
> ---------------------------
>
>                 Key: HIVE-24802
>                 URL: https://issues.apache.org/jira/browse/HIVE-24802
>             Project: Hive
>          Issue Type: Improvement
>          Components: HiveServer2
>            Reporter: Zhihua Deng
>            Assignee: Zhihua Deng
>            Priority: Minor
>              Labels: pull-request-available
>         Attachments: operationlog.png
>
>          Time Spent: 1h 10m
>  Remaining Estimate: 0h
>
> Currently we provide getQueryLog in HiveStatement to fetch the operation log,  and the operation log would be deleted on operation closing(delay for the canceled operation).  Sometimes it's would be not easy for the user(jdbc) or administrators to deep into the details of the finished(failed) operation, so we present the operation log on webui and keep the operation log for some time for latter analysis.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)