Skip to content

Commit f322018

Browse files
committed
add memcache and mongodb
1 parent aaeedc3 commit f322018

File tree

12 files changed

+1643
-0
lines changed

12 files changed

+1643
-0
lines changed

bigbang/cache/__init__.py

Whitespace-only changes.

bigbang/cache/cache_utils.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Copyright 2010 United States Government as represented by the
2+
# Administrator of the National Aeronautics and Space Administration.
3+
# All Rights Reserved.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
6+
# not use this file except in compliance with the License. You may obtain
7+
# a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14+
# License for the specific language governing permissions and limitations
15+
# under the License.
16+
17+
"""Simple wrapper for oslo_cache."""
18+
19+
from oslo_cache import core as cache
20+
from oslo_log import log as logging
21+
22+
from oslo_config import cfg
23+
from bigbang.common.i18n import _, _LW
24+
25+
CONF = cfg.CONF
26+
27+
LOG = logging.getLogger(__name__)
28+
29+
WEEK = 604800
30+
31+
32+
def _warn_if_null_backend():
33+
if CONF.cache.backend == 'dogpile.cache.null':
34+
LOG.warning(_LW("Cache enabled with backend dogpile.cache.null."))
35+
36+
37+
def get_memcached_client(expiration_time=0):
38+
"""Used ONLY when memcached is explicitly needed."""
39+
# If the operator has [cache]/enabled flag on then we let oslo_cache
40+
# configure the region from the configuration settings
41+
if CONF.cache.enabled and CONF.cache.memcache_servers:
42+
_warn_if_null_backend()
43+
return CacheClient(
44+
_get_default_cache_region(expiration_time=expiration_time))
45+
46+
47+
def get_client(expiration_time=0):
48+
"""Used to get a caching client."""
49+
# If the operator has [cache]/enabled flag on then we let oslo_cache
50+
# configure the region from configuration settings.
51+
if CONF.cache.enabled:
52+
_warn_if_null_backend()
53+
return CacheClient(
54+
_get_default_cache_region(expiration_time=expiration_time))
55+
# If [cache]/enabled flag is off, we use the dictionary backend
56+
return CacheClient(
57+
_get_custom_cache_region(expiration_time=expiration_time,
58+
backend='oslo_cache.dict'))
59+
60+
61+
def _get_default_cache_region(expiration_time):
62+
region = cache.create_region()
63+
if expiration_time != 0:
64+
CONF.cache.expiration_time = expiration_time
65+
cache.configure_cache_region(CONF, region)
66+
return region
67+
68+
69+
def _get_custom_cache_region(expiration_time=WEEK,
70+
backend=None,
71+
url=None):
72+
"""Create instance of oslo_cache client.
73+
74+
For backends you can pass specific parameters by kwargs.
75+
For 'dogpile.cache.memcached' backend 'url' parameter must be specified.
76+
77+
:param backend: backend name
78+
:param expiration_time: interval in seconds to indicate maximum
79+
time-to-live value for each key
80+
:param url: memcached url(s)
81+
"""
82+
83+
region = cache.create_region()
84+
region_params = {}
85+
if expiration_time != 0:
86+
region_params['expiration_time'] = expiration_time
87+
88+
if backend == 'oslo_cache.dict':
89+
region_params['arguments'] = {'expiration_time': expiration_time}
90+
elif backend == 'dogpile.cache.memcached':
91+
region_params['arguments'] = {'url': url}
92+
else:
93+
raise RuntimeError(_('old style configuration can use '
94+
'only dictionary or memcached backends'))
95+
96+
region.configure(backend, **region_params)
97+
return region
98+
99+
100+
class CacheClient(object):
101+
"""Replicates a tiny subset of memcached client interface."""
102+
103+
def __init__(self, region):
104+
self.region = region
105+
106+
def get(self, key):
107+
value = self.region.get(key)
108+
if value == cache.NO_VALUE:
109+
return None
110+
return value
111+
112+
def get_or_create(self, key, creator):
113+
return self.region.get_or_create(key, creator)
114+
115+
def set(self, key, value):
116+
return self.region.set(key, value)
117+
118+
def add(self, key, value):
119+
return self.region.get_or_create(key, lambda: value)
120+
121+
def delete(self, key):
122+
return self.region.delete(key)
123+
124+
def get_multi(self, keys):
125+
values = self.region.get_multi(keys)
126+
return [None if value is cache.NO_VALUE else value for value in
127+
values]
128+
129+
def delete_multi(self, keys):
130+
return self.region.delete_multi(keys)

bigbang/controller/api.pyc

0 Bytes
Binary file not shown.

