-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathbulk.py
More file actions
362 lines (278 loc) · 15.1 KB
/
bulk.py
File metadata and controls
362 lines (278 loc) · 15.1 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# pylint: disable=protected-access
import csv
import json
import sys
import time
import tempfile
import singer
from singer import metrics
from requests.exceptions import RequestException
import xmltodict
from tap_salesforce.salesforce.exceptions import (
TapSalesforceException, TapSalesforceQuotaExceededException)
BATCH_STATUS_POLLING_SLEEP = 20
PK_CHUNKED_BATCH_STATUS_POLLING_SLEEP = 60
ITER_CHUNK_SIZE = 1024
DEFAULT_CHUNK_SIZE = 50000
LOGGER = singer.get_logger()
# pylint: disable=inconsistent-return-statements
def find_parent(stream):
parent_stream = stream
if stream.endswith("CleanInfo"):
parent_stream = stream[:stream.find("CleanInfo")]
elif stream.endswith("FieldHistory"):
parent_stream = stream[:stream.find("FieldHistory")]
elif stream.endswith("History"):
parent_stream = stream[:stream.find("History")]
# If the stripped stream ends with "__" we can assume the parent is a custom table
if parent_stream.endswith("__"):
parent_stream += 'c'
return parent_stream
class Bulk():
bulk_url = "{}/services/async/53.0/{}"
def __init__(self, sf):
# Set csv max reading size to the platform's max size available.
csv.field_size_limit(sys.maxsize)
self.sf = sf
def query(self, catalog_entry, state):
self.check_bulk_quota_usage()
pk_chunking = self.sf.pk_chunking.get('enabled')
chunked_tables = self.sf.pk_chunking.get('tables', [])
if pk_chunking and catalog_entry['stream'] in chunked_tables:
for record in self._bulk_query_with_pk_chunking(catalog_entry, state):
yield record
else:
for record in self._bulk_query(catalog_entry, state):
yield record
self.sf.jobs_completed += 1
# pylint: disable=line-too-long
def check_bulk_quota_usage(self):
endpoint = "limits"
url = self.sf.data_url.format(self.sf.instance_url, endpoint)
with metrics.http_request_timer(endpoint):
resp = self.sf._make_request('GET', url, headers=self.sf.auth.rest_headers).json()
quota_max = resp['DailyBulkApiBatches']['Max']
max_requests_for_run = int((self.sf.quota_percent_per_run * quota_max) / 100)
quota_remaining = resp['DailyBulkApiBatches']['Remaining']
percent_used = (1 - (quota_remaining / quota_max)) * 100
if percent_used > self.sf.quota_percent_total:
total_message = ("Salesforce has reported {}/{} ({:3.2f}%) total Bulk API quota " +
"used across all Salesforce Applications. Terminating " +
"replication to not continue past configured percentage " +
"of {}% total quota.").format(quota_max - quota_remaining,
quota_max,
percent_used,
self.sf.quota_percent_total)
raise TapSalesforceQuotaExceededException(total_message)
elif self.sf.jobs_completed > max_requests_for_run:
partial_message = ("This replication job has completed {} Bulk API jobs ({:3.2f}% of " +
"total quota). Terminating replication due to allotted " +
"quota of {}% per replication.").format(self.sf.jobs_completed,
(self.sf.jobs_completed / quota_max) * 100,
self.sf.quota_percent_per_run)
raise TapSalesforceQuotaExceededException(partial_message)
def _get_bulk_headers(self):
return {**self.sf.auth.bulk_headers, "Content-Type": "application/json"}
def _bulk_query(self, catalog_entry, state):
job_id = self._create_job(catalog_entry)
start_date = self.sf.get_start_date(state, catalog_entry)
batch_id = self._add_batch(catalog_entry, job_id, start_date)
self._close_job(job_id)
batch_status = self._poll_on_batch_status(job_id, batch_id)
if batch_status['state'] == 'Failed':
message = batch_status['stateMessage']
if "QUERY_TIMEOUT" in message:
LOGGER.info("Retrying Bulk Query for %s with PK Chunking: %s", catalog_entry['stream'], message)
# Set pk_chunking to True to indicate that we should write a bookmark differently
self.sf.pk_chunking['enabled'] = True
self._bulk_query_with_pk_chunking(catalog_entry, state)
else:
raise TapSalesforceException(message)
else:
for result in self.get_batch_results(job_id, batch_id, catalog_entry):
yield result
def _bulk_query_with_pk_chunking(self, catalog_entry, state):
start_date = self.sf.get_start_date(state, catalog_entry)
batch_status = self._perform_pk_chunking_query(catalog_entry, start_date)
job_id = batch_status['job_id']
# Add the bulk Job ID and its batches to the state so it can be resumed if necessary
tap_stream_id = catalog_entry['tap_stream_id']
state = singer.write_bookmark(state, tap_stream_id, 'JobID', job_id)
state = singer.write_bookmark(state, tap_stream_id, 'BatchIDs', batch_status['completed'][:])
for completed_batch_id in batch_status['completed']:
for result in self.get_batch_results(job_id, completed_batch_id, catalog_entry):
yield result
# Remove the completed batch ID and write state
self._complete_batch(state, tap_stream_id, completed_batch_id)
singer.write_state(state)
def _perform_pk_chunking_query(self, catalog_entry, start_date):
LOGGER.info("Making Bulk Query for %s with PK Chunking", catalog_entry['stream'])
# Create a new job
job_id = self._create_job(catalog_entry, True)
self._add_batch(catalog_entry, job_id, start_date, False)
batch_status = self._poll_on_pk_chunked_batch_status(job_id)
batch_status['job_id'] = job_id
if batch_status['failed']:
for batch in [b for b in batch_status['all'] if b['state'] == "Failed"]:
LOGGER.info(batch['stateMessage'])
raise TapSalesforceException("One or more batches failed during PK chunked job")
# Close the job after all the batches are complete
self._close_job(job_id)
return batch_status
def _create_job(self, catalog_entry, pk_chunking=False):
url = self.bulk_url.format(self.sf.instance_url, "job")
body = {"operation": "queryAll", "object": catalog_entry['stream'], "contentType": "CSV"}
headers = self._get_bulk_headers()
headers['Sforce-Disable-Batch-Retry'] = "true"
if pk_chunking:
chunk_size = self.sf.pk_chunking.get('chunk_size', DEFAULT_CHUNK_SIZE)
LOGGER.info("Adding PK chunking header w/chunk size: %d", chunk_size)
headers['Sforce-Enable-PKChunking'] = "true; chunkSize={}".format(chunk_size)
# If the stream ends with 'CleanInfo' or 'History', we can PK Chunk on the object's parent
if any(catalog_entry['stream'].endswith(suffix) for suffix in ["CleanInfo", "History"]):
parent = find_parent(catalog_entry['stream'])
headers['Sforce-Enable-PKChunking'] = headers['Sforce-Enable-PKChunking'] + "; parent={}".format(parent)
with metrics.http_request_timer("create_job") as timer:
timer.tags['sobject'] = catalog_entry['stream']
resp = self.sf._make_request(
'POST',
url,
headers=headers,
body=json.dumps(body))
job = resp.json()
return job['id']
def _add_batch(self, catalog_entry, job_id, start_date, order_by_clause=True):
endpoint = "job/{}/batch".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
body = self.sf._build_query_string(catalog_entry, start_date, order_by_clause=order_by_clause)
headers = self._get_bulk_headers()
headers['Content-Type'] = 'text/csv'
with metrics.http_request_timer("add_batch") as timer:
timer.tags['sobject'] = catalog_entry['stream']
resp = self.sf._make_request('POST', url, headers=headers, body=body)
batch = xmltodict.parse(resp.text)
return batch['batchInfo']['id']
def _complete_batch(self, state, tap_stream_id, batch_id):
stream = state['bookmarks'][tap_stream_id]
batch_ids = stream['BatchIDs']
batch_ids.remove(batch_id)
LOGGER.info("%s: finished syncing batch %s. Removing batch from state.", tap_stream_id, batch_id)
LOGGER.info("%s: batches to go %d", tap_stream_id, len(batch_ids))
if len(batch_ids) <= 0:
stream.pop('BatchIDs', None)
stream.pop('JobID', None)
stream.pop('JobHighestBookmarkSeen', None)
def _poll_on_pk_chunked_batch_status(self, job_id):
batches = self._get_batches(job_id)
while True:
queued_batches = [b['id'] for b in batches if b['state'] == "Queued"]
in_progress_batches = [b['id'] for b in batches if b['state'] == "InProgress"]
if not queued_batches and not in_progress_batches:
completed_batches = [b['id'] for b in batches if b['state'] == "Completed"]
failed_batches = [b['id'] for b in batches if b['state'] == "Failed"]
return {'completed': completed_batches, 'failed': failed_batches, 'all': batches}
else:
sleep_time = self.sf.pk_chunking.get('polling_sleep_time', PK_CHUNKED_BATCH_STATUS_POLLING_SLEEP)
time.sleep(sleep_time)
batches = self._get_batches(job_id)
def _poll_on_batch_status(self, job_id, batch_id):
batch_status = self._get_batch(job_id=job_id,
batch_id=batch_id)
while batch_status['state'] not in ['Completed', 'Failed', 'Not Processed']:
time.sleep(BATCH_STATUS_POLLING_SLEEP)
batch_status = self._get_batch(job_id=job_id,
batch_id=batch_id)
return batch_status
def job_exists(self, job_id):
try:
endpoint = "job/{}".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers = self._get_bulk_headers()
with metrics.http_request_timer("get_job"):
self.sf._make_request('GET', url, headers=headers)
return True # requests will raise for a 400 InvalidJob
except RequestException as ex:
if ex.response.headers["Content-Type"] == 'application/json':
exception_code = ex.response.json()['exceptionCode']
if exception_code == 'InvalidJob':
return False
raise
def _get_batches(self, job_id):
endpoint = "job/{}/batch".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers = self._get_bulk_headers()
with metrics.http_request_timer("get_batches"):
resp = self.sf._make_request('GET', url, headers=headers)
batches = xmltodict.parse(resp.text,
xml_attribs=False,
force_list=('batchInfo',))['batchInfoList']['batchInfo']
return batches
def _get_batch(self, job_id, batch_id):
endpoint = "job/{}/batch/{}".format(job_id, batch_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers = self._get_bulk_headers()
with metrics.http_request_timer("get_batch"):
resp = self.sf._make_request('GET', url, headers=headers)
batch = xmltodict.parse(resp.text)
return batch['batchInfo']
def get_batch_results(self, job_id, batch_id, catalog_entry):
"""Given a job_id and batch_id, queries the batches results and reads
CSV lines yielding each line as a record."""
headers = self._get_bulk_headers()
endpoint = "job/{}/batch/{}/result".format(job_id, batch_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
with metrics.http_request_timer("batch_result_list") as timer:
timer.tags['sobject'] = catalog_entry['stream']
batch_result_resp = self.sf._make_request('GET', url, headers=headers)
# Returns a Dict where input:
# <result-list><result>1</result><result>2</result></result-list>
# will return: {'result', ['1', '2']}
batch_result_list = xmltodict.parse(batch_result_resp.text,
xml_attribs=False,
force_list={'result'})['result-list']
for result in batch_result_list['result']:
endpoint = "job/{}/batch/{}/result/{}".format(job_id, batch_id, result)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
headers['Content-Type'] = 'text/csv'
with tempfile.NamedTemporaryFile(mode="w+", encoding="utf8") as csv_file:
resp = self.sf._make_request('GET', url, headers=headers, stream=True)
for chunk in resp.iter_content(chunk_size=ITER_CHUNK_SIZE, decode_unicode=True):
if chunk:
# Replace any NULL bytes in the chunk so it can be safely given to the CSV reader
csv_file.write(chunk.replace('\0', ''))
csv_file.seek(0)
csv_reader = csv.reader(csv_file,
delimiter=',',
quotechar='"')
column_name_list = next(csv_reader)
for line in csv_reader:
rec = dict(zip(column_name_list, line))
yield rec
def _close_job(self, job_id):
endpoint = "job/{}".format(job_id)
url = self.bulk_url.format(self.sf.instance_url, endpoint)
body = {"state": "Closed"}
with metrics.http_request_timer("close_job"):
self.sf._make_request(
'POST',
url,
headers=self._get_bulk_headers(),
body=json.dumps(body))
# pylint: disable=no-self-use
def _iter_lines(self, response):
"""Clone of the iter_lines function from the requests library with the change
to pass keepends=True in order to ensure that we do not strip the line breaks
from within a quoted value from the CSV stream."""
pending = None
for chunk in response.iter_content(decode_unicode=True, chunk_size=ITER_CHUNK_SIZE):
if pending is not None:
chunk = pending + chunk
lines = chunk.splitlines(keepends=True)
if lines and lines[-1] and chunk and lines[-1][-1] == chunk[-1]:
pending = lines.pop()
else:
pending = None
for line in lines:
yield line
if pending is not None:
yield pending