Skip to content

Commit

Permalink
support jenkins
Browse files Browse the repository at this point in the history
Signed-off-by: husharp <[email protected]>
  • Loading branch information
HuSharp committed Dec 5, 2023
1 parent dfff690 commit 788b32a
Show file tree
Hide file tree
Showing 22 changed files with 1,010 additions and 14 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/real_tiup.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: PD Real TiUP Test
on:
push:
branches:
- master
- release-*
pull_request:
branches:
- master
- release-*
concurrency:
group: ${{ github.ref }}-${{ github.workflow }}
cancel-in-progress: true
jobs:
real-cluster:
runs-on: ubuntu-latest
steps:
- uses: actions/setup-go@v3
with:
go-version: '1.21'
- name: Checkout code
uses: actions/checkout@v3
- name: Restore cache
uses: actions/cache@v3
with:
path: |
~/go/pkg/mod
~/.cache/go-build
**/.tools
**/.dashboard_download_cache
key: ${{ runner.os }}-go-${{ matrix.worker_id }}-${{ hashFiles('**/go.sum') }}

- name: Test
run: make check
working-directory: tests/integrations/realtiup
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ ifeq ($(ENABLE_FIPS), 1)
BUILD_TOOL_CGO_ENABLED := 1
endif

LDFLAGS += -X "$(PD_PKG)/pkg/versioninfo.PDReleaseVersion=$(shell git describe --tags --dirty --always)"
RELEASE_VERSION ?= $(shell git describe --tags --dirty --always)
ifeq ($(RUN_CI), 1)
RELEASE_VERSION := None
endif

LDFLAGS += -X "$(PD_PKG)/pkg/versioninfo.PDReleaseVersion=$(RELEASE_VERSION)"
LDFLAGS += -X "$(PD_PKG)/pkg/versioninfo.PDBuildTS=$(shell date -u '+%Y-%m-%d %I:%M:%S')"
LDFLAGS += -X "$(PD_PKG)/pkg/versioninfo.PDGitHash=$(shell git rev-parse HEAD)"
LDFLAGS += -X "$(PD_PKG)/pkg/versioninfo.PDGitBranch=$(shell git rev-parse --abbrev-ref HEAD)"
Expand Down
14 changes: 14 additions & 0 deletions client/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const (
MinResolvedTSPrefix = "/pd/api/v1/min-resolved-ts"
Status = "/pd/api/v1/status"
Version = "/pd/api/v1/version"

Store = "/pd/api/v1/store"
CheckLeader = "/pd/api/v1/leader"
TransferLeader = "/pd/api/v1/leader/transfer"
)

// RegionByID returns the path of PD HTTP API to get region by ID.
Expand Down Expand Up @@ -173,3 +177,13 @@ func PProfProfileAPIWithInterval(interval time.Duration) string {
func PProfGoroutineWithDebugLevel(level int) string {
return fmt.Sprintf("%s?debug=%d", PProfGoroutine, level)
}

// LabelByStore returns the path of PD HTTP API to set store label.
func LabelByStore(storeID int64) string {
return fmt.Sprintf("%s/%d/label", Store, storeID)

Check warning on line 183 in client/http/api.go

View check run for this annotation

Codecov / codecov/patch

client/http/api.go#L183

Added line #L183 was not covered by tests
}

// TransferLeaderID returns the path of PD HTTP API to transfer leader by ID.
func TransferLeaderID(leaderID string) string {
return fmt.Sprintf("%s/%s", TransferLeader, leaderID)

Check warning on line 188 in client/http/api.go

View check run for this annotation

Codecov / codecov/patch

client/http/api.go#L188

Added line #L188 was not covered by tests
}
64 changes: 64 additions & 0 deletions client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"github.com/pingcap/kvproto/pkg/pdpb"
"io"
"net/http"
"strings"
Expand Down Expand Up @@ -87,6 +88,14 @@ type Client interface {
// Additionally, it is important for the caller to handle the content of the response body properly
// in order to ensure that it can be read and marshaled correctly into `res`.
WithRespHandler(func(resp *http.Response, res interface{}) error) Client
SetStoreLabel(context.Context, int64, map[string]string) error

GetLeader(context.Context) (*pdpb.Member, error)
TransferLeader(context.Context, string) error

GetSchedulers(context.Context) ([]string, error)
AddScheduler(context.Context, string, map[string]interface{}) error

Close()
}

Expand Down Expand Up @@ -729,3 +738,58 @@ func (c *client) GetMinResolvedTSByStoresIDs(ctx context.Context, storeIDs []uin
}
return resp.MinResolvedTS, resp.StoresMinResolvedTS, nil
}

