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

add uptime #1422

Merged
merged 2 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions sql/plan/show_status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2020-2022 Dolthub, 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 plan

import (
"io"
"testing"

"github.com/stretchr/testify/require"

"github.com/dolthub/go-mysql-server/sql"
)

func TestShowStatus(t *testing.T) {
require := require.New(t)
ctx := sql.NewEmptyContext()

var res sql.Row
var err error
n := NewShowStatus(ShowStatusModifier_Global)
iter, err := n.RowIter(ctx, nil)
require.NoError(err)

for {
res, err = iter.Next(ctx)
if err == io.EOF {
break
}
if res[0] == "uptime" {
require.True(res[1].(int) >= 0)
}
require.NoError(err)
}
}
22 changes: 19 additions & 3 deletions sql/system_variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"math"
"strings"
"sync"
"time"
)

// SystemVariableScope represents the scope of a system variable.
Expand All @@ -38,6 +39,9 @@ const (
SystemVariableScope_ResetPersist
)

// serverStartUpTime is needed by uptime status variable
var serverStartUpTime = time.Now()

// String returns the scope as an uppercase string.
func (s SystemVariableScope) String() string {
switch s {
Expand Down Expand Up @@ -138,10 +142,14 @@ func (sv *globalSystemVariables) GetGlobal(name string) (SystemVariable, interfa
defer sv.mutex.RUnlock()
name = strings.ToLower(name)
v, ok := systemVars[name]
if ok {
return v, sv.sysVarVals[name], true
if !ok {
return SystemVariable{}, nil, false
}
if name == "uptime" {
sv.sysVarVals[name] = int(time.Now().Sub(serverStartUpTime).Seconds())
}
return SystemVariable{}, nil, false

return v, sv.sysVarVals[name], true
}

// SetGlobal sets the system variable with the given name to the given value. If the system variable does not exist,
Expand Down Expand Up @@ -2720,6 +2728,14 @@ var systemVars = map[string]SystemVariable{
Type: NewSystemBoolType("updatable_views_with_limit"),
Default: int8(1),
},
"uptime": {
Name: "uptime",
Scope: SystemVariableScope_Both,
Dynamic: true,
SetVarHintApplies: true,
Type: NewSystemBoolType("updatable_views_with_limit"),
Default: int8(1),
},
"use_secondary_engine": {
Name: "use_secondary_engine",
Scope: SystemVariableScope_Session,
Expand Down