This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
hnswpsql.py
421 lines (360 loc) · 15.3 KB
/
hnswpsql.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
__copyright__ = "Copyright (c) 2021 Jina AI Limited. All rights reserved."
__license__ = "Apache-2.0"
import copy
import inspect
import threading
import time
import traceback
from contextlib import nullcontext
from datetime import datetime, timezone
from threading import Thread
from typing import Optional, Tuple, Dict, Union
import numpy as np
from jina import Executor, requests, DocumentArray, Document
from jina.logging.logger import JinaLogger
from .hnswlib_searcher import HnswlibSearcher, DEFAULT_METRIC
from .postgres_indexer import PostgreSQLStorage
def _get_method_args():
frame = inspect.currentframe().f_back
keys, _, _, values = inspect.getargvalues(frame)
kwargs = {}
for key in keys:
if key != 'self':
kwargs[key] = values[key]
return kwargs
class HNSWPostgresIndexer(Executor):
"""
Production-ready, scalable Indexer for the Jina neural search framework.
Combines the reliability of PostgreSQL with the speed and efficiency of the
HNSWlib nearest neighbor library.
"""
def __init__(
self,
total_shards: Optional[int] = None,
startup_sync: bool = True,
sync_interval: Optional[int] = None,
limit: int = 10,
metric: str = DEFAULT_METRIC,
dim: int = 0,
max_elements: int = 1_000_000,
ef_construction: int = 400,
ef_query: int = 50,
max_connection: int = 64,
is_distance: bool = True,
num_threads: int = -1,
traversal_paths: str = '@r',
hostname: str = '127.0.0.1',
port: int = 5432,
username: str = 'postgres',
password: str = '123456',
database: str = 'postgres',
table: str = 'default_table',
return_embeddings: bool = True,
dry_run: bool = False,
partitions: int = 128,
mute_unique_warnings: bool = False,
**kwargs,
):
"""
:param startup_sync: whether to sync from PSQL into HNSW on start-up
:param total_shards: the total nr of shards that this shard is part of.
:param sync_interval: the interval between automatic background PSQL-HNSW
syncs
(if None, sync will be turned off)
:param limit: (HNSW) Number of results to get for each query document in
search
:param metric: (HNSW) Distance metric type. Can be 'euclidean',
'inner_product', or 'cosine'
:param dim: (HNSW) dimensionality of vectors to index
:param max_elements: (HNSW) maximum number of elements (vectors) to index
:param ef_construction: (HNSW) construction time/accuracy trade-off
:param ef_query: (HNSW) query time accuracy/speed trade-off. High is more
accurate but slower
:param max_connection: (HNSW) The maximum number of outgoing connections in
the graph (the "M" parameter)
:param is_distance: (HNSW) if distance metric needs to be reinterpreted as
similarity
:param last_timestamp: (HNSW) the last time we synced into this HNSW index
:param num_threads: (HNSW) nr of threads to use during indexing. -1 is default
:param traversal_paths: (PSQL) default traversal paths on docs
(used for indexing, delete and update), e.g. '@r', '@c', '@r,c'
:param hostname: (PSQL) hostname of the machine
:param port: (PSQL) the port
:param username: (PSQL) the username to authenticate
:param password: (PSQL) the password to authenticate
:param database: (PSQL) the database
:param table: (PSQL) the table to use
:param return_embeddings: (PSQL) whether to return embeddings on
search
:param dry_run: (PSQL) If True, no database connection will be built
:param partitions: (PSQL) the number of shards to distribute
the data (used when syncing into HNSW)
:param mute_unique_warnings: (PSQL) whether to mute warnings about unique
ids constraint failing (useful when indexing with shards and polling = 'all')
NOTE:
- `total_shards` is REQUIRED in k8s, since there
`runtime_args.parallel` is always 1
- some arguments are passed to the inner classes. They are documented
here for easier reference
"""
super().__init__(**kwargs)
self.logger = JinaLogger(getattr(self.metas, 'name', self.__class__.__name__))
# TODO is there a way to improve this?
# done because we want to have the args exposed in hub website
# but we want to avoid having to manually pass every arg to the classes
self._init_kwargs = _get_method_args()
self._init_kwargs.update(kwargs)
self.sync_interval = sync_interval
self.lock = nullcontext()
self._prepare_shards(total_shards)
self._kv_indexer: Optional[PostgreSQLStorage] = None
self._vec_indexer: Optional[HnswlibSearcher] = None
(
self._kv_indexer,
self._vec_indexer,
) = self._init_executors(self._init_kwargs)
if startup_sync:
self._sync()
if self.sync_interval:
self.lock = threading.Lock()
self.stop_sync_thread = False
self._start_auto_sync()
def _prepare_shards(self, total_shards):
warning_issued = False
if total_shards is None:
self.total_shards = getattr(self.runtime_args, 'shards', None)
else:
self.total_shards = total_shards
if self.total_shards is None:
self.logger.warning(
'total_shards was None. '
'Setting it to 1 to allow non-sharded syncing. '
'This can happen when running Executor outside a Flow or on k8s'
)
self.total_shards = 1
warning_issued = True
if not hasattr(self.runtime_args, 'shard_id'):
self.runtime_args.shard_id = 0
if not warning_issued:
self.logger.warning(
'shard_id was None. '
'setting it to 1 to allow non-sharded syncing. '
'This can happen when running the Executor outside a Flow'
)
else:
# shards is passed as str from Flow.add in yaml
self.total_shards = int(self.total_shards)
@requests(on='/sync')
def sync(self, parameters: Dict, **kwargs):
"""
Perform a sync between PSQL and HNSW
:param parameters: dictionary with options for sync
Keys accepted:
- 'rebuild' (bool): whether to rebuild HNSW or do
incremental syncing
- 'timestamp' (str): ISO-formatted timestamp string. Time
from which to get data for syncing into HNSW
- 'batch_size' (int): The batch size for indexing in HNSW
"""
self._sync(**parameters)
def _sync(
self,
rebuild: bool = False,
timestamp: str = None,
batch_size: int = 100,
**kwargs,
):
timestamp: Optional[datetime] = self._compute_timestamp_for_sync(
timestamp, rebuild
)
if timestamp is None:
return
iterator = self._kv_indexer._get_delta(
shard_id=self.runtime_args.shard_id,
total_shards=self.total_shards,
timestamp=timestamp,
)
# we prevent race conditions with search
with self.lock:
if rebuild or self._vec_indexer.size == 0:
# call with just indexing
self._vec_indexer = HnswlibSearcher(**self._init_kwargs)
self._vec_indexer.index_sync(iterator, batch_size)
self.logger.info(
f'Rebuilt HNSW index with {self._vec_indexer.size} docs'
)
else:
prev_size = self._vec_indexer.size
self._vec_indexer.sync(iterator)
if prev_size != self._vec_indexer.size:
self.logger.info(
f'Synced HNSW index from {prev_size} docs to '
f'{self._vec_indexer.size}'
)
else:
self.logger.info(
f'Performed empty sync. HNSW index size is still {prev_size}'
)
def _init_executors(
self, _init_kwargs
) -> Tuple[PostgreSQLStorage, HnswlibSearcher]:
kv_indexer = PostgreSQLStorage(dump_dtype=np.float32, **_init_kwargs)
vec_indexer = HnswlibSearcher(**_init_kwargs)
return kv_indexer, vec_indexer
@requests(on='/index')
def index(self, docs: DocumentArray, parameters: Dict, **kwargs):
"""Index new documents
NOTE: PSQL has a uniqueness constraint on ID
:param docs: the Documents to index
:param parameters: dictionary with options for indexing
Keys accepted:
- 'traversal_paths' (str): traversal path for the docs
"""
self._kv_indexer.add(docs, parameters, **kwargs)
@requests(on='/update')
def update(self, docs: DocumentArray, parameters: Dict, **kwargs):
"""Update existing documents
:param docs: the Documents to update
:param parameters: dictionary with options for updating
Keys accepted:
- 'traversal_paths' (str): traversal path for the docs
"""
self._kv_indexer.update(docs, parameters, **kwargs)
@requests(on='/delete')
def delete(self, docs: DocumentArray, parameters: Dict, **kwargs):
"""Delete existing documents, by id
:param docs: the Documents to delete
:param parameters: dictionary with options for deleting
Keys accepted:
- 'traversal_paths' (str): traversal path for the docs
- 'soft_delete' (bool, default `True`): whether to perform soft delete
(doc is marked as empty but still exists in db, for retrieval purposes)
"""
if 'soft_delete' not in parameters:
parameters['soft_delete'] = True
self._kv_indexer.delete(docs, parameters, **kwargs)
@requests(on='/clear')
def clear(self, **kwargs):
"""
Delete all data from PSQL and HNSW
"""
if self._kv_indexer.initialized:
self._kv_indexer.clear()
self._vec_indexer = HnswlibSearcher(**self._init_kwargs)
self._vec_indexer.clear()
assert self._kv_indexer.size == 0
assert self._vec_indexer.size == 0
@requests(on='/status')
def status(self, **kwargs):
"""
Get information on status of this Indexer inside a Document's tags
:return: DocumentArray with one Document with tags 'psql_docs', 'hnsw_docs',
'last_sync', 'shard_id'
"""
psql_docs = None
hnsw_docs = None
last_sync = None
if self._kv_indexer and self._kv_indexer.initialized:
psql_docs = self._kv_indexer.size
else:
self.logger.warning(f'PSQL connection has not been initialized')
if self._vec_indexer:
hnsw_docs = self._vec_indexer.size
last_sync = self._vec_indexer.last_timestamp
last_sync = last_sync.isoformat()
else:
self.logger.warning(f'HNSW index has not been initialized')
status = {
'psql_docs': psql_docs,
'hnsw_docs': hnsw_docs,
'last_sync': last_sync,
'shard_id': self.runtime_args.shard_id,
}
return DocumentArray([Document(tags=status)])
@requests(on='/search')
def search(self, docs: 'DocumentArray', parameters: Dict = None, **kwargs):
"""Search the vec embeddings in HNSW and then lookup the metadata in PSQL
The `HNSWSearcher` attaches matches to the `Documents` sent as
inputs with the id of the match, and its embedding.
Then, the `PostgreSQLStorage` retrieves the full metadata
(original text or image blob) and attaches
those to the Document. You receive back the full Documents as matches
to your search Documents.
:param docs: `Document` with `.embedding` the same shape as the
`Documents` stored in the `HNSW` index. The ids of the `Documents`
stored in `HNSW` need to exist in the PSQL.
Otherwise you will not get back the original metadata.
:param parameters: dictionary for parameters for the search operation
- 'traversal_paths' (str): traversal paths for the docs
- 'limit' (int): nr of matches to get per Document
- 'ef_query' (int): query time accuracy/speed trade-off. High is more
accurate but slower
"""
if self._kv_indexer and self._vec_indexer:
# we prevent race conditions with sync
with self.lock:
self._vec_indexer.search(docs, parameters)
kv_parameters = copy.deepcopy(parameters)
kv_parameters['traversal_paths'] = ','.join(
[
path + 'm'
for path in kv_parameters.get('traversal_paths', '@r').split(',')
]
)
self._kv_indexer.search(docs, kv_parameters)
else:
self.logger.warning('Indexers have not been initialized. Empty results')
return
@requests(on='/cleanup')
def cleanup(self, **kwargs):
"""
Completely remove the entries in PSQL that have been
soft-deleted (via the /delete endpoint)
"""
if self._kv_indexer:
self._kv_indexer.cleanup()
else:
self.logger.warning(f'PSQL has not been initialized')
def _start_auto_sync(self):
self.sync_thread = Thread(target=self._sync_loop, daemon=False)
self.sync_thread.start()
def close(self) -> None:
if hasattr(self, 'sync_thread'):
# wait for sync thread to finish
self.stop_sync_thread = True
try:
self.sync_thread.join()
except Exception as e:
self.logger.warning(f'Error when stopping sync thread: {e}')
def _sync_loop(self):
try:
self.logger.warning(f'started sync thread')
while True:
self._sync(rebuild=False, timestamp=None, batch_size=100)
self.logger.info(f'sync thread: Completed sync')
time.sleep(self.sync_interval)
if self.stop_sync_thread:
self.logger.info(f'Exiting sync thread')
return
except Exception as e:
self.logger.error(f'Sync thread failed: {e}')
self.logger.error(traceback.format_exc())
def _compute_timestamp_for_sync(
self, timestamp: Union[datetime, str], rebuild: bool
) -> Optional[datetime]:
if timestamp is None:
if rebuild:
# we assume all db timestamps are UTC +00
timestamp = datetime.fromtimestamp(0, timezone.utc)
elif self._vec_indexer.last_timestamp:
timestamp = self._vec_indexer.last_timestamp
else:
self.logger.error(
f'No timestamp provided in parameters: '
f'and vec_indexer.last_timestamp'
f'was None. Cannot do sync'
)
return None
else:
timestamp = datetime.fromisoformat(timestamp)
return timestamp