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

session: refine non-transactional delete #34273

Merged
merged 12 commits into from
May 9, 2022
Prev Previous commit
Next Next commit
add telemetry
Signed-off-by: ekexium <ekexium@gmail.com>
ekexium committed May 7, 2022
commit b0e052c1e862eb75a3836ff8e403602be958cc96
1 change: 1 addition & 0 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -165,6 +165,7 @@ func RegisterMetrics() {
prometheus.MustRegister(CPUProfileCounter)
prometheus.MustRegister(ReadFromTableCacheCounter)
prometheus.MustRegister(LoadTableCacheDurationHistogram)
prometheus.MustRegister(NonTransactionalDeleteCount)

tikvmetrics.InitMetrics(TiDB, TiKVClient)
tikvmetrics.RegisterMetrics()
8 changes: 8 additions & 0 deletions metrics/session.go
Original file line number Diff line number Diff line change
@@ -127,6 +127,14 @@ var (
Name: "validate_read_ts_from_pd_count",
Help: "Counter of validating read ts by getting a timestamp from PD",
})

NonTransactionalDeleteCount = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: "tidb",
Subsystem: "session",
Name: "non_transactional_delete_count",
Help: "Counter of non-transactional delete",
})
)

// Label constants.
19 changes: 19 additions & 0 deletions metrics/telemetry.go
Original file line number Diff line number Diff line change
@@ -67,3 +67,22 @@ func GetCTECounter() CTEUsageCounter {
NonCTEUsed: readCounter(TelemetrySQLCTECnt.With(prometheus.Labels{LblCTEType: "notCTE"})),
}
}

// NonTransactionalStmtCounter records the usages of non-transactional statements.
type NonTransactionalStmtCounter struct {
DeleteCount int64 `json:"delete"`
}

// Sub returns the difference of two counters.
func (n NonTransactionalStmtCounter) Sub(rhs NonTransactionalStmtCounter) NonTransactionalStmtCounter {
return NonTransactionalStmtCounter{
DeleteCount: n.DeleteCount - rhs.DeleteCount,
}
}

// GetNonTransactionalStmtCounter gets the NonTransactionalStmtCounter.
func GetNonTransactionalStmtCounter() NonTransactionalStmtCounter {
return NonTransactionalStmtCounter{
DeleteCount: readCounter(NonTransactionalDeleteCount),
}
}
2 changes: 2 additions & 0 deletions session/nontransactional.go
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ import (
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/metrics"
"github.com/pingcap/tidb/parser/ast"
"github.com/pingcap/tidb/parser/format"
"github.com/pingcap/tidb/parser/model"
@@ -70,6 +71,7 @@ func HandleNonTransactionalDelete(ctx context.Context, stmt *ast.NonTransactiona
if err := checkConstraint(ctx, stmt, se); err != nil {
return nil, err
}
metrics.NonTransactionalDeleteCount.Inc()
tableName, selectSQL, shardColumnInfo, err := buildSelectSQL(stmt, se)
if err != nil {
return nil, err
1 change: 1 addition & 0 deletions telemetry/data.go
Original file line number Diff line number Diff line change
@@ -58,4 +58,5 @@ func postReportTelemetryData() {
postReportTxnUsage()
postReportCTEUsage()
postReportSlowQueryStats()
postReportNonTransactionalCounter()
}
29 changes: 22 additions & 7 deletions telemetry/data_feature_usage.go
Original file line number Diff line number Diff line change
@@ -36,13 +36,14 @@ type featureUsage struct {
Txn *TxnUsage `json:"txn"`
// cluster index usage information
// key is the first 6 characters of sha2(TABLE_NAME, 256)
ClusterIndex *ClusterIndexUsage `json:"clusterIndex"`
NewClusterIndex *NewClusterIndexUsage `json:"newClusterIndex"`
TemporaryTable bool `json:"temporaryTable"`
CTE *m.CTEUsageCounter `json:"cte"`
CachedTable bool `json:"cachedTable"`
AutoCapture bool `json:"autoCapture"`
PlacementPolicyUsage *placementPolicyUsage `json:"placementPolicy"`
ClusterIndex *ClusterIndexUsage `json:"clusterIndex"`
NewClusterIndex *NewClusterIndexUsage `json:"newClusterIndex"`
TemporaryTable bool `json:"temporaryTable"`
CTE *m.CTEUsageCounter `json:"cte"`
CachedTable bool `json:"cachedTable"`
AutoCapture bool `json:"autoCapture"`
PlacementPolicyUsage *placementPolicyUsage `json:"placementPolicy"`
NonTransactionalUsage *m.NonTransactionalStmtCounter `json:"nonTransactional"`
}

type placementPolicyUsage struct {
@@ -70,6 +71,9 @@ func getFeatureUsage(ctx sessionctx.Context) (*featureUsage, error) {
usage.AutoCapture = getAutoCaptureUsageInfo(ctx)

collectFeatureUsageFromInfoschema(ctx, &usage)

usage.NonTransactionalUsage = getNonTransactionalUsage()

return &usage, nil
}

@@ -192,6 +196,7 @@ type TxnUsage struct {

var initialTxnCommitCounter metrics.TxnCommitCounter
var initialCTECounter m.CTEUsageCounter
var initialNonTransactionalCounter m.NonTransactionalStmtCounter

// getTxnUsageInfo gets the usage info of transaction related features. It's exported for tests.
func getTxnUsageInfo(ctx sessionctx.Context) *TxnUsage {
@@ -243,3 +248,13 @@ func getAutoCaptureUsageInfo(ctx sessionctx.Context) bool {
}
return false
}

func getNonTransactionalUsage() *m.NonTransactionalStmtCounter {
curr := m.GetNonTransactionalStmtCounter()
diff := curr.Sub(initialNonTransactionalCounter)
return &diff
}

func postReportNonTransactionalCounter() {
initialNonTransactionalCounter = m.GetNonTransactionalStmtCounter()
}
18 changes: 18 additions & 0 deletions telemetry/data_feature_usage_test.go
Original file line number Diff line number Diff line change
@@ -204,3 +204,21 @@ func TestClusterIndexUsageInfo(t *testing.T) {
require.Equal(t, uint64(1), usage.NewClusterIndex.NumClusteredTables)
require.Equal(t, uint64(2), usage.NewClusterIndex.NumTotalTables)
}

func TestNonTransactionalUsage(t *testing.T) {
store, clean := testkit.CreateMockStore(t)
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")

usage, err := telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(0), usage.NonTransactionalUsage.DeleteCount)

tk.MustExec("create table t(a int);")
tk.MustExec("split limit 1 delete from t")
usage, err = telemetry.GetFeatureUsage(tk.Session())
require.NoError(t, err)
require.Equal(t, int64(1), usage.NonTransactionalUsage.DeleteCount)
}