// GetSessionSystemVar gets a system variable.
// If it is a session only variable, use the default value defined in code.
// Returns error if there is no such variable.
355151
func GetSessionSystemVar(s *SessionVars, key string) (string, error) {
	key = strings.ToLower(key)
	gVal, ok, err := GetSessionOnlySysVars(s, key)
	if err != nil || ok {
		return gVal, err
	}
	gVal, err = s.GlobalVarsAccessor.GetGlobalSysVar(key)
	if err != nil {
		return "", err
	}
	s.systems[key] = gVal
	return gVal, nil
}
494610
func (s *IdentityService) GetWebSession(user, sid string) (services.WebSession, error) {
	if user == "" {
		return nil, trace.BadParameter("missing username")
	}
	if sid == "" {
		return nil, trace.BadParameter("missing session id")
	}
	item, err := s.Get(context.TODO(), backend.Key(webPrefix, usersPrefix, user, sessionsPrefix, sid))
	if err != nil {
		return nil, trace.Wrap(err)
	}
	session, err := services.GetWebSessionMarshaler().UnmarshalWebSession(item.Value)
	if err != nil {
		return nil, trace.Wrap(err)
	}
	// this is for backwards compatibility to ensure we
	// always have these values
	session.SetUser(user)
	session.SetName(sid)
	return session, nil
}
1240210
func getIntVar(key string, defaultVal int) int {
	valueString := os.Getenv(key)
	valueInt := defaultVal

	if valueString != "" {
		i, err := strconv.Atoi(valueString)

		if err != nil {
			logfWarn("Failed to parse env property %s: %s is not "+
				"an integer. Using default.", key, valueString)
		} else {
			valueInt = i
		}
	}

	return valueInt
}
897476
func (handle *ContextHandle) GetSession() (session mPkcs11.SessionHandle) {
	handle.lock.RLock()
	select {
	case session = <-handle.sessions:
		logger.Debugf("Reusing existing pkcs11 session %+v on slot %d\n", session, handle.slot)

	default:

		// cache is empty (or completely in use), create a new session
		s, err := handle.OpenSession()
		if err != nil {
			handle.lock.RUnlock()
			panic(fmt.Errorf("OpenSession failed [%s]", err))
		}
		logger.Debugf("Created new pkcs11 session %+v on slot %d\n", s, handle.slot)
		session = s
		cachebridge.ClearSession(fmt.Sprintf("%d", session))
	}
	handle.lock.RUnlock()
	return handle.validateSession(session)
}
416635
func checkEnvVars(envVars ...string) *string {
	var res string
	for _, envVar := range envVars {
		v, isSet := os.LookupEnv(envVar)

		// If we don't explicitly opt out from unit tests
		// by specifying F8_RESOURCE_UNIT_TEST=0
		// we're going to run them
		if !isSet && envVar == UnitTest {
			continue
		}

		// Skip test if environment variable is not set.
		if !isSet {
			res = fmt.Sprintf(StSkipReasonNotSet, envVar)
			return &res
		}
		// Try to convert to boolean value
		isTrue, err := strconv.ParseBool(v)
		if err != nil {
			res = fmt.Sprintf(StSkipReasonParseError, envVar, v)
			return &res
		}

		if !isTrue {
			res = fmt.Sprintf(StSkipReasonValueFalse, envVar, v)
			return &res
		}
	}
	return nil
}
478790
func loadSystemTZ(se *session) (string, error) {
	sql := `select variable_value from mysql.tidb where variable_name = 'system_tz'`
	rss, errLoad := se.Execute(context.Background(), sql)
	if errLoad != nil {
		return "", errLoad
	}
	// the record of mysql.tidb under where condition: variable_name = "system_tz" should shall only be one.
	defer func() {
		if err := rss[0].Close(); err != nil {
			logutil.Logger(context.Background()).Error("close result set error", zap.Error(err))
		}
	}()
	req := rss[0].NewRecordBatch()
	if err := rss[0].Next(context.Background(), req); err != nil {
		return "", err
	}
	return req.GetRow(0).GetString(0), nil
}
1192451
func GetScopeNoneSystemVar(key string) (string, bool, error) {
	sysVar := SysVars[key]
	if sysVar == nil {
		return "", false, UnknownSystemVar.GenWithStackByArgs(key)
	}
	if sysVar.Scope == ScopeNone {
		return sysVar.Value, true, nil
	}
	return "", false, nil
}
918660
func (builder *RequestBuilder) SetFromSessionVars(sv *variable.SessionVars) *RequestBuilder {
	builder.Request.Concurrency = sv.DistSQLScanConcurrency
	builder.Request.IsolationLevel = builder.getIsolationLevel()
	builder.Request.NotFillCache = sv.StmtCtx.NotFillCache
	builder.Request.Priority = builder.getKVPriority(sv)
	return builder
}
827930
func (sg *sessionPool) get() (sessionctx.Context, error) {
	if sg.resPool == nil {
		return mock.NewContext(), nil
	}

	sg.mu.Lock()
	if sg.mu.closed {
		sg.mu.Unlock()
		return nil, errors.Errorf("sessionPool is closed.")
	}
	sg.mu.Unlock()

	// no need to protect sg.resPool
	resource, err := sg.resPool.Get()
	if err != nil {
		return nil, errors.Trace(err)
	}

	ctx := resource.(sessionctx.Context)
	ctx.GetSessionVars().SetStatusFlag(mysql.ServerStatusAutocommit, true)
	ctx.GetSessionVars().InRestrictedSQL = true
	return ctx, nil
}
780319
func (s *Service) FindSession(ctx context.Context, key string) (*platform.Session, error) {
	result, found := s.sessionKV.Load(key)
	if !found {
		return nil, &platform.Error{
			Code: platform.ENotFound,
			Msg:  platform.ErrSessionNotFound,
		}
	}

	sess := new(platform.Session)
	*sess = result.(platform.Session)

	// TODO(desa): these values should be cached so it's not so expensive to lookup each time.
	f := platform.UserResourceMappingFilter{UserID: sess.UserID}
	mappings, _, err := s.FindUserResourceMappings(ctx, f)
	if err != nil {
		return nil, &platform.Error{
			Err: err,
		}
	}

	ps := make([]platform.Permission, 0, len(mappings))
	for _, m := range mappings {
		p, err := m.ToPermissions()
		if err != nil {
			return nil, &platform.Error{
				Err: err,
			}
		}

		ps = append(ps, p...)
	}
	ps = append(ps, platform.MePermissions(sess.UserID)...)
	sess.Permissions = ps
	return sess, nil
}
457282
func (service *Service) GetSession(request *http.Request, kind SessionType, name string) (*sessions.Session, error) {
	return service.Sessions[kind].Get(request, name)
}
1245769
func (c *Client) GetSystemUser() (user.User, error) {
	systemUser := user.User{}
	err := c.call("GetSystemUser", empty, &systemUser)
	return systemUser, err
}
876492
func (f *Facade) GetSystemUser(ctx datastore.Context) (userdomain.User, error) {
	defer ctx.Metrics().Stop(ctx.Metrics().Start("Facade.GetSystemUser"))
	plog.Debug("Started Facade.GetSystemUser")
	defer plog.Debug("Finished Facade.GetSystemUser")

	systemUser := userdomain.User{
		Name:     SYSTEM_USER_NAME,
		Password: INSTANCE_PASSWORD,
	}
	return systemUser, nil
}
1089788
func (c *Client) GetWebSessionInfo(user string, sid string) (services.WebSession, error) {
	out, err := c.Get(
		c.Endpoint("users", user, "web", "sessions", sid), url.Values{})
	if err != nil {
		return nil, trace.Wrap(err)
	}
	return services.GetWebSessionMarshaler().UnmarshalWebSession(out.Bytes())
}
602173
func (c *Context) GetGlobalSysVar(ctx sessionctx.Context, name string) (string, error) {
	v := variable.GetSysVar(name)
	if v == nil {
		return "", variable.UnknownSystemVar.GenWithStackByArgs(name)
	}
	return v.Value, nil
}
1334292
func (getter Getter) GetVar(name string) (Value, bool) {
	if getter == nil {
		return nilValue, false
	}
	v, ok := getter[name]
	return v, ok
}
5203
func (env *CustomEnv) Var(ident string) reflect.Value {
	// Note that variables are always pointer values.
	if v := env.Env.Var(ident); v.IsValid() && v.Type().Elem().Kind() == reflect.Int {
		i := v.Elem().Int()
		plusOne := reflect.New(v.Type().Elem()).Elem()
		plusOne.Set(reflect.ValueOf(int(i + 1)))
		return plusOne.Addr()
	} else {
		return v
	}
}