Esempio n. 1
0
    def test_get_instance_entity(self):
        entity = shared.get_instance_entity()
        # Save the modification, make sure it sticks.
        entity.task_num = 42
        entity.put()
        entity2 = shared.get_instance_entity()
        self.assertEqual(42, entity2.task_num)

        # Make sure it does not pollute the default namespace.
        self.assertIsNone(shared.Instance.get_by_id(entity.key.id()))
Esempio n. 2
0
  def test_get_instance_entity(self):
    entity = shared.get_instance_entity()
    # Save the modification, make sure it sticks.
    entity.task_num = 42
    entity.put()
    entity2 = shared.get_instance_entity()
    self.assertEqual(42, entity2.task_num)

    # Make sure it does not pollute the default namespace.
    self.assertIsNone(shared.Instance.get_by_id(entity.key.id()))
Esempio n. 3
0
def _flush_metrics(time_now):
    """Return True if metrics were actually sent."""
    if interface.state.target is None:
        # ts_mon is not configured.
        return False

    datetime_now = datetime.datetime.utcfromtimestamp(time_now)
    entity = shared.get_instance_entity()
    if entity.task_num < 0:
        if interface.state.target.task_num >= 0:
            _reset_cumulative_metrics()
        interface.state.target.task_num = -1
        interface.state.last_flushed = entity.last_updated
        updated_sec_ago = (datetime_now - entity.last_updated).total_seconds()
        if updated_sec_ago > shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC:
            logging.warning('Instance %s is %d seconds old with no task_num.',
                            shared.instance_key_id(), updated_sec_ago)
        return False
    interface.state.target.task_num = entity.task_num

    entity.last_updated = datetime_now
    entity_deferred = entity.put_async()

    interface.flush()

    for metric in interface.state.global_metrics.itervalues():
        metric.reset()

    entity_deferred.get_result()
    return True
Esempio n. 4
0
def _flush_metrics(time_now):
  """Return True if metrics were actually sent."""
  datetime_now = datetime.datetime.utcfromtimestamp(time_now)
  entity = shared.get_instance_entity()
  if entity.task_num < 0:
    if interface.state.target.task_num >= 0:
      _reset_cumulative_metrics()
    interface.state.target.task_num = -1
    interface.state.last_flushed = entity.last_updated
    updated_sec_ago = (datetime_now - entity.last_updated).total_seconds()
    if updated_sec_ago > shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC:
      logging.warning('Instance %s is %d seconds old with no task_num.',
                      shared.instance_key_id(), updated_sec_ago)
    return False
  interface.state.target.task_num = entity.task_num

  entity.last_updated = datetime_now
  entity_deferred = entity.put_async()

  interface.flush()

  for metric in shared.global_metrics.itervalues():
    metric.reset()

  entity_deferred.get_result()
  return True
Esempio n. 5
0
 def test_shutdown_hook_not_flushed(self, _mock_flush):
     time_now = 10000
     id = shared.get_instance_entity().key.id()
     with shared.instance_namespace_context():
         self.assertIsNotNone(shared.Instance.get_by_id(id))
     config._shutdown_hook(time_fn=lambda: time_now)
     with shared.instance_namespace_context():
         self.assertIsNone(shared.Instance.get_by_id(id))
Esempio n. 6
0
 def test_shutdown_hook_not_flushed(self, _mock_flush):
   time_now = 10000
   id = shared.get_instance_entity().key.id()
   with shared.instance_namespace_context():
     self.assertIsNotNone(shared.Instance.get_by_id(id))
   config._shutdown_hook(time_fn=lambda: time_now)
   with shared.instance_namespace_context():
     self.assertIsNone(shared.Instance.get_by_id(id))
Esempio n. 7
0
 def test_flush_metrics_too_early(self):
     # Too early to send metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     less_than_min_ago = datetime_now - datetime.timedelta(seconds=59)
     interface.state.last_flushed = less_than_min_ago
     entity = shared.get_instance_entity()
     entity.task_num = 2
     self.assertFalse(config.flush_metrics_if_needed(time_now))
Esempio n. 8
0
 def test_flush_metrics_too_early(self):
   # Too early to send metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   less_than_min_ago = datetime_now - datetime.timedelta(seconds=59)
   interface.state.last_flushed = less_than_min_ago
   entity = shared.get_instance_entity()
   entity.task_num = 2
   self.assertFalse(config.flush_metrics_if_needed(time_now))
