/
hook.py
301 lines (257 loc) · 8.96 KB
/
hook.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
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
# -*- coding: utf-8 -*-
from collective.elasticsearch.indexes import getIndex
from collective.elasticsearch.interfaces import IAdditionalIndexDataProvider
from collective.elasticsearch.utils import getESOnlyIndexes
from collective.elasticsearch.utils import getUID
from plone import api
from plone.app.uuid.utils import uuidToObject
from plone.indexer.interfaces import IIndexableObject
from plone.indexer.interfaces import IIndexer
from plone.uuid.interfaces import IUUID
from Products.CMFCore.interfaces import ISiteRoot
from zope.component import getAdapters
from zope.component import queryMultiAdapter
from zope.component.hooks import getSite
from zope.component.hooks import setSite
import logging
import random
import time
import traceback
import transaction
import urllib3
logger = logging.getLogger('collective.elasticsearch')
def index_batch(remove, index, positions, es=None):
if es is None:
from collective.elasticsearch.es import ElasticSearchCatalog
es = ElasticSearchCatalog(api.portal.get_tool('portal_catalog'))
setSite(api.portal.get())
conn = es.connection
bulk_size = es.get_setting('bulk_size', 50)
if len(remove) > 0:
bulk_data = []
for uid in remove:
bulk_data.append({
'delete': {
'_index': es.index_name,
'_type': es.doc_type,
'_id': uid
}
})
es.connection.bulk(
index=es.index_name,
doc_type=es.doc_type,
body=bulk_data)
if len(index) > 0:
if type(index) in (list, tuple, set):
# does not contain objects, must be async, convert to dict
index = dict([(k, None) for k in index])
bulk_data = []
for uid, obj in index.items():
if obj is None:
obj = uuidToObject(uid)
if obj is None:
continue
bulk_data.extend([{
'index': {
'_index': es.index_name,
'_type': es.doc_type,
'_id': uid
}
}, get_index_data(obj, es)])
if len(bulk_data) % bulk_size == 0:
conn.bulk(
index=es.index_name,
doc_type=es.doc_type,
body=bulk_data)
bulk_data = []
if len(bulk_data) > 0:
conn.bulk(
index=es.index_name,
doc_type=es.doc_type,
body=bulk_data)
if len(positions) > 0:
bulk_data = []
index = getIndex(es.catalogtool._catalog, 'getObjPositionInParent')
for uid, ids in positions.items():
if uid == '/':
parent = getSite()
else:
parent = uuidToObject(uid)
if parent is None:
logger.warn('could not find object to index positions')
continue
for _id in ids:
ob = parent[_id]
wrapped_object = get_wrapped_object(ob, es)
try:
value = index.get_value(wrapped_object)
except Exception:
continue
bulk_data.extend([{
'update': {
'_index': es.index_name,
'_type': es.doc_type,
'_id': IUUID(ob)
}
}, {
'doc': {
'getObjPositionInParent': value
}
}])
if len(bulk_data) % bulk_size == 0:
conn.bulk(
index=es.index_name,
doc_type=es.doc_type,
body=bulk_data)
bulk_data = []
if len(bulk_data) > 0:
conn.bulk(
index=es.index_name,
doc_type=es.doc_type,
body=bulk_data)
def get_wrapped_object(obj, es):
wrapped_object = None
if not IIndexableObject.providedBy(obj):
# This is the CMF 2.2 compatible approach, which should be used
# going forward
wrapper = queryMultiAdapter((obj, es.catalogtool),
IIndexableObject)
if wrapper is not None:
wrapped_object = wrapper
else:
wrapped_object = obj
else:
wrapped_object = obj
return wrapped_object
def get_index_data(obj, es):
catalog = es.catalogtool._catalog
wrapped_object = get_wrapped_object(obj, es)
index_data = {}
for index_name in catalog.indexes.keys():
index = getIndex(catalog, index_name)
if index is not None:
try:
value = index.get_value(wrapped_object)
except Exception:
logger.error('Error indexing value: %s: %s\n%s' % (
'/'.join(obj.getPhysicalPath()),
index_name,
traceback.format_exc()))
value = None
if value in (None, 'None'):
# yes, we'll index null data...
value = None
# Ignore errors in converting to unicode, so json.dumps
# does not barf when we're trying to send data to ES.
if isinstance(value, str):
value = unicode(value, 'utf-8', 'ignore')
index_data[index_name] = value
# in case these indexes are deleted
# (to increase performance and improve ram usage)
for name in getESOnlyIndexes():
if name in index_data:
continue
indexer = queryMultiAdapter((obj, es.catalogtool), IIndexer, name=name)
if indexer is not None:
try:
val = indexer()
if isinstance(value, str):
val = unicode(val, 'utf-8', 'ignore')
index_data[name] = val
except Exception:
logger.error('Error indexing value: %s: %s\n%s' % (
'/'.join(obj.getPhysicalPath()),
name,
traceback.format_exc()))
else:
val = getattr(obj, name, None)
if callable(val):
val = val()
index_data[name] = val
for _, adapter in getAdapters((obj,), IAdditionalIndexDataProvider):
index_data.update(adapter(es, index_data))
return index_data
try:
from collective.celery import task
@task.as_admin()
def index_batch_async(remove, index, positions):
retries = 0
while True:
# if doing batch updates, this can give ES problems
if retries < 4:
try:
index_batch(remove, index, positions)
break
except urllib3.exceptions.ReadTimeoutError:
retries += 1
if retries >= 4:
raise
time.sleep(random.choice([0.5, 0.75, 1, 1.25, 1.5]))
CELERY_INSTALLED = True
except ImportError:
CELERY_INSTALLED = False
class CommitHook(object):
def __init__(self, es):
self.remove = set()
self.index = {}
self.positions = {}
self.es = es
def schedule_celery(self):
index_batch_async.apply_async(
args=[self.remove, self.index.keys(), self.positions],
kwargs={},
without_transaction=True)
def __call__(self, trns):
if not trns:
return
if CELERY_INSTALLED:
self.schedule_celery()
else:
index_batch(self.remove, self.index, self.positions, self.es)
self.index = {}
self.remove = []
self.positions = {}
def getHook(es=None):
if es is None:
from collective.elasticsearch.es import ElasticSearchCatalog
es = ElasticSearchCatalog(api.portal.get_tool('portal_catalog'))
if not es.enabled:
return
trns = transaction.get()
hook = None
for _hook in trns._after_commit:
if isinstance(_hook[0], CommitHook):
hook = _hook[0]
break
if hook is None:
hook = CommitHook(es)
trns.addAfterCommitHook(hook)
return hook
def remove_object(es, obj):
hook = getHook(es)
uid = getUID(obj)
if uid is None:
logger.error("Tried to unindex an object of None uid")
return
hook.remove.add(uid)
if uid in hook.index:
del hook.index[uid]
def add_object(es, obj):
hook = getHook(es)
uid = getUID(obj)
if uid is None:
logger.error("Tried to index an object of None uid")
return
hook.index[uid] = obj
if uid in hook.remove:
hook.remove.remove(uid)
def index_positions(obj, ids):
hook = getHook()
if ISiteRoot.providedBy(obj):
hook.positions['/'] = ids
else:
uid = getUID(obj)
if uid is None:
logger.error("Tried to index an object of None uid")
return
hook.positions[getUID(obj)] = ids