lbry-sdk/lbry/wallet/server/db/elasticsearch/sync.py

84 lines
2.6 KiB
Python
Raw Normal View History

2021-01-20 05:41:54 +01:00
import argparse
import asyncio
import logging
2021-01-20 05:41:54 +01:00
from elasticsearch import AsyncElasticsearch
from elasticsearch.helpers import async_bulk
from lbry.wallet.server.env import Env
from lbry.wallet.server.coin import LBC
2021-06-18 03:22:23 +02:00
from lbry.wallet.server.leveldb import LevelDB
from lbry.wallet.server.db.elasticsearch.search import extract_doc, SearchIndex, IndexVersionMismatch
2021-01-20 05:41:54 +01:00
2021-06-18 03:22:23 +02:00
async def get_all_claims(index_name='claims', db=None):
env = Env(LBC)
2021-06-18 03:22:23 +02:00
need_open = db is None
db = db or LevelDB(env)
if need_open:
await db.open_dbs()
2021-06-18 03:30:31 +02:00
try:
cnt = 0
for claim in db.all_claims_producer():
yield extract_doc(claim, index_name)
cnt += 1
if cnt % 10000 == 0:
print(f"{cnt} claims sent")
finally:
if need_open:
db.close()
2021-02-12 05:10:30 +01:00
2021-05-12 02:38:05 +02:00
async def make_es_index(index=None):
env = Env(LBC)
2021-05-12 02:38:05 +02:00
if index is None:
index = SearchIndex('', elastic_host=env.elastic_host, elastic_port=env.elastic_port)
2021-02-12 05:10:30 +01:00
try:
return await index.start()
except IndexVersionMismatch as err:
logging.info(
"dropping ES search index (version %s) for upgrade to version %s", err.got_version, err.expected_version
)
await index.delete_index()
2021-05-12 05:21:03 +02:00
await index.stop()
return await index.start()
2021-02-12 05:10:30 +01:00
finally:
index.stop()
2021-06-18 03:22:23 +02:00
async def run_sync(index_name='claims', db=None):
env = Env(LBC)
logging.info("ES sync host: %s:%i", env.elastic_host, env.elastic_port)
es = AsyncElasticsearch([{'host': env.elastic_host, 'port': env.elastic_port}])
try:
await async_bulk(es, get_all_claims(index_name=index_name, db=db), request_timeout=120)
await es.indices.refresh(index=index_name)
finally:
await es.close()
2021-01-27 05:43:06 +01:00
2021-02-12 05:10:30 +01:00
2021-01-27 05:43:06 +01:00
def __run(args, shard):
2021-06-18 03:22:23 +02:00
asyncio.run(run_sync())
2021-01-27 05:43:06 +01:00
2021-02-12 05:10:30 +01:00
def run_elastic_sync():
logging.basicConfig(level=logging.INFO)
logging.getLogger('aiohttp').setLevel(logging.WARNING)
logging.getLogger('elasticsearch').setLevel(logging.WARNING)
logging.info('lbry.server starting')
2021-03-24 21:07:17 +01:00
parser = argparse.ArgumentParser(prog="lbry-hub-elastic-sync")
2021-06-18 03:22:23 +02:00
# parser.add_argument("db_path", type=str)
2021-01-27 05:43:06 +01:00
parser.add_argument("-c", "--clients", type=int, default=16)
parser.add_argument("-b", "--blocks", type=int, default=0)
parser.add_argument("-f", "--force", default=False, action='store_true')
2021-01-27 05:43:06 +01:00
args = parser.parse_args()
2021-02-12 05:10:30 +01:00
2021-06-18 03:22:23 +02:00
# if not args.force and not os.path.exists(args.db_path):
# logging.info("DB path doesnt exist")
# return
2021-02-16 16:52:32 +01:00
if not args.force and not asyncio.run(make_es_index()):
logging.info("ES is already initialized")
2021-02-12 05:10:30 +01:00
return
2021-06-18 03:22:23 +02:00
asyncio.run(run_sync())