diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java index 984e3fc..df777e9 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java @@ -284,14 +284,7 @@ private final void logAuditEvent(String cmd) { final Formatter fmt = auditFormatter.get(); ((StringBuilder) fmt.out()).setLength(0); - String address = null; - if (useSasl) { - if (saslServer != null && saslServer.getRemoteAddress() != null) { - address = String.valueOf(saslServer.getRemoteAddress()); - } - } else { - address = getIpAddress(); - } + String address = getIPAddress(); if (address == null) { address = "unknown-ip-addr"; } @@ -300,6 +293,18 @@ private final void logAuditEvent(String cmd) { address, cmd).toString()); } + String getIPAddress() { + if (useSasl) { + if (saslServer != null && saslServer.getRemoteAddress() != null) { + return String.valueOf(saslServer.getRemoteAddress()); + } + } else { + // if kerberos is not enabled + return getThreadLocalIpAddress(); + } + return null; + } + private static int nextSerialNum = 0; private static ThreadLocal threadLocalId = new ThreadLocal() { @Override @@ -310,7 +315,7 @@ protected Integer initialValue() { // This will only be set if the metastore is being accessed from a metastore Thrift server, // not if it is from the CLI. Also, only if the TTransport being used to connect is an - // instance of TSocket. + // instance of TSocket. This is also not set when kerberos is used. private static ThreadLocal threadLocalIpAddress = new ThreadLocal() { @Override protected String initialValue() { @@ -318,13 +323,14 @@ protected String initialValue() { } }; - public static void setIpAddress(String ipAddress) { + public static void setThreadLocalIpAddress(String ipAddress) { threadLocalIpAddress.set(ipAddress); } // This will return null if the metastore is not being accessed from a metastore Thrift server, - // or if the TTransport being used to connect is not an instance of TSocket. - public static String getIpAddress() { + // or if the TTransport being used to connect is not an instance of TSocket, or if kereberos + // is used + public static String getThreadLocalIpAddress() { return threadLocalIpAddress.get(); } @@ -727,7 +733,7 @@ private void logInfo(String m) { private String startFunction(String function, String extraLogInfo) { incrementCounter(function); - logInfo((getIpAddress() == null ? "" : "source:" + getIpAddress() + " ") + + logInfo((getThreadLocalIpAddress() == null ? "" : "source:" + getThreadLocalIpAddress() + " ") + function + extraLogInfo); if (MetricsFactory.getInstance() != null) { try { @@ -5242,7 +5248,7 @@ public String get_delegation_token(String token_owner, try { ret = HiveMetaStore.getDelegationToken(token_owner, - renewer_kerberos_principal_name, getIpAddress()); + renewer_kerberos_principal_name, getIPAddress()); } catch (IOException e) { ex = e; throw new MetaException(e.getMessage()); diff --git a/metastore/src/java/org/apache/hadoop/hive/metastore/TSetIpAddressProcessor.java b/metastore/src/java/org/apache/hadoop/hive/metastore/TSetIpAddressProcessor.java index 4a56bfa..38b0875 100644 --- a/metastore/src/java/org/apache/hadoop/hive/metastore/TSetIpAddressProcessor.java +++ b/metastore/src/java/org/apache/hadoop/hive/metastore/TSetIpAddressProcessor.java @@ -57,6 +57,6 @@ protected void setIpAddress(final TProtocol in) { } protected void setIpAddress(final Socket inSocket) { - HMSHandler.setIpAddress(inSocket.getInetAddress().getHostAddress()); + HMSHandler.setThreadLocalIpAddress(inSocket.getInetAddress().getHostAddress()); } } diff --git a/metastore/src/test/org/apache/hadoop/hive/metastore/IpAddressListener.java b/metastore/src/test/org/apache/hadoop/hive/metastore/IpAddressListener.java index d8ec281..e40edca 100644 --- a/metastore/src/test/org/apache/hadoop/hive/metastore/IpAddressListener.java +++ b/metastore/src/test/org/apache/hadoop/hive/metastore/IpAddressListener.java @@ -50,7 +50,7 @@ public IpAddressListener(Configuration config) { private void checkIpAddress() { try { String localhostIp = InetAddress.getByName(LOCAL_HOST).getHostAddress(); - Assert.assertEquals(localhostIp, HMSHandler.getIpAddress()); + Assert.assertEquals(localhostIp, HMSHandler.getThreadLocalIpAddress()); } catch (UnknownHostException e) { Assert.assertTrue("InetAddress.getLocalHost threw an exception: " + e.getMessage(), false); }