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

Support PD with Dashboard #2086

Merged
merged 15 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ default*
.vscode/
bindata.go
/pkg/ui/pd-web/*
/.dashboard_asset_cache
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ INTEGRATION_TEST_PKGS := $(shell find . -iname "*_test.go" -exec dirname {} \; |
sort -u | sed -e "s/^\./github.com\/pingcap\/pd/" | grep -E "tests")
BASIC_TEST_PKGS := $(filter-out $(INTEGRATION_TEST_PKGS),$(TEST_PKGS))

PACKAGES := go list ./...
PACKAGES := go list ./... | grep -v 'dashboard/uiserver'
breezewish marked this conversation as resolved.
Show resolved Hide resolved
PACKAGE_DIRECTORIES := $(PACKAGES) | sed 's|github.com/pingcap/pd/||'
GOCHECKER := awk '{ print } END { if (NR > 0) { exit 1 } }'
RETOOL := ./scripts/retool
Expand Down Expand Up @@ -53,13 +53,16 @@ tools: pd-tso-bench pd-recover pd-analysis pd-heartbeat-bench
pd-server: export GO111MODULE=on
ifeq ("$(WITH_RACE)", "1")
pd-server:
./scripts/embed-dashboard-ui.sh
CGO_ENABLED=1 go build -race -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o bin/pd-server cmd/pd-server/main.go
else ifeq ("$(PD_WEB)", "1")
pd-server: retool-setup
./scripts/embed-dashboard-ui.sh
@./scripts/build-ui.sh
CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags web -o bin/pd-server cmd/pd-server/main.go
else
pd-server:
./scripts/embed-dashboard-ui.sh
CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o bin/pd-server cmd/pd-server/main.go
endif

Expand Down Expand Up @@ -104,7 +107,7 @@ check-all: static lint tidy
@echo "checking"

retool-setup: export GO111MODULE=off
retool-setup:
retool-setup:
@which retool >/dev/null 2>&1 || go get github.com/twitchtv/retool
@./scripts/retool sync

Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ require (
github.com/golang/protobuf v1.3.2
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db // indirect
github.com/google/btree v1.0.0
github.com/gorilla/context v0.0.0-20160226214623-1ea25387ff6f // indirect
github.com/gorilla/mux v1.6.1
github.com/gorilla/mux v1.7.3
github.com/gorilla/websocket v1.2.0 // indirect
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
github.com/juju/ratelimit v1.0.1
Expand All @@ -29,6 +28,7 @@ require (
github.com/onsi/gomega v1.4.2 // indirect
github.com/opentracing/opentracing-go v1.0.2
github.com/phf/go-queue v0.0.0-20170504031614-9abe38d0371d
github.com/pingcap-incubator/tidb-dashboard v0.0.0-20200110133619-1c1c65dd8750
github.com/pingcap/check v0.0.0-20191107115940-caf2b9e6ccf4
github.com/pingcap/errcode v0.0.0-20180921232412-a1a7271709d9
github.com/pingcap/failpoint v0.0.0-20191029060244-12f4ac2fd11d
Expand All @@ -47,6 +47,7 @@ require (
go.etcd.io/etcd v0.5.0-alpha.5.0.20191023171146-3cf2f69b5738
go.uber.org/goleak v0.10.0
go.uber.org/zap v1.12.0
golang.org/x/sys v0.0.0-20200113162924-86b910548bc1 // indirect
google.golang.org/grpc v1.25.1
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)
117 changes: 113 additions & 4 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/dashboard/uiserver/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/embedded_assets_handler.go
24 changes: 24 additions & 0 deletions pkg/dashboard/uiserver/uiserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package uiserver

import (
"net/http"
)

// Handler returns an http.Handler that serves the dashboard UI.
func Handler() http.Handler {
fileServer := http.FileServer(assetFS())
return fileServer
}
43 changes: 43 additions & 0 deletions scripts/embed-dashboard-ui.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash
set -euo pipefail

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
BASE_DIR="$(dirname "$DIR")"
CACHE_DIR=${BASE_DIR}/.dashboard_asset_cache

echo '+ Create cache directory'
mkdir -p "${CACHE_DIR}"

echo '+ Fetch Dashboard Go module'
go mod download
breezewish marked this conversation as resolved.
Show resolved Hide resolved

echo '+ Discover Dashboard UI version'

DASHBOARD_DIR=$(go list -f "{{.Dir}}" -m github.com/pingcap-incubator/tidb-dashboard)
echo " - Dashboard directory: ${DASHBOARD_DIR}"

DASHBOARD_UI_VERSION=$(grep -v '^#' "${DASHBOARD_DIR}/ui/.github_release_version")
echo " - Dashboard ui version: ${DASHBOARD_UI_VERSION}"

echo '+ Check embedded assets exists in cache'
CACHE_FILE=${CACHE_DIR}/embedded-assets-golang-${DASHBOARD_UI_VERSION}.zip
if [[ -f "$CACHE_FILE" ]]; then
echo " - Cached archive exists: ${CACHE_FILE}"
else
echo ' - Cached archive does not exist'
echo ' - Download pre-built embedded assets from GitHub release'

DOWNLOAD_URL="https://github.com/pingcap-incubator/tidb-dashboard/releases/download/ui_release_${DASHBOARD_UI_VERSION}/embedded-assets-golang.zip"
echo " - Download ${DOWNLOAD_URL}"
curl -L "${DOWNLOAD_URL}" > embedded-assets-golang.zip

echo " - Save archive to cache: ${CACHE_FILE}"
mv embedded-assets-golang.zip "${CACHE_FILE}"
fi

echo '+ Unpack embedded asset from archive'
unzip -o "${CACHE_FILE}"
MOVE_FILE=embedded_assets_handler.go
MOVE_DEST=pkg/dashboard/uiserver/${MOVE_FILE}
mv ${MOVE_FILE} ${MOVE_DEST}
echo " - Unpacked ${MOVE_DEST}"
4 changes: 4 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ type Config struct {
logProps *log.ZapProperties

EnableConfigManager bool

EnableDashboard bool
HunDunDM marked this conversation as resolved.
Show resolved Hide resolved
}

// NewConfig creates a new config.
Expand Down Expand Up @@ -165,6 +167,8 @@ func NewConfig() *Config {

fs.BoolVar(&cfg.EnableConfigManager, "enable-config-manager", false, "Enable configuration manager")

fs.BoolVar(&cfg.EnableDashboard, "enable-dashboard", true, "Enable Dashboard API and UI on this node")

return cfg
}

Expand Down
23 changes: 19 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ import (
"github.com/coreos/go-semver/semver"
"github.com/golang/protobuf/proto"
"github.com/gorilla/mux"
"github.com/pingcap-incubator/tidb-dashboard/pkg/apiserver"
dashboardConfig "github.com/pingcap-incubator/tidb-dashboard/pkg/config"
"github.com/pingcap/failpoint"
"github.com/pingcap/kvproto/pkg/configpb"
"github.com/pingcap/kvproto/pkg/diagnosticspb"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/pingcap/kvproto/pkg/pdpb"
"github.com/pingcap/log"
"github.com/pingcap/pd/pkg/dashboard/uiserver"
"github.com/pingcap/pd/pkg/etcdutil"
"github.com/pingcap/pd/pkg/grpcutil"
"github.com/pingcap/pd/pkg/logutil"
Expand Down Expand Up @@ -65,10 +68,12 @@ const (
serverMetricsInterval = time.Minute
leaderTickInterval = 50 * time.Millisecond
// pdRootPath for all pd servers.
pdRootPath = "/pd"
pdAPIPrefix = "/pd/"
webPath = "/web/"
pdClusterIDPath = "/pd/cluster_id"
pdRootPath = "/pd"
pdAPIPrefix = "/pd/"
webPath = "/web/"
dashboardUIPath = "/dashboard/"
dashboardAPIPath = "/dashboard/api/"
pdClusterIDPath = "/pd/cluster_id"
nolouch marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand Down Expand Up @@ -210,6 +215,16 @@ func CreateServer(ctx context.Context, cfg *config.Config, apiBuilders ...Handle
pdAPIPrefix: apiHandler,
webPath: http.StripPrefix(webPath, ui.Handler()),
}

if cfg.EnableDashboard {
HunDunDM marked this conversation as resolved.
Show resolved Hide resolved
etcdCfg.UserHandlers[dashboardUIPath] = http.StripPrefix(dashboardUIPath, uiserver.Handler())
etcdCfg.UserHandlers[dashboardAPIPath] = apiserver.Handler(dashboardAPIPath, &dashboardConfig.Config{
DataDir: cfg.DataDir,
PDEndPoint: etcdCfg.ACUrls[0].String(),
})
log.Info("Enabled Dashboard API", zap.String("path", dashboardAPIPath))
log.Info("Enabled Dashboard UI", zap.String("path", dashboardUIPath))
}
}
etcdCfg.ServiceRegister = func(gs *grpc.Server) {
pdpb.RegisterPDServer(gs, s)
Expand Down