diff --git a/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java b/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java index e77b7f1..b6fe77c 100644 --- a/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java +++ b/cli/src/java/org/apache/hadoop/hive/cli/CliDriver.java @@ -118,8 +118,8 @@ public int processCmd(String cmd) { CliSessionState ss = (CliSessionState) SessionState.get(); ss.setLastCommand(cmd); - String callerInfo = ss.getConf().getLogIdVar(ss.getSessionId()); - Thread.currentThread().setName(callerInfo + " " + originalThreadName); + ss.updateThreadName(); + // Flush the print stream, so it doesn't include output from the last command ss.err.flush(); String cmd_trimmed = cmd.trim(); @@ -711,7 +711,8 @@ public int run(String[] args) throws Exception { SessionState.start(ss); } - Thread.currentThread().setName(conf.getLogIdVar(ss.getSessionId()) + " " + originalThreadName); + ss.updateThreadName(); + // execute cli driver work try { return executeDriver(ss, conf, oproc); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java index 70b2bc0..bfe04a2 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/session/SessionState.java @@ -399,6 +399,28 @@ public String getSessionId() { return (conf.getVar(HiveConf.ConfVars.HIVESESSIONID)); } + public void updateThreadName() { + final String sessionId = getSessionId(); + final String logPrefix = getConf().getLogIdVar(sessionId); + final String currThreadName = Thread.currentThread().getName(); + if (!currThreadName.contains(logPrefix)) { + final String newThreadName = logPrefix + " " + currThreadName; + Thread.currentThread().setName(newThreadName); + LOG.info("Updated thread name to {}", newThreadName); + } + } + + public void resetThreadName() { + final String sessionId = getSessionId(); + final String logPrefix = getConf().getLogIdVar(sessionId); + final String currThreadName = Thread.currentThread().getName(); + if (currThreadName.contains(logPrefix)) { + final String[] names = currThreadName.split(logPrefix); + Thread.currentThread().setName(names[names.length - 1]); + LOG.info("Resetting thread name to {}", names[names.length - 1]); + } + } + /** * Initialize the transaction manager. This is done lazily to avoid hard wiring one * transaction manager at the beginning of the session. diff --git a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java index 4aa1875..3926d1b 100644 --- a/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java +++ b/service/src/java/org/apache/hive/service/cli/session/HiveSessionImpl.java @@ -330,10 +330,7 @@ protected synchronized void acquire(boolean userAccess) { lastAccessTime = System.currentTimeMillis(); } // set the thread name with the logging prefix. - String logPrefix = getHiveConf().getLogIdVar(sessionState.getSessionId()); - LOG.info( - "Prefixing the thread name (" + Thread.currentThread().getName() + ") with " + logPrefix); - Thread.currentThread().setName(logPrefix + Thread.currentThread().getName()); + sessionState.updateThreadName(); Hive.set(sessionHive); } @@ -348,17 +345,7 @@ protected synchronized void release(boolean userAccess) { if (sessionState != null) { // can be null in-case of junit tests. skip reset. // reset thread name at release time. - String[] names = Thread.currentThread().getName() - .split(getHiveConf().getLogIdVar(sessionState.getSessionId())); - String threadName = null; - if (names.length > 1) { - threadName = names[names.length - 1]; - } else if (names.length == 1) { - threadName = names[0]; - } else { - threadName = ""; - } - Thread.currentThread().setName(threadName); + sessionState.resetThreadName(); } SessionState.detachSession();