Skip to content

Commit

Permalink
ringhash: the balancer (#4741)
Browse files Browse the repository at this point in the history
  • Loading branch information
menghanl committed Sep 15, 2021
1 parent 4c5f7fb commit 4f093b9
Show file tree
Hide file tree
Showing 5 changed files with 938 additions and 32 deletions.
34 changes: 34 additions & 0 deletions xds/internal/balancer/ringhash/logging.go
@@ -0,0 +1,34 @@
/*
*
* Copyright 2021 gRPC authors.
*
* Licensed 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 ringhash

import (
"fmt"

"google.golang.org/grpc/grpclog"
internalgrpclog "google.golang.org/grpc/internal/grpclog"
)

const prefix = "[ring-hash-lb %p] "

var logger = grpclog.Component("xds")

func prefixLogger(p *ringhashBalancer) *internalgrpclog.PrefixLogger {
return internalgrpclog.NewPrefixLogger(logger, fmt.Sprintf(prefix, p))
}
17 changes: 11 additions & 6 deletions xds/internal/balancer/ringhash/picker.go
Expand Up @@ -24,11 +24,17 @@ import (
"google.golang.org/grpc/balancer"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/internal/grpclog"
"google.golang.org/grpc/status"
)

type picker struct {
ring *ring
ring *ring
logger *grpclog.PrefixLogger
}

func newPicker(ring *ring, logger *grpclog.PrefixLogger) *picker {
return &picker{ring: ring, logger: logger}
}

// handleRICSResult is the return type of handleRICS. It's needed to wrap the
Expand All @@ -47,7 +53,7 @@ type handleRICSResult struct {
// The first return value indicates if the state is in Ready, Idle, Connecting
// or Shutdown. If it's true, the PickResult and error should be returned from
// Pick() as is.
func handleRICS(e *ringEntry) (handleRICSResult, bool) {
func (p *picker) handleRICS(e *ringEntry) (handleRICSResult, bool) {
switch state := e.sc.effectiveState(); state {
case connectivity.Ready:
return handleRICSResult{pr: balancer.PickResult{SubConn: e.sc.sc}}, true
Expand All @@ -67,15 +73,14 @@ func handleRICS(e *ringEntry) (handleRICSResult, bool) {
default:
// Should never reach this. All the connectivity states are already
// handled in the cases.
//
// FIXME: add an error log.
p.logger.Errorf("SubConn has undefined connectivity state: %v", state)
return handleRICSResult{err: status.Errorf(codes.Unavailable, "SubConn has undefined connectivity state: %v", state)}, true
}
}

func (p *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
e := p.ring.pick(getRequestHash(info.Ctx))
if hr, ok := handleRICS(e); ok {
if hr, ok := p.handleRICS(e); ok {
return hr.pr, hr.err
}
// ok was false, the entry is in transient failure.
Expand All @@ -95,7 +100,7 @@ func (p *picker) handleTransientFailure(e *ringEntry) (balancer.PickResult, erro

// For the second SubConn, also check Ready/Idle/Connecting as if it's the
// first entry.
if hr, ok := handleRICS(e2); ok {
if hr, ok := p.handleRICS(e2); ok {
return hr.pr, hr.err
}

Expand Down

0 comments on commit 4f093b9

Please sign in to comment.