bigbang/mongodb/__init__.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import six
14+
15+
from ceilometer import utils
16+
17+
18+
class EventFilter(object):
19+
"""Properties for building an Event query.
20+
21+
:param start_timestamp: UTC start datetime (mandatory)
22+
:param end_timestamp: UTC end datetime (mandatory)
23+
:param event_type: the name of the event. None for all.
24+
:param message_id: the message_id of the event. None for all.
25+
:param admin_proj: the project_id of admin role. None if non-admin user.
26+
:param traits_filter: the trait filter dicts, all of which are optional.
27+
This parameter is a list of dictionaries that specify trait values:
28+
29+
.. code-block:: python
30+
31+
{'key': <key>,
32+
'string': <value>,
33+
'integer': <value>,
34+
'datetime': <value>,
35+
'float': <value>,
36+
'op': <eq, lt, le, ne, gt or ge> }
37+
"""
38+
39+
def __init__(self, start_timestamp=None, end_timestamp=None,
40+
event_type=None, message_id=None, traits_filter=None,
41+
admin_proj=None):
42+
self.start_timestamp = utils.sanitize_timestamp(start_timestamp)
43+
self.end_timestamp = utils.sanitize_timestamp(end_timestamp)
44+
self.message_id = message_id
45+
self.event_type = event_type
46+
self.traits_filter = traits_filter or []
47+
self.admin_proj = admin_proj
48+
49+
def __repr__(self):
50+
return ("<EventFilter(start_timestamp: %s,"
51+
" end_timestamp: %s,"
52+
" event_type: %s,"
53+
" traits: %s)>" %
54+
(self.start_timestamp,
55+
self.end_timestamp,
56+
self.event_type,
57+
six.text_type(self.traits_filter)))

bigbang/mongodb/base.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
#
2+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
3+
# not use this file except in compliance with the License. You may obtain
4+
# a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11+
# License for the specific language governing permissions and limitations
12+
# under the License.
13+
import ceilometer
14+
15+
16+
class Connection(object):
17+
"""Base class for event storage system connections."""
18+
19+
# A dictionary representing the capabilities of this driver.
20+
CAPABILITIES = {
21+
'events': {'query': {'simple': False}},
22+
}
23+
24+
STORAGE_CAPABILITIES = {
25+
'storage': {'production_ready': False},
26+
}
27+
28+
def __init__(self, conf, url):
29+
self.conf = conf
30+
31+
@staticmethod
32+
def upgrade():
33+
"""Migrate the database to `version` or the most recent version."""
34+
35+
@staticmethod
36+
def clear():
37+
"""Clear database."""
38+
39+
@staticmethod
40+
def record_events(events):
41+
"""Write the events to the backend storage system.
42+
43+
:param events: a list of model.Event objects.
44+
"""
45+
raise ceilometer.NotImplementedError('Events not implemented.')
46+
47+
@staticmethod
48+
def get_events(event_filter, limit=None):
49+
"""Return an iterable of model.Event objects."""
50+
raise ceilometer.NotImplementedError('Events not implemented.')
51+
52+
@staticmethod
53+
def get_event_types():
54+
"""Return all event types as an iterable of strings."""
55+
raise ceilometer.NotImplementedError('Events not implemented.')
56+
57+
@staticmethod
58+
def get_trait_types(event_type):
59+
"""Return a dictionary containing the name and data type of the trait.
60+
61+
Only trait types for the provided event_type are
62+
returned.
63+
:param event_type: the type of the Event
64+
"""
65+
raise ceilometer.NotImplementedError('Events not implemented.')
66+
67+
@staticmethod
68+
def get_traits(event_type, trait_type=None):
69+
"""Return all trait instances associated with an event_type.
70+
71+
If trait_type is specified, only return instances of that trait type.
72+
:param event_type: the type of the Event to filter by
73+
:param trait_type: the name of the Trait to filter by
74+
"""
75+
76+
raise ceilometer.NotImplementedError('Events not implemented.')
77+
78+
@classmethod
79+
def get_capabilities(cls):
80+
"""Return an dictionary with the capabilities of each driver."""
81+
return cls.CAPABILITIES
82+
83+
@classmethod
84+
def get_storage_capabilities(cls):
85+
"""Return a dictionary representing the performance capabilities.
86+
87+
This is needed to evaluate the performance of each driver.
88+
"""
89+
return cls.STORAGE_CAPABILITIES
90+
91+
@staticmethod
92+
def clear_expired_event_data(ttl):
93+
"""Clear expired data from the backend storage system.
94+
95+
Clearing occurs according to the time-to-live.
96+
97+
:param ttl: Number of seconds to keep records for.
98+
"""
99+
raise ceilometer.NotImplementedError('Clearing events not implemented')

0 commit comments

Comments
 (0)