// SetStoreLabel sets the label of a store.
func (c *client) SetStoreLabel(ctx context.Context, storeID int64, storeLabel map[string]string) error {
jsonBody, err := json.Marshal(storeLabel)
if err != nil {
return err

Check warning on line 746 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L744-L746

Added lines #L744 - L746 were not covered by tests
}

return c.requestWithRetry(ctx, "SetStoreLabel", LabelByStore(storeID),
http.MethodPost, bytes.NewBuffer(jsonBody), nil)

Check warning on line 750 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L749-L750

Added lines #L749 - L750 were not covered by tests
}

// GetLeader gets the leader of PD cluster.
func (c *client) GetLeader(context.Context) (*pdpb.Member, error) {
var leader pdpb.Member
err := c.requestWithRetry(context.Background(), "GetLeader", CheckLeader,
http.MethodGet, http.NoBody, &leader)
if err != nil {
return nil, err

Check warning on line 759 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L755-L759

Added lines #L755 - L759 were not covered by tests
}
return &leader, nil

Check warning on line 761 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L761

Added line #L761 was not covered by tests
}

// TransferLeader transfers the PD leader.
func (c *client) TransferLeader(ctx context.Context, newLeader string) error {
return c.requestWithRetry(ctx, "TransferLeader", TransferLeaderID(newLeader),
http.MethodPost, http.NoBody, nil)

Check warning on line 767 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L766-L767

Added lines #L766 - L767 were not covered by tests
}

// GetSchedulers gets the schedulers from PD cluster.
func (c *client) GetSchedulers(ctx context.Context) ([]string, error) {
var schedulers []string
err := c.requestWithRetry(ctx, "GetSchedulers", Schedulers,
http.MethodGet, http.NoBody, &schedulers)
if err != nil {
return nil, err

Check warning on line 776 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L772-L776

Added lines #L772 - L776 were not covered by tests
}
return schedulers, nil

Check warning on line 778 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L778

Added line #L778 was not covered by tests
}

