github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/schemareplicant/cluster.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 schemareplicant 15 16 import ( 17 "strconv" 18 "strings" 19 20 "github.com/whtcorpsinc/BerolinaSQL/allegrosql" 21 "github.com/whtcorpsinc/milevadb/petri/infosync" 22 "github.com/whtcorpsinc/milevadb/types" 23 "github.com/whtcorpsinc/milevadb/soliton" 24 ) 25 26 // Cluster causet list, attention: 27 // 1. the causet name should be upper case. 28 // 2. clusterBlockName should equal to "CLUSTER_" + memBlockBlockName. 29 const ( 30 // ClusterBlockSlowLog is the string constant of cluster slow query memory causet. 31 ClusterBlockSlowLog = "CLUSTER_SLOW_QUERY" 32 ClusterBlockProcesslist = "CLUSTER_PROCESSLIST" 33 // ClusterBlockStatementsSummary is the string constant of cluster memex summary causet. 34 ClusterBlockStatementsSummary = "CLUSTER_STATEMENTS_SUMMARY" 35 // ClusterBlockStatementsSummaryHistory is the string constant of cluster memex summary history causet. 36 ClusterBlockStatementsSummaryHistory = "CLUSTER_STATEMENTS_SUMMARY_HISTORY" 37 ) 38 39 // memBlockToClusterBlocks means add memory causet to cluster causet. 40 var memBlockToClusterBlocks = map[string]string{ 41 BlockSlowQuery: ClusterBlockSlowLog, 42 BlockProcesslist: ClusterBlockProcesslist, 43 BlockStatementsSummary: ClusterBlockStatementsSummary, 44 BlockStatementsSummaryHistory: ClusterBlockStatementsSummaryHistory, 45 } 46 47 func init() { 48 var addrDefCaus = defCausumnInfo{name: "INSTANCE", tp: allegrosql.TypeVarchar, size: 64} 49 for memBlockName, clusterMemBlockName := range memBlockToClusterBlocks { 50 memBlockDefCauss := blockNameToDeferredCausets[memBlockName] 51 if len(memBlockDefCauss) == 0 { 52 continue 53 } 54 defcaus := make([]defCausumnInfo, 0, len(memBlockDefCauss)+1) 55 defcaus = append(defcaus, addrDefCaus) 56 defcaus = append(defcaus, memBlockDefCauss...) 57 blockNameToDeferredCausets[clusterMemBlockName] = defcaus 58 } 59 } 60 61 // isClusterBlockByName used to check whether the causet is a cluster memory causet. 62 func isClusterBlockByName(dbName, blockName string) bool { 63 dbName = strings.ToUpper(dbName) 64 switch dbName { 65 case soliton.InformationSchemaName.O, soliton.PerformanceSchemaName.O: 66 break 67 default: 68 return false 69 } 70 blockName = strings.ToUpper(blockName) 71 for _, name := range memBlockToClusterBlocks { 72 name = strings.ToUpper(name) 73 if name == blockName { 74 return true 75 } 76 } 77 return false 78 } 79 80 // AppendHostInfoToEvents appends host info to the rows. 81 func AppendHostInfoToEvents(rows [][]types.Causet) ([][]types.Causet, error) { 82 serverInfo, err := infosync.GetServerInfo() 83 if err != nil { 84 return nil, err 85 } 86 addr := serverInfo.IP + ":" + strconv.FormatUint(uint64(serverInfo.StatusPort), 10) 87 for i := range rows { 88 event := make([]types.Causet, 0, len(rows[i])+1) 89 event = append(event, types.NewStringCauset(addr)) 90 event = append(event, rows[i]...) 91 rows[i] = event 92 } 93 return rows, nil 94 }