zaqar/marconi/proxy/utils/partition.py
Alejandro Cabrera ca26627ec4 proxy: adds oslo.cache, config support, & stevedore
This patch brings together oslo.cache, oslo.config, and stevedore to
provide pluggable, hierarchical catalogue caching for marconi proxy.

Here's the list of changes:
- add configuration/driver loading at the app level
- remove select from proxy storage driver - unnecessary intelligence
  at storage layer
- node.weighted_select -> partition.weighted_select (clearer name)
- forwarding logic further refactored, placed in own module
- caching logic placed in lookup module
- selector passed down at app level to handle round-robin state
  globally
    * open to becoming configurable
- adds several TODOs for a better proxy

Change-Id: I3bc568315e685486d63cdce3ec278c89e3f2b2bc
Implements: blueprint placement-service
2013-09-25 13:16:54 +00:00

46 lines
1.5 KiB
Python

# Copyright (c) 2013 Rackspace Hosting, Inc.
#
# 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.
"""partition: utilities for implementing partition selections."""
import random
def weighted_select(partitions):
"""Select a partition from all the partitions passed in.
:param partitions: gen({'name': ..., 'weight': ..., 'hosts': ...}, ...)
:return: (name, hosts)
"""
acc = 0
lookup = []
# TODO(cpp-cabrera): the lookup table can be constructed once each time
# an entry is added to/removed from the catalogue,
# rather than each time a queue is created.
# construct the (partition, weight) lookup table
for p in partitions:
acc += p['weight']
lookup.append((p, acc))
if not lookup:
return None
# select a partition from the lookup table
selector = random.randint(0, acc - 1)
last = 0
for p, w in lookup:
if last <= selector < w:
return p
last = w