You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aurora.apache.org by wi...@apache.org on 2015/03/09 22:40:01 UTC

incubator-aurora git commit: Add aurora-specific entry point for thermos observer and fix /vars

Repository: incubator-aurora
Updated Branches:
  refs/heads/master c47a25f31 -> e55113daf


Add aurora-specific entry point for thermos observer and fix /vars

Just like the aurora-specific thermos entry point, adds an aurora-specific
thermos_observer entry point.  Just allows it to search for thermos runners
in /var/lib/thermos and also relative to mesos sandboxes.

Testing Done:
mba=aurora=; ./pants test.pytest --no-fast src/test/python::

Bugs closed: AURORA-1133

Reviewed at https://reviews.apache.org/r/31774/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/e55113da
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/e55113da
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/e55113da

Branch: refs/heads/master
Commit: e55113daf0226b9164544096f801c628cfc3573c
Parents: c47a25f
Author: Brian Wickman <wi...@apache.org>
Authored: Mon Mar 9 14:39:16 2015 -0700
Committer: Brian Wickman <wi...@apache.org>
Committed: Mon Mar 9 14:39:16 2015 -0700

----------------------------------------------------------------------
 src/main/python/apache/aurora/tools/BUILD       | 16 +++++
 .../apache/aurora/tools/thermos_observer.py     | 76 ++++++++++++++++++++
 src/main/python/apache/thermos/observer/BUILD   |  1 +
 .../python/apache/thermos/observer/bin/BUILD    |  5 +-
 .../thermos/observer/bin/thermos_observer.py    | 37 ++++------
 .../python/apache/thermos/observer/http/BUILD   | 29 ++++++++
 .../apache/thermos/observer/http/configure.py   | 33 +++++++++
 .../apache/thermos/observer/http/diagnostics.py | 51 +++++++++++++
 .../thermos/observer/http/vars_endpoint.py      | 69 ++++++++++++++++++
 .../apache/thermos/observer/task_observer.py    |  2 -
 10 files changed, 291 insertions(+), 28 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/aurora/tools/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/tools/BUILD b/src/main/python/apache/aurora/tools/BUILD
index 36117b4..9dfeb6c 100644
--- a/src/main/python/apache/aurora/tools/BUILD
+++ b/src/main/python/apache/aurora/tools/BUILD
@@ -21,3 +21,19 @@ python_binary(
     'src/main/python/apache/aurora/executor/common:path_detector',
   ]
 )
+
+python_binary(
+  name = 'thermos_observer',
+  source = 'thermos_observer.py',
+  dependencies = [
+    '3rdparty/python:CherryPy',
+    '3rdparty/python:twitter.common.app',
+    '3rdparty/python:twitter.common.exceptions',
+    '3rdparty/python:twitter.common.log',
+    'src/main/python/apache/aurora/executor/common:path_detector',
+    'src/main/python/apache/thermos/common:constants',
+    'src/main/python/apache/thermos/monitoring:detector',
+    'src/main/python/apache/thermos/observer:task_observer',
+    'src/main/python/apache/thermos/observer/http:configure',
+  ]
+)

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/aurora/tools/thermos_observer.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/aurora/tools/thermos_observer.py b/src/main/python/apache/aurora/tools/thermos_observer.py
new file mode 100644
index 0000000..4b534d3
--- /dev/null
+++ b/src/main/python/apache/aurora/tools/thermos_observer.py
@@ -0,0 +1,76 @@
+#
+# Licensed 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.
+#
+
+"""A Mesos-customized entry point to the thermos_observer webserver."""
+
+import time
+
+from twitter.common import app
+from twitter.common.exceptions import ExceptionalThread
+from twitter.common.log.options import LogOptions
+
+from apache.aurora.executor.common.path_detector import MesosPathDetector
+from apache.thermos.common.constants import DEFAULT_CHECKPOINT_ROOT
+from apache.thermos.monitoring.detector import ChainedPathDetector, FixedPathDetector
+from apache.thermos.observer.http.configure import configure_server
+from apache.thermos.observer.task_observer import TaskObserver
+
+app.add_option(
+    '--root',
+    dest='root',
+    type='string',
+    default=DEFAULT_CHECKPOINT_ROOT,
+    help='The thermos checkpoint root directory to search for Thermos tasks [default: %default]')
+
+
+app.add_option(
+    '--mesos-root',
+    dest='mesos_root',
+    type='string',
+    default=MesosPathDetector.DEFAULT_MESOS_ROOT,
+    help='The mesos root directory to search for Thermos executor sandboxes [default: %default]')
+
+
+app.add_option(
+    '--port',
+    dest='port',
+    type='int',
+    default=1338,
+    help='The port on which the observer should listen.')
+
+
+# Allow an interruptible sleep so that ^C works.
+def sleep_forever():
+  while True:
+    time.sleep(1)
+
+
+def main(_, options):
+  path_detector = ChainedPathDetector(
+      FixedPathDetector(options.root),
+      MesosPathDetector(options.mesos_root),
+  )
+  observer = TaskObserver(path_detector)
+  observer.start()
+  root_server = configure_server(observer)
+
+  thread = ExceptionalThread(target=lambda: root_server.run('0.0.0.0', options.port, 'cherrypy'))
+  thread.daemon = True
+  thread.start()
+
+  sleep_forever()
+
+
+LogOptions.set_stderr_log_level('google:INFO')
+app.main()

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/BUILD b/src/main/python/apache/thermos/observer/BUILD
index 28995b9..41db2cc 100644
--- a/src/main/python/apache/thermos/observer/BUILD
+++ b/src/main/python/apache/thermos/observer/BUILD
@@ -36,6 +36,7 @@ python_library(
   ]
 )
 
