You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/10/06 16:13:46 UTC

[GitHub] [beam] damondouglas commented on a change in pull request #15654: [BEAM-12998] [Playground] Environment service

damondouglas commented on a change in pull request #15654:
URL: https://github.com/apache/beam/pull/15654#discussion_r723369806



##########
File path: playground/backend/internal/environment/environment_service.go
##########
@@ -0,0 +1,98 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package environment
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"google.golang.org/grpc/grpclog"
+	"os"
+	"strconv"
+)
+
+const (
+	serverIpKey   = "SERVER_IP"
+	serverPortKey = "SERVER_PORT"
+	beamSdkKey    = "BEAM_SDK"
+	defaultIp     = "localhost"
+	defaultPort   = 8080
+	defaultSdk    = pb.Sdk_SDK_JAVA
+)
+
+// Service operates with environment structures: ServerEnvs, LogWriters, BeamEnvs
+type Service struct {
+	ServerEnvs  ServerEnvs
+	LogWriters  LogWriters
+	BeamSdkEnvs BeamEnvs
+}
+
+// NewService is a constructor for Service.
+// Default values:
+// LogWriters: by default using os.Stdout
+// ServerEnvs: by default using defaultIp and defaultPort from constants
+// BeamEnvs: by default using pb.Sdk_SDK_JAVA
+func NewService(writers *LogWriters) *Service {
+	if writers == nil {
+		writers = NewLogWriters(os.Stdout, os.Stdout, os.Stderr)
+	}
+	svc := Service{LogWriters: *writers}
+	svc.ServerEnvs = getServerEnvsFromOsEnvs()
+	svc.BeamSdkEnvs = getSdkEnvsFromOsEnvs()
+
+	return &svc
+}
+
+// getServerEnvsFromOsEnvs lookups in os environment variables and takes value for ip and port. If not exists - using default
+func getServerEnvsFromOsEnvs() ServerEnvs {

Review comment:
       May we return `*ServerEnvs` pointer instead?

##########
File path: playground/backend/internal/environment/environment_service.go
##########
@@ -0,0 +1,98 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package environment
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"google.golang.org/grpc/grpclog"
+	"os"
+	"strconv"
+)
+
+const (
+	serverIpKey   = "SERVER_IP"
+	serverPortKey = "SERVER_PORT"
+	beamSdkKey    = "BEAM_SDK"
+	defaultIp     = "localhost"
+	defaultPort   = 8080
+	defaultSdk    = pb.Sdk_SDK_JAVA
+)
+
+// Service operates with environment structures: ServerEnvs, LogWriters, BeamEnvs
+type Service struct {
+	ServerEnvs  ServerEnvs
+	LogWriters  LogWriters
+	BeamSdkEnvs BeamEnvs
+}
+
+// NewService is a constructor for Service.
+// Default values:
+// LogWriters: by default using os.Stdout
+// ServerEnvs: by default using defaultIp and defaultPort from constants
+// BeamEnvs: by default using pb.Sdk_SDK_JAVA
+func NewService(writers *LogWriters) *Service {
+	if writers == nil {
+		writers = NewLogWriters(os.Stdout, os.Stdout, os.Stderr)
+	}
+	svc := Service{LogWriters: *writers}
+	svc.ServerEnvs = getServerEnvsFromOsEnvs()
+	svc.BeamSdkEnvs = getSdkEnvsFromOsEnvs()
+
+	return &svc
+}
+
+// getServerEnvsFromOsEnvs lookups in os environment variables and takes value for ip and port. If not exists - using default
+func getServerEnvsFromOsEnvs() ServerEnvs {
+	ip := defaultIp
+	port := defaultPort
+	if value, present := os.LookupEnv(serverIpKey); present {
+		ip = value
+	}
+
+	if value, present := os.LookupEnv(serverPortKey); present {
+		if converted, err := strconv.Atoi(value); err == nil {
+			port = converted
+		} else {
+			grpclog.Errorf("couldn't convert provided port. Using default %s\n", defaultPort)
+		}
+	}
+
+	return *NewServerEnvs(ip, port)
+}
+
+// getServerEnvsFromOsEnvs lookups in os environment variables and takes value for Apache Beam SDK. If not exists - using default
+func getSdkEnvsFromOsEnvs() BeamEnvs {

Review comment:
       May we return `*BeamEnvs` instead?

##########
File path: playground/backend/internal/environment/environment_service_test.go
##########
@@ -0,0 +1,96 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package environment
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"os"
+	"reflect"
+	"testing"
+)
+
+func setOsEnvs(envsToSet map[string]string) {
+	for key, value := range envsToSet {
+		if err := os.Setenv(key, value); err != nil {
+			panic(err)

Review comment:
       Should we return the error instead of panicing?

##########
File path: playground/backend/internal/environment/logs.go
##########
@@ -0,0 +1,30 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package environment
+
+import "io"
+
+// LogWriters contains writes for logger. Writers should implement io.Writer interface.
+type LogWriters struct {

Review comment:
       May we consider having a separate `internal/log` package?  I like that we are centralizing logging and I would additionally propose enforcing structured logging.  I've seen two approaches.  One, using the uber zap library.  Another using the native go library but logging out a struct adherent to https://pkg.go.dev/google.golang.org/cloud/logging#Entry.  In the second case, just the `Entry` is populated and written out to stdout or stderr without using `logging.Client`.  In GCP, structured logs are automatically parsed with the applied key/value pairs.  I've used both approaches and found the second more flexible as there are settings where one just wants a single line string in the log.

##########
File path: playground/backend/internal/environment/network.go
##########
@@ -0,0 +1,34 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package environment
+
+import "fmt"
+
+// ServerEnvs contains all environment variables that need to run server.
+type ServerEnvs struct {
+	ip   string
+	port int
+}
+
+// NewServerEnvs constructor for ServerEnvs
+func NewServerEnvs(ip string, port int) *ServerEnvs {
+	return &ServerEnvs{ip: ip, port: port}
+}
+
+// GetAddress returns concatenated ip and port through ':'
+func (serverEnvs ServerEnvs) GetAddress() string {

Review comment:
       Instead of `GetAddress()` may we rename to `Address()`?

##########
File path: playground/backend/internal/environment/environment_service.go
##########
@@ -0,0 +1,98 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You under the Apache License, Version 2.0
+// (the "License"); you may not use this file except in compliance with
+// the License.  You may obtain a copy of the License at
+//
+//    http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package environment
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"google.golang.org/grpc/grpclog"
+	"os"
+	"strconv"
+)
+
+const (
+	serverIpKey   = "SERVER_IP"
+	serverPortKey = "SERVER_PORT"
+	beamSdkKey    = "BEAM_SDK"
+	defaultIp     = "localhost"
+	defaultPort   = 8080
+	defaultSdk    = pb.Sdk_SDK_JAVA
+)
+
+// Service operates with environment structures: ServerEnvs, LogWriters, BeamEnvs
+type Service struct {

Review comment:
       I was having difficulty understanding the responsibility of this struct given its package context.  Is its purpose to hold environment state in-memory or to do work using values from the environment?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org