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

ttl: A demo #39320

Closed
wants to merge 15 commits into from
2 changes: 2 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/statistics/handle"
"github.com/pingcap/tidb/telemetry"
"github.com/pingcap/tidb/ttl/ttlworker"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util"
"github.com/pingcap/tidb/util/dbterror"
Expand Down Expand Up @@ -1057,6 +1058,7 @@ func (do *Domain) Init(
return err
}

ttlworker.NewJobManager(do.ddl.GetID(), do.sysSessionPool).Start()
return nil
}

Expand Down
2 changes: 2 additions & 0 deletions kv/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,6 @@ const (
InternalTxnBR = InternalTxnTools
// InternalTxnTrace handles the trace statement.
InternalTxnTrace = "Trace"
// InternalTxnTTL handles the TTL jobs
InternalTxnTTL = "TTL"
)
41 changes: 41 additions & 0 deletions ttl/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "ttl",
srcs = [
"sql.go",
"table.go",
],
importpath = "github.com/pingcap/tidb/ttl",
visibility = ["//visibility:public"],
deps = [
"//parser/ast",
"//parser/format",
"//parser/model",
"//parser/mysql",
"//types",
"//util/sqlexec",
"@com_github_pingcap_errors//:errors",
"@com_github_pkg_errors//:errors",
],
)

go_test(
name = "ttl_test",
srcs = [
"main_test.go",
"sql_test.go",
],
deps = [
":ttl",
"//kv",
"//parser/model",
"//parser/mysql",
"//testkit",
"//testkit/testsetup",
"//types",
"//util/sqlexec",
"@com_github_stretchr_testify//require",
"@org_uber_go_goleak//:goleak",
],
)
15 changes: 15 additions & 0 deletions ttl/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.

package ttl
33 changes: 33 additions & 0 deletions ttl/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.

package ttl_test

import (
"testing"

"github.com/pingcap/tidb/testkit/testsetup"
"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
testsetup.SetupForCommonTest()
opts := []goleak.Option{
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"),
goleak.IgnoreTopFunction("github.com/lestrrat-go/httprc.runFetchWorker"),
goleak.IgnoreTopFunction("go.etcd.io/etcd/client/pkg/v3/logutil.(*MergeLogger).outputLoop"),
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
}
goleak.VerifyTestMain(m, opts...)
}
115 changes: 115 additions & 0 deletions ttl/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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.

package ttl

import (
"context"

"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser/terror"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/sqlexec"
)

type Session struct {
Sctx sessionctx.Context
Executor sqlexec.SQLExecutor
CloseFn func()
}

func (s *Session) GetDomainInfoSchema() infoschema.InfoSchema {
is, ok := s.Sctx.GetDomainInfoSchema().(infoschema.InfoSchema)
if !ok {
return nil
}

if ext, ok := is.(*infoschema.SessionExtendedInfoSchema); ok {
return ext.InfoSchema
}

return is
}

func (s *Session) ExecuteSQL(ctx context.Context, sql string, args ...interface{}) ([]chunk.Row, error) {
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnTTL)
rs, err := s.Executor.ExecuteInternal(ctx, sql, args...)
if err != nil {
return nil, err
}

if err != nil {
return nil, err
}

if rs == nil {
return nil, nil
}

defer func() {
terror.Log(rs.Close())
}()

return sqlexec.DrainRecordSet(ctx, rs, 8)
}

func (s *Session) RunInTxn(ctx context.Context, fn func() error) (err error) {
if _, err = s.ExecuteSQL(ctx, "BEGIN"); err != nil {
return err
}

success := false
defer func() {
if !success {
_, err = s.ExecuteSQL(ctx, "ROLLBACK")
terror.Log(err)
}
}()

if err = fn(); err != nil {
return err
}

success = true
return nil
}

func (s *Session) ExecuteSQLWithTTLCheck(ctx context.Context, tbl *PhysicalTable, sql string) (result [][]types.Datum, retryable bool, err error) {
err = s.RunInTxn(ctx, func() error {
// TODO: check schema
rows, execErr := s.ExecuteSQL(ctx, sql)
// TODO: check retryable err
if execErr != nil {
return execErr
}

result = make([][]types.Datum, len(rows))
for i, row := range rows {
result[i] = row.GetDatumRow(tbl.KeyFieldTypes)
}

return nil
})

return
}

func (s *Session) Close() {
if s.CloseFn != nil {
s.CloseFn()
}
}
Loading