+
 python_library(
   name = 'task_observer',
   sources = ['task_observer.py'],

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/bin/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/bin/BUILD b/src/main/python/apache/thermos/observer/bin/BUILD
index a42dbf3..0abe2cc 100644
--- a/src/main/python/apache/thermos/observer/bin/BUILD
+++ b/src/main/python/apache/thermos/observer/bin/BUILD
@@ -15,15 +15,14 @@
 python_binary(
   name = 'thermos_observer',
   source = 'thermos_observer.py',
-  entry_point = 'apache.thermos.observer.bin.thermos_observer:proxy_main',
+  entry_point = 'apache.thermos.observer.bin.thermos_observer:run',
   dependencies = [
     '3rdparty/python:CherryPy',
     '3rdparty/python:twitter.common.app',
     '3rdparty/python:twitter.common.exceptions',
-    '3rdparty/python:twitter.common.http',
     'src/main/python/apache/thermos/common:constants',
     'src/main/python/apache/thermos/monitoring:detector',
-    'src/main/python/apache/thermos/observer/http:http_observer',
+    'src/main/python/apache/thermos/observer/http:configure',
     'src/main/python/apache/thermos/observer:task_observer',
   ],
 )

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/bin/thermos_observer.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/bin/thermos_observer.py b/src/main/python/apache/thermos/observer/bin/thermos_observer.py
index 213a48e..39d3994 100644
--- a/src/main/python/apache/thermos/observer/bin/thermos_observer.py
+++ b/src/main/python/apache/thermos/observer/bin/thermos_observer.py
@@ -14,16 +14,14 @@
 
 from __future__ import print_function
 
-import sys
+import time
 
 from twitter.common import app
 from twitter.common.exceptions import ExceptionalThread
-from twitter.common.http import HttpServer
-from twitter.common.http.diagnostics import DiagnosticsEndpoints
 
 from apache.thermos.common.constants import DEFAULT_CHECKPOINT_ROOT
 from apache.thermos.monitoring.detector import FixedPathDetector
-from apache.thermos.observer.http.http_observer import BottleObserver
+from apache.thermos.observer.http.configure import configure_server
 from apache.thermos.observer.task_observer import TaskObserver
 
 app.add_option("--root",
@@ -40,29 +38,22 @@ app.add_option("--port",
                help="port number to listen on.")
 
 
-def proxy_main():
-  def main(args, opts):
-    if args:
-      print("ERROR: unrecognized arguments: %s\n" % (" ".join(args)), file=sys.stderr)
-      app.help()
-      sys.exit(1)
+def sleep_forever():
+  while True:
+    time.sleep(1)
 
-    root_server = HttpServer()
-    root_server.mount_routes(DiagnosticsEndpoints())
 
-    task_observer = TaskObserver(FixedPathDetector(opts.root))
+def run():
+  def main(_, opts):
+    path_detector = FixedPathDetector(opts.root)
+    task_observer = TaskObserver(path_detector)
     task_observer.start()
+    server = configure_server(task_observer)
 
-    bottle_wrapper = BottleObserver(task_observer)
+    thread = ExceptionalThread(target=lambda: server.run('0.0.0.0', opts.port, 'cherrypy'))
+    thread.daemon = True
+    thread.start()
 
-    root_server.mount_routes(bottle_wrapper)
-
-    def run():
-      root_server.run('0.0.0.0', opts.port, 'cherrypy')
-
-    et = ExceptionalThread(target=run)
-    et.daemon = True
-    et.start()
-    et.join()
+    sleep_forever()
 
   app.main()

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/http/BUILD
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/http/BUILD b/src/main/python/apache/thermos/observer/http/BUILD
index cc8eb77..0bd77045 100644
--- a/src/main/python/apache/thermos/observer/http/BUILD
+++ b/src/main/python/apache/thermos/observer/http/BUILD
@@ -36,6 +36,35 @@ python_library(
 )
 
 python_library(
+  name = 'diagnostics',
+  sources = ['diagnostics.py'],
+  dependencies = [
+    '3rdparty/python:pex',
+    '3rdparty/python:twitter.common.metrics',
+  ]
+)
+
+python_library(
+  name = 'vars_endpoint',
+  sources = ['vars_endpoint.py'],
+  dependencies = [
+    '3rdparty/python:twitter.common.http',
+    '3rdparty/python:twitter.common.metrics',
+  ]
+)
+
+python_library(
+  name = 'configure',
+  sources = ['configure.py'],
+  dependencies = [
+    '3rdparty/python:twitter.common.http',
+    ':diagnostics',
+    ':http_observer',
+    ':vars_endpoint',
+  ]
+)
+
+python_library(
   name = 'file_browser',
   sources = ['file_browser.py'],
   dependencies = [

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/http/configure.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/http/configure.py b/src/main/python/apache/thermos/observer/http/configure.py
new file mode 100644
index 0000000..9a50f21
--- /dev/null
+++ b/src/main/python/apache/thermos/observer/http/configure.py
@@ -0,0 +1,33 @@
+#
+# Licensed 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 twitter.common.http import HttpServer
+from twitter.common.http.diagnostics import DiagnosticsEndpoints
+from twitter.common.metrics import RootMetrics
+
+from .diagnostics import register_build_properties, register_diagnostics
+from .http_observer import BottleObserver
+from .vars_endpoint import VarsEndpoint
+
+
+def configure_server(task_observer):
+  bottle_wrapper = BottleObserver(task_observer)
+  root_metrics = RootMetrics()
+  server = HttpServer()
+  server.mount_routes(bottle_wrapper)
+  server.mount_routes(DiagnosticsEndpoints())
+  server.mount_routes(VarsEndpoint())
+  register_build_properties(root_metrics)
+  register_diagnostics(root_metrics)
+  return server

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/http/diagnostics.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/http/diagnostics.py b/src/main/python/apache/thermos/observer/http/diagnostics.py
new file mode 100644
index 0000000..1fefd3e
--- /dev/null
+++ b/src/main/python/apache/thermos/observer/http/diagnostics.py
@@ -0,0 +1,51 @@
+#
+# Licensed 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.
+#
+
+import os
+import sys
+import time
+
+from twitter.common.metrics import Label, LambdaGauge
+
+try:
+  from pex import PEX
+  HAS_PEX = True
+except ImportError:
+  HAS_PEX = False
+
+
+def register_diagnostics(metrics):
+  rm = metrics.scope('sys')
+  now = time.time()
+  rm.register(LambdaGauge('uptime', lambda: time.time() - now))
+  rm.register(Label('argv', repr(sys.argv)))
+  rm.register(Label('path', repr(sys.path)))
+  rm.register(Label('version', sys.version))
+  rm.register(Label('platform', sys.platform))
+  rm.register(Label('executable', sys.executable))
+  rm.register(Label('prefix', sys.prefix))
+  rm.register(Label('exec_prefix', sys.exec_prefix))
+  rm.register(Label('uname', ' '.join(os.uname())))
+
+
+def register_build_properties(metrics):
+  if not HAS_PEX:
+    return
+  rm = metrics.scope('build')
+  try:
+    build_properties = PEX().info.build_properties
+  except PEX.NotFound:
+    return
+  for key, value in build_properties.items():
+    rm.register(Label(str(key), str(value)))

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/http/vars_endpoint.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/http/vars_endpoint.py b/src/main/python/apache/thermos/observer/http/vars_endpoint.py
new file mode 100644
index 0000000..f0cdce6
--- /dev/null
+++ b/src/main/python/apache/thermos/observer/http/vars_endpoint.py
@@ -0,0 +1,69 @@
+#
+# Licensed 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 twitter.common.http import HttpServer
+from twitter.common.http.server import request
+from twitter.common.metrics import MetricSampler, RootMetrics
+
+
+class VarsEndpoint(object):
+  """
+    Wrap a MetricSampler to export the /vars endpoint for applications that register
+    exported variables.
+  """
+
+  def __init__(self, period=None, stats_filter=None):
+    self._metrics = RootMetrics()
+    self._stats_filter = stats_filter
+    if period is not None:
+      self._monitor = MetricSampler(self._metrics, period)
+    else:
+      self._monitor = MetricSampler(self._metrics)
+    self._monitor.start()
+
+  @HttpServer.route("/vars")
+  @HttpServer.route("/vars/:var")
+  def handle_vars(self, var=None):
+    HttpServer.set_content_type('text/plain; charset=iso-8859-1')
+    filtered = self._parse_filtered_arg()
+    samples = self._monitor.sample()
+
+    if var is None and filtered and self._stats_filter:
+      return '\n'.join(
+          '%s %s' % (key, val) for key, val in sorted(samples.items())
+          if not self._stats_filter.match(key))
+    elif var is None:
+      return '\n'.join(
+          '%s %s' % (key, val) for key, val in sorted(samples.items()))
+    else:
+      if var in samples:
+        return samples[var]
+      else:
+        HttpServer.abort(404, 'Unknown exported variable')
+
+  @HttpServer.route("/vars.json")
+  def handle_vars_json(self, var=None, value=None):
+    filtered = self._parse_filtered_arg()
+    sample = self._monitor.sample()
+    if filtered and self._stats_filter:
+      return dict((key, val) for key, val in sample.items() if not self._stats_filter.match(key))
+    else:
+      return sample
+
+  def shutdown(self):
+    self._monitor.shutdown()
+    self._monitor.join()
+
+  def _parse_filtered_arg(self):
+    return request.GET.get('filtered', '') in ('true', '1')

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/e55113da/src/main/python/apache/thermos/observer/task_observer.py
----------------------------------------------------------------------
diff --git a/src/main/python/apache/thermos/observer/task_observer.py b/src/main/python/apache/thermos/observer/task_observer.py
index 6e7517b..b9a37de 100644
--- a/src/main/python/apache/thermos/observer/task_observer.py
+++ b/src/main/python/apache/thermos/observer/task_observer.py
@@ -297,7 +297,6 @@ class TaskObserver(ExceptionalThread, Lockable):
 
   def _sample(self, task_id):
     if task_id not in self.active_tasks:
-      log.debug("Task %s not found in active tasks" % task_id)
       sample = ProcessSample.empty().to_dict()
       sample['disk'] = 0
     else:
@@ -414,7 +413,6 @@ class TaskObserver(ExceptionalThread, Lockable):
   @Lockable.sync
   def _get_process_resource_consumption(self, task_id, process_name):
     if task_id not in self.active_tasks:
-      log.debug("Task %s not found in active tasks" % task_id)
       return ProcessSample.empty().to_dict()
     sample = self.active_tasks[task_id].resource_monitor.sample_by_process(process_name).to_dict()
     log.debug('Resource consumption (%s, %s) => %s' % (task_id, process_name, sample))