text: begin_unit
  comment|'# Copyright (c) 2014 Rackspace Hosting'
  nl|'\n'
  comment|'# All Rights Reserved.'
  nl|'\n'
  comment|'#'
  nl|'\n'
  comment|'#    Licensed under the Apache License, Version 2.0 (the "License"); you may'
  nl|'\n'
  comment|'#    not use this file except in compliance with the License. You may obtain'
  nl|'\n'
  comment|'#    a copy of the License at'
  nl|'\n'
  comment|'#'
  nl|'\n'
  comment|'#         http://www.apache.org/licenses/LICENSE-2.0'
  nl|'\n'
  comment|'#'
  nl|'\n'
  comment|'#    Unless required by applicable law or agreed to in writing, software'
  nl|'\n'
  comment|'#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT'
  nl|'\n'
  comment|'#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the'
  nl|'\n'
  comment|'#    License for the specific language governing permissions and limitations'
  nl|'\n'
  comment|'#    under the License.'
  nl|'\n'
  nl|'\n'
  name|'from'
  name|'nova'
  op|'.'
  name|'scheduler'
  name|'import'
  name|'filter_scheduler'
  newline|'\n'
  nl|'\n'
  nl|'\n'
  DECL|class|CachingScheduler
  name|'class'
  name|'CachingScheduler'
  op|'('
  name|'filter_scheduler'
  op|'.'
  name|'FilterScheduler'
  op|')'
  op|':'
  newline|'\n'
  indent|'    '
  string|'"""Scheduler to test aggressive caching of the host list.\n\n    Please note, this is a very opinionated scheduler. Be sure to\n    review the caveats listed here before selecting this scheduler.\n\n    The aim of this scheduler is to reduce server build times when\n    you have large bursts of server builds, by reducing the time it\n    takes, from the users point of view, to service each schedule\n    request.\n\n    There are two main parts to scheduling a users request:\n    * getting the current state of the system\n    * using filters and weights to pick the best host\n\n    This scheduler tries its best to cache in memory the current\n    state of the system, so we don\'t need to make the expensive\n    call to get the current state of the system while processing\n    a user\'s request, we can do that query in a periodic task\n    before the user even issues their request.\n\n    To reduce races, cached info of the chosen host is updated using\n    the existing host state call: consume_from_instance\n\n    Please note, the way this works, each scheduler worker has its own\n    copy of the cache. So if you run multiple schedulers, you will get\n    more retries, because the data stored on any additional scheduler will\n    be more out of date, than if it was fetched from the database.\n\n    In a similar way, if you have a high number of server deletes, the\n    extra capacity from those deletes will not show up until the cache is\n    refreshed.\n    """'
  newline|'\n'
  nl|'\n'
  DECL|member|__init__
  name|'def'
  name|'__init__'
  op|'('
  name|'self'
  op|','
  op|'*'
  name|'args'
  op|','
  op|'**'
  name|'kwargs'
  op|')'
  op|':'
  newline|'\n'
  indent|'        '
  name|'super'
  op|'('
  name|'CachingScheduler'
  op|','
  name|'self'
  op|')'
  op|'.'
  name|'__init__'
  op|'('
  op|'*'
  name|'args'
  op|','
  op|'**'
  name|'kwargs'
  op|')'
  newline|'\n'
  name|'self'
  op|'.'
  name|'all_host_states'
  op|'='
  name|'None'
  newline|'\n'
  nl|'\n'
  DECL|member|run_periodic_tasks
  dedent|''
  name|'def'
  name|'run_periodic_tasks'
  op|'('
  name|'self'
  op|','
  name|'context'
  op|')'
  op|':'
  newline|'\n'
  indent|'        '
  string|'"""Called from a periodic tasks in the manager."""'
  newline|'\n'
  name|'elevated'
  op|'='
  name|'context'
  op|'.'
  name|'elevated'
  op|'('
  op|')'
  newline|'\n'
  comment|'# NOTE(johngarbutt) Fetching the list of hosts before we get'
  nl|'\n'
  comment|'# a user request, so no user requests have to wait while we'
  nl|'\n'
  comment|'# fetch the list of hosts.'
  nl|'\n'
  name|'self'
  op|'.'
  name|'all_host_states'
  op|'='
  name|'self'
  op|'.'
  name|'_get_up_hosts'
  op|'('
  name|'elevated'
  op|')'
  newline|'\n'
  nl|'\n'
  DECL|member|_get_all_host_states
  dedent|''
  name|'def'
  name|'_get_all_host_states'
  op|'('
  name|'self'
  op|','
  name|'context'
  op|')'
  op|':'
  newline|'\n'
  indent|'        '
  string|'"""Called from the filter scheduler, in a template pattern."""'
  newline|'\n'
  name|'if'
  name|'self'
  op|'.'
  name|'all_host_states'
  name|'is'
  name|'None'
  op|':'
  newline|'\n'
  comment|'# NOTE(johngarbutt) We only get here when we a scheduler request'
  nl|'\n'
  comment|'# comes in before the first run of the periodic task.'
  nl|'\n'
  comment|'# Rather than raise an error, we fetch the list of hosts.'
  nl|'\n'
  indent|'            '
  name|'self'
  op|'.'
  name|'all_host_states'
  op|'='
  name|'self'
  op|'.'
  name|'_get_up_hosts'
  op|'('
  name|'context'
  op|')'
  newline|'\n'
  nl|'\n'
  dedent|''
  name|'return'
  name|'self'
  op|'.'
  name|'all_host_states'
  newline|'\n'
  nl|'\n'
  DECL|member|_get_up_hosts
  dedent|''
  name|'def'
  name|'_get_up_hosts'
  op|'('
  name|'self'
  op|','
  name|'context'
  op|')'
  op|':'
  newline|'\n'
  indent|'        '
  name|'all_hosts_iterator'
  op|'='
  name|'self'
  op|'.'
  name|'host_manager'
  op|'.'
  name|'get_all_host_states'
  op|'('
  name|'context'
  op|')'
  newline|'\n'
  name|'return'
  name|'list'
  op|'('
  name|'all_hosts_iterator'
  op|')'
  newline|'\n'
  dedent|''
  dedent|''
  endmarker|''
  end_unit
  