vitess.io/vitess@v0.16.2/go/test/endtoend/cluster/vtgr_process.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package cluster
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"os/exec"
    23  	"path"
    24  	"strings"
    25  	"syscall"
    26  	"time"
    27  
    28  	"vitess.io/vitess/go/vt/log"
    29  )
    30  
    31  // VtgrProcess represents the vtgr process
    32  type VtgrProcess struct {
    33  	VtctlProcess
    34  	LogDir    string
    35  	ExtraArgs []string
    36  	clusters  []string
    37  	config    string
    38  	grPort    int
    39  	proc      *exec.Cmd
    40  	exit      chan error
    41  }
    42  
    43  // Start starts vtgr process with required arguements
    44  func (vtgr *VtgrProcess) Start(alias string) (err error) {
    45  	/* minimal command line arguments:
    46  	$ vtgr --topo_implementation etcd2 \
    47  	--topo_global_server_address localhost:2379 \
    48  	--topo_global_root /vitess/global \
    49  	--clusters_to_watch ks/0
    50  	*/
    51  	vtgr.proc = exec.Command(
    52  		vtgr.Binary,
    53  		"--topo_implementation", vtgr.TopoImplementation,
    54  		"--topo_global_server_address", vtgr.TopoGlobalAddress,
    55  		"--topo_global_root", vtgr.TopoGlobalRoot,
    56  		"--tablet_manager_protocol", "grpc",
    57  		"--scan_repair_timeout", "50s",
    58  		"--clusters_to_watch", strings.Join(vtgr.clusters, ","),
    59  	)
    60  	if vtgr.config != "" {
    61  		vtgr.proc.Args = append(vtgr.proc.Args, fmt.Sprintf("--config=%s", vtgr.config))
    62  	}
    63  	if vtgr.grPort != 0 {
    64  		vtgr.proc.Args = append(vtgr.proc.Args, fmt.Sprintf("--gr_port=%d", vtgr.grPort))
    65  	}
    66  	vtgr.proc.Args = append(vtgr.proc.Args, vtgr.ExtraArgs...)
    67  	errFile, _ := os.Create(path.Join(vtgr.LogDir, fmt.Sprintf("vtgr-stderr-%v.txt", alias)))
    68  	vtgr.proc.Stderr = errFile
    69  	vtgr.proc.Env = append(vtgr.proc.Env, os.Environ()...)
    70  	log.Infof("Running vtgr with command: %v", strings.Join(vtgr.proc.Args, " "))
    71  	err = vtgr.proc.Start()
    72  	if err != nil {
    73  		return
    74  	}
    75  
    76  	vtgr.exit = make(chan error)
    77  	go func() {
    78  		if vtgr.proc != nil {
    79  			vtgr.exit <- vtgr.proc.Wait()
    80  			close(vtgr.exit)
    81  		}
    82  	}()
    83  
    84  	return nil
    85  }
    86  
    87  // TearDown shuts down the running vtgr service
    88  func (vtgr *VtgrProcess) TearDown() error {
    89  	if vtgr.proc == nil || vtgr.exit == nil {
    90  		return nil
    91  	}
    92  	// Attempt graceful shutdown with SIGTERM first
    93  	_ = vtgr.proc.Process.Signal(syscall.SIGTERM)
    94  
    95  	select {
    96  	case <-vtgr.exit:
    97  		vtgr.proc = nil
    98  		return nil
    99  
   100  	case <-time.After(10 * time.Second):
   101  		vtgr.proc.Process.Kill()
   102  		err := <-vtgr.exit
   103  		vtgr.proc = nil
   104  		return err
   105  	}
   106  }