2024-11-25 20:46:35 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
2024-11-26 16:58:47 +00:00
|
|
|
"""This utility takes a knowledge core and loads it into a running TrustGraph
|
|
|
|
|
through the API. The knowledge core should be in msgpack format, which is the
|
|
|
|
|
default format produce by tg-save-kg-core.
|
|
|
|
|
"""
|
|
|
|
|
|
2024-11-25 20:46:35 +00:00
|
|
|
import aiohttp
|
|
|
|
|
import asyncio
|
|
|
|
|
import msgpack
|
|
|
|
|
import json
|
|
|
|
|
import sys
|
|
|
|
|
import argparse
|
|
|
|
|
import os
|
2024-12-06 15:16:09 +00:00
|
|
|
import signal
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
class Running:
|
|
|
|
|
def __init__(self): self.running = True
|
|
|
|
|
def get(self): return self.running
|
|
|
|
|
def stop(self): self.running = False
|
|
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
ge_counts = 0
|
|
|
|
|
t_counts = 0
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
async def load_ge(running, queue, url):
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
global ge_counts
|
|
|
|
|
|
2024-11-25 20:46:35 +00:00
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
|
|
|
|
|
|
async with session.ws_connect(f"{url}load/graph-embeddings") as ws:
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
while running.get():
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
try:
|
|
|
|
|
msg = await asyncio.wait_for(queue.get(), 1)
|
2024-12-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
# End of load
|
|
|
|
|
if msg is None:
|
|
|
|
|
break
|
|
|
|
|
|
2024-12-09 00:01:01 +00:00
|
|
|
except:
|
|
|
|
|
# Hopefully it's TimeoutError. Annoying to match since
|
|
|
|
|
# it changed in 3.11.
|
2024-12-06 15:16:09 +00:00
|
|
|
continue
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
msg = {
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": msg["m"]["i"],
|
|
|
|
|
"metadata": msg["m"]["m"],
|
|
|
|
|
"user": msg["m"]["u"],
|
|
|
|
|
"collection": msg["m"]["c"],
|
|
|
|
|
},
|
|
|
|
|
"vectors": msg["v"],
|
|
|
|
|
"entity": msg["e"],
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
try:
|
|
|
|
|
await ws.send_json(msg)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
|
|
|
|
|
|
|
|
|
ge_counts += 1
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
async def load_triples(running, queue, url):
|
|
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
global t_counts
|
|
|
|
|
|
2024-11-25 20:46:35 +00:00
|
|
|
async with aiohttp.ClientSession() as session:
|
2024-12-06 15:16:09 +00:00
|
|
|
|
2024-11-25 20:46:35 +00:00
|
|
|
async with session.ws_connect(f"{url}load/triples") as ws:
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
while running.get():
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
try:
|
|
|
|
|
msg = await asyncio.wait_for(queue.get(), 1)
|
2024-12-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
# End of load
|
|
|
|
|
if msg is None:
|
|
|
|
|
break
|
|
|
|
|
|
2024-12-09 00:01:01 +00:00
|
|
|
except:
|
|
|
|
|
# Hopefully it's TimeoutError. Annoying to match since
|
|
|
|
|
# it changed in 3.11.
|
2024-12-06 15:16:09 +00:00
|
|
|
continue
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
msg ={
|
|
|
|
|
"metadata": {
|
|
|
|
|
"id": msg["m"]["i"],
|
|
|
|
|
"metadata": msg["m"]["m"],
|
|
|
|
|
"user": msg["m"]["u"],
|
|
|
|
|
"collection": msg["m"]["c"],
|
|
|
|
|
},
|
|
|
|
|
"triples": msg["t"],
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
try:
|
|
|
|
|
await ws.send_json(msg)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(e)
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
t_counts += 1
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
async def stats(running):
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
global t_counts
|
|
|
|
|
global ge_counts
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
while running.get():
|
2024-12-09 12:44:30 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
await asyncio.sleep(2)
|
2024-12-09 12:44:30 +00:00
|
|
|
|
2024-11-25 20:46:35 +00:00
|
|
|
print(
|
|
|
|
|
f"Graph embeddings: {ge_counts:10d} Triples: {t_counts:10d}"
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
async def loader(running, ge_queue, t_queue, path, format, user, collection):
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
if format == "json":
|
|
|
|
|
|
|
|
|
|
raise RuntimeError("Not implemented")
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
|
|
with open(path, "rb") as f:
|
|
|
|
|
|
|
|
|
|
unpacker = msgpack.Unpacker(f, raw=False)
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
while running.get():
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
unpacked = unpacker.unpack()
|
|
|
|
|
except:
|
|
|
|
|
break
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
if user:
|
|
|
|
|
unpacked["metadata"]["user"] = user
|
|
|
|
|
|
|
|
|
|
if collection:
|
|
|
|
|
unpacked["metadata"]["collection"] = collection
|
|
|
|
|
|
|
|
|
|
if unpacked[0] == "t":
|
2024-12-06 15:16:09 +00:00
|
|
|
qtype = t_queue
|
2024-11-25 20:46:35 +00:00
|
|
|
else:
|
|
|
|
|
if unpacked[0] == "ge":
|
2024-12-06 15:16:09 +00:00
|
|
|
qtype = ge_queue
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
while running.get():
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.wait_for(qtype.put(unpacked[1]), 0.5)
|
2024-12-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
# Successful put message, move on
|
|
|
|
|
break
|
|
|
|
|
|
2024-12-09 00:31:03 +00:00
|
|
|
except:
|
|
|
|
|
# Hopefully it's TimeoutError. Annoying to match since
|
|
|
|
|
# it changed in 3.11.
|
2024-12-06 15:16:09 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
if not running.get(): break
|
|
|
|
|
|
2024-12-09 12:44:30 +00:00
|
|
|
# Put 'None' on end of queue to finish
|
|
|
|
|
while running.get():
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.wait_for(t_queue.put(None), 1)
|
|
|
|
|
|
|
|
|
|
# Successful put message, move on
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
# Hopefully it's TimeoutError. Annoying to match since
|
|
|
|
|
# it changed in 3.11.
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# Put 'None' on end of queue to finish
|
|
|
|
|
while running.get():
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
await asyncio.wait_for(ge_queue.put(None), 1)
|
|
|
|
|
|
|
|
|
|
# Successful put message, move on
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
# Hopefully it's TimeoutError. Annoying to match since
|
|
|
|
|
# it changed in 3.11.
|
|
|
|
|
continue
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
async def run(running, **args):
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-11-27 17:24:27 +00:00
|
|
|
# Maxsize on queues reduces back-pressure so tg-load-kg-core doesn't
|
|
|
|
|
# grow to eat all memory
|
2024-12-09 12:44:30 +00:00
|
|
|
ge_q = asyncio.Queue(maxsize=10)
|
|
|
|
|
t_q = asyncio.Queue(maxsize=10)
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
load_task = asyncio.create_task(
|
|
|
|
|
loader(
|
2024-12-06 15:16:09 +00:00
|
|
|
running=running,
|
2024-11-25 20:46:35 +00:00
|
|
|
ge_queue=ge_q, t_queue=t_q,
|
|
|
|
|
path=args["input_file"], format=args["format"],
|
|
|
|
|
user=args["user"], collection=args["collection"],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
ge_task = asyncio.create_task(
|
|
|
|
|
load_ge(
|
2024-12-06 15:16:09 +00:00
|
|
|
running=running,
|
2024-11-25 20:46:35 +00:00
|
|
|
queue=ge_q, url=args["url"] + "api/v1/"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
triples_task = asyncio.create_task(
|
|
|
|
|
load_triples(
|
2024-12-06 15:16:09 +00:00
|
|
|
running=running,
|
2024-11-25 20:46:35 +00:00
|
|
|
queue=t_q, url=args["url"] + "api/v1/"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
stats_task = asyncio.create_task(stats(running))
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
await triples_task
|
|
|
|
|
await ge_task
|
2024-12-09 12:44:30 +00:00
|
|
|
|
|
|
|
|
running.stop()
|
|
|
|
|
|
|
|
|
|
await load_task
|
2024-11-25 20:46:35 +00:00
|
|
|
await stats_task
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
async def main(running):
|
2024-11-25 20:46:35 +00:00
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
2024-11-26 16:58:47 +00:00
|
|
|
prog='tg-load-kg-core',
|
2024-11-25 20:46:35 +00:00
|
|
|
description=__doc__,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
default_url = os.getenv("TRUSTGRAPH_API", "http://localhost:8088/")
|
|
|
|
|
default_user = "trustgraph"
|
|
|
|
|
collection = "default"
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-u', '--url',
|
|
|
|
|
default=default_url,
|
|
|
|
|
help=f'TrustGraph API URL (default: {default_url})',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'-i', '--input-file',
|
|
|
|
|
# Make it mandatory, difficult to over-write an existing file
|
|
|
|
|
required=True,
|
|
|
|
|
help=f'Output file'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--format',
|
|
|
|
|
default="msgpack",
|
|
|
|
|
choices=["msgpack", "json"],
|
|
|
|
|
help=f'Output format (default: msgpack)',
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--user',
|
|
|
|
|
help=f'User ID to load as (default: from input)'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
parser.add_argument(
|
|
|
|
|
'--collection',
|
|
|
|
|
help=f'Collection ID to load as (default: from input)'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
await run(running, **vars(args))
|
|
|
|
|
|
|
|
|
|
running = Running()
|
|
|
|
|
|
|
|
|
|
def interrupt(sig, frame):
|
|
|
|
|
running.stop()
|
|
|
|
|
print('Interrupt')
|
|
|
|
|
|
|
|
|
|
signal.signal(signal.SIGINT, interrupt)
|
2024-11-25 20:46:35 +00:00
|
|
|
|
2024-12-06 15:16:09 +00:00
|
|
|
asyncio.run(main(running))
|
2024-11-25 20:46:35 +00:00
|
|
|
|