Skip to content

Commit bea58f6

Browse files
authored
support Threads_connected and Threads_running status variables (#2432)
1 parent f149a5a commit bea58f6

File tree

7 files changed

+170
-319
lines changed

7 files changed

+170
-319
lines changed

enginetest/enginetests.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3942,17 +3942,6 @@ func TestVariables(t *testing.T, harness Harness) {
39423942
}
39433943
}
39443944

3945-
func TestStatusVariables(t *testing.T, harness Harness) {
3946-
e := mustNewEngine(t, harness)
3947-
if IsServerEngine(e) {
3948-
t.Skip("prepare statements cause status variables to incorrectly increment twice")
3949-
}
3950-
for _, script := range queries.StatusVariableScripts {
3951-
variables.InitStatusVariables()
3952-
TestScript(t, harness, script)
3953-
}
3954-
}
3955-
39563945
func TestPreparedInsert(t *testing.T, harness Harness) {
39573946
harness.Setup(setup.MydbData, setup.MytableData)
39583947
e := mustNewEngine(t, harness)

enginetest/memory_engine_test.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -927,10 +927,6 @@ func TestTransactions(t *testing.T) {
927927
enginetest.TestTransactionScripts(t, enginetest.NewSkippingMemoryHarness())
928928
}
929929

930-
func TestStatusVariables(t *testing.T) {
931-
enginetest.TestStatusVariables(t, enginetest.NewDefaultMemoryHarness())
932-
}
933-
934930
func mergableIndexDriver(dbs []sql.Database) sql.IndexDriver {
935931
return memory.NewIndexDriver("mydb", map[string][]sql.DriverIndex{
936932
"mytable": {

enginetest/queries/variable_queries.go

Lines changed: 0 additions & 265 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"math"
1919

2020
"github.com/dolthub/go-mysql-server/sql"
21-
"github.com/dolthub/go-mysql-server/sql/plan"
2221
"github.com/dolthub/go-mysql-server/sql/types"
2322
)
2423

@@ -661,267 +660,3 @@ var VariableErrorTests = []QueryErrorTest{
661660
ExpectedErr: sql.ErrSyntaxError,
662661
},
663662
}
664-
665-
var StatusVariableScripts = []ScriptTest{
666-
{
667-
Name: "Com_delete",
668-
SetUpScript: []string{
669-
"create table t (i int primary key)",
670-
"insert into t values (1), (2), (3)",
671-
},
672-
Assertions: []ScriptTestAssertion{
673-
{
674-
Query: "show status like 'Com_delete'",
675-
Expected: []sql.Row{
676-
{"Com_delete", uint64(0)},
677-
},
678-
},
679-
{
680-
// syntax errors do not trigger the status variable
681-
Query: "delete abc",
682-
ExpectedErrStr: "syntax error at position 11 near 'abc'",
683-
},
684-
{
685-
Query: "show status like 'Com_delete'",
686-
Expected: []sql.Row{
687-
{"Com_delete", uint64(0)},
688-
},
689-
},
690-
691-
{
692-
// delete 1 row
693-
Query: "delete from t where i = 1",
694-
Expected: []sql.Row{
695-
{types.NewOkResult(1)},
696-
},
697-
},
698-
{
699-
Query: "show status like 'Com_delete'",
700-
Expected: []sql.Row{
701-
{"Com_delete", uint64(1)},
702-
},
703-
},
704-
705-
{
706-
// delete 2 rows
707-
Query: "delete from t where i > 0",
708-
Expected: []sql.Row{
709-
{types.NewOkResult(2)},
710-
},
711-
},
712-
{
713-
Query: "show status like 'Com_delete'",
714-
Expected: []sql.Row{
715-
{"Com_delete", uint64(2)},
716-
},
717-
},
718-
719-
{
720-
// delete 0 rows
721-
Query: "delete from t where false",
722-
Expected: []sql.Row{
723-
{types.NewOkResult(0)},
724-
},
725-
},
726-
{
727-
Query: "show status like 'Com_delete'",
728-
Expected: []sql.Row{
729-
{"Com_delete", uint64(3)},
730-
},
731-
},
732-
733-
{
734-
Query: "delete from t where notarealcolumn > 0",
735-
ExpectedErr: sql.ErrColumnNotFound,
736-
},
737-
{
738-
Query: "show status like 'Com_delete'",
739-
Expected: []sql.Row{
740-
{"Com_delete", uint64(4)},
741-
},
742-
},
743-
744-
{
745-
Query: "delete from notarealtable",
746-
ExpectedErr: sql.ErrTableNotFound,
747-
},
748-
{
749-
Query: "show status like 'Com_delete'",
750-
Expected: []sql.Row{
751-
{"Com_delete", uint64(5)},
752-
},
753-
},
754-
},
755-
},
756-
{
757-
Name: "Com_insert",
758-
SetUpScript: []string{
759-
"create table t (i int primary key)",
760-
},
761-
Assertions: []ScriptTestAssertion{
762-
{
763-
Query: "show status like 'Com_insert'",
764-
Expected: []sql.Row{
765-
{"Com_insert", uint64(0)},
766-
},
767-
},
768-
769-
{
770-
Query: "insert syntax error",
771-
ExpectedErrStr: "syntax error at position 20 near 'error'",
772-
},
773-
{
774-
Query: "show status like 'Com_insert'",
775-
Expected: []sql.Row{
776-
{"Com_insert", uint64(0)},
777-
},
778-
},
779-
780-
{
781-
Query: "insert into t values (1)",
782-
Expected: []sql.Row{
783-
{types.NewOkResult(1)},
784-
},
785-
},
786-
{
787-
Query: "show status like 'Com_insert'",
788-
Expected: []sql.Row{
789-
{"Com_insert", uint64(1)},
790-
},
791-
},
792-
793-
{
794-
Query: "insert into t values (2), (3)",
795-
Expected: []sql.Row{
796-
{types.NewOkResult(2)},
797-
},
798-
},
799-
{
800-
Query: "show status like 'Com_insert'",
801-
Expected: []sql.Row{
802-
{"Com_insert", uint64(2)},
803-
},
804-
},
805-
806-
{
807-
Query: "insert into faketable values (2), (3)",
808-
ExpectedErr: sql.ErrTableNotFound,
809-
},
810-
{
811-
Query: "show status like 'Com_insert'",
812-
Expected: []sql.Row{
813-
{"Com_insert", uint64(3)},
814-
},
815-
},
816-
817-
{
818-
Query: "insert into t values (1)",
819-
ExpectedErr: sql.ErrPrimaryKeyViolation,
820-
},
821-
{
822-
Query: "show status like 'Com_insert'",
823-
Expected: []sql.Row{
824-
{"Com_insert", uint64(4)},
825-
},
826-
},
827-
828-
{
829-
Query: "insert into t(bad) values (1)",
830-
ExpectedErr: sql.ErrUnknownColumn,
831-
},
832-
{
833-
Query: "show status like 'Com_insert'",
834-
Expected: []sql.Row{
835-
{"Com_insert", uint64(5)},
836-
},
837-
},
838-
},
839-
},
840-
{
841-
Name: "Com_update",
842-
SetUpScript: []string{
843-
"create table t (i int primary key)",
844-
"insert into t values (1), (2), (3)",
845-
},
846-
Assertions: []ScriptTestAssertion{
847-
{
848-
Query: "show status like 'Com_update'",
849-
Expected: []sql.Row{
850-
{"Com_update", uint64(0)},
851-
},
852-
},
853-
854-
{
855-
Query: "update t abc",
856-
ExpectedErrStr: "syntax error at position 13 near 'abc'",
857-
},
858-
{
859-
Query: "show status like 'Com_update'",
860-
Expected: []sql.Row{
861-
{"Com_update", uint64(0)},
862-
},
863-
},
864-
865-
{
866-
Query: "update t set i = 10 where i = 1",
867-
Expected: []sql.Row{
868-
{types.OkResult{RowsAffected: 1, Info: plan.UpdateInfo{Matched: 1, Updated: 1}}},
869-
},
870-
},
871-
{
872-
Query: "show status like 'Com_update'",
873-
Expected: []sql.Row{
874-
{"Com_update", uint64(1)},
875-
},
876-
},
877-
878-
{
879-
Query: "update t set i = i * 10 where i < 10",
880-
Expected: []sql.Row{
881-
{types.OkResult{RowsAffected: 2, Info: plan.UpdateInfo{Matched: 2, Updated: 2}}},
882-
},
883-
},
884-
{
885-
Query: "show status like 'Com_update'",
886-
Expected: []sql.Row{
887-
{"Com_update", uint64(2)},
888-
},
889-
},
890-
891-
{
892-
Query: "update t set i = 1000 where false",
893-
Expected: []sql.Row{
894-
{types.OkResult{Info: plan.UpdateInfo{}}},
895-
},
896-
},
897-
{
898-
Query: "show status like 'Com_update'",
899-
Expected: []sql.Row{
900-
{"Com_update", uint64(3)},
901-
},
902-
},
903-
904-
{
905-
Query: "update badtbl set i = 1000",
906-
ExpectedErr: sql.ErrTableNotFound,
907-
},
908-
{
909-
Query: "show status like 'Com_update'",
910-
Expected: []sql.Row{
911-
{"Com_update", uint64(4)},
912-
},
913-
},
914-
915-
{
916-
Query: "update t set badcol = 1000",
917-
ExpectedErr: sql.ErrColumnNotFound,
918-
},
919-
{
920-
Query: "show status like 'Com_update'",
921-
Expected: []sql.Row{
922-
{"Com_update", uint64(5)},
923-
},
924-
},
925-
},
926-
},
927-
}

processlist.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ func (pl *ProcessList) Processes() []sql.Process {
7171
func (pl *ProcessList) AddConnection(id uint32, addr string) {
7272
pl.mu.Lock()
7373
defer pl.mu.Unlock()
74+
go func() {
75+
sql.StatusVariables.IncrementGlobal("Threads_connected", 1)
76+
}()
7477
pl.procs[id] = &sql.Process{
7578
Connection: id,
7679
Command: sql.ProcessCommandConnect,
@@ -98,6 +101,9 @@ func (pl *ProcessList) RemoveConnection(connID uint32) {
98101
defer pl.mu.Unlock()
99102
p := pl.procs[connID]
100103
if p != nil {
104+
go func() {
105+
sql.StatusVariables.IncrementGlobal("Threads_connected", -1)
106+
}()
101107
if p.Kill != nil {
102108
p.Kill()
103109
}
@@ -112,6 +118,9 @@ func (pl *ProcessList) BeginQuery(
112118
) (*sql.Context, error) {
113119
pl.mu.Lock()
114120
defer pl.mu.Unlock()
121+
122+
sql.IncrementStatusVariable(ctx, "Threads_running", 1)
123+
115124
id := ctx.Session.ID()
116125
pid := ctx.Pid()
117126
p := pl.procs[id]
@@ -145,6 +154,7 @@ func (pl *ProcessList) EndQuery(ctx *sql.Context) {
145154
delete(pl.byQueryPid, pid)
146155
p := pl.procs[id]
147156
if p != nil && p.QueryPid == pid {
157+
sql.IncrementStatusVariable(ctx, "Threads_running", -1)
148158
p.Command = sql.ProcessCommandSleep
149159
p.Query = ""
150160
p.StartedAt = time.Now()

0 commit comments

Comments
 (0)