1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
// Copyright 2020 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package task
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/pingcap/tiup/pkg/cluster/ctxt"
operator "github.com/pingcap/tiup/pkg/cluster/operation"
"github.com/pingcap/tiup/pkg/cluster/spec"
)
// the check types
var (
CheckTypeSystemInfo = "insight"
CheckTypeSystemLimits = "limits"
CheckTypeSystemConfig = "system"
CheckTypePort = "port"
CheckTypeService = "service"
CheckTypePackage = "package"
CheckTypePartitions = "partitions"
CheckTypeFIO = "fio"
CheckTypePermission = "permission"
ChecktypeIsExist = "exist"
CheckTypeTimeZone = "timezone"
)
// place the check utilities are stored
var (
CheckToolsPathDir = "/tmp/tiup"
)
// CheckSys performs checks of system information
type CheckSys struct {
host string
topo *spec.Specification
opt *operator.CheckOptions
check string // check type name
checkDir string
}
func storeResults(ctx context.Context, host string, results []*operator.CheckResult) {
rr := []any{}
for _, r := range results {
rr = append(rr, r)
}
ctxt.GetInner(ctx).SetCheckResults(host, rr)
}
// Execute implements the Task interface
func (c *CheckSys) Execute(ctx context.Context) error {
stdout, stderr, _ := ctxt.GetInner(ctx).GetOutputs(c.host)
if len(stderr) > 0 && len(stdout) == 0 {
return ErrNoOutput
}
sudo := true
if c.topo.BaseTopo().GlobalOptions.SystemdMode == spec.UserMode {
sudo = false
}
switch c.check {
case CheckTypeSystemInfo:
storeResults(ctx, c.host, operator.CheckSystemInfo(c.opt, stdout))
case CheckTypeSystemLimits:
storeResults(ctx, c.host, operator.CheckSysLimits(c.opt, c.topo.GlobalOptions.User, stdout))
case CheckTypeSystemConfig:
results := operator.CheckKernelParameters(c.opt, stdout)
e, ok := ctxt.GetInner(ctx).GetExecutor(c.host)
if !ok {
return ErrNoExecutor
}
results = append(
results,
operator.CheckSELinux(ctx, e, sudo),
operator.CheckTHP(ctx, e, sudo),
)
storeResults(ctx, c.host, results)
case CheckTypePort:
storeResults(ctx, c.host, operator.CheckListeningPort(c.opt, c.host, c.topo, stdout))
case CheckTypeService:
e, ok := ctxt.GetInner(ctx).GetExecutor(c.host)
if !ok {
return ErrNoExecutor
}
var results []*operator.CheckResult
// check services
results = append(
results,
operator.CheckServices(ctx, e, c.host, "irqbalance", false, spec.SystemdMode(string(c.topo.BaseTopo().GlobalOptions.SystemdMode))),
// FIXME: set firewalld rules in deploy, and not disabling it anymore
operator.CheckServices(ctx, e, c.host, "firewalld", true, spec.SystemdMode(string(c.topo.BaseTopo().GlobalOptions.SystemdMode))),
)
storeResults(ctx, c.host, results)
case CheckTypePackage: // check if a command present, and if a package installed
e, ok := ctxt.GetInner(ctx).GetExecutor(c.host)
if !ok {
return ErrNoExecutor
}
var results []*operator.CheckResult
// check if numactl is installed
stdout, stderr, err := e.Execute(ctx, "numactl --show", false)
if err != nil || len(stderr) > 0 {
results = append(results, &operator.CheckResult{
Name: operator.CheckNameCommand,
Err: fmt.Errorf("numactl not usable, %s", strings.Trim(string(stderr), "\n")),
Msg: "numactl is not installed properly",
})
} else {
results = append(results, &operator.CheckResult{
Name: operator.CheckNameCommand,
Msg: "numactl: " + strings.Split(string(stdout), "\n")[0],
})
}
// check if JRE is available for TiSpark
results = append(results, operator.CheckJRE(ctx, e, c.host, c.topo)...)
storeResults(ctx, c.host, results)
case CheckTypePartitions:
// check partition mount options for data_dir
storeResults(ctx, c.host, operator.CheckPartitions(c.opt, c.host, c.topo, stdout))
case CheckTypeFIO:
if !c.opt.EnableDisk || c.checkDir == "" {
break
}
rr, rw, lat, err := c.runFIO(ctx)
if err != nil {
return err
}
storeResults(ctx, c.host, operator.CheckFIOResult(rr, rw, lat))
case CheckTypePermission:
e, ok := ctxt.GetInner(ctx).GetExecutor(c.host)
if !ok {
return ErrNoExecutor
}
storeResults(ctx, c.host, operator.CheckDirPermission(ctx, e, c.topo.GlobalOptions.User, c.checkDir))
case ChecktypeIsExist:
e, ok := ctxt.GetInner(ctx).GetExecutor(c.host)
if !ok {
return ErrNoExecutor
}
// check partition mount options for data_dir
storeResults(ctx, c.host, operator.CheckDirIsExist(ctx, e, c.checkDir))
case CheckTypeTimeZone:
storeResults(ctx, c.host, operator.CheckTimeZone(ctx, c.topo, c.host, stdout))
}
return nil
}
// Rollback implements the Task interface
func (c *CheckSys) Rollback(ctx context.Context) error {
return ErrUnsupportedRollback
}
// String implements the fmt.Stringer interface
func (c *CheckSys) String() string {
return fmt.Sprintf("CheckSys: host=%s type=%s", c.host, c.check)
}
// runFIO performs FIO checks
func (c *CheckSys) runFIO(ctx context.Context) (outRR []byte, outRW []byte, outLat []byte, err error) {
e, ok := ctxt.GetInner(ctx).GetExecutor(c.host)
if !ok {
err = ErrNoExecutor
return
}
checkDir := spec.Abs(c.topo.GlobalOptions.User, c.checkDir)
testWd := filepath.Join(checkDir, "tiup-fio-test")
fioBin := filepath.Join(CheckToolsPathDir, "bin", "fio")
notExistDir := testWd
for {
parent := filepath.Dir(notExistDir)
if len(parent) <= 1 {
break
}
results := operator.CheckDirIsExist(ctx, e, parent)
if len(results) > 0 {
break
}
notExistDir = parent
}
var stderr []byte
// rand read
var (
fileRR = "fio_randread_test.txt"
resRR = "fio_randread_result.json"
)
cmdRR := strings.Join([]string{
fmt.Sprintf("mkdir -p %s && cd %s", testWd, testWd),
fmt.Sprintf("rm -f %s %s", fileRR, resRR), // cleanup any legancy files
strings.Join([]string{
fioBin,
"-ioengine=psync",
"-bs=32k",
"-fdatasync=1",
"-thread",
"-rw=randread",
"-name='fio randread test'",
"-iodepth=4",
"-runtime=60",
"-numjobs=4",
fmt.Sprintf("-filename=%s", fileRR),
"-size=1G",
"-group_reporting",
"--output-format=json",
fmt.Sprintf("--output=%s", resRR),
"> /dev/null", // ignore output
}, " "),
fmt.Sprintf("cat %s", resRR),
}, " && ")
outRR, stderr, err = e.Execute(ctx, cmdRR, false, time.Second*600)
if err != nil {
return
}
if len(stderr) > 0 {
err = fmt.Errorf("%s", stderr)
return
}
// rand read write
var (
fileRW = "fio_randread_write_test.txt"
resRW = "fio_randread_write_test.json"
)
cmdRW := strings.Join([]string{
fmt.Sprintf("mkdir -p %s && cd %s", testWd, testWd),
fmt.Sprintf("rm -f %s %s", fileRW, resRW), // cleanup any legancy files
strings.Join([]string{
fioBin,
"-ioengine=psync",
"-bs=32k",
"-fdatasync=1",
"-thread",
"-rw=randrw",
"-percentage_random=100,0",
"-name='fio mixed randread and sequential write test'",
"-iodepth=4",
"-runtime=60",
"-numjobs=4",
fmt.Sprintf("-filename=%s", fileRW),
"-size=1G",
"-group_reporting",
"--output-format=json",
fmt.Sprintf("--output=%s", resRW),
"> /dev/null", // ignore output
}, " "),
fmt.Sprintf("cat %s", resRW),
}, " && ")
outRW, stderr, err = e.Execute(ctx, cmdRW, false, time.Second*600)
if err != nil {
return
}
if len(stderr) > 0 {
err = fmt.Errorf("%s", stderr)
return
}
// rand read write
var (
fileLat = "fio_randread_write_latency_test.txt"
resLat = "fio_randread_write_latency_test.json"
)
cmdLat := strings.Join([]string{
fmt.Sprintf("mkdir -p %s && cd %s", testWd, testWd),
fmt.Sprintf("rm -f %s %s", fileLat, resLat), // cleanup any legancy files
strings.Join([]string{
fioBin,
"-ioengine=psync",
"-bs=32k",
"-fdatasync=1",
"-thread",
"-rw=randrw",
"-percentage_random=100,0",
"-name='fio mixed randread and sequential write test'",
"-iodepth=1",
"-runtime=60",
"-numjobs=1",
fmt.Sprintf("-filename=%s", fileLat),
"-size=1G",
"-group_reporting",
"--output-format=json",
fmt.Sprintf("--output=%s", resLat),
"> /dev/null", // ignore output
}, " "),
fmt.Sprintf("cat %s", resLat),
}, " && ")
outLat, stderr, err = e.Execute(ctx, cmdLat, false, time.Second*600)
if err != nil {
return
}
if len(stderr) > 0 {
err = fmt.Errorf("%s", stderr)
return
}
// cleanup
_, stderr, err = e.Execute(
ctx,
fmt.Sprintf("rm -rf %s", notExistDir),
false,
)
if err != nil {
return
}
if len(stderr) > 0 {
err = fmt.Errorf("%s", stderr)
}
return
}
|