Skip to content

Release 2.0.3 #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
license='Apache License 2.0',
install_requires=install_requires,
tests_require=tests_require,
extras_require={'test': tests_require},
setup_requires=['nose'],
extra_requires={
extras_require={
'test': tests_require,
'redis': ['redis>=2.6', 'jsonpickle>=0.9.3']
},
setup_requires=['nose'],
classifiers=[
'Development Status :: 3 - Alpha',
'Environment :: Console',
Expand All @@ -38,4 +38,4 @@
'Programming Language :: Python :: 3',
'Topic :: Software Development :: Libraries'
],
packages=['splitio','splitio.update_scripts'])
packages=['splitio','splitio.update_scripts','splitio.bin'])
2 changes: 2 additions & 0 deletions splitio/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from splitio.redis_support import RedisSplitCache
from splitio.splits import (CacheBasedSplitFetcher, SplitView)
from splitio.utils import bytes_to_string

class SplitManager(object):
def __init__(self):
Expand Down Expand Up @@ -55,6 +56,7 @@ def split_names(self):
splits = self._split_cache.get_splits_keys()
split_names = []
for split_name in splits:
split_name = bytes_to_string(split_name)
split_names.append(split_name.replace(RedisSplitCache._KEY_TEMPLATE.format(suffix=''), ''))

return split_names
Expand Down
23 changes: 18 additions & 5 deletions splitio/redis_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def missing_redis_dependencies(*args, **kwargs):
from splitio.segments import Segment
from splitio.splits import Split, SplitParser
from splitio.impressions import Impression
from splitio.utils import bytes_to_string

# Template for Split.io related Cache keys
_SPLITIO_CACHE_KEY_TEMPLATE = 'SPLITIO.{suffix}'
Expand Down Expand Up @@ -101,6 +102,7 @@ def _get_segment_key_set_key(self, segment_name):
:return: The cache key for the segment key set
:rtype: str
"""
segment_name = bytes_to_string(segment_name)
return RedisSegmentCache._SEGMENT_KEY_SET_KEY_TEMPLATE.format(
segment_name=segment_name)

Expand All @@ -111,6 +113,7 @@ def _get_segment_change_number_key(self, segment_name):
:return: The cache key for the segment change number
:rtype: str
"""
segment_name = bytes_to_string(segment_name)
return RedisSegmentCache._SEGMENT_CHANGE_NUMBER_KEY_TEMPLATE.format(
segment_name=segment_name)

Expand All @@ -133,6 +136,7 @@ def get_change_number(self, segment_name):

class RedisSplitCache(SplitCache):
_KEY_TEMPLATE = _SPLITIO_CACHE_KEY_TEMPLATE.format(suffix='split.{suffix}')
_KEY_TILL_TEMPLATE = _SPLITIO_CACHE_KEY_TEMPLATE.format(suffix='splits.{suffix}')
_DISABLED_KEY = _KEY_TEMPLATE.format(suffix='__disabled__')

def __init__(self, redis, disabled_period=300):
Expand Down Expand Up @@ -182,12 +186,12 @@ def is_enabled(self):
return not self._redis.exists(RedisSplitCache._DISABLED_KEY)

