Skip to content

Commit

Permalink
Merge branch 'master' into fix_statement_summary
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkingrei authored Dec 7, 2022
2 parents 95fcfaf + 05d4256 commit 97aade2
Show file tree
Hide file tree
Showing 22 changed files with 287 additions and 35 deletions.
12 changes: 10 additions & 2 deletions DEPS.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,14 @@ def go_deps():
sum = "h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=",
version = "v0.3.4",
)
go_repository(
name = "com_github_cloudfoundry_gosigar",
build_file_proto_mode = "disable",
importpath = "github.com/cloudfoundry/gosigar",
sum = "h1:T3MoGdugg1vdHn8Az7wDn7cZ4+QCjZph+eXf2CjSjo4=",
version = "v1.3.4",
)

go_repository(
name = "com_github_cloudykit_fastprinter",
build_file_proto_mode = "disable_global",
Expand Down Expand Up @@ -4438,8 +4446,8 @@ def go_deps():
name = "org_golang_x_sys",
build_file_proto_mode = "disable_global",
importpath = "golang.org/x/sys",
sum = "h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=",
version = "v0.2.0",
sum = "h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=",
version = "v0.3.0",
)
go_repository(
name = "org_golang_x_term",
Expand Down
2 changes: 1 addition & 1 deletion ddl/cancel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var allTestCase = []testCancelJob{
{"alter table t modify column c11 char(10)", false, model.StatePublic, false, true, nil},
// Add foreign key.
{"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", true, model.StateNone, true, false, []string{"create table t_ref (c1 int key, c2 int, c3 int, c11 tinyint);"}},
{"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", false, model.StatePublic, false, true, nil},
{"alter table t add constraint fk foreign key a(c1) references t_ref(c1)", false, model.StatePublic, false, true, []string{"insert into t_ref (c1) select c1 from t;"}},
// Drop foreign key.
{"alter table t drop foreign key fk", true, model.StatePublic, true, false, nil},
{"alter table t drop foreign key fk", false, model.StateNone, false, true, nil},
Expand Down
3 changes: 1 addition & 2 deletions ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1812,8 +1812,7 @@ func TestChangingAttributeOfColumnWithFK(t *testing.T) {
tk.MustExec("use test")

prepare := func() {
tk.MustExec("drop table if exists users")
tk.MustExec("drop table if exists orders")
tk.MustExec("drop table if exists users, orders")
tk.MustExec("CREATE TABLE users (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, doc JSON);")
tk.MustExec("CREATE TABLE orders (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, user_id INT NOT NULL, doc JSON, FOREIGN KEY fk_user_id (user_id) REFERENCES users(id));")
}
Expand Down
6 changes: 2 additions & 4 deletions ddl/db_foreign_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func TestDuplicateForeignKey(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t, t1")
// Foreign table.
tk.MustExec("create table t(id int key)")
// Create target table with duplicate fk.
Expand All @@ -38,8 +37,7 @@ func TestDuplicateForeignKey(t *testing.T) {
// Alter target table with duplicate fk.
tk.MustGetErrCode("alter table t1 add CONSTRAINT `fk_aaa` FOREIGN KEY (`id_fk`) REFERENCES `t` (`id`)", mysql.ErrFkDupName)
tk.MustGetErrCode("alter table t1 add CONSTRAINT `fk_aAa` FOREIGN KEY (`id_fk`) REFERENCES `t` (`id`)", mysql.ErrFkDupName)
tk.MustExec("drop table if exists t")
tk.MustExec("drop table if exists t1")
tk.MustExec("drop table if exists t, t1")
}

func TestTemporaryTableForeignKey(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2378,6 +2378,7 @@ func TestParserIssue284(t *testing.T) {

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@foreign_key_checks=0")
tk.MustExec("create table test.t_parser_issue_284(c1 int not null primary key)")
_, err := tk.Exec("create table test.t_parser_issue_284_2(id int not null primary key, c1 int not null, constraint foreign key (c1) references t_parser_issue_284(c1))")
require.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ func TestCreateTableWithLike(t *testing.T) {
tk.MustExec("use ctwl_db")
tk.MustExec("create table tt(id int primary key)")
tk.MustExec("create table t (c1 int not null auto_increment, c2 int, constraint cc foreign key (c2) references tt(id), primary key(c1)) auto_increment = 10")
tk.MustExec("set @@foreign_key_checks=0")
tk.MustExec("insert into t set c2=1")
tk.MustExec("create table t1 like ctwl_db.t")
tk.MustExec("insert into t1 set c2=11")
Expand Down
1 change: 1 addition & 0 deletions executor/explain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ func TestExplainAnalyzeExecutionInfo(t *testing.T) {
checkExecutionInfo(t, tk, "explain analyze select * from t use index(k)")
checkExecutionInfo(t, tk, "explain analyze with recursive cte(a) as (select 1 union select a + 1 from cte where a < 1000) select * from cte;")

tk.MustExec("set @@foreign_key_checks=0")
tk.MustExec("CREATE TABLE IF NOT EXISTS nation ( N_NATIONKEY BIGINT NOT NULL,N_NAME CHAR(25) NOT NULL,N_REGIONKEY BIGINT NOT NULL,N_COMMENT VARCHAR(152),PRIMARY KEY (N_NATIONKEY));")
tk.MustExec("CREATE TABLE IF NOT EXISTS part ( P_PARTKEY BIGINT NOT NULL,P_NAME VARCHAR(55) NOT NULL,P_MFGR CHAR(25) NOT NULL,P_BRAND CHAR(10) NOT NULL,P_TYPE VARCHAR(25) NOT NULL,P_SIZE BIGINT NOT NULL,P_CONTAINER CHAR(10) NOT NULL,P_RETAILPRICE DECIMAL(15,2) NOT NULL,P_COMMENT VARCHAR(23) NOT NULL,PRIMARY KEY (P_PARTKEY));")
tk.MustExec("CREATE TABLE IF NOT EXISTS supplier ( S_SUPPKEY BIGINT NOT NULL,S_NAME CHAR(25) NOT NULL,S_ADDRESS VARCHAR(40) NOT NULL,S_NATIONKEY BIGINT NOT NULL,S_PHONE CHAR(15) NOT NULL,S_ACCTBAL DECIMAL(15,2) NOT NULL,S_COMMENT VARCHAR(101) NOT NULL,PRIMARY KEY (S_SUPPKEY),CONSTRAINT FOREIGN KEY SUPPLIER_FK1 (S_NATIONKEY) references nation(N_NATIONKEY));")
Expand Down
1 change: 1 addition & 0 deletions executor/issuetest/executor_issue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ func TestFix31537(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@foreign_key_checks=0")
tk.MustExec(`CREATE TABLE trade (
t_id bigint(16) NOT NULL AUTO_INCREMENT,
t_dts datetime NOT NULL,
Expand Down
14 changes: 7 additions & 7 deletions executor/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,15 +799,15 @@ func TestSetVar(t *testing.T) {

// test variable 'foreign_key_checks'
// global scope
tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("0")) // default value
tk.MustExec("set global foreign_key_checks = 1")
tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("1"))
tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("1")) // default value
tk.MustExec("set global foreign_key_checks = 0")
tk.MustQuery("select @@global.foreign_key_checks").Check(testkit.Rows("0"))
// session scope
tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("0")) // default value
tk.MustExec("set session foreign_key_checks = 1")
tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("1"))
tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("1")) // default value
tk.MustExec("set session foreign_key_checks = 0")
tk.MustQuery("select @@session.foreign_key_checks").Check(testkit.Rows("0"))

// test variable 'foreign_key_checks'
// test variable 'tidb_enable_foreign_key'
// global scope
tk.MustQuery("select @@global.tidb_enable_foreign_key").Check(testkit.Rows("1")) // default value
tk.MustExec("set global tidb_enable_foreign_key = 0")
Expand Down
3 changes: 2 additions & 1 deletion executor/showtest/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,10 @@ func TestShowCreateTable(t *testing.T) {
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin",
))

// TiDB defaults (and only supports) foreign_key_checks=0
// set @@foreign_key_checks=0,
// This means that the child table can be created before the parent table.
// This behavior is required for mysqldump restores.
tk.MustExec("set @@foreign_key_checks=0")
tk.MustExec(`DROP TABLE IF EXISTS parent, child`)
tk.MustExec(`CREATE TABLE child (id INT NOT NULL PRIMARY KEY auto_increment, parent_id INT NOT NULL, INDEX par_ind (parent_id), CONSTRAINT child_ibfk_1 FOREIGN KEY (parent_id) REFERENCES parent(id))`)
tk.MustExec(`CREATE TABLE parent ( id INT NOT NULL PRIMARY KEY auto_increment )`)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/charithe/durationcheck v0.0.9
github.com/cheggaaa/pb/v3 v3.0.8
github.com/cheynewallace/tabby v1.1.1
github.com/cloudfoundry/gosigar v1.3.4
github.com/cockroachdb/errors v1.8.1
github.com/cockroachdb/pebble v0.0.0-20210719141320-8c3bd06debb5
github.com/coocood/freecache v1.2.1
Expand Down Expand Up @@ -110,7 +111,7 @@ require (
golang.org/x/net v0.2.0
golang.org/x/oauth2 v0.2.0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.2.0
golang.org/x/sys v0.3.0
golang.org/x/term v0.2.0
golang.org/x/text v0.4.0
golang.org/x/time v0.2.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudfoundry/gosigar v1.3.4 h1:T3MoGdugg1vdHn8Az7wDn7cZ4+QCjZph+eXf2CjSjo4=
github.com/cloudfoundry/gosigar v1.3.4/go.mod h1:g9r7ETZ1tpvJCT9TpqxO53+5BUZiM2FDSFSENzjK5Z8=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
Expand Down Expand Up @@ -1314,8 +1316,8 @@ golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220909162455-aba9fc2a8ff2/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0 h1:z85xZCsEl7bi/KwbNADeBYoOP0++7W1ipu+aGnpwzRM=
Expand Down
10 changes: 6 additions & 4 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,10 +1687,12 @@ func buildPointDeletePlan(ctx sessionctx.Context, pointPlan PhysicalPlan, dbName
var err error
is := sessiontxn.GetTxnManager(ctx).GetTxnInfoSchema()
t, _ := is.TableByID(tbl.ID)
tblID2Table := map[int64]table.Table{tbl.ID: t}
err = delPlan.buildOnDeleteFKTriggers(ctx, is, tblID2Table)
if err != nil {
return nil
if t != nil {
tblID2Table := map[int64]table.Table{tbl.ID: t}
err = delPlan.buildOnDeleteFKTriggers(ctx, is, tblID2Table)
if err != nil {
return nil
}
}
return delPlan
}
Expand Down
1 change: 1 addition & 0 deletions session/session_test/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2961,6 +2961,7 @@ func TestIgnoreForeignKey(t *testing.T) {

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("set @@foreign_key_checks=0")
sqlText := `CREATE TABLE address (
id bigint(20) NOT NULL AUTO_INCREMENT,
user_id bigint(20) NOT NULL,
Expand Down
2 changes: 1 addition & 1 deletion sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ const (
DefTiDBAutoBuildStatsConcurrency = 1
DefTiDBSysProcScanConcurrency = 1
DefTiDBRcWriteCheckTs = false
DefTiDBForeignKeyChecks = false
DefTiDBForeignKeyChecks = true
DefTiDBAnalyzePartitionConcurrency = 1
DefTiDBOptRangeMaxSize = 64 * int64(size.MB) // 64 MB
DefTiDBCostModelVer = 2
Expand Down
5 changes: 4 additions & 1 deletion util/cgroup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ go_library(

go_test(
name = "cgroup_test",
srcs = ["cgroup_mock_test.go"],
srcs = [
"cgroup_cpu_test.go",
"cgroup_mock_test.go",
],
embed = [":cgroup"],
flaky = True,
deps = ["@com_github_stretchr_testify//require"],
Expand Down
9 changes: 9 additions & 0 deletions util/cgroup/cgroup_cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ func getCgroupCPU(root string) (CPUUsage, error) {

return res, nil
}

// CPUShares returns the number of CPUs this cgroup can be expected to
// max out. If there's no limit, NumCPU is returned.
func (c CPUUsage) CPUShares() float64 {
if c.Period <= 0 || c.Quota <= 0 {
return float64(c.NumCPU)
}
return float64(c.Quota) / float64(c.Period)
}
9 changes: 0 additions & 9 deletions util/cgroup/cgroup_cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ import (
"strings"
)

// CPUShares returns the number of CPUs this cgroup can be expected to
// max out. If there's no limit, NumCPU is returned.
func (c CPUUsage) CPUShares() float64 {
if c.Period <= 0 || c.Quota <= 0 {
return float64(c.NumCPU)
}
return float64(c.Quota) / float64(c.Period)
}

// GetCgroupCPU returns the CPU usage and quota for the current cgroup.
func GetCgroupCPU() (CPUUsage, error) {
cpuusage, err := getCgroupCPU("/")
Expand Down
50 changes: 50 additions & 0 deletions util/cgroup/cgroup_cpu_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2022 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,
// 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.

//go:build linux

package cgroup

import (
"runtime"
"sync"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetCgroupCPU(t *testing.T) {
exit := make(chan struct{})
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-exit:
return
default:
runtime.Gosched()
}
}
}()
}
cpu, err := GetCgroupCPU()
require.NoError(t, err)
require.NotZero(t, cpu.Period)
require.Less(t, int64(1), cpu.Period)
close(exit)
wg.Wait()
}
23 changes: 23 additions & 0 deletions util/cpu/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "cpu",
srcs = ["cpu.go"],
importpath = "github.com/pingcap/tidb/util/cpu",
visibility = ["//visibility:public"],
deps = [
"//util/cgroup",
"//util/mathutil",
"@com_github_cloudfoundry_gosigar//:gosigar",
"@com_github_pingcap_log//:log",
"@org_uber_go_atomic//:atomic",
"@org_uber_go_zap//:zap",
],
)

go_test(
name = "cpu_test",
srcs = ["cpu_test.go"],
embed = [":cpu"],
deps = ["@com_github_stretchr_testify//require"],
)
Loading

0 comments on commit 97aade2

Please sign in to comment.