Skip to content

Commit 46abb11

Browse files
committed
Merge pull request #468 from basho/fixes/lrb/ttb-cover-context-gh-466
READY: Ensure msg code 0 is always handled.
2 parents d26efdb + 8eec219 commit 46abb11

File tree

6 files changed

+48
-25
lines changed

6 files changed

+48
-25
lines changed

riak/codecs/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import collections
22

3+
import riak.pb.messages
4+
35
from riak import RiakError
6+
from riak.codecs.util import parse_pbuf_msg
7+
from riak.util import bytes_to_str
48

59
Msg = collections.namedtuple('Msg',
610
['msg_code', 'data', 'resp_code'],
@@ -16,10 +20,10 @@ def maybe_incorrect_code(self, resp_code, expect=None):
1620
raise RiakError("unexpected message code: %d, expected %d"
1721
% (resp_code, expect))
1822

19-
def maybe_riak_error(self, err_code, msg_code, data=None):
20-
if msg_code == err_code:
23+
def maybe_riak_error(self, msg_code, data=None):
24+
if msg_code == riak.pb.messages.MSG_CODE_ERROR_RESP:
2125
if data is None:
2226
raise RiakError('no error provided!')
23-
return data
24-
else:
25-
return None
27+
else:
28+
err = parse_pbuf_msg(msg_code, data)
29+
raise RiakError(bytes_to_str(err.errmsg))

riak/codecs/pbuf.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from riak import RiakError
1111
from riak.codecs import Codec, Msg
12+
from riak.codecs.util import parse_pbuf_msg
1213
from riak.content import RiakContent
1314
from riak.pb.riak_ts_pb2 import TsColumnType
1415
from riak.riak_object import VClock
@@ -90,20 +91,7 @@ def __init__(self,
9091
self._bucket_types = bucket_types
9192

9293
def parse_msg(self, msg_code, data):
93-
pbclass = riak.pb.messages.MESSAGE_CLASSES.get(msg_code, None)
94-
if pbclass is None:
95-
return None
96-
pbo = pbclass()
97-
pbo.ParseFromString(data)
98-
return pbo
99-
100-
def maybe_riak_error(self, msg_code, data=None):
101-
err_code = riak.pb.messages.MSG_CODE_ERROR_RESP
102-
err_data = super(PbufCodec, self).maybe_riak_error(
103-
err_code, msg_code, data)
104-
if err_data:
105-
err = self.parse_msg(msg_code, err_data)
106-
raise RiakError(bytes_to_str(err.errmsg))
94+
return parse_pbuf_msg(msg_code, data)
10795

10896
def encode_auth(self, username, password):
10997
req = riak.pb.riak_pb2.RpbAuthReq()

riak/codecs/ttb.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ def maybe_err_ttb(self, err_ttb):
5050
# errcode = err_ttb[2]
5151
raise RiakError(bytes_to_str(errmsg))
5252

53-
def maybe_riak_error(self, msg_code, data=None):
54-
pass
55-
5653
def encode_to_ts_cell(self, cell):
5754
if cell is None:
5855
return []
@@ -133,7 +130,7 @@ def encode_timeseries_query(self, table, query, interpolations=None):
133130
if '{table}' in q:
134131
q = q.format(table=table.name)
135132
tsi = tsinterpolation_a, q, []
136-
req = tsqueryreq_a, tsi, False, []
133+
req = tsqueryreq_a, tsi, False, udef_a
137134
mc = MSG_CODE_TS_TTB_MSG
138135
rc = MSG_CODE_TS_TTB_MSG
139136
return Msg(mc, encode(req), rc)

riak/codecs/util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import riak.pb.messages
2+
3+
4+
def parse_pbuf_msg(msg_code, data):
5+
pbclass = riak.pb.messages.MESSAGE_CLASSES.get(msg_code, None)
6+
if pbclass is None:
7+
return None
8+
pbo = pbclass()
9+
pbo.ParseFromString(data)
10+
return pbo

riak/tests/test_timeseries_ttb.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from riak.table import Table
1212
from riak.ts_object import TsObject
1313
from riak.codecs.ttb import TtbCodec
14-
from riak.util import str_to_bytes, \
14+
from riak.util import str_to_bytes, bytes_to_str, \
1515
unix_time_millis, is_timeseries_supported
1616
from riak.tests import RUN_TIMESERIES
1717
from riak.tests.base import IntegrationTestBase
@@ -141,7 +141,7 @@ def test_query_that_returns_table_description(self):
141141
row = ts_obj.rows[0]
142142
self.assertEqual(len(row), 5)
143143

144-
def test_store_and_fetch(self):
144+
def test_store_and_fetch_and_query(self):
145145
now = datetime.datetime.utcfromtimestamp(144379690.987000)
146146
fiveMinsAgo = now - fiveMins
147147
tenMinsAgo = fiveMinsAgo - fiveMins
@@ -187,6 +187,29 @@ def test_store_and_fetch(self):
187187
self.assertEqual(len(row), 5)
188188
self.assertEqual(row, exp)
189189

190+
fmt = """
191+
select * from {table} where
192+
time > {t1} and time < {t2} and
193+
geohash = 'hash1' and
194+
user = 'user2'
195+
"""
196+
query = fmt.format(
197+
table=table_name,
198+
t1=unix_time_millis(tenMinsAgo),
199+
t2=unix_time_millis(now))
200+
ts_obj = self.client.ts_query(table_name, query)
201+
if ts_obj.columns is not None:
202+
self.assertEqual(len(ts_obj.columns.names), 5)
203+
self.assertEqual(len(ts_obj.columns.types), 5)
204+
self.assertEqual(len(ts_obj.rows), 1)
205+
row = ts_obj.rows[0]
206+
self.assertEqual(bytes_to_str(row[0]), 'hash1')
207+
self.assertEqual(bytes_to_str(row[1]), 'user2')
208+
self.assertEqual(row[2], fiveMinsAgo)
209+
self.assertEqual(row[2].microsecond, 987000)
210+
self.assertEqual(bytes_to_str(row[3]), 'wind')
211+
self.assertIsNone(row[4])
212+
190213
def test_create_error_via_put(self):
191214
table = Table(self.client, table_name)
192215
ts_obj = table.new([])

riak/transports/tcp/transport.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ def _request(self, msg, codec=None):
535535
raise ValueError('expected a Codec argument')
536536

537537
resp_code, data = self._send_recv(msg_code, data)
538+
# NB: decodes errors with msg code 0
538539
codec.maybe_riak_error(resp_code, data)
539540
codec.maybe_incorrect_code(resp_code, expect)
540541
if resp_code == MSG_CODE_TS_TTB_MSG or \

0 commit comments

Comments
 (0)