go.etcd.io/etcd@v3.3.27+incompatible/functional/agent/utils.go (about)

     1  // Copyright 2018 The etcd Authors
     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 agent
    16  
    17  import (
    18  	"net"
    19  	"net/url"
    20  	"os"
    21  	"os/exec"
    22  	"path/filepath"
    23  	"strconv"
    24  	"time"
    25  
    26  	"github.com/coreos/etcd/pkg/fileutil"
    27  )
    28  
    29  // TODO: support separate WAL directory
    30  func archive(baseDir, etcdLogPath, dataDir string) error {
    31  	dir := filepath.Join(baseDir, "etcd-failure-archive", time.Now().Format(time.RFC3339))
    32  	if existDir(dir) {
    33  		dir = filepath.Join(baseDir, "etcd-failure-archive", time.Now().Add(time.Second).Format(time.RFC3339))
    34  	}
    35  	if err := fileutil.TouchDirAll(dir); err != nil {
    36  		return err
    37  	}
    38  
    39  	if err := os.Rename(etcdLogPath, filepath.Join(dir, "etcd.log")); err != nil {
    40  		if !os.IsNotExist(err) {
    41  			return err
    42  		}
    43  	}
    44  	if err := os.Rename(dataDir, filepath.Join(dir, filepath.Base(dataDir))); err != nil {
    45  		if !os.IsNotExist(err) {
    46  			return err
    47  		}
    48  	}
    49  
    50  	return nil
    51  }
    52  
    53  func existDir(fpath string) bool {
    54  	st, err := os.Stat(fpath)
    55  	if err != nil {
    56  		if os.IsNotExist(err) {
    57  			return false
    58  		}
    59  	} else {
    60  		return st.IsDir()
    61  	}
    62  	return false
    63  }
    64  
    65  func getURLAndPort(addr string) (urlAddr *url.URL, port int, err error) {
    66  	urlAddr, err = url.Parse(addr)
    67  	if err != nil {
    68  		return nil, -1, err
    69  	}
    70  	var s string
    71  	_, s, err = net.SplitHostPort(urlAddr.Host)
    72  	if err != nil {
    73  		return nil, -1, err
    74  	}
    75  	port, err = strconv.Atoi(s)
    76  	if err != nil {
    77  		return nil, -1, err
    78  	}
    79  	return urlAddr, port, err
    80  }
    81  
    82  func stopWithSig(cmd *exec.Cmd, sig os.Signal) error {
    83  	err := cmd.Process.Signal(sig)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	errc := make(chan error)
    89  	go func() {
    90  		_, ew := cmd.Process.Wait()
    91  		errc <- ew
    92  		close(errc)
    93  	}()
    94  
    95  	select {
    96  	case <-time.After(5 * time.Second):
    97  		cmd.Process.Kill()
    98  	case e := <-errc:
    99  		return e
   100  	}
   101  	err = <-errc
   102  	return err
   103  }
   104  
   105  func cleanPageCache() error {
   106  	// https://www.kernel.org/doc/Documentation/sysctl/vm.txt
   107  	// https://github.com/torvalds/linux/blob/master/fs/drop_caches.c
   108  	cmd := exec.Command("/bin/sh", "-c", `echo "echo 1 > /proc/sys/vm/drop_caches" | sudo sh`)
   109  	return cmd.Run()
   110  }