Skip to content

Commit

Permalink
address
Browse files Browse the repository at this point in the history
Signed-off-by: nolouch <[email protected]>
  • Loading branch information
nolouch committed Jul 5, 2023
1 parent 2596ac9 commit b2b79e4
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 15 deletions.
31 changes: 16 additions & 15 deletions util/request_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
InternalTxnMeta = InternalTxnOthers
)

// explict source types.
// explicit source types.
const (
ExplicitTypeDefault = ""
ExplicitTypeLightning = "lightning"
Expand All @@ -36,8 +36,8 @@ const (
ExplicitTypeBackground = "background"
)

// ExplictTypeList is the list of all explict source types.
var ExplictTypeList = []string{ExplicitTypeDefault, ExplicitTypeLightning, ExplicitTypeBR, ExplicitTypeDumpling, ExplicitTypeBackground}
// ExplicitTypeList is the list of all explicit source types.
var ExplicitTypeList = []string{ExplicitTypeDefault, ExplicitTypeLightning, ExplicitTypeBR, ExplicitTypeDumpling, ExplicitTypeBackground}

const (
// InternalRequest is the scope of internal queries
Expand Down Expand Up @@ -96,23 +96,24 @@ func (r *RequestSource) GetRequestSource() string {
if r == nil || (len(r.RequestSourceType) == 0 && len(r.ExplicitRequestSourceType) == 0) {
return SourceUnknown
}
appendType := func(list []string) []string {
if len(r.ExplicitRequestSourceType) > 0 {
list = append(list, r.ExplicitRequestSourceType)
return list
clearEmpty := func(list []string) []string {
i := 0
for _, v := range list {
if len(v) != 0 {
list[i] = v
i++
}
}
return list
return list[:i]
}
if r.RequestSourceInternal {
labels := []string{InternalRequest, r.RequestSourceType}
labels = appendType(labels)
if len(r.ExplicitRequestSourceType) > 0 {
labels = append(labels, r.ExplicitRequestSourceType)
}
labels := []string{InternalRequest, r.RequestSourceType, r.ExplicitRequestSourceType}
labels = clearEmpty(labels)

return strings.Join(labels, "_")
}
labels := []string{ExternalRequest, r.RequestSourceType}
labels = appendType(labels)
labels := []string{ExternalRequest, r.RequestSourceType, r.ExplicitRequestSourceType}
labels = clearEmpty(labels)
return strings.Join(labels, "_")
}

Expand Down
88 changes: 88 additions & 0 deletions util/request_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2023 TiKV 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.

// NOTE: The code in this file is based on code from the
// TiDB project, licensed under the Apache License v 2.0
//
// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/util/execdetails.go
//

// Copyright 2023 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 util

import (
"testing"

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

func TestGetRequestSource(t *testing.T) {
rsi := true
rst := "test"
ers := "explicit_test"
rs := &RequestSource{
RequestSourceInternal: rsi,
RequestSourceType: rst,
ExplicitRequestSourceType: ers,
}

// Test internal request
expected := "internal_test_explicit_test"
actual := rs.GetRequestSource()
assert.Equal(t, expected, actual)

// Test external request
rs.RequestSourceInternal = false
expected = "external_test_explicit_test"
actual = rs.GetRequestSource()
assert.Equal(t, expected, actual)

// Test nil pointer
rs = nil
expected = "unknown"
actual = rs.GetRequestSource()
assert.Equal(t, expected, actual)

// Test empty RequestSourceType and ExplicitRequestSourceType
rs = &RequestSource{}
expected = "unknown"
actual = rs.GetRequestSource()
assert.Equal(t, expected, actual)

// Test empty ExplicitRequestSourceType
rs.RequestSourceType = "test"
expected = "external_test"
actual = rs.GetRequestSource()
assert.Equal(t, expected, actual)

// Test empty RequestSourceType
rs.RequestSourceType = ""
rs.ExplicitRequestSourceType = "explicit_test"
expected = "external_explicit_test"
actual = rs.GetRequestSource()
assert.Equal(t, expected, actual)
}

0 comments on commit b2b79e4

Please sign in to comment.