github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/function/ctl/cmd_task.go (about) 1 // Copyright 2021 - 2022 Matrix Origin 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package ctl 16 17 import ( 18 "context" 19 "strings" 20 "time" 21 22 "github.com/matrixorigin/matrixone/pkg/clusterservice" 23 "github.com/matrixorigin/matrixone/pkg/common/moerr" 24 "github.com/matrixorigin/matrixone/pkg/pb/metadata" 25 querypb "github.com/matrixorigin/matrixone/pkg/pb/query" 26 taskpb "github.com/matrixorigin/matrixone/pkg/pb/task" 27 qclient "github.com/matrixorigin/matrixone/pkg/queryservice/client" 28 "github.com/matrixorigin/matrixone/pkg/taskservice" 29 "github.com/matrixorigin/matrixone/pkg/vm/process" 30 ) 31 32 var ( 33 disableTask = "disable" 34 enableTask = "enable" 35 getUser = "getuser" 36 37 taskMap = map[string]int32{ 38 "storageusage": int32(taskpb.TaskCode_MetricStorageUsage), 39 } 40 ) 41 42 // handleTask handles task command 43 // parameter format: 44 // 1. enable 45 // 2. disable 46 // 3. [uuid:]taskId 47 func handleTask(proc *process.Process, 48 service serviceType, 49 parameter string, 50 sender requestSender) (Result, error) { 51 parameter = strings.ToLower(parameter) 52 switch parameter { 53 case disableTask: 54 taskservice.DebugCtlTaskFramework(true) 55 return Result{ 56 Method: TaskMethod, 57 Data: "OK", 58 }, nil 59 case enableTask: 60 taskservice.DebugCtlTaskFramework(false) 61 return Result{ 62 Method: TaskMethod, 63 Data: "OK", 64 }, nil 65 case getUser: 66 ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) 67 state, err := proc.Hakeeper.GetClusterState(ctx) 68 cancel() 69 if err != nil { 70 return Result{Method: TaskMethod, Data: "failed to get cluster state"}, err 71 } 72 user := state.GetTaskTableUser() 73 return Result{Method: TaskMethod, Data: user}, nil 74 default: 75 } 76 77 target, taskCode, err := checkRunTaskParameter(parameter) 78 if err != nil { 79 return Result{}, err 80 } 81 resp, err := transferTaskToCN(proc.QueryClient, target, taskCode) 82 if err != nil { 83 return Result{}, err 84 } 85 return Result{ 86 Method: TaskMethod, 87 Data: resp, 88 }, nil 89 } 90 91 func checkRunTaskParameter(param string) (string, int32, error) { 92 // uuid:taskId 93 args := strings.Split(param, ":") 94 if len(args) != 2 { 95 return "", 0, moerr.NewInternalErrorNoCtx("cmd invalid, expected uuid:task") 96 } 97 taskCode, ok := taskMap[args[1]] 98 if !ok { 99 return "", 0, moerr.NewInternalErrorNoCtx("cmd invalid, task %s not found", args[1]) 100 } 101 return args[0], taskCode, nil 102 } 103 104 func transferTaskToCN(qc qclient.QueryClient, target string, taskCode int32) (resp *querypb.Response, err error) { 105 clusterservice.GetMOCluster().GetCNService( 106 clusterservice.NewServiceIDSelector(target), 107 func(cn metadata.CNService) bool { 108 req := qc.NewRequest(querypb.CmdMethod_RunTask) 109 req.RunTask = &querypb.RunTaskRequest{TaskCode: taskCode} 110 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 111 defer cancel() 112 113 resp, err = qc.SendMessage(ctx, cn.QueryAddress, req) 114 return true 115 }) 116 return 117 }