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

Refactor grpc server creation in own package #1487

Merged
merged 9 commits into from
Apr 26, 2019

Conversation

stefanvassilev
Copy link
Contributor

Signed-off-by: stefan vassilev [email protected]

Which problem is this PR solving?

Short description of the changes

  • Move creation of grpcserver to grpcserver package

@codecov
Copy link

codecov bot commented Apr 23, 2019

Codecov Report

Merging #1487 into master will decrease coverage by 0.02%.
The diff coverage is 96.66%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1487      +/-   ##
==========================================
- Coverage   99.81%   99.79%   -0.03%     
==========================================
  Files         181      182       +1     
  Lines        8652     8711      +59     
==========================================
+ Hits         8636     8693      +57     
- Misses          8        9       +1     
- Partials        8        9       +1
Impacted Files Coverage Δ
cmd/query/app/grpc_handler.go 97.29% <100%> (ø) ⬆️
cmd/query/app/server.go 96.61% <96.61%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 76c0164...7d3b922. Read the comment docs.

Copy link
Member

@yurishkuro yurishkuro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good start!

// Prepare cmux conn.
conn, err := net.Listen("tcp", fmt.Sprintf(":%d", g.QueryOptions.Port))
if err != nil {
g.Logger.Fatal("Could not start listener", zap.Error(err))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a reusable package, it's better to return errors than to log them (main will log)

QuerySvc querysvc.QueryService
Logger *zap.Logger
Tracer opentracing.Tracer
QueryOptions *app.QueryOptions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like only Port is used, I would pass the Port explicitly and narrow the API surface

// See the License for the specific language governing permissions and
// limitations under the License.

package grpcserver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not, strictly speaking, a grpc server, I would call it just "server". It can go under app/server.go

}

// Build a GrpcServer
func (g *Builder) Build() *GrpcServer {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the value in separating Build() and Start()? Maybe just have a single Server struct and Start() function.

// Start http, GRPC and cmux servers concurrently
func (s *GrpcServer) Start() {

logger := s.logger // shortcut
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not necessary in new code

@@ -122,58 +117,17 @@ func main() {
compressHandler := handlers.CompressHandler(r)
recoveryHandler := recoveryhandler.NewRecoveryHandler(logger, true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like L102-118 could also be refactored into httpserver pkg, and reused in all-in-one


func (g *Builder) createHandler() *app.GRPCHandler {
return app.NewGRPCHandler(g.QuerySvc, g.Logger, g.Tracer)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to have similar function for HTTP Handler.

Svc *flags.Service
RecoveryHandler http.Handler
QuerySvc querysvc.QueryService
Logger *zap.Logger
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Svc includes logger

// Builder provides the configuration used to build a GrpcServer
type Builder struct {
Svc *flags.Service
RecoveryHandler http.Handler
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"recovery" is not relevant here, it's just HTTP handler.

I would prefer symmetry between GRPC and HTTP, i.e. either we accept fully instantiated handlers, OR we create both of them in this class (the latter is better, can share code w/ all-in-one)

if err := s.httpServer.Serve(s.listeners[httpListener]); err != nil {
logger.Fatal("Could not start HTTP server", zap.Error(err))
}
s.svc.HC().Set(healthcheck.Unavailable)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a goal of this PR, but we should really revisit this, because it's feasible that one of the servers will fail to start and set HC=not-healthy, while the others would start up fine and main will set HC to OK.

The goroutines should just be signaling to main() that they failed, and main can set the healthcheck.

Maybe add a TODO to fix this separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the docs for logger.Fatal it says that

// ....
// The logger then calls os.Exit(1), even if logging at FatalLevel is

wouldn't that mean that anything below the log is dead code and won't be executed? If so should we log in the goroutine with fatal or just have a mechanism for propagation of the errs to main(), where they'll be logged.

@stefanvassilev stefanvassilev force-pushed the grpc-creation branch 2 times, most recently from a982dcd to 0a8c857 Compare April 24, 2019 16:54
Signed-off-by: stefan vassilev <[email protected]>
Signed-off-by: stefan vassilev <[email protected]>
Resolves racing condition when healthcheck's status is set to available
after it is set to unavailable by server initialization process

Signed-off-by: stefan vassilev <[email protected]>
cmd/query/main.go Show resolved Hide resolved
@@ -49,18 +49,23 @@ type Service struct {
MetricsFactory metrics.Factory

signalsChannel chan os.Signal

// HcStatusChannel is used for error propagation
HcStatusChannel chan healthcheck.Status
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this doesn't need to be exposed publicly, instead you can have a method SetHealthCheckStatus() - assuming this is how we want goroutines to interact with Service

cmd/query/app/server.go Show resolved Hide resolved
cmd/flags/service.go Show resolved Hide resolved
}

// NewServer creates and initializes Server
func NewServer(svc *flags.Service, router *mux.Router, querySvc querysvc.QueryService, tracker opentracing.Tracer, port int) (*Server, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tracker -> tracer

httpListener: cmuxServer.Match(cmux.Any()),
httpServer: createHTTPServer(router, svc.Logger),
muxServer: cmuxServer,
querySvc: querysvc.QueryService{},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

querysvc.QueryService{} ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my IDE does this sometimes, my bad

err := flagsSvc.Admin.Serve()
assert.NoError(t, err)
flagsSvc.Logger = zap.NewNop()
go flagsSvc.RunAndThen(func() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens here?

Signed-off-by: stefan vassilev <[email protected]>
Yuri Shkuro added 2 commits April 26, 2019 12:45
Signed-off-by: Yuri Shkuro <[email protected]>
Signed-off-by: Yuri Shkuro <[email protected]>
@yurishkuro
Copy link
Member

build is failing on gosec #1501

@yurishkuro yurishkuro merged commit bb786ae into jaegertracing:master Apr 26, 2019
@yurishkuro
Copy link
Member

Thanks for PR!

@stefanvassilev stefanvassilev deleted the grpc-creation branch April 26, 2019 19:11
@yurishkuro
Copy link
Member

Follow-up: #1503

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[query] Refactor HTTP/gRPC server creation out of main()
3 participants