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 6 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/*
/.cache
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ pd-server: export GO111MODULE=on
ifeq ("$(WITH_RACE)", "1")
pd-server:
CGO_ENABLED=1 go build -race -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -o bin/pd-server cmd/pd-server/main.go
else ifeq ("$(WITH_DASHBOARD)", "1")
pd-server:
@./scripts/embed-dashboard-ui.sh
CGO_ENABLED=0 go build -gcflags '$(GCFLAGS)' -ldflags '$(LDFLAGS)' -tags dashboard_ui -o bin/pd-server cmd/pd-server/main.go
else ifeq ("$(PD_WEB)", "1")
pd-server: retool-setup
@./scripts/build-ui.sh
Expand Down Expand Up @@ -104,7 +108,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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ PD supports distribution and fault-tolerance by embedding [etcd](https://github.
1. Make sure [​*Go*​](https://golang.org/) (version 1.13) is installed.
2. Use `make` to install PD. PD is installed in the `bin` directory.

### Build with [TiDB Dashboard](https://github.com/pingcap-incubator/tidb-dashboard)

```sh
WITH_DASHBOARD=1 make pd-server
# Start PD: bin/pd-server
```

Dashboard UI is available at http://127.0.0.1:2379/dashboard by default.

## Usage

### Command flags
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-20200108014525-06bfab74f3b7
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-20200107162124-548cf772de50 // 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
23 changes: 23 additions & 0 deletions pkg/dashboard/uiserver/empty_assets_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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.
// +build !dashboard_ui
breezewish marked this conversation as resolved.
Show resolved Hide resolved

package uiserver

import (
assetfs "github.com/elazarl/go-bindata-assetfs"
)

func assetFS() *assetfs.AssetFS {
return nil
}
32 changes: 32 additions & 0 deletions pkg/dashboard/uiserver/uiserver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// 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 (
"io"
"net/http"
)

// Handler returns an http.Handler that serves the dashboard UI.
func Handler() http.Handler {
fs := assetFS()
if fs != nil {
fileServer := http.FileServer(fs)
return fileServer
}

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = io.WriteString(w, "Dashboard UI is not built.\n")
})
}
2 changes: 1 addition & 1 deletion pkg/ui/pd-web
42 changes: 42 additions & 0 deletions scripts/embed-dashboard-ui.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash
set -euo pipefail

BASE_DIR=$(git rev-parse --show-toplevel)
CACHE_DIR=${BASE_DIR}/.cache

echo '+ Create cache directory'
mkdir -p ${CACHE_DIR}
breezewish marked this conversation as resolved.
Show resolved Hide resolved

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)
breezewish marked this conversation as resolved.
Show resolved Hide resolved
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
breezewish marked this conversation as resolved.
Show resolved Hide resolved

echo " - Save archive to cache: ${CACHE_FILE}"
mv embedded-assets-golang.zip ${CACHE_FILE}
breezewish marked this conversation as resolved.
Show resolved Hide resolved
fi

echo '+ Unpack embedded asset from archive'
unzip -o ${CACHE_FILE}
breezewish marked this conversation as resolved.
Show resolved Hide resolved
MOVE_FILE=embedded_assets_handler.go
MOVE_DEST=pkg/dashboard/uiserver
mv ${MOVE_FILE} ${MOVE_DEST}
echo " - Unpacked ${MOVE_DEST}/${MOVE_FILE}"
18 changes: 12 additions & 6 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ import (
"github.com/coreos/go-semver/semver"
"github.com/golang/protobuf/proto"
"github.com/gorilla/mux"
"github.com/pingcap-incubator/tidb-dashboard/pkg/apiserver"
"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/logutil"
"github.com/pingcap/pd/pkg/typeutil"
Expand Down Expand Up @@ -64,10 +66,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 @@ -206,8 +210,10 @@ func CreateServer(ctx context.Context, cfg *config.Config, apiBuilders ...Handle
}

etcdCfg.UserHandlers = map[string]http.Handler{
pdAPIPrefix: apiHandler,
webPath: http.StripPrefix(webPath, ui.Handler()),
pdAPIPrefix: apiHandler,
webPath: http.StripPrefix(webPath, ui.Handler()),
dashboardUIPath: http.StripPrefix(dashboardUIPath, uiserver.Handler()),
dashboardAPIPath: apiserver.Handler(dashboardAPIPath),
}
}
etcdCfg.ServiceRegister = func(gs *grpc.Server) {
Expand Down