Repository: pingcap/tidb
File: pkg/util/config/config.go (Go)

Code before:
// 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 config

import (
	"fmt"
	"io"

	"github.com/BurntSushi/toml"
	"github.com/pingcap/errors"
	"github.com/pingcap/tidb/pkg/sessionctx"
	"github.com/pingcap/tidb/pkg/sessionctx/vardef"
	"github.com/pingcap/tidb/pkg/sessionctx/variable"
	"github.com/pingcap/tidb/pkg/util/logutil"
	"go.uber.org/zap"
)

var ignoredSystemVariablesForPlanReplayerLoad = map[string]struct{}{
	"innodb_lock_wait_timeout": {}, // It is unncessary to load this variable for plan replayer.
}

// LoadConfig loads system variables from a toml reader. it is only for plan replayer and test.
func LoadConfig(ctx sessionctx.Context, v io.ReadCloser) (unLoadVars []string, err error) {
	varMap := make(map[string]string)

	_, err = toml.NewDecoder(v).Decode(&varMap)
	if err != nil {
		return nil, errors.AddStack(err)
	}
	unLoadVars = make([]string, 0)
	vars := ctx.GetSessionVars()
	for name, value := range varMap {
		if _, ok := ignoredSystemVariablesForPlanReplayerLoad[name]; ok {
			continue
		}
		sysVar := variable.GetSysVar(name)
		if sysVar == nil {
			unLoadVars = append(unLoadVars, name)
			logutil.BgLogger().Warn(fmt.Sprintf("skip set variable %s:%s", name, value), zap.Error(err))
			continue
		}
		sVal, err := sysVar.Validate(vars, value, vardef.ScopeSession)
		if err != nil {
			unLoadVars = append(unLoadVars, name)
			logutil.BgLogger().Warn(fmt.Sprintf("skip variable %s:%s", name, value), zap.Error(err))
			continue
		}

Review comment (suggestion):
The function documentation should be updated to mention that certain system variables are excluded from loading. Consider updating the comment to something like "LoadConfig loads system variables from a toml reader, excluding variables in the exclusion list. It is only for plan replayer and test."
```suggestion
// LoadConfig loads system variables from a toml reader, excluding variables in the exclusion list.
// It is only for plan replayer and test.
```

Code after:
// 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 config

import (
	"fmt"
	"io"

	"github.com/BurntSushi/toml"
	"github.com/pingcap/errors"
	"github.com/pingcap/tidb/pkg/sessionctx"
	"github.com/pingcap/tidb/pkg/sessionctx/vardef"
	"github.com/pingcap/tidb/pkg/sessionctx/variable"
	"github.com/pingcap/tidb/pkg/util/logutil"
	"go.uber.org/zap"
)

var ignoredSystemVariablesForPlanReplayerLoad = map[string]struct{}{
	vardef.InnodbLockWaitTimeout: {}, // It is unnecessary to load this variable for plan replayer.
}

// LoadConfigForPlanReplayerLoad loads system variables from a toml reader. it is only for plan replayer and test.
func LoadConfigForPlanReplayerLoad(ctx sessionctx.Context, v io.ReadCloser) (unLoadVars []string, err error) {
	varMap := make(map[string]string)

	_, err = toml.NewDecoder(v).Decode(&varMap)
	if err != nil {
		return nil, errors.AddStack(err)
	}
	unLoadVars = make([]string, 0)
	vars := ctx.GetSessionVars()
	for name, value := range varMap {
		if _, ok := ignoredSystemVariablesForPlanReplayerLoad[name]; ok {
			logutil.BgLogger().Warn(fmt.Sprintf("ignore set variable %s:%s", name, value), zap.Error(err))
			continue
		}
		sysVar := variable.GetSysVar(name)
		if sysVar == nil {
			unLoadVars = append(unLoadVars, name)
			logutil.BgLogger().Warn(fmt.Sprintf("skip set variable %s:%s", name, value), zap.Error(err))
			continue
		}
		sVal, err := sysVar.Validate(vars, value, vardef.ScopeSession)
		if err != nil {
			unLoadVars = append(unLoadVars, name)
			logutil.BgLogger().Warn(fmt.Sprintf("skip variable %s:%s", name, value), zap.Error(err))
			continue