You are viewing a plain text version of this content. The canonical link for it is here.
Posted to gitbox@hive.apache.org by GitBox <gi...@apache.org> on 2020/08/03 09:47:02 UTC

[GitHub] [hive] dengzhhu653 opened a new pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

dengzhhu653 opened a new pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   ## NOTICE
   
   Please create an issue in ASF JIRA before opening a pull request,
   and you need to set the title of the pull request which starts with
   the corresponding JIRA issue number. (e.g. HIVE-XXXXX: Fix a typo in YYY)
   For more details, please see https://cwiki.apache.org/confluence/display/Hive/HowToContribute
   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 closed pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 closed pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 closed pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 closed pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   


----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk merged pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
kgyrtkirk merged pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   


----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on a change in pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r450699040



##########
File path: service/src/java/org/apache/hive/service/server/HiveServer2OomHookRunner.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HiveServer2OomHookRunner implements Runnable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(HiveServer2OomHookRunner.class);
+  private OomHookContext context;
+  private final List<OomHookWithContext> hooks = new ArrayList<OomHookWithContext>();
+
+  HiveServer2OomHookRunner(HiveServer2 hiveServer2, HiveConf hiveConf) {
+    context = new OomHookContext(hiveServer2);
+    // The hs2 has not been initialized yet, hiveServer2.getHiveConf() would be null
+    init(hiveConf);
+  }
+
+  private void init(HiveConf hiveConf) {
+    String csHooks = hiveConf.getVar(ConfVars.HIVE_SERVER2_OOM_HOOKS);
+    if (!StringUtils.isBlank(csHooks)) {
+      String[] hookClasses = csHooks.split(",");
+      for (String hookClass : hookClasses) {
+        try {
+          Class clazz =  JavaUtils.loadClass(hookClass.trim());
+          Constructor ctor = clazz.getDeclaredConstructor();
+          ctor.setAccessible(true);

Review comment:
       please reuse parts of `HookRunner` for loading stuff
   
   and why would you need `setAccessible` ?

##########
File path: service/src/java/org/apache/hive/service/server/HiveServer2OomHookRunner.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HiveServer2OomHookRunner implements Runnable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(HiveServer2OomHookRunner.class);
+  private OomHookContext context;
+  private final List<OomHookWithContext> hooks = new ArrayList<OomHookWithContext>();
+
+  HiveServer2OomHookRunner(HiveServer2 hiveServer2, HiveConf hiveConf) {
+    context = new OomHookContext(hiveServer2);
+    // The hs2 has not been initialized yet, hiveServer2.getHiveConf() would be null
+    init(hiveConf);
+  }
+
+  private void init(HiveConf hiveConf) {
+    String csHooks = hiveConf.getVar(ConfVars.HIVE_SERVER2_OOM_HOOKS);
+    if (!StringUtils.isBlank(csHooks)) {
+      String[] hookClasses = csHooks.split(",");
+      for (String hookClass : hookClasses) {
+        try {
+          Class clazz =  JavaUtils.loadClass(hookClass.trim());
+          Constructor ctor = clazz.getDeclaredConstructor();
+          ctor.setAccessible(true);
+          hooks.add((OomHookWithContext)ctor.newInstance());
+        } catch (Exception e) {
+          LOG.error("Skip adding oom hook '" + hookClass + "'", e);

Review comment:
       don't swallow exceptions...

##########
File path: service/src/java/org/apache/hive/service/server/HiveServer2OomHookRunner.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HiveServer2OomHookRunner implements Runnable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(HiveServer2OomHookRunner.class);
+  private OomHookContext context;
+  private final List<OomHookWithContext> hooks = new ArrayList<OomHookWithContext>();
+
+  HiveServer2OomHookRunner(HiveServer2 hiveServer2, HiveConf hiveConf) {
+    context = new OomHookContext(hiveServer2);
+    // The hs2 has not been initialized yet, hiveServer2.getHiveConf() would be null
+    init(hiveConf);
+  }
+
+  private void init(HiveConf hiveConf) {
+    String csHooks = hiveConf.getVar(ConfVars.HIVE_SERVER2_OOM_HOOKS);
+    if (!StringUtils.isBlank(csHooks)) {
+      String[] hookClasses = csHooks.split(",");
+      for (String hookClass : hookClasses) {
+        try {
+          Class clazz =  JavaUtils.loadClass(hookClass.trim());
+          Constructor ctor = clazz.getDeclaredConstructor();
+          ctor.setAccessible(true);
+          hooks.add((OomHookWithContext)ctor.newInstance());
+        } catch (Exception e) {
+          LOG.error("Skip adding oom hook '" + hookClass + "'", e);
+        }
+      }
+    }
+  }
+
+  @VisibleForTesting
+  public HiveServer2OomHookRunner(HiveConf hiveConf) {
+    init(hiveConf);
+  }
+
+  @VisibleForTesting
+  public List<OomHookWithContext> getHooks() {
+    return hooks;
+  }
+
+  @Override
+  public void run() {
+    for (OomHookWithContext hook : hooks) {
+      hook.run(context);
+    }
+  }
+
+  public static interface OomHookWithContext {
+    public void run(OomHookContext context);
+  }
+
+  public static class OomHookContext {
+    private final HiveServer2 hiveServer2;
+    public OomHookContext(HiveServer2 hiveServer2) {
+      this.hiveServer2 = hiveServer2;
+    }
+    public HiveServer2 getHiveServer2() {
+      return hiveServer2;
+    }
+  }
+
+  /**
+   * Used as default oom hook
+   */
+  private static class DefaultOomHook implements OomHookWithContext {

Review comment:
       why is this private?




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 closed pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 closed pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r489144324



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HooksLoader.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.hooks;
+
+import com.cronutils.utils.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.common.util.HiveStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *  Loads and stores different kinds of hooks, provides {@link HooksLoader#addHook(HookContext.HookType, Object)}} to
+ *  add hook alone or {@link HooksLoader#getHooks(HookContext.HookType, Class)} to get all hooks
+ *  corresponding to the specific hook type.
+ */
+public class HooksLoader {
+  private static final Logger LOG = LoggerFactory.getLogger(HooksLoader.class);
+  private final HiveConf conf;
+  private final Hooks[] hooks;
+  private SessionState.LogHelper console;
+
+  public HooksLoader(HiveConf conf) {
+    this.conf = conf;
+    this.hooks = new Hooks[HookContext.HookType.values().length];
+    for (int i = 0; i < hooks.length; i++) {
+      hooks[i] = new Hooks();
+    }
+  }
+
+  public HooksLoader(HiveConf conf, SessionState.LogHelper console) {
+    this(conf);
+    this.console = console;
+  }
+
+  /**
+   * Loads the configured hooks corresponding to the specific hook type.
+   * @param type hook type
+   */
+  @VisibleForTesting
+  void loadHooksFromConf(HookContext.HookType type) {
+    Hooks container = hooks[type.ordinal()];
+    if (!container.loadedFromConf) {

Review comment:
       When we use HooksLoader to load the session hooks, redactor hooks that running outside the HookRunner, maybe there is no need to initialize other types of hooks, so I make the hooks loading lazily on demand.
   




----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r500961287



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/HookRunner.java
##########
@@ -39,57 +36,27 @@
 import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHook;
 import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;
 import org.apache.hadoop.hive.ql.session.SessionState;
-import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
 import org.apache.hive.common.util.HiveStringUtils;
 
+import static org.apache.hadoop.hive.ql.hooks.HookContext.HookType.*;
+
 /**
  * Handles hook executions for {@link Driver}.
  */
 public class HookRunner {
 
   private static final String CLASS_NAME = Driver.class.getName();
   private final HiveConf conf;
-  private LogHelper console;
-  private List<QueryLifeTimeHook> queryHooks = new ArrayList<>();
-  private List<HiveSemanticAnalyzerHook> saHooks = new ArrayList<>();
-  private List<HiveDriverRunHook> driverRunHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> preExecHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> postExecHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> onFailureHooks = new ArrayList<>();
-  private boolean initialized = false;
+  private final HooksLoader loader;

Review comment:
       this is great!
   since from now on we can also dynamically add new hooks to it at runtime - we may rename it from "Loader" to something else.

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java
##########
@@ -45,7 +47,50 @@
 public class HookContext {
 
   static public enum HookType {
-    PRE_EXEC_HOOK, POST_EXEC_HOOK, ON_FAILURE_HOOK
+

Review comment:
       I like this approach - could you make a small check:
   
   * if we have hook compiled for the old api (which uses say the enum key `HookType.PRE_EXEC_HOOK`)
   * will it work or not  (without recompilation) with the new implementation




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r502158480



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java
##########
@@ -45,7 +47,50 @@
 public class HookContext {
 
   static public enum HookType {
-    PRE_EXEC_HOOK, POST_EXEC_HOOK, ON_FAILURE_HOOK
+

Review comment:
       Checked on my test and production env,  it shows that the hooks compiled for the old api can be reused without any changes  with the new implementation.

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/HookRunner.java
##########
@@ -39,57 +36,27 @@
 import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHook;
 import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;
 import org.apache.hadoop.hive.ql.session.SessionState;
-import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
 import org.apache.hive.common.util.HiveStringUtils;
 
+import static org.apache.hadoop.hive.ql.hooks.HookContext.HookType.*;
+
 /**
  * Handles hook executions for {@link Driver}.
  */
 public class HookRunner {
 
   private static final String CLASS_NAME = Driver.class.getName();
   private final HiveConf conf;
-  private LogHelper console;
-  private List<QueryLifeTimeHook> queryHooks = new ArrayList<>();
-  private List<HiveSemanticAnalyzerHook> saHooks = new ArrayList<>();
-  private List<HiveDriverRunHook> driverRunHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> preExecHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> postExecHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> onFailureHooks = new ArrayList<>();
-  private boolean initialized = false;
+  private final HooksLoader loader;

Review comment:
       Rename it to HiveHooks instead.




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 edited a comment on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 edited a comment on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-669832758


   > sorry @dengzhhu653 lately I was a little bit flooded with all kind of stuff...and right now I'm on holiday - will get back to your patch next week!
   
   sorry for disturbing you. Have a nice holiday!


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 closed pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 closed pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   


----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r488617988



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HooksLoader.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.hooks;
+
+import com.cronutils.utils.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.common.util.HiveStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *  Loads and stores different kinds of hooks, provides {@link HooksLoader#addHook(HookContext.HookType, Object)}} to
+ *  add hook alone or {@link HooksLoader#getHooks(HookContext.HookType, Class)} to get all hooks
+ *  corresponding to the specific hook type.
+ */
+public class HooksLoader {
+  private static final Logger LOG = LoggerFactory.getLogger(HooksLoader.class);
+  private final HiveConf conf;
+  private final Hooks[] hooks;
+  private SessionState.LogHelper console;
+
+  public HooksLoader(HiveConf conf) {
+    this.conf = conf;
+    this.hooks = new Hooks[HookContext.HookType.values().length];
+    for (int i = 0; i < hooks.length; i++) {
+      hooks[i] = new Hooks();
+    }
+  }
+
+  public HooksLoader(HiveConf conf, SessionState.LogHelper console) {
+    this(conf);
+    this.console = console;
+  }
+
+  /**
+   * Loads the configured hooks corresponding to the specific hook type.
+   * @param type hook type
+   */
+  @VisibleForTesting
+  void loadHooksFromConf(HookContext.HookType type) {
+    Hooks container = hooks[type.ordinal()];
+    if (!container.loadedFromConf) {
+      container.loadedFromConf = true;
+      List hooks = container.getHooks();
+      HiveConf.ConfVars confVars = type.getConfVar();

Review comment:
       I think this should be only `confVar`

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HooksLoader.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.hooks;
+
+import com.cronutils.utils.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.common.util.HiveStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *  Loads and stores different kinds of hooks, provides {@link HooksLoader#addHook(HookContext.HookType, Object)}} to
+ *  add hook alone or {@link HooksLoader#getHooks(HookContext.HookType, Class)} to get all hooks
+ *  corresponding to the specific hook type.
+ */
+public class HooksLoader {
+  private static final Logger LOG = LoggerFactory.getLogger(HooksLoader.class);
+  private final HiveConf conf;
+  private final Hooks[] hooks;

Review comment:
       this will make an index based contract - instead of that we could utrilize a `Map<HookType,Hook> `

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java
##########
@@ -45,7 +47,50 @@
 public class HookContext {
 
   static public enum HookType {
-    PRE_EXEC_HOOK, POST_EXEC_HOOK, ON_FAILURE_HOOK
+
+    PRE_EXEC_HOOK(HiveConf.ConfVars.PREEXECHOOKS, ExecuteWithHookContext.class,
+        "Pre-execution hooks to be invoked for each statement"),
+    POST_EXEC_HOOK(HiveConf.ConfVars.POSTEXECHOOKS, ExecuteWithHookContext.class,
+        "Post-execution hooks to be invoked for each statement"),
+    ON_FAILURE_HOOK(HiveConf.ConfVars.ONFAILUREHOOKS, ExecuteWithHookContext.class,
+        "On-failure hooks to be invoked for each statement"),
+    QUERY_LIFETIME_HOOKS(HiveConf.ConfVars.HIVE_QUERY_LIFETIME_HOOKS, QueryLifeTimeHook.class,
+      "Hooks that will be triggered before/after query compilation and before/after query execution"),
+    SEMANTIC_ANALYZER_HOOK(HiveConf.ConfVars.SEMANTIC_ANALYZER_HOOK, HiveSemanticAnalyzerHook.class,
+      "Hooks that invoked before/after Hive performs its own semantic analysis on a statement"),
+    DRIVER_RUN_HOOKS(HiveConf.ConfVars.HIVE_DRIVER_RUN_HOOKS, HiveDriverRunHook.class,
+      "Hooks that Will be run at the beginning and end of Driver.run"),
+    REDACTOR(HiveConf.ConfVars.QUERYREDACTORHOOKS, Redactor.class,
+      "Hooks to be invoked for each query which can tranform the query before it's placed in the job.xml file"),
+    // The HiveSessionHook.class cannot access, use Hook.class instead
+    HIVE_SERVER2_SESSION_HOOK(HiveConf.ConfVars.HIVE_SERVER2_SESSION_HOOK, Hook.class,
+      "Hooks to be executed when session manager starts a new session"),
+    OOM(HiveConf.ConfVars.HIVE_SERVER2_OOM_HOOKS, Runnable.class,

Review comment:
       please keep some kind of naming contract between the varname and the enum key

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HooksLoader.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.hooks;
+
+import com.cronutils.utils.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.common.util.HiveStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *  Loads and stores different kinds of hooks, provides {@link HooksLoader#addHook(HookContext.HookType, Object)}} to
+ *  add hook alone or {@link HooksLoader#getHooks(HookContext.HookType, Class)} to get all hooks
+ *  corresponding to the specific hook type.
+ */
+public class HooksLoader {
+  private static final Logger LOG = LoggerFactory.getLogger(HooksLoader.class);
+  private final HiveConf conf;
+  private final Hooks[] hooks;
+  private SessionState.LogHelper console;
+
+  public HooksLoader(HiveConf conf) {
+    this.conf = conf;
+    this.hooks = new Hooks[HookContext.HookType.values().length];
+    for (int i = 0; i < hooks.length; i++) {
+      hooks[i] = new Hooks();
+    }
+  }
+
+  public HooksLoader(HiveConf conf, SessionState.LogHelper console) {
+    this(conf);
+    this.console = console;
+  }
+
+  /**
+   * Loads the configured hooks corresponding to the specific hook type.
+   * @param type hook type
+   */
+  @VisibleForTesting
+  void loadHooksFromConf(HookContext.HookType type) {
+    Hooks container = hooks[type.ordinal()];
+    if (!container.loadedFromConf) {

Review comment:
       I don't see any particular benefit of doing the loading lazily - just load all of them upfront in the constructor - the Conf *may not* change after the creation of this object




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r502158480



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java
##########
@@ -45,7 +47,50 @@
 public class HookContext {
 
   static public enum HookType {
-    PRE_EXEC_HOOK, POST_EXEC_HOOK, ON_FAILURE_HOOK
+

Review comment:
       Checked on my test and production env,  it shows that the hooks compiled for the old api can be reused without any changes  with the new implementation.

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/HookRunner.java
##########
@@ -39,57 +36,27 @@
 import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHook;
 import org.apache.hadoop.hive.ql.parse.HiveSemanticAnalyzerHookContext;
 import org.apache.hadoop.hive.ql.session.SessionState;
-import org.apache.hadoop.hive.ql.session.SessionState.LogHelper;
 import org.apache.hive.common.util.HiveStringUtils;
 
+import static org.apache.hadoop.hive.ql.hooks.HookContext.HookType.*;
+
 /**
  * Handles hook executions for {@link Driver}.
  */
 public class HookRunner {
 
   private static final String CLASS_NAME = Driver.class.getName();
   private final HiveConf conf;
-  private LogHelper console;
-  private List<QueryLifeTimeHook> queryHooks = new ArrayList<>();
-  private List<HiveSemanticAnalyzerHook> saHooks = new ArrayList<>();
-  private List<HiveDriverRunHook> driverRunHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> preExecHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> postExecHooks = new ArrayList<>();
-  private List<ExecuteWithHookContext> onFailureHooks = new ArrayList<>();
-  private boolean initialized = false;
+  private final HooksLoader loader;

Review comment:
       Rename it to HiveHooks instead.




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-708113764


   Rerun tests to check the failed reading of xml.


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 edited a comment on pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 edited a comment on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-654678113


   > I don't see the need for this....what's wrong with [afterexecute](https://github.com/apache/hive/blob/e4256fc91fe2c123428400f3737883a83208d29e/ql/src/java/org/apache/hadoop/hive/ql/Executor.java#L533) or [failurehooks](https://github.com/apache/hive/blob/e4256fc91fe2c123428400f3737883a83208d29e/ql/src/java/org/apache/hadoop/hive/ql/Executor.java#L521)?
   > do you have a use case which could not be handled by those?
   
   The oom hook holds a hiveserver2 instance, which calls hiveserver2::stop() to end hiveserver2 gracefully, which would cleanup the session directory/scratch(staging) directory/operation log and so on . Although the hooks in the driver can handle oom, he may not be able to stop the hiveserver2 gracefully as the oom hook does. Sometimes we may want to dump the heap for futher analysis when oom happens or alter the devops, so it may be better to make the oom hook here an interface.
   


----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-669804983


   sorry @dengzhhu653 lately I was a little bit flooded with all kind of stuff...and right now I'm on holiday - will get back to your patch next week!
   


----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-654663722


   I don't see the need for this....what's wrong with [afterexecute](https://github.com/apache/hive/blob/e4256fc91fe2c123428400f3737883a83208d29e/ql/src/java/org/apache/hadoop/hive/ql/Executor.java#L533) or [failurehooks](https://github.com/apache/hive/blob/e4256fc91fe2c123428400f3737883a83208d29e/ql/src/java/org/apache/hadoop/hive/ql/Executor.java#L521)?
   do you have a use case which could not be handled by those?
   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 closed pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 closed pull request #1205:
URL: https://github.com/apache/hive/pull/1205


   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r451539918



##########
File path: service/src/java/org/apache/hive/service/server/HiveServer2OomHookRunner.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HiveServer2OomHookRunner implements Runnable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(HiveServer2OomHookRunner.class);
+  private OomHookContext context;
+  private final List<OomHookWithContext> hooks = new ArrayList<OomHookWithContext>();
+
+  HiveServer2OomHookRunner(HiveServer2 hiveServer2, HiveConf hiveConf) {
+    context = new OomHookContext(hiveServer2);
+    // The hs2 has not been initialized yet, hiveServer2.getHiveConf() would be null
+    init(hiveConf);
+  }
+
+  private void init(HiveConf hiveConf) {
+    String csHooks = hiveConf.getVar(ConfVars.HIVE_SERVER2_OOM_HOOKS);
+    if (!StringUtils.isBlank(csHooks)) {
+      String[] hookClasses = csHooks.split(",");
+      for (String hookClass : hookClasses) {
+        try {
+          Class clazz =  JavaUtils.loadClass(hookClass.trim());
+          Constructor ctor = clazz.getDeclaredConstructor();
+          ctor.setAccessible(true);
+          hooks.add((OomHookWithContext)ctor.newInstance());
+        } catch (Exception e) {
+          LOG.error("Skip adding oom hook '" + hookClass + "'", e);
+        }
+      }
+    }
+  }
+
+  @VisibleForTesting
+  public HiveServer2OomHookRunner(HiveConf hiveConf) {
+    init(hiveConf);
+  }
+
+  @VisibleForTesting
+  public List<OomHookWithContext> getHooks() {
+    return hooks;
+  }
+
+  @Override
+  public void run() {
+    for (OomHookWithContext hook : hooks) {
+      hook.run(context);
+    }
+  }
+
+  public static interface OomHookWithContext {
+    public void run(OomHookContext context);
+  }
+
+  public static class OomHookContext {
+    private final HiveServer2 hiveServer2;
+    public OomHookContext(HiveServer2 hiveServer2) {
+      this.hiveServer2 = hiveServer2;
+    }
+    public HiveServer2 getHiveServer2() {
+      return hiveServer2;
+    }
+  }
+
+  /**
+   * Used as default oom hook
+   */
+  private static class DefaultOomHook implements OomHookWithContext {

Review comment:
       done




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-667482135


   @kgyrtkirk could you please take a look at the changes? thank you:)


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-669832758


   > sorry @dengzhhu653 lately I was a little bit flooded with all kind of stuff...and right now I'm on holiday - will get back to your patch next week!
   sorry for disturbing you. Have a nice holiday!


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-657896053


   @kgyrtkirk could you please take another look at the changes? thanks.


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-658484942


   > when I was using some of these hooks earlier: they are too specific to some purpose.
   > right now the proposed callback is a {{Runnable}} ... which means you will have to create a completely new one in case you want to pass some things to it later...
   > 
   > so...I would recommend the following:
   > 
   > * remove the "oom" keyword from the name of the hook/etc - it could be diag, debug or something like that
   > * add a callback which has 2 arguments:
   >   
   >   * a callback type (some enum; which should have a value which is specific to "oom")
   >   * some callback payload object to which we can add things later on... it could be an object using the {{Adaptable}} pattern
   > 
   > all this stuff is to make it a bit more reusable if we need to reuse it later on...
   
   ...Sorry,  I don't get much clear about "some callback payload object to which we can add things later on.",  is something like this:
   ```
    public class HookRuntime {
       private static final HookRuntime runtime = new HookRuntime();
       public static HookRuntime getHookRuntime() {
         return runtime;
       }
   
       private Map<Enum, HookPayloadObject> hooks = new HashMap<>();
   
       void callback(Enum Type, HookPayloadObject payload) {
       }
   
       HookPayloadObject getHookPayLoad(Enum typs) {
   
         return null;
       }
     }
   
     public interface HookPayloadObject<T> extends Runnable {
       void addHook(Hook hook);
       void setContext(T context);
     }
   ```
   Thanks a lot for your time!


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-654678113


   > I don't see the need for this....what's wrong with [afterexecute](https://github.com/apache/hive/blob/e4256fc91fe2c123428400f3737883a83208d29e/ql/src/java/org/apache/hadoop/hive/ql/Executor.java#L533) or [failurehooks](https://github.com/apache/hive/blob/e4256fc91fe2c123428400f3737883a83208d29e/ql/src/java/org/apache/hadoop/hive/ql/Executor.java#L521)?
   > do you have a use case which could not be handled by those?
   
   The oom hook holds a hiveserver2 instance, which calls hiveserver2::stop() to end hiveserver2 gracefully, which would cleanup the session directory/scratch directory/operation log and so on . Although the hooks in the driver can handle oom, he may not be able to stop the hiveserver2 gracefully as the oom hook does. Sometimes we may want to dump the heap for futher analysis when oom happens or alter the devops, so it may be better to make the oom hook here an interface.
   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 edited a comment on pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 edited a comment on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-656069559


   Make sense, nice suggestion!


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-656069559


   Make sense, nich suggestion!


----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-658002425


   when I was using some of these hooks earlier: they are too specific to some purpose.
   right now the proposed callback is a {{Runnable}} ... which means you will have to create a completely new one in case you want to pass some things to it later...
   
   so...I would recommend the following:
   * remove the "oom" keyword from the name of the hook/etc - it could be diag, debug or something like that
   * add a callback which has 2 arguments:
     * a callback type (some enum; which should have a value which is specific to "oom")
     * some callback payload object to which we can add things later on... it could be an object using the {{Adaptable}} pattern
   
   all this stuff is to make it a bit more reusable if we need to reuse it later on...


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r489144427



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HookContext.java
##########
@@ -45,7 +47,50 @@
 public class HookContext {
 
   static public enum HookType {
-    PRE_EXEC_HOOK, POST_EXEC_HOOK, ON_FAILURE_HOOK
+
+    PRE_EXEC_HOOK(HiveConf.ConfVars.PREEXECHOOKS, ExecuteWithHookContext.class,
+        "Pre-execution hooks to be invoked for each statement"),
+    POST_EXEC_HOOK(HiveConf.ConfVars.POSTEXECHOOKS, ExecuteWithHookContext.class,
+        "Post-execution hooks to be invoked for each statement"),
+    ON_FAILURE_HOOK(HiveConf.ConfVars.ONFAILUREHOOKS, ExecuteWithHookContext.class,
+        "On-failure hooks to be invoked for each statement"),
+    QUERY_LIFETIME_HOOKS(HiveConf.ConfVars.HIVE_QUERY_LIFETIME_HOOKS, QueryLifeTimeHook.class,
+      "Hooks that will be triggered before/after query compilation and before/after query execution"),
+    SEMANTIC_ANALYZER_HOOK(HiveConf.ConfVars.SEMANTIC_ANALYZER_HOOK, HiveSemanticAnalyzerHook.class,
+      "Hooks that invoked before/after Hive performs its own semantic analysis on a statement"),
+    DRIVER_RUN_HOOKS(HiveConf.ConfVars.HIVE_DRIVER_RUN_HOOKS, HiveDriverRunHook.class,
+      "Hooks that Will be run at the beginning and end of Driver.run"),
+    REDACTOR(HiveConf.ConfVars.QUERYREDACTORHOOKS, Redactor.class,
+      "Hooks to be invoked for each query which can tranform the query before it's placed in the job.xml file"),
+    // The HiveSessionHook.class cannot access, use Hook.class instead
+    HIVE_SERVER2_SESSION_HOOK(HiveConf.ConfVars.HIVE_SERVER2_SESSION_HOOK, Hook.class,
+      "Hooks to be executed when session manager starts a new session"),
+    OOM(HiveConf.ConfVars.HIVE_SERVER2_OOM_HOOKS, Runnable.class,

Review comment:
       done, thank you




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r450788257



##########
File path: service/src/java/org/apache/hive/service/server/HiveServer2OomHookRunner.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HiveServer2OomHookRunner implements Runnable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(HiveServer2OomHookRunner.class);
+  private OomHookContext context;
+  private final List<OomHookWithContext> hooks = new ArrayList<OomHookWithContext>();
+
+  HiveServer2OomHookRunner(HiveServer2 hiveServer2, HiveConf hiveConf) {
+    context = new OomHookContext(hiveServer2);
+    // The hs2 has not been initialized yet, hiveServer2.getHiveConf() would be null
+    init(hiveConf);
+  }
+
+  private void init(HiveConf hiveConf) {
+    String csHooks = hiveConf.getVar(ConfVars.HIVE_SERVER2_OOM_HOOKS);
+    if (!StringUtils.isBlank(csHooks)) {
+      String[] hookClasses = csHooks.split(",");
+      for (String hookClass : hookClasses) {
+        try {
+          Class clazz =  JavaUtils.loadClass(hookClass.trim());
+          Constructor ctor = clazz.getDeclaredConstructor();
+          ctor.setAccessible(true);

Review comment:
       Thanks for the review! Like DefaultOomHook, the hook declared as private as nobody would access it  except the HiveServer2OomHookRunner.  In this case, the default constructor cannot be used directly to create a instance until calling setAccessible(true).  The hooks loaded by HookRunner::loadHooksFromConf should be declared as public access and I'm willing to follow this principal.




----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r489141594



##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HooksLoader.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.hooks;
+
+import com.cronutils.utils.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.common.util.HiveStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *  Loads and stores different kinds of hooks, provides {@link HooksLoader#addHook(HookContext.HookType, Object)}} to
+ *  add hook alone or {@link HooksLoader#getHooks(HookContext.HookType, Class)} to get all hooks
+ *  corresponding to the specific hook type.
+ */
+public class HooksLoader {
+  private static final Logger LOG = LoggerFactory.getLogger(HooksLoader.class);
+  private final HiveConf conf;
+  private final Hooks[] hooks;
+  private SessionState.LogHelper console;
+
+  public HooksLoader(HiveConf conf) {
+    this.conf = conf;
+    this.hooks = new Hooks[HookContext.HookType.values().length];
+    for (int i = 0; i < hooks.length; i++) {
+      hooks[i] = new Hooks();
+    }
+  }
+
+  public HooksLoader(HiveConf conf, SessionState.LogHelper console) {
+    this(conf);
+    this.console = console;
+  }
+
+  /**
+   * Loads the configured hooks corresponding to the specific hook type.
+   * @param type hook type
+   */
+  @VisibleForTesting
+  void loadHooksFromConf(HookContext.HookType type) {
+    Hooks container = hooks[type.ordinal()];
+    if (!container.loadedFromConf) {
+      container.loadedFromConf = true;
+      List hooks = container.getHooks();
+      HiveConf.ConfVars confVars = type.getConfVar();

Review comment:
       Thank you very much for the careful review!  fixed.

##########
File path: ql/src/java/org/apache/hadoop/hive/ql/hooks/HooksLoader.java
##########
@@ -0,0 +1,162 @@
+/*
+ * 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.hooks;
+
+import com.cronutils.utils.VisibleForTesting;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.exec.Utilities;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.common.util.HiveStringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+
+/**
+ *  Loads and stores different kinds of hooks, provides {@link HooksLoader#addHook(HookContext.HookType, Object)}} to
+ *  add hook alone or {@link HooksLoader#getHooks(HookContext.HookType, Class)} to get all hooks
+ *  corresponding to the specific hook type.
+ */
+public class HooksLoader {
+  private static final Logger LOG = LoggerFactory.getLogger(HooksLoader.class);
+  private final HiveConf conf;
+  private final Hooks[] hooks;

Review comment:
       done




----------------------------------------------------------------
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



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


[GitHub] [hive] kgyrtkirk commented on pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
kgyrtkirk commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-656010567


   Hey @dengzhhu653  this comment might have not caught your attention:
   
   > I don't think this is a move in the right direction; you are moving the `HS2::stop` out into an "optional" hook stuff...which could be just removed by a conf change - I think it should remain part of the system.
   > 
   > ...your objective seems to be to add diagnostic capabilities around when the OOM happens; I think in case you get an oom the best is to capture a heapdump...and analyze that: to see what's wrong
   > but I may not know all the details...
   > 
   > I think creating some generic hook stuff which also gets notified about OOM - would be a better option
   
   what do you think about creating some more generic hook stuff (and add  - and then we can tie that in into more places if needed...if we put OOM in the name of things we may not re-use it later
   
   I've noticed one more thing: do we really need to put the `HS2` instance into the hookcontext? I don't think we wan't to guarantee that the interface of that class will not change...and as such: it should not be placed in a context accesible by extensions
   


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on pull request #1205: HIVE-23800: Add hooks when HiveServer2 stops due to OutOfMemoryError

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on pull request #1205:
URL: https://github.com/apache/hive/pull/1205#issuecomment-669698519


   @kgyrtkirk could you take some time to look at this again? any advices are appreciated,  thank you! 


----------------------------------------------------------------
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



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


[GitHub] [hive] dengzhhu653 commented on a change in pull request #1205: HIVE-23800: Make HiveServer2 oom hook interface

Posted by GitBox <gi...@apache.org>.
dengzhhu653 commented on a change in pull request #1205:
URL: https://github.com/apache/hive/pull/1205#discussion_r451539648



##########
File path: service/src/java/org/apache/hive/service/server/HiveServer2OomHookRunner.java
##########
@@ -0,0 +1,102 @@
+/*
+ * 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.server;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.hive.common.JavaUtils;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Constructor;
+import java.util.ArrayList;
+import java.util.List;
+
+public class HiveServer2OomHookRunner implements Runnable {
+
+  private static final Logger LOG = LoggerFactory.getLogger(HiveServer2OomHookRunner.class);
+  private OomHookContext context;
+  private final List<OomHookWithContext> hooks = new ArrayList<OomHookWithContext>();
+
+  HiveServer2OomHookRunner(HiveServer2 hiveServer2, HiveConf hiveConf) {
+    context = new OomHookContext(hiveServer2);
+    // The hs2 has not been initialized yet, hiveServer2.getHiveConf() would be null
+    init(hiveConf);
+  }
+
+  private void init(HiveConf hiveConf) {
+    String csHooks = hiveConf.getVar(ConfVars.HIVE_SERVER2_OOM_HOOKS);
+    if (!StringUtils.isBlank(csHooks)) {
+      String[] hookClasses = csHooks.split(",");
+      for (String hookClass : hookClasses) {
+        try {
+          Class clazz =  JavaUtils.loadClass(hookClass.trim());
+          Constructor ctor = clazz.getDeclaredConstructor();
+          ctor.setAccessible(true);
+          hooks.add((OomHookWithContext)ctor.newInstance());
+        } catch (Exception e) {
+          LOG.error("Skip adding oom hook '" + hookClass + "'", e);

Review comment:
       done




----------------------------------------------------------------
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



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