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

v1alpha2 api server implementation #456

Merged
merged 5 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
136 changes: 102 additions & 34 deletions cmd/manager/v1alpha2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log"
"net"

Expand All @@ -24,94 +26,160 @@ type server struct {
}

// Register a Experiment to DB.
func (s *server) RegisterExperiment(context.Context, *api_pb.RegisterExperimentRequest) (*api_pb.RegisterExperimentReply, error) {
return nil, nil
func (s *server) RegisterExperiment(ctx context.Context, in *api_pb.RegisterExperimentRequest) (*api_pb.RegisterExperimentReply, error) {
err := dbIf.RegisterExperiment(in.Experiment)
return &api_pb.RegisterExperimentReply{}, err
}

// Delete a Experiment from DB by name.
func (s *server) DeleteExperiment(context.Context, *api_pb.DeleteExperimentRequest) (*api_pb.DeleteExperimentReply, error) {
return nil, nil
func (s *server) DeleteExperiment(ctx context.Context, in *api_pb.DeleteExperimentRequest) (*api_pb.DeleteExperimentReply, error) {
err := dbIf.DeleteExperiment(in.ExperimentName)
return &api_pb.DeleteExperimentReply{}, err
}

// Get a Experiment from DB by name.
func (s *server) GetExperiment(context.Context, *api_pb.GetExperimentRequest) (*api_pb.GetExperimentReply, error) {
return nil, nil
func (s *server) GetExperiment(ctx context.Context, in *api_pb.GetExperimentRequest) (*api_pb.GetExperimentReply, error) {
exp, err := dbIf.GetExperiment(in.ExperimentName)
return &api_pb.GetExperimentReply{
Experiment: exp,
}, err
}

// Get a summary list of Experiment from DB.
// The summary includes name and condition.
func (s *server) GetExperimentList(context.Context, *api_pb.GetExperimentListRequest) (*api_pb.GetExperimentListReply, error) {
return nil, nil
func (s *server) GetExperimentList(ctx context.Context, in *api_pb.GetExperimentListRequest) (*api_pb.GetExperimentListReply, error) {
expList, err := dbIf.GetExperimentList()
return &api_pb.GetExperimentListReply{
ExperimentSummaries: expList,
}, err
}

// Update Status of a experiment.
func (s *server) UpdateExperimentStatus(context.Context, *api_pb.UpdateExperimentStatusRequest) (*api_pb.UpdateExperimentStatusReply, error) {
return nil, nil
func (s *server) UpdateExperimentStatus(ctx context.Context, in *api_pb.UpdateExperimentStatusRequest) (*api_pb.UpdateExperimentStatusReply, error) {
err := dbIf.UpdateExperimentStatus(in.ExperimentName, in.NewStatus)
return &api_pb.UpdateExperimentStatusReply{}, err
}

// Update AlgorithmExtraSettings.
// The ExtraSetting is created if it does not exist, otherwise it is overwrited.
func (s *server) UpdateAlgorithmExtraSettings(context.Context, *api_pb.UpdateAlgorithmExtraSettingsRequest) (*api_pb.UpdateAlgorithmExtraSettingsReply, error) {
return nil, nil
func (s *server) UpdateAlgorithmExtraSettings(ctx context.Context, in *api_pb.UpdateAlgorithmExtraSettingsRequest) (*api_pb.UpdateAlgorithmExtraSettingsReply, error) {
err := dbIf.UpdateAlgorithmExtraSettings(in.ExperimentName, in.ExtraAlgorithmSettings)
return &api_pb.UpdateAlgorithmExtraSettingsReply{}, err
}

// Get all AlgorithmExtraSettings.
func (s *server) GetAlgorithmExtraSettings(context.Context, *api_pb.GetAlgorithmExtraSettingsRequest) (*api_pb.GetAlgorithmExtraSettingsReply, error) {
return nil, nil
func (s *server) GetAlgorithmExtraSettings(ctx context.Context, in *api_pb.GetAlgorithmExtraSettingsRequest) (*api_pb.GetAlgorithmExtraSettingsReply, error) {
eas, err := dbIf.GetAlgorithmExtraSettings(in.ExperimentName)
return &api_pb.GetAlgorithmExtraSettingsReply{
ExtraAlgorithmSettings: eas,
}, err
}

// Register a Trial to DB.
// ID will be filled by manager automatically.
func (s *server) RegisterTrial(context.Context, *api_pb.RegisterTrialRequest) (*api_pb.RegisterTrialReply, error) {
return nil, nil
func (s *server) RegisterTrial(ctx context.Context, in *api_pb.RegisterTrialRequest) (*api_pb.RegisterTrialReply, error) {
err := dbIf.RegisterTrial(in.Trial)
return &api_pb.RegisterTrialReply{}, err
}

// Delete a Trial from DB by ID.
func (s *server) DeleteTrial(context.Context, *api_pb.DeleteTrialRequest) (*api_pb.DeleteTrialReply, error) {
return nil, nil
func (s *server) DeleteTrial(ctx context.Context, in *api_pb.DeleteTrialRequest) (*api_pb.DeleteTrialReply, error) {
err := dbIf.DeleteTrial(in.TrialName)
return &api_pb.DeleteTrialReply{}, err
}

// Get a list of Trial from DB by name of a Experiment.
func (s *server) GetTrialList(context.Context, *api_pb.GetTrialListRequest) (*api_pb.GetTrialListReply, error) {
return nil, nil
func (s *server) GetTrialList(ctx context.Context, in *api_pb.GetTrialListRequest) (*api_pb.GetTrialListReply, error) {
trList, err := dbIf.GetTrialList(in.ExperimentName, in.Filter)
return &api_pb.GetTrialListReply{
Trials: trList,
}, err
}

// Get a Trial from DB by ID of Trial.
func (s *server) GetTrial(context.Context, *api_pb.GetTrialRequest) (*api_pb.GetTrialReply, error) {
return nil, nil
func (s *server) GetTrial(ctx context.Context, in *api_pb.GetTrialRequest) (*api_pb.GetTrialReply, error) {
tr, err := dbIf.GetTrial(in.TrialName)
return &api_pb.GetTrialReply{
Trial: tr,
}, err
}

// Update Status of a trial.
func (s *server) UpdateTrialStatus(context.Context, *api_pb.UpdateTrialStatusRequest) (*api_pb.UpdateTrialStatusReply, error) {
return nil, nil
func (s *server) UpdateTrialStatus(ctx context.Context, in *api_pb.UpdateTrialStatusRequest) (*api_pb.UpdateTrialStatusReply, error) {
err := dbIf.UpdateTrialStatus(in.TrialName, in.NewStatus)
return &api_pb.UpdateTrialStatusReply{}, err
}

// Report a log of Observations for a Trial.
// The log consists of timestamp and value of metric.
// Katib store every log of metrics.
// You can see accuracy curve or other metric logs on UI.
func (s *server) ReportObservationLog(context.Context, *api_pb.ReportObservationLogRequest) (*api_pb.ReportObservationLogReply, error) {
return nil, nil
func (s *server) ReportObservationLog(ctx context.Context, in *api_pb.ReportObservationLogRequest) (*api_pb.ReportObservationLogReply, error) {
err := dbIf.RegisterObservationLog(in.TrialName, in.ObservationLog)
return &api_pb.ReportObservationLogReply{}, err
}

// Get all log of Observations for a Trial.
func (s *server) GetObservationLog(context.Context, *api_pb.GetObservationLogRequest) (*api_pb.GetObservationLogReply, error) {
return nil, nil
func (s *server) GetObservationLog(ctx context.Context, in *api_pb.GetObservationLogRequest) (*api_pb.GetObservationLogReply, error) {
ol, err := dbIf.GetObservationLog(in.TrialName, in.StartTime, in.EndTime)
return &api_pb.GetObservationLogReply{
ObservationLog: ol,
}, err
}

func (s *server) getSuggestionServiceConnection(algoName string) (*grpc.ClientConn, error) {
if algoName == "" {
return nil, errors.New("No algorithm name is specified")
}
return grpc.Dial("katib-suggestion-"+algoName+":6789", grpc.WithInsecure())
}

// Get Suggestions from a Suggestion service.
func (s *server) GetSuggestions(context.Context, *api_pb.GetSuggestionsRequest) (*api_pb.GetSuggestionsReply, error) {
return nil, nil
func (s *server) GetSuggestions(ctx context.Context, in *api_pb.GetSuggestionsRequest) (*api_pb.GetSuggestionsReply, error) {
conn, err := s.getSuggestionServiceConnection(in.AlgorithmName)
if err != nil {
return &api_pb.GetSuggestionsReply{Trials: []*api_pb.Trial{}}, err
}
defer conn.Close()
c := api_pb.NewSuggestionClient(conn)
r, err := c.GetSuggestions(ctx, in)
if err != nil {
return &api_pb.GetSuggestionsReply{Trials: []*api_pb.Trial{}}, err
}
return r, nil
}

// Validate AlgorithmSettings in an Experiment.
// Suggestion service should return INVALID_ARGUMENT Error when the parameter is invalid
func (s *server) ValidateAlgorithmSettings(context.Context, *api_pb.ValidateAlgorithmSettingsRequest) (*api_pb.ValidateAlgorithmSettingsReply, error) {
return nil, nil
func (s *server) ValidateAlgorithmSettings(ctx context.Context, in *api_pb.ValidateAlgorithmSettingsRequest) (*api_pb.ValidateAlgorithmSettingsReply, error) {
conn, err := s.getSuggestionServiceConnection(in.AlgorithmName)
if err != nil {
return &api_pb.ValidateAlgorithmSettingsReply{}, err
}
defer conn.Close()
c := api_pb.NewSuggestionClient(conn)
return c.ValidateAlgorithmSettings(ctx, in)
}

func (s *server) Check(context.Context, *health_pb.HealthCheckRequest) (*health_pb.HealthCheckResponse, error) {
return nil, nil
func (s *server) Check(ctx context.Context, in *health_pb.HealthCheckRequest) (*health_pb.HealthCheckResponse, error) {
resp := health_pb.HealthCheckResponse{
Status: health_pb.HealthCheckResponse_SERVING,
}

// We only accept optional service name only if it's set to suggested format.
if in != nil && in.Service != "" && in.Service != "grpc.health.v1.Health" {
resp.Status = health_pb.HealthCheckResponse_UNKNOWN
return &resp, fmt.Errorf("grpc.health.v1.Health can only be accepted if you specify service name.")
}

// Check if connection to katib-db is okay since otherwise manager could not serve most of its methods.
err := dbIf.SelectOne()
if err != nil {
resp.Status = health_pb.HealthCheckResponse_NOT_SERVING
return &resp, fmt.Errorf("Failed to execute `SELECT 1` probe: %v", err)
}

return &resp, nil
}

func main() {
Expand Down
Loading