Skip to content
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
8 changes: 0 additions & 8 deletions br/pkg/httputil/BUILD.bazel

This file was deleted.

23 changes: 0 additions & 23 deletions br/pkg/httputil/http.go
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to pkg/util/httputil/

This file was deleted.

2 changes: 1 addition & 1 deletion br/pkg/pdutil/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//br/pkg/errors",
"//br/pkg/httputil",
"//pkg/kv",
"//pkg/store/pdtypes",
"//pkg/tablecodec",
"//pkg/util/codec",
"//pkg/util/httputil",
"@com_github_coreos_go_semver//semver",
"@com_github_docker_go_units//:go-units",
"@com_github_google_uuid//:uuid",
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/pdutil/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

"github.com/pingcap/errors"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/pkg/store/pdtypes"
"github.com/pingcap/tidb/pkg/tablecodec"
"github.com/pingcap/tidb/pkg/util/codec"
"github.com/pingcap/tidb/pkg/util/httputil"
pd "github.com/tikv/pd/client/http"
)

Expand Down
2 changes: 1 addition & 1 deletion br/pkg/stream/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ go_library(
"//br/pkg/encryption",
"//br/pkg/errors",
"//br/pkg/glue",
"//br/pkg/httputil",
"//br/pkg/logutil",
"//br/pkg/metautil",
"//br/pkg/restore/ingestrec",
Expand All @@ -43,6 +42,7 @@ go_library(
"//pkg/tablecodec",
"//pkg/util",
"//pkg/util/codec",
"//pkg/util/httputil",
"//pkg/util/mathutil",
"//pkg/util/table-filter",
"//pkg/util/versioninfo",
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/stream/stream_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/tidb/br/pkg/conn"
"github.com/pingcap/tidb/br/pkg/glue"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/br/pkg/logutil"
. "github.com/pingcap/tidb/br/pkg/streamhelper"
"github.com/pingcap/tidb/br/pkg/utils"
"github.com/pingcap/tidb/pkg/objstore"
"github.com/pingcap/tidb/pkg/util/httputil"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
"go.uber.org/zap"
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/task/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ go_library(
"//br/pkg/encryption",
"//br/pkg/errors",
"//br/pkg/glue",
"//br/pkg/httputil",
"//br/pkg/logutil",
"//br/pkg/metautil",
"//br/pkg/pdutil",
Expand Down Expand Up @@ -74,6 +73,7 @@ go_library(
"//pkg/util/codec",
"//pkg/util/collate",
"//pkg/util/engine",
"//pkg/util/httputil",
"//pkg/util/table-filter",
"@com_github_docker_go_units//:go-units",
"@com_github_fatih_color//:color",
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
connutil "github.com/pingcap/tidb/br/pkg/conn/util"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/glue"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/br/pkg/logutil"
"github.com/pingcap/tidb/br/pkg/metautil"
"github.com/pingcap/tidb/br/pkg/pdutil"
Expand Down Expand Up @@ -59,6 +58,7 @@ import (
"github.com/pingcap/tidb/pkg/util/codec"
"github.com/pingcap/tidb/pkg/util/collate"
"github.com/pingcap/tidb/pkg/util/engine"
"github.com/pingcap/tidb/pkg/util/httputil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/tikv/client-go/v2/tikv"
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/task/restore_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
"github.com/pingcap/tidb/br/pkg/conn"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/glue"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/br/pkg/logutil"
"github.com/pingcap/tidb/br/pkg/metautil"
"github.com/pingcap/tidb/br/pkg/restore"
snapclient "github.com/pingcap/tidb/br/pkg/restore/snap_client"
restoreutils "github.com/pingcap/tidb/br/pkg/restore/utils"
"github.com/pingcap/tidb/br/pkg/rtree"
"github.com/pingcap/tidb/br/pkg/summary"
"github.com/pingcap/tidb/pkg/util/httputil"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.uber.org/zap"
Expand Down
2 changes: 1 addition & 1 deletion br/pkg/task/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import (
"github.com/pingcap/tidb/br/pkg/encryption"
berrors "github.com/pingcap/tidb/br/pkg/errors"
"github.com/pingcap/tidb/br/pkg/glue"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/br/pkg/logutil"
"github.com/pingcap/tidb/br/pkg/metautil"
"github.com/pingcap/tidb/br/pkg/pdutil"
Expand All @@ -67,6 +66,7 @@ import (
"github.com/pingcap/tidb/pkg/tablecodec"
"github.com/pingcap/tidb/pkg/util/cdcutil"
"github.com/pingcap/tidb/pkg/util/codec"
"github.com/pingcap/tidb/pkg/util/httputil"
"github.com/spf13/pflag"
"github.com/tikv/client-go/v2/oracle"
pd "github.com/tikv/pd/client"
Expand Down
2 changes: 1 addition & 1 deletion br/tests/br_key_locked/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ go_library(
importpath = "github.com/pingcap/tidb/br/tests/br_key_locked",
visibility = ["//visibility:private"],
deps = [
"//br/pkg/httputil",
"//br/pkg/task",
"//pkg/config",
"//pkg/kv",
"//pkg/meta/model",
"//pkg/store/driver",
"//pkg/tablecodec",
"//pkg/util/codec",
"//pkg/util/httputil",
"@com_github_pingcap_errors//:errors",
"@com_github_pingcap_kvproto//pkg/kvrpcpb",
"@com_github_pingcap_kvproto//pkg/metapb",
Expand Down
2 changes: 1 addition & 1 deletion br/tests/br_key_locked/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ import (
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/kvrpcpb"
"github.com/pingcap/log"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/br/pkg/task"
"github.com/pingcap/tidb/pkg/config"
"github.com/pingcap/tidb/pkg/kv"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/store/driver"
"github.com/pingcap/tidb/pkg/tablecodec"
"github.com/pingcap/tidb/pkg/util/httputil"
"github.com/tikv/client-go/v2/oracle"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/tikvrpc"
Expand Down
4 changes: 2 additions & 2 deletions pkg/lightning/common/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//br/pkg/errors",
"//br/pkg/httputil",
"//br/pkg/logutil",
"//pkg/errno",
"//pkg/ingestor/errdef",
Expand All @@ -38,6 +37,7 @@ go_library(
"//pkg/util/codec",
"//pkg/util/dbutil",
"//pkg/util/format",
"//pkg/util/httputil",
"//pkg/util/logutil",
"@com_github_cockroachdb_pebble//:pebble",
"@com_github_go_sql_driver_mysql//:mysql",
Expand Down Expand Up @@ -118,7 +118,7 @@ go_test(
],
embed = [":common"],
flaky = True,
shard_count = 30,
shard_count = 29,
deps = [
"//br/pkg/errors",
"//pkg/autoid_service",
Expand Down
4 changes: 2 additions & 2 deletions pkg/lightning/common/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"strings"

"github.com/pingcap/errors"
"github.com/pingcap/tidb/br/pkg/httputil"
"github.com/pingcap/tidb/pkg/util"
"github.com/pingcap/tidb/pkg/util/httputil"
"github.com/tikv/client-go/v2/config"
pd "github.com/tikv/pd/client"
"google.golang.org/grpc"
Expand Down Expand Up @@ -132,7 +132,7 @@ func (tc *TLS) WrapListener(l net.Listener) net.Listener {

// GetJSON performs a GET request to the given path and unmarshals the response
func (tc *TLS) GetJSON(ctx context.Context, path string, v any) error {
return GetJSON(ctx, tc.client, tc.url+path, v)
return httputil.GetJSON(ctx, tc.client, tc.url+path, v)
}

// ToPDSecurityOption converts the TLS configuration to a PD security option.
Expand Down
38 changes: 0 additions & 38 deletions pkg/lightning/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ import (
"crypto/tls"
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -414,42 +412,6 @@ func SchemaExists(ctx context.Context, db dbutil.QueryExecutor, schema string) (
}
}

// GetJSON fetches a page and parses it as JSON. The parsed result will be
// stored into the `v`. The variable `v` must be a pointer to a type that can be
// unmarshalled from JSON.
//
// Example:
//
// client := &http.Client{}
// var resp struct { IP string }
// if err := util.GetJSON(client, "http://api.ipify.org/?format=json", &resp); err != nil {
// return errors.Trace(err)
// }
// fmt.Println(resp.IP)
func GetJSON(ctx context.Context, client *http.Client, url string, v any) error {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are 2 such method, they are merged into one in pkg/util/httputil

req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return errors.Trace(err)
}

resp, err := client.Do(req)
if err != nil {
return errors.Trace(err)
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, err := io.ReadAll(resp.Body)
if err != nil {
return errors.Trace(err)
}
return errors.Errorf("get %s http status code != 200, message %s", url, string(body))
}

return errors.Trace(json.NewDecoder(resp.Body).Decode(v))
}

// KillMySelf sends sigint to current process, used in integration test only
//
// Only works on Unix. Signaling on Windows is not supported.
Expand Down
44 changes: 0 additions & 44 deletions pkg/lightning/common/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ package common_test
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/DATA-DOG/go-sqlmock"
"github.com/go-sql-driver/mysql"
Expand All @@ -44,46 +40,6 @@ func TestDirNotExist(t *testing.T) {
require.False(t, common.IsDirExists("not-exists"))
}

func TestGetJSON(t *testing.T) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to pkg/util/httputil

type TestPayload struct {
Username string `json:"username"`
Password string `json:"password"`
}
request := TestPayload{
Username: "lightning",
Password: "lightning-ctl",
}

ctx := context.Background()
// Mock success response
handle := func(res http.ResponseWriter, _ *http.Request) {
res.WriteHeader(http.StatusOK)
err := json.NewEncoder(res).Encode(request)
require.NoError(t, err)
}
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
handle(res, req)
}))
defer testServer.Close()

client := &http.Client{Timeout: time.Second}

response := TestPayload{}
err := common.GetJSON(ctx, client, "http://localhost:1", &response)
require.Error(t, err)
err = common.GetJSON(ctx, client, testServer.URL, &response)
require.NoError(t, err)
require.Equal(t, request, response)

// Mock `StatusNoContent` response
handle = func(res http.ResponseWriter, _ *http.Request) {
res.WriteHeader(http.StatusNoContent)
}
err = common.GetJSON(ctx, client, testServer.URL, &response)
require.Error(t, err)
require.Regexp(t, ".*http status code != 200.*", err.Error())
}

func TestConnect(t *testing.T) {
plainPsw := "dQAUoDiyb1ucWZk7"

Expand Down
3 changes: 2 additions & 1 deletion pkg/objstore/ossstore/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ go_library(
"//pkg/objstore/s3like",
"//pkg/objstore/storeapi",
"//pkg/util",
"//pkg/util/httputil",
"//pkg/util/logutil",
"@com_github_alibabacloud_go_tea//tea",
"@com_github_aliyun_alibabacloud_oss_go_sdk_v2//oss",
Expand All @@ -42,7 +43,7 @@ go_test(
],
embed = [":ossstore"],
flaky = True,
shard_count = 8,
shard_count = 10,
deps = [
"//dumpling/context",
"//pkg/objstore/ossstore/mock",
Expand Down
10 changes: 7 additions & 3 deletions pkg/objstore/ossstore/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package ossstore

import (
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/pingcap/log"
tidblogutil "github.com/pingcap/tidb/pkg/util/logutil"
"go.uber.org/zap"
)
Expand All @@ -25,9 +24,14 @@ type logPrinter struct {
logger *zap.Logger
}

func newLogPrinter(extraFields ...zap.Field) *logPrinter {
func newLogPrinter(logger *zap.Logger) *logPrinter {
return &logPrinter{
logger: log.L().WithOptions(zap.AddCallerSkip(1)).With(extraFields...),
// skip 3 levels to reach the original caller
// top: this logger
// top-1: standardLogger.printf of OSS
// top-2: standardLogger.Info/Error/Debug/Warn of OSS
// top-3: the actual caller in OSS SDK
logger: logger.WithOptions(zap.AddCallerSkip(3)),
}
}

Expand Down
Loading