Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added gRPC endpoint and python client #21

Merged
merged 16 commits into from
Apr 28, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve gRPC error handling, improve client UX
johnaohara committed Apr 27, 2022
commit 52fc81e763518d36c82f705f13cd5502a6b244b9
7 changes: 7 additions & 0 deletions src/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Error(Exception):
"""Base class for other exceptions"""
pass

class ExperimentNotFoundError(Error):
"""Raised when the input value is too small"""
pass
48 changes: 24 additions & 24 deletions src/gRPC/hpo_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions src/gRPC/hpo_pb2_grpc.py
Original file line number Diff line number Diff line change
@@ -32,7 +32,7 @@ def __init__(self, channel):
)
self.GetExperimentDetails = channel.unary_unary(
'/helloworld.HpoService/GetExperimentDetails',
request_serializer=hpo__pb2.ExperimentNameParams.SerializeToString,
request_serializer=hpo__pb2.ExperimentIdParams.SerializeToString,
response_deserializer=hpo__pb2.ExperimentDetails.FromString,
)
self.GetTrialConfig = channel.unary_unary(
@@ -47,7 +47,7 @@ def __init__(self, channel):
)
self.GenerateNextConfig = channel.unary_unary(
'/helloworld.HpoService/GenerateNextConfig',
request_serializer=hpo__pb2.ExperimentNameParams.SerializeToString,
request_serializer=hpo__pb2.ExperimentIdParams.SerializeToString,
response_deserializer=hpo__pb2.NewExperimentsReply.FromString,
)