// AddScheduler adds a scheduler to PD cluster.
func (c *client) AddScheduler(ctx context.Context, name string, args map[string]interface{}) error {
request := map[string]interface{}{
"name": name,

Check warning on line 784 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L783-L784

Added lines #L783 - L784 were not covered by tests
}
for arg, val := range args {
request[arg] = val

Check warning on line 787 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L787

Added line #L787 was not covered by tests
}
data, err := json.Marshal(request)
if err != nil {
return err

Check warning on line 791 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L789-L791

Added lines #L789 - L791 were not covered by tests
}
return c.requestWithRetry(ctx, "AddScheduler", Schedulers,
http.MethodPost, bytes.NewBuffer(data), nil)

Check warning on line 794 in client/http/client.go

View check run for this annotation

Codecov / codecov/patch

client/http/client.go#L793-L794

Added lines #L793 - L794 were not covered by tests
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
go.uber.org/atomic v1.10.0
go.uber.org/goleak v1.1.12
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a
golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4
golang.org/x/text v0.13.0
golang.org/x/time v0.1.0
golang.org/x/tools v0.6.0
Expand Down Expand Up @@ -185,7 +185,7 @@ require (
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/image v0.5.0 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/mod v0.11.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,8 @@ golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a h1:tlXy25amD5A7gOfbXdqCGN5k8ESEed/Ee1E5RcrYnqU=
golang.org/x/exp v0.0.0-20230108222341-4b8118a2686a/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4 h1:QLureRX3moex6NVu/Lr4MGakp9FdA7sBHGBmvRW7NaM=
golang.org/x/exp v0.0.0-20230711005742-c3f37128e5a4/go.mod h1:FXUEEKJgO7OQYeo8N01OfiKP8RXMtf6e8aTskBGqWdc=
golang.org/x/image v0.0.0-20200119044424-58c23975cae1/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.5.0 h1:5JMiNunQeQw++mMOz48/ISeNu3Iweh/JaZU8ZLqHRrI=
golang.org/x/image v0.5.0/go.mod h1:FVC7BI/5Ym8R25iw5OLsgshdUBbT1h5jZTpA+mvAdZ4=
Expand All @@ -700,8 +700,8 @@ golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/mod v0.11.0 h1:bUO06HqtnRcc/7l71XBe4WcqTZ+3AH1J59zWDDwLKgU=
golang.org/x/mod v0.11.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
4 changes: 2 additions & 2 deletions pkg/member/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type EmbeddedEtcdMember struct {
id uint64 // etcd server id.
member *pdpb.Member // current PD's info.
rootPath string
// memberValue is the serialized string of `member`. It will be save in
// memberValue is the serialized string of `member`. It will be saved in
// etcd leader key when the PD node is successfully elected as the PD leader
// of the cluster. Every write will use it to check PD leadership.
memberValue string
Expand Down Expand Up @@ -200,7 +200,7 @@ func (m *EmbeddedEtcdMember) KeepLeader(ctx context.Context) {
m.leadership.Keep(ctx)
}

// PreCheckLeader does some pre-check before checking whether or not it's the leader.
// PreCheckLeader does some pre-check before checking whether it's the leader.
func (m *EmbeddedEtcdMember) PreCheckLeader() error {
if m.GetEtcdLeader() == 0 {
return errs.ErrEtcdLeaderNotFound
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/client/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3
github.com/stretchr/testify v1.8.3
github.com/tikv/pd v0.0.0-00010101000000-000000000000
github.com/tikv/pd/client v0.0.0-00010101000000-000000000000
github.com/tikv/pd/client v0.0.0-20231101084237-a1a1eea8dafd
go.etcd.io/etcd v0.5.0-alpha.5.0.20220915004622-85b640cee793
go.uber.org/goleak v1.1.12
go.uber.org/zap v1.24.0
Expand Down
2 changes: 1 addition & 1 deletion tests/integrations/mcs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/pingcap/log v1.1.1-0.20221110025148-ca232912c9f3
github.com/stretchr/testify v1.8.3
github.com/tikv/pd v0.0.0-00010101000000-000000000000
github.com/tikv/pd/client v0.0.0-00010101000000-000000000000
github.com/tikv/pd/client v0.0.0-20231101084237-a1a1eea8dafd
go.etcd.io/etcd v0.5.0-alpha.5.0.20220915004622-85b640cee793
go.uber.org/goleak v1.1.12
go.uber.org/zap v1.24.0
Expand Down
49 changes: 49 additions & 0 deletions tests/integrations/realtiup/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2023 TiKV Project 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.

ROOT_PATH := ../../..
GO_TOOLS_BIN_PATH := $(ROOT_PATH)/.tools/bin
PATH := $(GO_TOOLS_BIN_PATH):$(PATH)
SHELL := env PATH='$(PATH)' GOBIN='$(GO_TOOLS_BIN_PATH)' $(shell which bash)

check: static deploy test clean

static: install-tools
@ echo "gofmt ..."
@ gofmt -s -l -d . 2>&1 | awk '{ print } END { if (NR > 0) { exit 1 } }'
@ echo "golangci-lint ..."
@ golangci-lint run -c $(ROOT_PATH)/.golangci.yml --verbose ./... --allow-parallel-runners
@ echo "revive ..."
@ revive -formatter friendly -config $(ROOT_PATH)/revive.toml ./...

tidy:
@ go mod tidy
git diff go.mod go.sum | cat
git diff --quiet go.mod go.sum

deploy:
./download_binaries.sh
rm -rf ./log
mkdir log
./deploy.sh log

clean:
rm -rf log
rm -rf bin

test:
CGO_ENABLED=1 go test ./... -v -tags deadlock -race -cover || { exit 1; }

install-tools:
cd $(ROOT_PATH) && $(MAKE) install-tools
99 changes: 99 additions & 0 deletions tests/integrations/realtiup/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash
# tools to run a TiDB cluster
# parameter 1: work directory
set -eu
WORK_DIR=$1

export PD_PEER_ADDR="127.0.0.1:2380"
export PD_ADDR="127.0.0.1:2379"

export TIDB_IP="127.0.0.1"
export TIDB_PORT="4000"
export TIDB_ADDR="127.0.0.1:4000"

export TIDB_STATUS_ADDR="127.0.0.1:10080"
export TIKV_ADDR="127.0.0.1:2016"
export TIKV_STATUS_ADDR="127.0.0.1:2018"

start_pd() {
echo "Starting PD..."

cat >"$WORK_DIR/pd.toml" <<EOF
[replication]
# The number of replicas for each region.
max-replicas = 1
EOF

./bin/pd-server --version
mkdir -p "$WORK_DIR/pd"
./bin/pd-server \
--client-urls "http://$PD_ADDR" \
--peer-urls "http://$PD_PEER_ADDR" \
--log-file "$WORK_DIR/pd.log" \
--config "$WORK_DIR/pd.toml" \
--data-dir "$WORK_DIR/pd" &
# wait until PD is online...
i=0
while ! curl "http://$PD_ADDR/pd/api/v1/version"; do
i=$((i + 1))
if [ "$i" -gt 20 ]; then
echo 'Failed to start PD'
return 1
fi
sleep 3
done
echo 'pd start successful'
}

start_tikv() {
echo "Starting TiKV..."
mkdir -p "$WORK_DIR/tikv"
./bin/tikv-server \
--pd "$PD_ADDR" \
-A "$TIKV_ADDR" \
--status-addr "$TIKV_STATUS_ADDR" \
--log-file "$WORK_DIR/tikv.log" \
--log-level info \
-s "$WORK_DIR/tikv" &
while ! curl "http://$PD_ADDR/pd/api/v1/cluster/status" | tee /dev/stderr | grep '"is_initialized": true'; do
echo "http://$PD_ADDR/pd/api/v1/cluster/status"
i=$((i + 1))
if [ "$i" -gt 20 ]; then
echo 'Failed to initialize TiKV cluster'
return 1
fi
sleep 5
done
}

start_tidb() {
echo "Starting TiDB..."
./bin/tidb-server \
-P 4000 \
--status 10080 \
--advertise-address="127.0.0.1" \
--store tikv \
--path "$PD_ADDR" \
--log-file "$WORK_DIR/tidb.log" &

i=0
while ! curl "http://$TIDB_IP:10080/status"; do
i=$((i + 1))
if [ "$i" -gt 50 ]; then
echo 'Failed to start TiDB'
return 1
fi
sleep 3
done
}

killall tidb-server 2>/dev/null || true
killall tikv-server 2>/dev/null || true
killall pd-server 2>/dev/null || true

start_pd
start_tikv
start_tidb

mysql -uroot -h127.0.0.1 -P4000 --default-character-set utf8 -e "CREATE USER 'test'@'%' IDENTIFIED BY '123456';" || true
mysql -uroot -h127.0.0.1 -P4000 --default-character-set utf8 -e "GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' WITH GRANT OPTION;" || true
Loading

0 comments on commit 788b32a

Please sign in to comment.