From 762589ea1be0a61d383bc557930d2ac7e552e08d Mon Sep 17 00:00:00 2001 From: David Schall Date: Wed, 2 Aug 2023 02:31:55 -0600 Subject: [PATCH] Remove Error handling Instead of failing right away the grpcclient returns an empty message and the error. Now the client (relay) that implements the grpcclient is responsibe to handle the error Signed-off-by: David Schall --- grpcclient/aes_client.go | 8 ++--- grpcclient/auth_client.go | 7 ++-- grpcclient/fibonacci_client.go | 7 ++-- grpcclient/grpcclient.go | 4 +-- grpcclient/helloworld_client.go | 7 ++-- grpcclient/hipstershop_clients.go | 57 +++++++++++++++---------------- grpcclient/hotel_clients.go | 42 +++++++++++------------ 7 files changed, 63 insertions(+), 69 deletions(-) diff --git a/grpcclient/aes_client.go b/grpcclient/aes_client.go index 4a263f1..b25a921 100644 --- a/grpcclient/aes_client.go +++ b/grpcclient/aes_client.go @@ -4,7 +4,6 @@ import ( "context" "math/rand" - log "github.com/sirupsen/logrus" pb "github.com/vhive-serverless/vSwarm-proto/proto/aes" ) @@ -50,15 +49,14 @@ type AesClient struct { } func (c *AesClient) Init(ctx context.Context, ip, port string) { - log.Printf("Connect to: %s:%s\n", ip, port) c.Connect(ctx, ip, port) c.client = pb.NewAesClient(c.conn) } -func (c *AesClient) Request(ctx context.Context, req Input) string { +func (c *AesClient) Request(ctx context.Context, req Input) (string, error) { r, err := c.client.ShowEncryption(ctx, &pb.PlainTextMessage{PlaintextMessage: req.Value}) if err != nil { - log.Fatalf("could not greet: %v", err) + return "", err } - return r.GetEncryptionInfo() + return r.GetEncryptionInfo(), nil } diff --git a/grpcclient/auth_client.go b/grpcclient/auth_client.go index eb97bea..916f73a 100644 --- a/grpcclient/auth_client.go +++ b/grpcclient/auth_client.go @@ -4,7 +4,6 @@ import ( "context" "math/rand" - log "github.com/sirupsen/logrus" pb "github.com/vhive-serverless/vSwarm-proto/proto/auth" ) @@ -43,11 +42,11 @@ func (c *AuthClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewGreeterClient(c.conn) } -func (c *AuthClient) Request(ctx context.Context, req Input) string { +func (c *AuthClient) Request(ctx context.Context, req Input) (string, error) { var authMessage = req.Value r, err := c.client.SayHello(ctx, &pb.HelloRequest{Name: authMessage}) if err != nil { - log.Fatalf("could not greet: %v", err) + return "", err } - return r.GetMessage() + return r.GetMessage(), nil } diff --git a/grpcclient/fibonacci_client.go b/grpcclient/fibonacci_client.go index 37c0058..f1a3a82 100644 --- a/grpcclient/fibonacci_client.go +++ b/grpcclient/fibonacci_client.go @@ -6,7 +6,6 @@ import ( "math/rand" pb "github.com/vhive-serverless/vSwarm-proto/proto/fibonacci" - log "github.com/sirupsen/logrus" ) type FibonacciGenerator struct { @@ -42,11 +41,11 @@ func (c *FibonacciClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewGreeterClient(c.conn) } -func (c *FibonacciClient) Request(ctx context.Context, req Input) string { +func (c *FibonacciClient) Request(ctx context.Context, req Input) (string, error) { var fibonacciMessage = req.Value r, err := c.client.SayHello(ctx, &pb.HelloRequest{Name: fibonacciMessage}) if err != nil { - log.Fatalf("could not greet: %v", err) + return "", err } - return r.GetMessage() + return r.GetMessage(), nil } diff --git a/grpcclient/grpcclient.go b/grpcclient/grpcclient.go index 65a00ee..c7704e8 100644 --- a/grpcclient/grpcclient.go +++ b/grpcclient/grpcclient.go @@ -78,7 +78,7 @@ func (s *GeneratorBase) Decrement() int { // Every client must implement this interface type GrpcClient interface { Init(ctx context.Context, ip, port string) - Request(ctx context.Context, req Input) string + Request(ctx context.Context, req Input) (string, error) Close() GetGenerator() Generator } @@ -95,7 +95,7 @@ func (c *ClientBase) Connect(ctx context.Context, ip, port string) { c.port = port // Connect to the given address address := fmt.Sprintf("%s:%s", c.ip, c.port) - log.Debug("Connect to ", address) + log.Println("Connect to ", address) var conn *grpc.ClientConn var err error if tracing.IsTracingEnabled() { diff --git a/grpcclient/helloworld_client.go b/grpcclient/helloworld_client.go index 9ef1317..f2b8f5e 100644 --- a/grpcclient/helloworld_client.go +++ b/grpcclient/helloworld_client.go @@ -4,7 +4,6 @@ import ( "context" pb "github.com/vhive-serverless/vSwarm-proto/proto/helloworld" - log "github.com/sirupsen/logrus" ) // type HelloWorldGenerator struct { @@ -63,10 +62,10 @@ func (c *HelloWorldClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewGreeterClient(c.conn) } -func (c *HelloWorldClient) Request(ctx context.Context, req Input) string { +func (c *HelloWorldClient) Request(ctx context.Context, req Input) (string, error) { r, err := c.client.SayHello(ctx, &pb.HelloRequest{Name: req.Value}) if err != nil { - log.Fatalf("could not greet: %v", err) + return "", err } - return r.GetMessage() + return r.GetMessage(), nil } diff --git a/grpcclient/hipstershop_clients.go b/grpcclient/hipstershop_clients.go index 2a5ed0d..b3116ab 100644 --- a/grpcclient/hipstershop_clients.go +++ b/grpcclient/hipstershop_clients.go @@ -5,8 +5,8 @@ import ( "fmt" "strconv" - pb "github.com/vhive-serverless/vSwarm-proto/proto/hipstershop" log "github.com/sirupsen/logrus" + pb "github.com/vhive-serverless/vSwarm-proto/proto/hipstershop" ) var ( @@ -76,7 +76,7 @@ func (c *ShopAdServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewAdServiceClient(c.conn) } -func (c *ShopAdServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopAdServiceClient) Request(ctx context.Context, req Input) (string, error) { payload := req.Value // Create a default forward request @@ -86,12 +86,12 @@ func (c *ShopAdServiceClient) Request(ctx context.Context, req Input) string { fw_res, err := c.client.GetAds(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Ad service: %v", err) + return "", err } msg := fmt.Sprintf("%+v", fw_res) // log.Println(msg) - return msg + return msg, nil } type ShopAdServiceGenerator struct { @@ -119,7 +119,7 @@ func (c *ShopCartServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewCartServiceClient(c.conn) } -func (c *ShopCartServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopCartServiceClient) Request(ctx context.Context, req Input) (string, error) { fw_method := req.Method payload := req.Value @@ -158,12 +158,12 @@ func (c *ShopCartServiceClient) Request(ctx context.Context, req Input) string { } if err != nil { - log.Fatalf("Fail to invoke Cart service: %v", err) + return "", err } msg = fmt.Sprintf("method: %s, %s", fw_method, msg) // log.Println(msg) - return msg + return msg, nil } type ShopCartServiceGenerator struct { @@ -191,7 +191,7 @@ func (c *ShopCheckoutServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewCheckoutServiceClient(c.conn) } -func (c *ShopCheckoutServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopCheckoutServiceClient) Request(ctx context.Context, req Input) (string, error) { // Pass on to the real service function payload := req.Value @@ -207,12 +207,12 @@ func (c *ShopCheckoutServiceClient) Request(ctx context.Context, req Input) stri fw_res, err := c.client.PlaceOrder(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Checkout service: %v", err) + return "", err } msg := fmt.Sprintf("%+v", fw_res) // log.Println(msg) - return msg + return msg, nil } type ShopCheckoutServiceGenerator struct { @@ -240,7 +240,7 @@ func (c *ShopCurrencyServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewCurrencyServiceClient(c.conn) } -func (c *ShopCurrencyServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopCurrencyServiceClient) Request(ctx context.Context, req Input) (string, error) { fw_method, payload := req.Method, req.Value // Pass on to the real service function @@ -271,12 +271,12 @@ func (c *ShopCurrencyServiceClient) Request(ctx context.Context, req Input) stri log.Fatalf("Failed to understand requested method: %s", fw_method) } if err != nil { - log.Fatalf("Fail to invoke Currency service: %v", err) + return "", err } msg = fmt.Sprintf("method: %s, %s", fw_method, msg) // log.Println(msg) - return msg + return msg, nil } type ShopCurrencyServiceGenerator struct { @@ -304,7 +304,7 @@ func (c *ShopEmailServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewEmailServiceClient(c.conn) } -func (c *ShopEmailServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopEmailServiceClient) Request(ctx context.Context, req Input) (string, error) { // Pass on to the real service function // _, payload := getMethodPayload(req) @@ -317,12 +317,11 @@ func (c *ShopEmailServiceClient) Request(ctx context.Context, req Input) string fw_res, err := c.client.SendOrderConfirmation(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Email service: %v", err) + return "", err } - msg := fmt.Sprintf("%+v", fw_res) // log.Println(msg) - return msg + return msg, nil } @@ -351,7 +350,7 @@ func (c *ShopPaymentServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewPaymentServiceClient(c.conn) } -func (c *ShopPaymentServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopPaymentServiceClient) Request(ctx context.Context, req Input) (string, error) { payload := req.Value // Create a default forward request @@ -366,12 +365,12 @@ func (c *ShopPaymentServiceClient) Request(ctx context.Context, req Input) strin fw_res, err := c.client.Charge(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Payment service: %v", err) + return "", err } msg := fmt.Sprintf("%+v", fw_res) // log.Println(msg) - return msg + return msg, nil } type ShopPaymentServiceGenerator struct { @@ -399,7 +398,7 @@ func (c *ShopProductCatalogServiceClient) Init(ctx context.Context, ip, port str c.client = pb.NewProductCatalogServiceClient(c.conn) } -func (c *ShopProductCatalogServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopProductCatalogServiceClient) Request(ctx context.Context, req Input) (string, error) { fw_method := req.Method payload := req.Value @@ -435,12 +434,12 @@ func (c *ShopProductCatalogServiceClient) Request(ctx context.Context, req Input log.Fatalf("Failed to understand requested method: %s", fw_method) } if err != nil { - log.Fatalf("Fail to invoke ProductCatalog service: %v", err) + return "", err } msg = fmt.Sprintf("method: %s, %s", fw_method, msg) // log.Println(msg) - return msg + return msg, nil } type ShopProductCatalogServiceGenerator struct { @@ -468,7 +467,7 @@ func (c *ShopRecommendationServiceClient) Init(ctx context.Context, ip, port str c.client = pb.NewRecommendationServiceClient(c.conn) } -func (c *ShopRecommendationServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopRecommendationServiceClient) Request(ctx context.Context, req Input) (string, error) { payload := req.Value // Create a default forward request @@ -479,11 +478,11 @@ func (c *ShopRecommendationServiceClient) Request(ctx context.Context, req Input fw_res, err := c.client.ListRecommendations(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Recommendation service: %v", err) + return "", err } msg := fmt.Sprintf("%+v", fw_res) // log.Println(msg) - return msg + return msg, nil } type ShopRecommendationServiceGenerator struct { @@ -511,7 +510,7 @@ func (c *ShopShippingServiceClient) Init(ctx context.Context, ip, port string) { c.client = pb.NewShippingServiceClient(c.conn) } -func (c *ShopShippingServiceClient) Request(ctx context.Context, req Input) string { +func (c *ShopShippingServiceClient) Request(ctx context.Context, req Input) (string, error) { fw_method := req.Method @@ -542,12 +541,12 @@ func (c *ShopShippingServiceClient) Request(ctx context.Context, req Input) stri log.Fatalf("Failed to understand requested method: %s", fw_method) } if err != nil { - log.Fatalf("Fail to invoke Shipping service: %v", err) + return "", err } msg = fmt.Sprintf("method: %s, %s", fw_method, msg) // log.Println(msg) - return msg + return msg, nil } type ShopShippingServiceGenerator struct { diff --git a/grpcclient/hotel_clients.go b/grpcclient/hotel_clients.go index 9bae0ef..2ba055d 100644 --- a/grpcclient/hotel_clients.go +++ b/grpcclient/hotel_clients.go @@ -31,7 +31,7 @@ func (c *HotelGeoClient) Init(ctx context.Context, ip, port string) { c.client = geo.NewGeoClient(c.conn) } -func (c *HotelGeoClient) Request(ctx context.Context, req Input) string { +func (c *HotelGeoClient) Request(ctx context.Context, req Input) (string, error) { // Create a default forward request coordinates := strings.Split(req.Value, ",") @@ -45,12 +45,12 @@ func (c *HotelGeoClient) Request(ctx context.Context, req Input) string { fw_res, err := c.client.Nearby(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Geo service: %v", err) + return "Fail to invoke Geo service", err } msg := fmt.Sprintf("req: { Lat:%f Lon:%f} resp: %+v", fw_req.Lon, fw_req.Lat, fw_res) // log.Println(msg) - return msg + return msg, err } type HotelGeoGenerator struct { @@ -92,7 +92,7 @@ func (c *HotelProfileClient) Init(ctx context.Context, ip, port string) { c.client = profile.NewProfileClient(c.conn) } -func (c *HotelProfileClient) Request(ctx context.Context, req Input) string { +func (c *HotelProfileClient) Request(ctx context.Context, req Input) (string, error) { payload := req.Value ids := strings.Split(payload, ",") // Create a forward request @@ -103,12 +103,12 @@ func (c *HotelProfileClient) Request(ctx context.Context, req Input) string { fw_res, err := c.client.GetProfiles(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Profile service: %v", err) + return "", err } msg := fmt.Sprintf("req: {HotelIds: %+v, Locale: \"\"} resp: %+v", ids, fw_res) // log.Println(msg) - return msg + return msg, nil } type HotelProfileGenerator struct { @@ -155,7 +155,7 @@ func (c *HotelRateClient) Init(ctx context.Context, ip, port string) { c.client = rate.NewRateClient(c.conn) } -func (c *HotelRateClient) Request(ctx context.Context, req Input) string { +func (c *HotelRateClient) Request(ctx context.Context, req Input) (string, error) { payload := req.Value ids := strings.Split(payload, ",") // Create a forward request @@ -166,12 +166,12 @@ func (c *HotelRateClient) Request(ctx context.Context, req Input) string { } fw_res, err := c.client.GetRates(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Rate service: %v", err) + return "", err } msg := fmt.Sprintf("req: {HotelIds: %+v, InDate: \"2015-04-09\", OutDate: \"2015-04-11\"}, resp: %+v", ids, fw_res) // log.Println(msg) - return msg + return msg, nil } type HotelRateGenerator struct { @@ -209,7 +209,7 @@ func (c *HotelRecommendationClient) Init(ctx context.Context, ip, port string) { c.client = recommendation.NewRecommendationClient(c.conn) } -func (c *HotelRecommendationClient) Request(ctx context.Context, req Input) string { +func (c *HotelRecommendationClient) Request(ctx context.Context, req Input) (string, error) { // Create a default forward request coordinates := strings.Split(req.Value, ",") @@ -232,12 +232,12 @@ func (c *HotelRecommendationClient) Request(ctx context.Context, req Input) stri fw_res, err := c.client.GetRecommendations(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Recommendation service: %v", err) + return "", err } msg := fmt.Sprintf("req: {Require: \"dis\", Lat:%f Lon:%f}, resp: %+v", fw_req.Lon, fw_req.Lat, fw_res) // log.Println(msg) - return msg + return msg, nil } type HotelRecommendationGenerator struct { @@ -273,7 +273,7 @@ func (c *HotelReservationClient) Init(ctx context.Context, ip, port string) { c.client = reservation.NewReservationClient(c.conn) } -func (c *HotelReservationClient) Request(ctx context.Context, req Input) string { +func (c *HotelReservationClient) Request(ctx context.Context, req Input) (string, error) { fw_method := req.Method payload := strings.Split(req.Value, ",") // Create a default forward request @@ -302,12 +302,12 @@ func (c *HotelReservationClient) Request(ctx context.Context, req Input) string log.Fatalf("Failed to understand requested method: %s", fw_method) } if err != nil { - log.Fatalf("Fail to invoke Reservation service: %v", err) + return "", err } msg := fmt.Sprintf("method: %s, req: {CustomerName: %s, HotelId: %v, InDate: \"2015-04-09\", OutDate: \"2015-04-11\", RoomNumber: 1,} resp: %+v", fw_method, fw_req.CustomerName, fw_req.HotelId, fw_res) // log.Println(msg) - return msg + return msg, nil } type HotelReservationGenerator struct { @@ -350,7 +350,7 @@ func (c *HotelUserClient) Init(ctx context.Context, ip, port string) { c.client = user.NewUserClient(c.conn) } -func (c *HotelUserClient) Request(ctx context.Context, req Input) string { +func (c *HotelUserClient) Request(ctx context.Context, req Input) (string, error) { payload := strings.Split(req.Value, ",") // Create a forward request @@ -361,12 +361,12 @@ func (c *HotelUserClient) Request(ctx context.Context, req Input) string { fw_res, err := c.client.CheckUser(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke User service: %v", err) + return "", err } msg := fmt.Sprintf("req: {Username: %s, Password: %s} resp: Correct:%v", fw_req.Username, fw_req.Password, fw_res.Correct) // log.Println(msg) - return msg + return msg, nil } type HotelUserGenerator struct { @@ -409,7 +409,7 @@ func (c *HotelSearchClient) Init(ctx context.Context, ip, port string) { c.client = search.NewSearchClient(c.conn) } -func (c *HotelSearchClient) Request(ctx context.Context, req Input) string { +func (c *HotelSearchClient) Request(ctx context.Context, req Input) (string, error) { // Create a forward request fw_req := search.NearbyRequest{ Lat: 37.7963, @@ -420,12 +420,12 @@ func (c *HotelSearchClient) Request(ctx context.Context, req Input) string { fw_res, err := c.client.Nearby(ctx, &fw_req) if err != nil { - log.Fatalf("Fail to invoke Search service: %v", err) + return "", err } msg := fmt.Sprintf("req: {Lat: 37.7963, Lon: -122.4015, InDate: \"2015-04-09\", OutDate: \"2015-04-11\"}, resp: %+v", fw_res) // log.Println(msg) - return msg + return msg, nil } type HotelSearchGenerator struct {