def get_change_number(self):
change_number = self._redis.get(RedisSplitCache._KEY_TEMPLATE.format(
change_number = self._redis.get(RedisSplitCache._KEY_TILL_TEMPLATE.format(
suffix='till'))
return int(change_number) if change_number is not None else -1

def set_change_number(self, change_number):
self._redis.set(RedisSplitCache._KEY_TEMPLATE.format(suffix='till'),
self._redis.set(RedisSplitCache._KEY_TILL_TEMPLATE.format(suffix='till'),
change_number, None)

def add_split(self, split_name, split):
Expand All @@ -200,6 +204,8 @@ def get_split(self, split_name):
if to_decode is None:
return None

to_decode = bytes_to_string(to_decode)

split_dump = decode(to_decode)

if split_dump is not None:
Expand All @@ -213,9 +219,6 @@ def get_split(self, split_name):
def get_splits(self):
keys = self.get_splits_keys()

if 'SPLITIO.split.till' in keys:
keys.remove('SPLITIO.split.till')

splits = self._redis.mget(keys)

to_return = []
Expand All @@ -224,6 +227,7 @@ def get_splits(self):
split_parser = RedisSplitParser(segment_cache)

for split in splits:
split = bytes_to_string(split)
split_dump = decode(split)
if split_dump is not None:
to_return.append(split_parser.parse(split_dump))
Expand Down Expand Up @@ -297,12 +301,14 @@ def fetch_all(self):
impressions_keys = self._redis.keys(self._IMPRESSIONS_KEY.format(feature_name='*'))

for impression_key in impressions_keys:
impression_key = bytes_to_string(impression_key)
if impression_key.replace(self._IMPRESSIONS_KEY.format(feature_name=''), '') == 'impressions':
continue

feature_name = impression_key.replace(self._IMPRESSIONS_KEY.format(feature_name=''), '')

for impression in self._redis.smembers(impression_key):
impression = bytes_to_string(impression)
impression_decoded = decode(impression)
impression_tuple = Impression(key=impression_decoded['keyName'],
feature_name=feature_name,
Expand Down Expand Up @@ -348,6 +354,9 @@ def fetch_all_and_clear(self):
impressions_keys = self._redis.keys(self._IMPRESSIONS_KEY.format(feature_name='*'))

for impression_key in impressions_keys:

impression_key = bytes_to_string(impression_key)

if impression_key.replace(self._IMPRESSIONS_KEY.format(feature_name=''), '') == 'impressions':
continue

Expand All @@ -356,6 +365,9 @@ def fetch_all_and_clear(self):
to_remove = list()
for impression in self._redis.smembers(impression_key):
to_remove.append(impression)

impression = bytes_to_string(impression)

impression_decoded = decode(impression)

label = ''
Expand Down Expand Up @@ -606,6 +618,7 @@ def fetch_all_times_and_clear(self):
time = defaultdict(lambda: [0] * len(BUCKETS))

for key in time_keys:
key = bytes_to_string(key)
time_match = RedisMetricsCache._LATENCY_FIELD_RE.match(key)
if time_match is not None:
time[time_match.group('operation')][int(time_match.group('bucket_index'))] = int(self._redis.getset(key, 0))
Expand Down
3 changes: 3 additions & 0 deletions splitio/segments.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,9 @@ def fetch(self, name, since):
:return: A dictionary with the changes
:rtype: dict
"""
if type(name).__name__ == 'bytes':
name = str(name,'utf-8')

try:
segment_change = self.fetch_from_backend(name, since)
except:
Expand Down
4 changes: 2 additions & 2 deletions splitio/tests/test_redis_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_set_change_number_sets_change_number_key(self):
"""Test that set_change_number sets the change number key"""
self.a_split_cache.set_change_number(self.some_change_number)
self.some_redis.set.assert_called_once_with(
'SPLITIO.split.till', self.some_change_number, None)
'SPLITIO.splits.till', self.some_change_number, None)

def test_get_change_number_gets_segment_change_number_key(self):
"""Test that get_change_number gets the change number key"""
Expand All @@ -165,7 +165,7 @@ def test_get_change_number_gets_segment_change_number_key(self):
self.assertEqual(int(self.some_redis.get.return_value), result)
self.assertIsInstance(result, int)
self.some_redis.get.assert_called_once_with(
'SPLITIO.split.till')
'SPLITIO.splits.till')

def test_get_change_number_returns_default_value_if_not_set(self):
"""Test that get_change_number returns -1 if the value is not set"""
Expand Down
7 changes: 7 additions & 0 deletions splitio/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals

def bytes_to_string(bytes, encode='utf-8'):
if type(bytes).__name__ == 'bytes':
return str(bytes, encode)

return bytes
2 changes: 1 addition & 1 deletion splitio/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.0.0'
__version__ = '2.0.3'