/
crashstorage.py
181 lines (159 loc) · 6.92 KB
/
crashstorage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import datetime
from socorro.external.crashstorage_base import (
CrashStorageBase, CrashIDNotFound)
from socorro.external.hbase import hbase_client
from socorro.database.transaction_executor import TransactionExecutor
from socorro.lib.util import DotDict
from configman import Namespace, class_converter
#==============================================================================
class HBaseCrashStorage(CrashStorageBase):
required_config = Namespace()
required_config.add_option(
'number_of_retries',
doc='Max. number of retries when fetching from hbaseClient',
default=0
)
required_config.add_option(
'hbase_host',
doc='Host to HBase server',
default='localhost',
)
required_config.add_option(
'hbase_port',
doc='Port to HBase server',
default=9090,
)
required_config.add_option(
'hbase_timeout',
doc='timeout in milliseconds for an HBase connection',
default=5000,
)
required_config.add_option(
'transaction_executor_class',
default=TransactionExecutor,
doc='a class that will execute transactions',
from_string_converter=class_converter
)
required_config.add_option(
'forbidden_keys',
default='email, url, user_id',
doc='a comma delimited list of keys banned from the processed crash '
'in HBase',
from_string_converter=lambda s: [x.strip() for x in s.split(',')]
)
#--------------------------------------------------------------------------
def __init__(self, config, quit_check_callback=None):
super(HBaseCrashStorage, self).__init__(config, quit_check_callback)
self.logger.info('connecting to hbase')
self.hbaseConnection = hbase_client.HBaseConnectionForCrashReports(
config.hbase_host,
config.hbase_port,
config.hbase_timeout,
logger=self.logger
)
self.transaction_executor = config.transaction_executor_class(
config,
self.hbaseConnection,
self.quit_check
)
self.exceptions_eligible_for_retry += \
self.hbaseConnection.hbaseThriftExceptions
self.exceptions_eligible_for_retry += \
(hbase_client.NoConnectionException,)
#--------------------------------------------------------------------------
def close(self):
self.hbaseConnection.close()
#--------------------------------------------------------------------------
def save_raw_crash(self, raw_crash, dump, crash_id):
# the transaction_executor will run the function given as the first
# parameter. To that function, the transaction_executor will pass
# self.hbaseConnection, crash_id, raw_crash, dump, and
# number_of_retries.
# notice that the function is an unbound method. Since
# self.hbaseConnection is passed in as the first parameter, that
# fills in the proper value for the function's 'self' parameter.
# warning: this breaks inheritance if a subclass of
# HBaseConnectionForCrashReports were desired instead.
self.transaction_executor(
hbase_client.HBaseConnectionForCrashReports.put_json_dump,
crash_id,
raw_crash,
dump,
number_of_retries=self.config.number_of_retries
)
self.logger.info('saved - %s', crash_id)
#--------------------------------------------------------------------------
def save_processed(self, processed_crash):
sanitized_processed_crash = self.sanitize_processed_crash(
processed_crash,
self.config.forbidden_keys
)
self._stringify_dates_in_dict(sanitized_processed_crash)
self.transaction_executor(
hbase_client.HBaseConnectionForCrashReports.put_processed_json,
sanitized_processed_crash['uuid'],
sanitized_processed_crash,
number_of_retries=self.config.number_of_retries
)
#--------------------------------------------------------------------------
def get_raw_crash(self, crash_id):
return DotDict(self.transaction_executor(
hbase_client.HBaseConnectionForCrashReports.get_json,
crash_id,
number_of_retries=self.config.number_of_retries
))
#--------------------------------------------------------------------------
def get_raw_dump(self, crash_id):
return self.transaction_executor(
hbase_client.HBaseConnectionForCrashReports.get_dump,
crash_id,
number_of_retries=self.config.number_of_retries
)
#--------------------------------------------------------------------------
def get_processed_crash(self, crash_id):
try:
return DotDict(self.transaction_executor(
hbase_client.HBaseConnectionForCrashReports.get_processed_json,
crash_id,
number_of_retries=self.config.number_of_retries
))
except hbase_client.OoidNotFoundException:
# we want a consistent set of exceptions for the API
raise CrashIDNotFound(crash_id)
#--------------------------------------------------------------------------
def new_crashes(self):
# TODO: how do we put this is in a transactactional retry wrapper?
return self.hbaseConnection.iterator_for_all_legacy_to_be_processed()
#--------------------------------------------------------------------------
@staticmethod
def sanitize_processed_crash(processed_crash, forbidden_keys):
"""returns a copy of a processed_crash with the forbidden keys removed.
parameters:
processed_crash - the processed crash in the form of a mapping
forbidden_keys - a list of strings to be removed from the
processed crash
returns:
a mapping that is a shallow copy of the original processed_crash
minus the forbidden keys and values"""
a_copy = processed_crash.copy()
for a_forbidden_key in forbidden_keys:
if a_forbidden_key in a_copy:
del a_copy[a_forbidden_key]
return a_copy
#--------------------------------------------------------------------------
@staticmethod
def _stringify_dates_in_dict(a_dict):
for name, value in a_dict.iteritems():
if isinstance(value, datetime.datetime):
a_dict[name] = ("%4d-%02d-%02d %02d:%02d:%02d.%d" %
(value.year,
value.month,
value.day,
value.hour,
value.minute,
value.second,
value.microsecond)
)