Skip to content

Commit

Permalink
debug page for viewing tabletbalancer state
Browse files Browse the repository at this point in the history
Signed-off-by: Venkatraju V <[email protected]>
  • Loading branch information
venkatraju committed Jun 25, 2024
1 parent a8a10aa commit 22318c4
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
15 changes: 15 additions & 0 deletions go/vt/vtgate/balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"math/rand"
"net/http"
"sync"

"vitess.io/vitess/go/vt/discovery"
Expand Down Expand Up @@ -89,6 +90,9 @@ converge on the desired balanced query load.
type TabletBalancer interface {
// Randomly shuffle the tablets into an order for routing queries
ShuffleTablets(target *querypb.Target, tablets []*discovery.TabletHealth)

// Balancer debug page request
DebugHandler(w http.ResponseWriter, r *http.Request)
}

func NewTabletBalancer(localCell string, vtGateCells []string) TabletBalancer {
Expand Down Expand Up @@ -146,6 +150,17 @@ func (b *tabletBalancer) print() string {
b.localCell, b.vtGateCells, string(allocations))
}

func (b *tabletBalancer) DebugHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "text/plain")
fmt.Fprintf(w, "Local Cell: %v\r\n", b.localCell)
fmt.Fprintf(w, "Vtgate Cells: %v\r\n", b.vtGateCells)

b.mu.Lock()
defer b.mu.Unlock()
allocations, _ := json.MarshalIndent(b.allocations, "", " ")
fmt.Fprintf(w, "Allocations: %v\r\n", string(allocations))
}

// ShuffleTablets is the main entry point to the balancer.
//
// It shuffles the tablets into a preference list for routing a given query.
Expand Down
10 changes: 10 additions & 0 deletions go/vt/vtgate/tabletgateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"math/rand"
"net/http"
"sort"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -257,6 +258,15 @@ func (gw *TabletGateway) CacheStatus() TabletCacheStatusList {
return res
}

func (gw *TabletGateway) DebugBalancerHandler(w http.ResponseWriter, r *http.Request) {
if balancerEnabled {
gw.balancer.DebugHandler(w, r)
} else {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("not enabled"))
}
}

// withRetry gets available connections and executes the action. If there are retryable errors,
// it retries retryCount times before failing. It does not retry if the connection is in
// the middle of a transaction. While returning the error check if it maybe a result of
Expand Down
7 changes: 7 additions & 0 deletions go/vt/vtgate/vtgate.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ func Init(
})
rpcVTGate.registerDebugHealthHandler()
rpcVTGate.registerDebugEnvHandler()
rpcVTGate.registerDebugBalancerHandler()
err := initQueryLogger(rpcVTGate)
if err != nil {
log.Fatalf("error initializing query logger: %v", err)
Expand Down Expand Up @@ -369,6 +370,12 @@ func (vtg *VTGate) registerDebugHealthHandler() {
})
}

func (vtg *VTGate) registerDebugBalancerHandler() {
http.HandleFunc("/debug/balancer", func(w http.ResponseWriter, r *http.Request) {
vtg.Gateway().DebugBalancerHandler(w, r)
})
}

// IsHealthy returns nil if server is healthy.
// Otherwise, it returns an error indicating the reason.
func (vtg *VTGate) IsHealthy() error {
Expand Down

0 comments on commit 22318c4

Please sign in to comment.