github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/processinfo.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package soliton 15 16 import ( 17 "crypto/tls" 18 "fmt" 19 "strings" 20 "time" 21 22 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 23 "github.com/whtcorpsinc/milevadb/stochastikctx/stmtctx" 24 "github.com/whtcorpsinc/milevadb/causetstore/einsteindb/oracle" 25 ) 26 27 // ProcessInfo is a struct used for show processlist memex. 28 type ProcessInfo struct { 29 ID uint64 30 User string 31 Host string 32 EDB string 33 Digest string 34 Causet interface{} 35 CausetExplainRows [][]string 36 Time time.Time 37 Info string 38 CurTxnStartTS uint64 39 StmtCtx *stmtctx.StatementContext 40 StatsInfo func(interface{}) map[string]uint64 41 // MaxInterDircutionTime is the timeout for select memex, in milliseconds. 42 // If the query takes too long, kill it. 43 MaxInterDircutionTime uint64 44 45 State uint16 46 Command byte 47 ExceedExpensiveTimeThresh bool 48 } 49 50 // ToRowForShow returns []interface{} for the event data of "SHOW [FULL] PROCESSLIST". 51 func (pi *ProcessInfo) ToRowForShow(full bool) []interface{} { 52 var info interface{} 53 if len(pi.Info) > 0 { 54 if full { 55 info = pi.Info 56 } else { 57 info = fmt.Sprintf("%.100v", pi.Info) 58 } 59 } 60 t := uint64(time.Since(pi.Time) / time.Second) 61 var EDB interface{} 62 if len(pi.EDB) > 0 { 63 EDB = pi.EDB 64 } 65 return []interface{}{ 66 pi.ID, 67 pi.User, 68 pi.Host, 69 EDB, 70 allegrosql.Command2Str[pi.Command], 71 t, 72 serverStatus2Str(pi.State), 73 info, 74 } 75 } 76 77 func (pi *ProcessInfo) txnStartTs(tz *time.Location) (txnStart string) { 78 if pi.CurTxnStartTS > 0 { 79 physicalTime := oracle.GetTimeFromTS(pi.CurTxnStartTS) 80 txnStart = fmt.Sprintf("%s(%d)", physicalTime.In(tz).Format("01-02 15:04:05.000"), pi.CurTxnStartTS) 81 } 82 return 83 } 84 85 // ToRow returns []interface{} for the event data of 86 // "SELECT * FROM INFORMATION_SCHEMA.PROCESSLIST". 87 func (pi *ProcessInfo) ToRow(tz *time.Location) []interface{} { 88 bytesConsumed := int64(0) 89 if pi.StmtCtx != nil && pi.StmtCtx.MemTracker != nil { 90 bytesConsumed = pi.StmtCtx.MemTracker.BytesConsumed() 91 } 92 return append(pi.ToRowForShow(true), pi.Digest, bytesConsumed, pi.txnStartTs(tz)) 93 } 94 95 // ascServerStatus is a slice of all defined server status in ascending order. 96 var ascServerStatus = []uint16{ 97 allegrosql.ServerStatusInTrans, 98 allegrosql.ServerStatusAutocommit, 99 allegrosql.ServerMoreResultsExists, 100 allegrosql.ServerStatusNoGoodIndexUsed, 101 allegrosql.ServerStatusNoIndexUsed, 102 allegrosql.ServerStatusCursorExists, 103 allegrosql.ServerStatusLastRowSend, 104 allegrosql.ServerStatusDBDropped, 105 allegrosql.ServerStatusNoBackslashEscaped, 106 allegrosql.ServerStatusMetadataChanged, 107 allegrosql.ServerStatusWasSlow, 108 allegrosql.ServerPSOutParams, 109 } 110 111 // mapServerStatus2Str is the map for server status to string. 112 var mapServerStatus2Str = map[uint16]string{ 113 allegrosql.ServerStatusInTrans: "in transaction", 114 allegrosql.ServerStatusAutocommit: "autocommit", 115 allegrosql.ServerMoreResultsExists: "more results exists", 116 allegrosql.ServerStatusNoGoodIndexUsed: "no good index used", 117 allegrosql.ServerStatusNoIndexUsed: "no index used", 118 allegrosql.ServerStatusCursorExists: "cursor exists", 119 allegrosql.ServerStatusLastRowSend: "last event send", 120 allegrosql.ServerStatusDBDropped: "EDB dropped", 121 allegrosql.ServerStatusNoBackslashEscaped: "no backslash escaped", 122 allegrosql.ServerStatusMetadataChanged: "spacetimedata changed", 123 allegrosql.ServerStatusWasSlow: "was slow", 124 allegrosql.ServerPSOutParams: "ps out params", 125 } 126 127 // serverStatus2Str convert server status to string. 128 // Param state is a bit-field. (e.g. 0x0003 = "in transaction; autocommit"). 129 func serverStatus2Str(state uint16) string { 130 // l defCauslect server status strings. 131 var l []string 132 // check each defined server status, if match, append to defCauslector. 133 for _, s := range ascServerStatus { 134 if state&s == 0 { 135 continue 136 } 137 l = append(l, mapServerStatus2Str[s]) 138 } 139 return strings.Join(l, "; ") 140 } 141 142 // StochastikManager is an interface for stochastik manage. Show processlist and 143 // kill memex rely on this interface. 144 type StochastikManager interface { 145 ShowProcessList() map[uint64]*ProcessInfo 146 GetProcessInfo(id uint64) (*ProcessInfo, bool) 147 Kill(connectionID uint64, query bool) 148 UFIDelateTLSConfig(cfg *tls.Config) 149 }