Esempio n. 9
0
 def test_flush_metrics_purged(self):
     # We lost our task_num; cannot send metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
     interface.state.last_flushed = more_than_min_ago
     entity = shared.get_instance_entity()
     entity.task_num = -1
     interface.state.target.task_num = 2
     self.assertFalse(config.flush_metrics_if_needed(time_now))
Esempio n. 10
0
 def test_flush_metrics_purged(self):
   # We lost our task_num; cannot send metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
   interface.state.last_flushed = more_than_min_ago
   entity = shared.get_instance_entity()
   entity.task_num = -1
   interface.state.target.task_num = 2
   self.assertFalse(config.flush_metrics_if_needed(time_now))
Esempio n. 11
0
 def test_flush_metrics_disabled(self, mock_flush):
     # We have task_num and due for sending metrics, but ts_mon is disabled.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
     interface.state.last_flushed = more_than_min_ago
     interface.state.flush_enabled_fn = lambda: False
     entity = shared.get_instance_entity()
     entity.task_num = 2
     self.assertFalse(config.flush_metrics_if_needed(time_now))
     self.assertEqual(0, mock_flush.call_count)
Esempio n. 12
0
 def test_flush_metrics_disabled(self, mock_flush):
   # We have task_num and due for sending metrics, but ts_mon is disabled.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
   interface.state.last_flushed = more_than_min_ago
   interface.state.flush_enabled_fn = lambda: False
   entity = shared.get_instance_entity()
   entity.task_num = 2
   self.assertFalse(config.flush_metrics_if_needed(time_now))
   self.assertEqual(0, mock_flush.call_count)
Esempio n. 13
0
 def test_flush_metrics_no_task_num_too_long(self):
     # We are not assigned task_num for too long; cannot send metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     too_long_ago = datetime_now - datetime.timedelta(
         seconds=shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC + 1)
     interface.state.last_flushed = too_long_ago
     entity = shared.get_instance_entity()
     entity.task_num = -1
     entity.last_updated = too_long_ago
     interface.state.target.task_num = -1
     self.assertFalse(config.flush_metrics_if_needed(time_now))
Esempio n. 14
0
 def test_flush_metrics_no_task_num_too_long(self):
   # We are not assigned task_num for too long; cannot send metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   too_long_ago = datetime_now - datetime.timedelta(
       seconds=shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC+1)
   interface.state.last_flushed = too_long_ago
   entity = shared.get_instance_entity()
   entity.task_num = -1
   entity.last_updated = too_long_ago
   interface.state.target.task_num = -1
   self.assertFalse(config.flush_metrics_if_needed(time_now))
Esempio n. 15
0
 def test_flush_metrics_successfully(self, mock_flush):
     # We have task_num and due for sending metrics.
     time_now = 10000
     datetime_now = datetime.datetime.utcfromtimestamp(time_now)
     more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
     interface.state.last_flushed = more_than_min_ago
     entity = shared.get_instance_entity()
     entity.task_num = 2
     # Global metrics must be erased after flush.
     test_global_metric = gae_ts_mon.GaugeMetric('test', 'foo', None)
     test_global_metric.set(42)
     interface.register_global_metrics([test_global_metric])
     self.assertEqual(42, test_global_metric.get())
     self.assertTrue(config.flush_metrics_if_needed(time_now))
     self.assertEqual(None, test_global_metric.get())
     mock_flush.assert_called_once_with()
Esempio n. 16
0
 def test_flush_metrics_successfully(self, mock_flush):
   # We have task_num and due for sending metrics.
   time_now = 10000
   datetime_now = datetime.datetime.utcfromtimestamp(time_now)
   more_than_min_ago = datetime_now - datetime.timedelta(seconds=61)
   interface.state.last_flushed = more_than_min_ago
   entity = shared.get_instance_entity()
   entity.task_num = 2
   # Global metrics must be erased after flush.
   test_global_metric = gae_ts_mon.GaugeMetric('test')
   test_global_metric.set(42)
   shared.register_global_metrics([test_global_metric])
   self.assertEqual(42, test_global_metric.get())
   self.assertTrue(config.flush_metrics_if_needed(time_now))
   self.assertEqual(None, test_global_metric.get())
   mock_flush.assert_called_once_with()