Skip to content

Commit

Permalink
Start the service when switching graph context on Interactive
Browse files Browse the repository at this point in the history
  • Loading branch information
lidongze0629 committed Jun 13, 2024
1 parent aff323a commit db9b9e4
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 181 deletions.
174 changes: 116 additions & 58 deletions python/graphscope/gsctl/commands/interactive/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@
import click
import yaml

from graphscope.gsctl.impl import bind_datasource_in_batch
from graphscope.gsctl.impl import create_graph
from graphscope.gsctl.impl import delete_graph_by_id
from graphscope.gsctl.impl import delete_job_by_id
from graphscope.gsctl.impl import get_datasource_by_id
from graphscope.gsctl.impl import get_graph_id_by_name
from graphscope.gsctl.impl import get_job_by_id
from graphscope.gsctl.impl import list_graphs
from graphscope.gsctl.impl import list_jobs
from graphscope.gsctl.impl import list_service_status
from graphscope.gsctl.impl import list_stored_procedures
from graphscope.gsctl.impl import restart_service
from graphscope.gsctl.impl import start_service
from graphscope.gsctl.impl import stop_service
from graphscope.gsctl.impl import submit_dataloading_job
from graphscope.gsctl.impl import switch_context
from graphscope.gsctl.impl import unbind_edge_datasource
from graphscope.gsctl.impl import unbind_vertex_datasource
from graphscope.gsctl.utils import TreeDisplay
from graphscope.gsctl.utils import err
from graphscope.gsctl.utils import info
Expand All @@ -47,19 +51,19 @@ def cli():

@cli.group()
def create():
"""Create a new graph in database"""
"""Create graph, data source, loader job from file"""
pass


@cli.group()
def delete():
"""Delete a graph by identifier"""
"""Delete graph, data source, loader job by id"""
pass


@cli.group()
def service():
"""Start, stop, and restart the database service"""
def desc():
"""Show job's details by id"""
pass


Expand All @@ -71,7 +75,13 @@ def service():
def use(context, graph_identifier):
"""Switch to GRAPH context, see identifier with `ls` command"""
try:
switch_context(get_graph_id_by_name(graph_identifier))
graph_identifier = get_graph_id_by_name(graph_identifier)
status = list_service_status()
for s in status:
if s.graph_id == graph_identifier and s.status != "Running":
info(f"Starting service on graph {graph_identifier}...")
start_service(graph_identifier)
switch_context(graph_identifier)
except Exception as e:
err(f"Failed to switch context: {str(e)}")
else:
Expand All @@ -89,7 +99,7 @@ def ls(l): # noqa: F811, E741
# schema
tree.create_graph_node(g, recursive=l)
if l:
# data source mappin
# data source mapping
datasource_mapping = get_datasource_by_id(g.id)
tree.create_datasource_mapping_node(g, datasource_mapping)
# stored procedure
Expand Down Expand Up @@ -139,79 +149,127 @@ def graph(graph_identifier): # noqa: F811
succ(f"Delete graph {graph_identifier} successfully.")


@service.command
def stop(): # noqa: F811
"""Stop current database service"""
@create.command
@click.option(
"-g",
"--graph_identifier",
required=True,
help="See graph identifier with `ls` command",
)
@click.option(
"-f",
"--filename",
required=True,
help="Path of yaml file",
)
def datasource(graph_identifier, filename): # noqa: F811
"""Bind data source mapping from file"""
if not is_valid_file_path(filename):
err(f"Invalid file: {filename}")
return
graph_identifier = get_graph_id_by_name(graph_identifier)
try:
datasource = read_yaml_file(filename)
bind_datasource_in_batch(graph_identifier, datasource)
except Exception as e:
err(f"Failed to bind data source: {str(e)}")
else:
succ("Bind data source successfully.")


@delete.command
@click.option(
"-g",
"--graph_identifier",
required=True,
help="See graph identifier with `ls` command",
)
@click.option(
"-t",
"--type",
required=True,
help="Vertex or edge type",
)
@click.option(
"-s",
"--source_vertex_type",
required=False,
help="Source vertex type of the edge [edge only]",
)
@click.option(
"-d",
"--destination_vertex_type",
required=False,
help="Destination vertex type of the edge [edge only]",
)
def datasource( # noqa: F811
graph_identifier, type, source_vertex_type, destination_vertex_type
):
"""Unbind data source mapping on vertex or edge type"""
try:
stop_service()
graph_identifier = get_graph_id_by_name(graph_identifier)
if source_vertex_type is not None and destination_vertex_type is not None:
unbind_edge_datasource(
graph_identifier, type, source_vertex_type, destination_vertex_type
)
else:
unbind_vertex_datasource(graph_identifier, type)
except Exception as e:
err(f"Failed to stop service: {str(e)}")
err(f"Failed to unbind data source: {str(e)}")
else:
succ("Service stopped.")
succ("Unbind data source successfully.")


@service.command
@create.command()
@click.option(
"-g",
"--graph_identifier",
required=True,
help="See graph identifier with `ls` command",
)
def start(graph_identifier): # noqa: F811
"""Start database service on a certain graph"""
@click.option(
"-f",
"--filename",
required=True,
help="Path of yaml file",
)
def loaderjob(graph_identifier, filename): # noqa: F811
"""Create a data loading job from file"""
if not is_valid_file_path(filename):
err(f"Invalid file: {filename}")
return
graph_identifier = get_graph_id_by_name(graph_identifier)
try:
start_service(get_graph_id_by_name(graph_identifier))
config = read_yaml_file(filename)
jobid = submit_dataloading_job(graph_identifier, config)
except Exception as e:
err(f"Failed to start service on graph {graph_identifier}: {str(e)}")
err(f"Failed to create a job: {str(e)}")
else:
succ(f"Start service on graph {graph_identifier} successfully")
succ(f"Create job {jobid} successfully.")


@service.command
def restart(): # noqa: F811
"""Restart database service on current graph"""
@delete.command()
@click.argument("identifier", required=True)
def job(identifier): # noqa: F811
"""Cancel a job, see identifier with `ls` command"""
try:
restart_service()
delete_job_by_id(identifier)
except Exception as e:
err(f"Failed to restart service: {str(e)}")
err(f"Failed to delete job {identifier}: {str(e)}")
else:
succ("Service restarted.")


@service.command
def ls(): # noqa: F811
"""Display current service status"""

def _construct_and_display_data(status):
head = [
"STATUS",
"SERVING_GRAPH(IDENTIFIER)",
"CYPHER_ENDPOINT",
"HQPS_ENDPOINT",
"GREMLIN_ENDPOINT",
]
data = [head]
for s in status:
if s.status == "Stopped":
data.append([s.status, s.graph_id, "-", "-", "-"])
else:
data.append(
[
s.status,
s.graph_id,
s.sdk_endpoints.cypher,
s.sdk_endpoints.hqps,
s.sdk_endpoints.gremlin,
]
)
terminal_display(data)
succ(f"Delete job {identifier} successfully.")


@desc.command()
@click.argument("identifier", required=True)
def job(identifier): # noqa: F811
"""Show details of job, see identifier with `ls` command"""
try:
status = list_service_status()
job = get_job_by_id(identifier)
except Exception as e:
err(f"Failed to list service status: {str(e)}")
err(f"Failed to get job: {str(e)}")
else:
_construct_and_display_data(status)
info(yaml.dump(job.to_dict()))


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit db9b9e4

Please sign in to comment.