Browse Source

[beeswax] Close HiveServer2 and Impala session command

  e.g. Close all HiveServer2 sessions created by Hue
  build/env/bin/hue close_sessions 0 hive

  e.g. Close all HiveServer2 and Impala created by Hue at least one week ago
  build/env/bin/hue close_sessions 7 all
Romain Rigaux 11 năm trước cách đây
mục cha
commit
9f3fb69

+ 64 - 0
apps/beeswax/src/beeswax/management/commands/close_sessions.py

@@ -0,0 +1,64 @@
+#!/usr/bin/env python
+# Licensed to Cloudera, Inc. under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  Cloudera, Inc. 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.
+
+from django.core.management.base import BaseCommand
+
+from datetime import datetime,  timedelta
+
+from beeswax.models import Session
+from beeswax.server import dbms
+
+
+class Command(BaseCommand):
+  """
+  Close old HiveServer2 and Impala sessions.
+
+  e.g.
+  build/env/bin/hue close_sessions 7 all
+  Closing (all=True) queries older than 7 days...
+  0 queries closed.
+  """
+  args = '<age_in_days> <hive,impala,all> (default is 7 and hive)'
+  help = 'Close finished Hive queries older than 7 days. If \'all\' is specified, also close the Impala ones.'
+
+  def handle(self, *args, **options):
+    days = int(args[0]) if len(args) >= 1 else 7
+    query_type = args[1] if len(args) >= 2 else None
+    if query_type == 'hive' or query_type is None:
+      query_type = 'beeswax'
+
+    self.stdout.write('Closing (all=%s) HiveServer2/Impala sessions older than %s days...\n' % (query_type, days))
+
+    n = 0
+    sessions = Session.objects.all()
+
+    if query_type != 'all':
+      sessions = sessions.filter(application=query_type)
+
+    sessions = sessions.filter(last_used__lte=datetime.today() - timedelta(days=days))
+
+    for session in sessions:
+      try:
+          resp = dbms.get(user=session.owner).close_session(session)
+          if not 'Session does not exist!' in str(resp):
+            self.stdout.write('Error: %s\n' % resp)
+            n += 1
+      except Exception, e:
+        if not 'Session does not exist!' in str(e):
+          self.stdout.write('Error: %s\n' % e)
+
+    self.stdout.write('%s sessions closed.\n' % n)

+ 3 - 0
apps/beeswax/src/beeswax/server/dbms.py

@@ -159,6 +159,9 @@ class HiveServer2Dbms(object):
   def open_session(self, user):
     return self.client.open_session(user)
 
+  def close_session(self, session):
+    return self.client.close_session(session)
+
   def cancel_operation(self, query_handle):
     resp = self.client.cancel_operation(query_handle)
     if self.client.query_server['server_name'] == 'impala':

+ 7 - 4
apps/beeswax/src/beeswax/server/hive_server2_lib.py

@@ -429,10 +429,8 @@ class HiveServerClient:
       return res
 
 
-  def close_session(self):
-    session = Session.objects.get_session(self.user, self.query_server['server_name']).get_handle()
-
-    req = TCloseSessionReq(sessionHandle=session)
+  def close_session(self, sessionHandle):
+    req = TCloseSessionReq(sessionHandle=sessionHandle)
     return self._client.CloseSession(req)
 
 
@@ -722,6 +720,11 @@ class HiveServerClientCompatible(object):
     return self._client.close_operation(operationHandle)
 
 
+  def close_session(self, session):
+    operationHandle = session.get_handle()
+    return self._client.close_session(operationHandle)
+
+
   def dump_config(self):
     return 'Does not exist in HS2'