@@ -118,7 +118,7 @@ def add_HpoServiceServicer_to_server(servicer, server):
),
'GetExperimentDetails': grpc.unary_unary_rpc_method_handler(
servicer.GetExperimentDetails,
request_deserializer=hpo__pb2.ExperimentNameParams.FromString,
request_deserializer=hpo__pb2.ExperimentIdParams.FromString,
response_serializer=hpo__pb2.ExperimentDetails.SerializeToString,
),
'GetTrialConfig': grpc.unary_unary_rpc_method_handler(
@@ -133,7 +133,7 @@ def add_HpoServiceServicer_to_server(servicer, server):
),
'GenerateNextConfig': grpc.unary_unary_rpc_method_handler(
servicer.GenerateNextConfig,
request_deserializer=hpo__pb2.ExperimentNameParams.FromString,
request_deserializer=hpo__pb2.ExperimentIdParams.FromString,
response_serializer=hpo__pb2.NewExperimentsReply.SerializeToString,
),
}
@@ -210,7 +210,7 @@ def GetExperimentDetails(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/helloworld.HpoService/GetExperimentDetails',
hpo__pb2.ExperimentNameParams.SerializeToString,
hpo__pb2.ExperimentIdParams.SerializeToString,
hpo__pb2.ExperimentDetails.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
@@ -261,7 +261,7 @@ def GenerateNextConfig(request,
timeout=None,
metadata=None):
return grpc.experimental.unary_unary(request, target, '/helloworld.HpoService/GenerateNextConfig',
hpo__pb2.ExperimentNameParams.SerializeToString,
hpo__pb2.ExperimentIdParams.SerializeToString,
hpo__pb2.NewExperimentsReply.FromString,
options, channel_credentials,
insecure, call_credentials, compression, wait_for_ready, timeout, metadata)
10 changes: 5 additions & 5 deletions src/gRPC/protos/hpo.proto
Original file line number Diff line number Diff line change
@@ -42,10 +42,10 @@ service HpoService {
rpc NumberExperiments(NumberExperimentsParams) returns (NumberExperimentsReply) {}
rpc ExperimentsList(ExperimentsListParams) returns (ExperimentsListReply) {}
rpc NewExperiment(ExperimentDetails) returns (NewExperimentsReply) {}
rpc GetExperimentDetails(ExperimentNameParams) returns (ExperimentDetails) {}
rpc GetExperimentDetails(ExperimentIdParams) returns (ExperimentDetails) {}
rpc GetTrialConfig(ExperimentTrial) returns (TrialConfig) {}
rpc UpdateTrialResult(ExperimentTrialResult) returns (ExperimentTrialReply) {}
rpc GenerateNextConfig(ExperimentNameParams) returns (NewExperimentsReply) {}
rpc GenerateNextConfig(ExperimentIdParams) returns (NewExperimentsReply) {}
}

message NumberExperimentsReply {
@@ -66,12 +66,12 @@ message ExperimentsListParams {}

message ExperimentTrialReply{}

message ExperimentNameParams {
string experiment_name = 1;
message ExperimentIdParams {
string experiment_id = 1;
}

message ExperimentTrial {
string experiment_name = 1;
string experiment_id = 1;
int32 trial = 2;
}

35 changes: 24 additions & 11 deletions src/grpc_client.py
Original file line number Diff line number Diff line change
@@ -35,30 +35,30 @@ def main():
def count():
"""Return a count of experiments currently running"""
empty = hpo_pb2.NumberExperimentsReply()
fun = lambda stub : stub.NumberExperiments(empty)
fun = lambda stub: stub.NumberExperiments(empty)
response = run(fun)
click.echo(" Number of running experiments: {}".format(response.count))

@main.command()
def list():
"""List names of all experiments currently running"""
empty = hpo_pb2.NumberExperimentsReply()
fun = lambda stub : stub.ExperimentsList(empty)
fun = lambda stub: stub.ExperimentsList(empty)
experiments: hpo_pb2.ExperimentsListReply = run(fun)
print("Running Experiments:")
for experiment in experiments.experiment:
click.echo(" %s" % experiment)


@main.command()
@click.option("--name", prompt=" Experiment name", type=str)
def show(name):
"""Show details of running experiment"""
expr: hpo_pb2.ExperimentNameParams = hpo_pb2.ExperimentNameParams()
expr.experiment_name = name
fun = lambda stub : stub.GetExperimentDetails(expr)
fun = lambda stub: stub.GetExperimentDetails(expr)
experiment: hpo_pb2.ExperimentDetails = run(fun)
json_obj = MessageToJson(experiment)
click.echo("Experiment Details:")
click.echo(json_obj)

@main.command()
@@ -71,7 +71,7 @@ def new(file):
# data = file.read().replace('\n', '')
message: hpo_pb2.ExperimentDetails = ParseDict(data, hpo_pb2.ExperimentDetails())
click.echo(" Adding new experiment: {}".format(message.experiment_name))
fun = lambda stub : stub.NewExperiment(message)
fun = lambda stub: stub.NewExperiment(message)
response: hpo_pb2.NewExperimentsReply = run(fun)
click.echo("Trial Number: {}".format(response.trial_number))

@@ -83,10 +83,9 @@ def config(name, trial):
expr: hpo_pb2.ExperimentTrial = hpo_pb2.ExperimentTrial()
expr.experiment_name = name
expr.trial = trial
fun = lambda stub : stub.GetTrialConfig(expr)
fun = lambda stub: stub.GetTrialConfig(expr)
trial_config: hpo_pb2.TrialConfig = run(fun)
json_obj = MessageToJson(trial_config)
click.echo("Trial Config:")
click.echo(json_obj)

@main.command()
@@ -103,9 +102,9 @@ def result(name, trial, result, value_type, value):
trialResult.result = hpo_pb2._EXPERIMENTTRIALRESULT_RESULT.values_by_name[result].number
trialResult.value_type = value_type
trialResult.value = value
fun = lambda stub : stub.UpdateTrialResult(trialResult)
fun = lambda stub: stub.UpdateTrialResult(trialResult)
hpo_pb2.TrialConfig = run(fun)
click.echo("Updated Trial Result")
click.echo("Success: Updated Trial Result")

@main.command()
@click.option("--name", prompt=" Enter name", type=str)
@@ -123,12 +122,26 @@ def run(func):
# of the code.
with grpc.insecure_channel('localhost:50051') as channel:
stub = hpo_pb2_grpc.HpoServiceStub(channel)
return func(stub)
try:
response = func(stub)
except grpc.RpcError as rpc_error:
if rpc_error.code() == grpc.StatusCode.CANCELLED:
pass
elif rpc_error.code() == grpc.StatusCode.UNAVAILABLE:
pass
elif rpc_error.code() == grpc.StatusCode.NOT_FOUND:
raise click.ClickException(rpc_error.details())
elif rpc_error.code() == grpc.StatusCode.INVALID_ARGUMENT:
raise click.ClickException(rpc_error.details())
else:
raise click.ClickException("Received unknown RPC error: code={rpc_error.code()} message={rpc_error.details()}")
return response


def NewExperiment(stub, **args):
empty = hpo_pb2.NumberExperimentsReply()
response = stub.NumberExperiments(empty)
print("HpoService client received: %s" % response.count)
click.echo("HpoService client received: %s" % response.count)


if __name__ == "__main__":
Loading