torba-elastic-sync

This commit is contained in:
Jack Robison 2021-02-11 23:10:30 -05:00 committed by Victor Shyba
parent 9251c87323
commit 24d11de5a7
3 changed files with 39 additions and 8 deletions

View file

@ -20,4 +20,6 @@ if [[ -n "$SNAPSHOT_URL" ]] && [[ ! -f /database/claims.db ]]; then
rm "$filename" rm "$filename"
fi fi
/home/lbry/.local/bin/torba-elastic-sync /database/claims.db
echo 'starting server'
/home/lbry/.local/bin/torba-server "$@" /home/lbry/.local/bin/torba-server "$@"

View file

@ -41,9 +41,25 @@ WHERE claim.height % {shards_total} = {shard_num}
async def consume(producer): async def consume(producer):
es = AsyncElasticsearch() es = AsyncElasticsearch()
await async_bulk(es, producer, request_timeout=120) try:
await es.indices.refresh(index=INDEX) await async_bulk(es, producer, request_timeout=120)
await es.close() await es.indices.refresh(index=INDEX)
finally:
await es.close()
async def make_es_index():
es = AsyncElasticsearch()
try:
if await es.indices.exists(index=INDEX):
print("already synced ES")
return 1
index = SearchIndex('')
await index.start()
await index.stop()
return 0
finally:
await es.close()
async def run(args, shard): async def run(args, shard):
@ -53,26 +69,38 @@ async def run(args, shard):
index = SearchIndex('') index = SearchIndex('')
await index.start() await index.start()
await index.stop() await index.stop()
producer = get_all(db.cursor(), shard, args.clients) producer = get_all(db.cursor(), shard, args.clients)
await asyncio.gather(*(consume(producer) for _ in range(min(8, args.clients)))) await asyncio.gather(*(consume(producer) for _ in range(min(8, args.clients))))
def __run(args, shard): def __run(args, shard):
asyncio.run(run(args, shard)) asyncio.run(run(args, shard))
def main(): def __make_index():
return asyncio.run(make_es_index())
def run_elastic_sync():
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument("db_path", type=str) parser.add_argument("db_path", type=str)
parser.add_argument("-c", "--clients", type=int, default=16) parser.add_argument("-c", "--clients", type=int, default=16)
args = parser.parse_args() args = parser.parse_args()
processes = [] processes = []
init_proc = Process(target=__make_index, args=())
init_proc.start()
init_proc.join()
exitcode = init_proc.exitcode
init_proc.close()
if exitcode:
print("ES is already initialized")
return
print("bulk-loading ES")
for i in range(args.clients): for i in range(args.clients):
processes.append(Process(target=__run, args=(args, i))) processes.append(Process(target=__run, args=(args, i)))
processes[-1].start() processes[-1].start()
for process in processes: for process in processes:
process.join() process.join()
process.close() process.close()
if __name__ == '__main__':
main()

View file

@ -30,6 +30,7 @@ setup(
'lbrynet=lbry.extras.cli:main', 'lbrynet=lbry.extras.cli:main',
'torba-server=lbry.wallet.server.cli:main', 'torba-server=lbry.wallet.server.cli:main',
'orchstr8=lbry.wallet.orchstr8.cli:main', 'orchstr8=lbry.wallet.orchstr8.cli:main',
'torba-elastic-sync=lbry.wallet.server.db.elastic_sync:run_elastic_sync'
], ],
}, },
install_requires=[ install_requires=[