handler.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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. package agent
  15. import (
  16. "errors"
  17. "fmt"
  18. "io/ioutil"
  19. "net/url"
  20. "os"
  21. "os/exec"
  22. "path/filepath"
  23. "syscall"
  24. "time"
  25. "github.com/coreos/etcd/functional/rpcpb"
  26. "github.com/coreos/etcd/pkg/fileutil"
  27. "github.com/coreos/etcd/pkg/proxy"
  28. "go.uber.org/zap"
  29. )
  30. // return error for system errors (e.g. fail to create files)
  31. // return status error in response for wrong configuration/operation (e.g. start etcd twice)
  32. func (srv *Server) handleTesterRequest(req *rpcpb.Request) (resp *rpcpb.Response, err error) {
  33. defer func() {
  34. if err == nil && req != nil {
  35. srv.last = req.Operation
  36. srv.lg.Info("handler success", zap.String("operation", req.Operation.String()))
  37. }
  38. }()
  39. if req != nil {
  40. srv.Member = req.Member
  41. srv.Tester = req.Tester
  42. }
  43. switch req.Operation {
  44. case rpcpb.Operation_INITIAL_START_ETCD:
  45. return srv.handle_INITIAL_START_ETCD(req)
  46. case rpcpb.Operation_RESTART_ETCD:
  47. return srv.handle_RESTART_ETCD()
  48. case rpcpb.Operation_SIGTERM_ETCD:
  49. return srv.handle_SIGTERM_ETCD()
  50. case rpcpb.Operation_SIGQUIT_ETCD_AND_REMOVE_DATA:
  51. return srv.handle_SIGQUIT_ETCD_AND_REMOVE_DATA()
  52. case rpcpb.Operation_SAVE_SNAPSHOT:
  53. return srv.handle_SAVE_SNAPSHOT()
  54. case rpcpb.Operation_RESTORE_RESTART_FROM_SNAPSHOT:
  55. return srv.handle_RESTORE_RESTART_FROM_SNAPSHOT()
  56. case rpcpb.Operation_RESTART_FROM_SNAPSHOT:
  57. return srv.handle_RESTART_FROM_SNAPSHOT()
  58. case rpcpb.Operation_SIGQUIT_ETCD_AND_ARCHIVE_DATA:
  59. return srv.handle_SIGQUIT_ETCD_AND_ARCHIVE_DATA()
  60. case rpcpb.Operation_SIGQUIT_ETCD_AND_REMOVE_DATA_AND_STOP_AGENT:
  61. return srv.handle_SIGQUIT_ETCD_AND_REMOVE_DATA_AND_STOP_AGENT()
  62. case rpcpb.Operation_BLACKHOLE_PEER_PORT_TX_RX:
  63. return srv.handle_BLACKHOLE_PEER_PORT_TX_RX()
  64. case rpcpb.Operation_UNBLACKHOLE_PEER_PORT_TX_RX:
  65. return srv.handle_UNBLACKHOLE_PEER_PORT_TX_RX()
  66. case rpcpb.Operation_DELAY_PEER_PORT_TX_RX:
  67. return srv.handle_DELAY_PEER_PORT_TX_RX()
  68. case rpcpb.Operation_UNDELAY_PEER_PORT_TX_RX:
  69. return srv.handle_UNDELAY_PEER_PORT_TX_RX()
  70. default:
  71. msg := fmt.Sprintf("operation not found (%v)", req.Operation)
  72. return &rpcpb.Response{Success: false, Status: msg}, errors.New(msg)
  73. }
  74. }
  75. func (srv *Server) handle_INITIAL_START_ETCD(req *rpcpb.Request) (*rpcpb.Response, error) {
  76. if srv.last != rpcpb.Operation_NOT_STARTED {
  77. return &rpcpb.Response{
  78. Success: false,
  79. Status: fmt.Sprintf("%q is not valid; last server operation was %q", rpcpb.Operation_INITIAL_START_ETCD.String(), srv.last.String()),
  80. Member: req.Member,
  81. }, nil
  82. }
  83. err := fileutil.TouchDirAll(srv.Member.BaseDir)
  84. if err != nil {
  85. return nil, err
  86. }
  87. srv.lg.Info("created base directory", zap.String("path", srv.Member.BaseDir))
  88. if err = srv.createEtcdLogFile(); err != nil {
  89. return nil, err
  90. }
  91. srv.creatEtcdCmd(false)
  92. if err = srv.saveTLSAssets(); err != nil {
  93. return nil, err
  94. }
  95. if err = srv.startEtcdCmd(); err != nil {
  96. return nil, err
  97. }
  98. srv.lg.Info("started etcd", zap.String("command-path", srv.etcdCmd.Path))
  99. if err = srv.loadAutoTLSAssets(); err != nil {
  100. return nil, err
  101. }
  102. // wait some time for etcd listener start
  103. // before setting up proxy
  104. time.Sleep(time.Second)
  105. if err = srv.startProxy(); err != nil {
  106. return nil, err
  107. }
  108. return &rpcpb.Response{
  109. Success: true,
  110. Status: "start etcd PASS",
  111. Member: srv.Member,
  112. }, nil
  113. }
  114. func (srv *Server) startProxy() error {
  115. if srv.Member.EtcdClientProxy {
  116. advertiseClientURL, advertiseClientURLPort, err := getURLAndPort(srv.Member.Etcd.AdvertiseClientURLs[0])
  117. if err != nil {
  118. return err
  119. }
  120. listenClientURL, _, err := getURLAndPort(srv.Member.Etcd.ListenClientURLs[0])
  121. if err != nil {
  122. return err
  123. }
  124. srv.advertiseClientPortToProxy[advertiseClientURLPort] = proxy.NewServer(proxy.ServerConfig{
  125. Logger: srv.lg,
  126. From: *advertiseClientURL,
  127. To: *listenClientURL,
  128. })
  129. select {
  130. case err = <-srv.advertiseClientPortToProxy[advertiseClientURLPort].Error():
  131. return err
  132. case <-time.After(2 * time.Second):
  133. srv.lg.Info("started proxy on client traffic", zap.String("url", advertiseClientURL.String()))
  134. }
  135. }
  136. if srv.Member.EtcdPeerProxy {
  137. advertisePeerURL, advertisePeerURLPort, err := getURLAndPort(srv.Member.Etcd.AdvertisePeerURLs[0])
  138. if err != nil {
  139. return err
  140. }
  141. listenPeerURL, _, err := getURLAndPort(srv.Member.Etcd.ListenPeerURLs[0])
  142. if err != nil {
  143. return err
  144. }
  145. srv.advertisePeerPortToProxy[advertisePeerURLPort] = proxy.NewServer(proxy.ServerConfig{
  146. Logger: srv.lg,
  147. From: *advertisePeerURL,
  148. To: *listenPeerURL,
  149. })
  150. select {
  151. case err = <-srv.advertisePeerPortToProxy[advertisePeerURLPort].Error():
  152. return err
  153. case <-time.After(2 * time.Second):
  154. srv.lg.Info("started proxy on peer traffic", zap.String("url", advertisePeerURL.String()))
  155. }
  156. }
  157. return nil
  158. }
  159. func (srv *Server) stopProxy() {
  160. if srv.Member.EtcdClientProxy && len(srv.advertiseClientPortToProxy) > 0 {
  161. for port, px := range srv.advertiseClientPortToProxy {
  162. if err := px.Close(); err != nil {
  163. srv.lg.Warn("failed to close proxy", zap.Int("port", port))
  164. continue
  165. }
  166. select {
  167. case <-px.Done():
  168. // enough time to release port
  169. time.Sleep(time.Second)
  170. case <-time.After(time.Second):
  171. }
  172. srv.lg.Info("closed proxy",
  173. zap.Int("port", port),
  174. zap.String("from", px.From()),
  175. zap.String("to", px.To()),
  176. )
  177. }
  178. srv.advertiseClientPortToProxy = make(map[int]proxy.Server)
  179. }
  180. if srv.Member.EtcdPeerProxy && len(srv.advertisePeerPortToProxy) > 0 {
  181. for port, px := range srv.advertisePeerPortToProxy {
  182. if err := px.Close(); err != nil {
  183. srv.lg.Warn("failed to close proxy", zap.Int("port", port))
  184. continue
  185. }
  186. select {
  187. case <-px.Done():
  188. // enough time to release port
  189. time.Sleep(time.Second)
  190. case <-time.After(time.Second):
  191. }
  192. srv.lg.Info("closed proxy",
  193. zap.Int("port", port),
  194. zap.String("from", px.From()),
  195. zap.String("to", px.To()),
  196. )
  197. }
  198. srv.advertisePeerPortToProxy = make(map[int]proxy.Server)
  199. }
  200. }
  201. func (srv *Server) createEtcdLogFile() error {
  202. var err error
  203. srv.etcdLogFile, err = os.Create(srv.Member.EtcdLogPath)
  204. if err != nil {
  205. return err
  206. }
  207. srv.lg.Info("created etcd log file", zap.String("path", srv.Member.EtcdLogPath))
  208. return nil
  209. }
  210. func (srv *Server) creatEtcdCmd(fromSnapshot bool) {
  211. etcdPath, etcdFlags := srv.Member.EtcdExecPath, srv.Member.Etcd.Flags()
  212. if fromSnapshot {
  213. etcdFlags = srv.Member.EtcdOnSnapshotRestore.Flags()
  214. }
  215. u, _ := url.Parse(srv.Member.FailpointHTTPAddr)
  216. srv.lg.Info("creating etcd command",
  217. zap.String("etcd-exec-path", etcdPath),
  218. zap.Strings("etcd-flags", etcdFlags),
  219. zap.String("failpoint-http-addr", srv.Member.FailpointHTTPAddr),
  220. zap.String("failpoint-addr", u.Host),
  221. )
  222. srv.etcdCmd = exec.Command(etcdPath, etcdFlags...)
  223. srv.etcdCmd.Env = []string{"GOFAIL_HTTP=" + u.Host}
  224. srv.etcdCmd.Stdout = srv.etcdLogFile
  225. srv.etcdCmd.Stderr = srv.etcdLogFile
  226. }
  227. // if started with manual TLS, stores TLS assets
  228. // from tester/client to disk before starting etcd process
  229. func (srv *Server) saveTLSAssets() error {
  230. if srv.Member.PeerCertPath != "" {
  231. if srv.Member.PeerCertData == "" {
  232. return fmt.Errorf("got empty data for %q", srv.Member.PeerCertPath)
  233. }
  234. if err := ioutil.WriteFile(srv.Member.PeerCertPath, []byte(srv.Member.PeerCertData), 0644); err != nil {
  235. return err
  236. }
  237. }
  238. if srv.Member.PeerKeyPath != "" {
  239. if srv.Member.PeerKeyData == "" {
  240. return fmt.Errorf("got empty data for %q", srv.Member.PeerKeyPath)
  241. }
  242. if err := ioutil.WriteFile(srv.Member.PeerKeyPath, []byte(srv.Member.PeerKeyData), 0644); err != nil {
  243. return err
  244. }
  245. }
  246. if srv.Member.PeerTrustedCAPath != "" {
  247. if srv.Member.PeerTrustedCAData == "" {
  248. return fmt.Errorf("got empty data for %q", srv.Member.PeerTrustedCAPath)
  249. }
  250. if err := ioutil.WriteFile(srv.Member.PeerTrustedCAPath, []byte(srv.Member.PeerTrustedCAData), 0644); err != nil {
  251. return err
  252. }
  253. }
  254. if srv.Member.PeerCertPath != "" &&
  255. srv.Member.PeerKeyPath != "" &&
  256. srv.Member.PeerTrustedCAPath != "" {
  257. srv.lg.Info(
  258. "wrote",
  259. zap.String("peer-cert", srv.Member.PeerCertPath),
  260. zap.String("peer-key", srv.Member.PeerKeyPath),
  261. zap.String("peer-trusted-ca", srv.Member.PeerTrustedCAPath),
  262. )
  263. }
  264. if srv.Member.ClientCertPath != "" {
  265. if srv.Member.ClientCertData == "" {
  266. return fmt.Errorf("got empty data for %q", srv.Member.ClientCertPath)
  267. }
  268. if err := ioutil.WriteFile(srv.Member.ClientCertPath, []byte(srv.Member.ClientCertData), 0644); err != nil {
  269. return err
  270. }
  271. }
  272. if srv.Member.ClientKeyPath != "" {
  273. if srv.Member.ClientKeyData == "" {
  274. return fmt.Errorf("got empty data for %q", srv.Member.ClientKeyPath)
  275. }
  276. if err := ioutil.WriteFile(srv.Member.ClientKeyPath, []byte(srv.Member.ClientKeyData), 0644); err != nil {
  277. return err
  278. }
  279. }
  280. if srv.Member.ClientTrustedCAPath != "" {
  281. if srv.Member.ClientTrustedCAData == "" {
  282. return fmt.Errorf("got empty data for %q", srv.Member.ClientTrustedCAPath)
  283. }
  284. if err := ioutil.WriteFile(srv.Member.ClientTrustedCAPath, []byte(srv.Member.ClientTrustedCAData), 0644); err != nil {
  285. return err
  286. }
  287. }
  288. if srv.Member.ClientCertPath != "" &&
  289. srv.Member.ClientKeyPath != "" &&
  290. srv.Member.ClientTrustedCAPath != "" {
  291. srv.lg.Info(
  292. "wrote",
  293. zap.String("client-cert", srv.Member.ClientCertPath),
  294. zap.String("client-key", srv.Member.ClientKeyPath),
  295. zap.String("client-trusted-ca", srv.Member.ClientTrustedCAPath),
  296. )
  297. }
  298. return nil
  299. }
  300. func (srv *Server) loadAutoTLSAssets() error {
  301. if srv.Member.Etcd.PeerAutoTLS {
  302. // in case of slow disk
  303. time.Sleep(time.Second)
  304. fdir := filepath.Join(srv.Member.Etcd.DataDir, "fixtures", "peer")
  305. srv.lg.Info(
  306. "loading client auto TLS assets",
  307. zap.String("dir", fdir),
  308. zap.String("endpoint", srv.EtcdClientEndpoint),
  309. )
  310. certPath := filepath.Join(fdir, "cert.pem")
  311. if !fileutil.Exist(certPath) {
  312. return fmt.Errorf("cannot find %q", certPath)
  313. }
  314. certData, err := ioutil.ReadFile(certPath)
  315. if err != nil {
  316. return fmt.Errorf("cannot read %q (%v)", certPath, err)
  317. }
  318. srv.Member.PeerCertData = string(certData)
  319. keyPath := filepath.Join(fdir, "key.pem")
  320. if !fileutil.Exist(keyPath) {
  321. return fmt.Errorf("cannot find %q", keyPath)
  322. }
  323. keyData, err := ioutil.ReadFile(keyPath)
  324. if err != nil {
  325. return fmt.Errorf("cannot read %q (%v)", keyPath, err)
  326. }
  327. srv.Member.PeerKeyData = string(keyData)
  328. srv.lg.Info(
  329. "loaded peer auto TLS assets",
  330. zap.String("peer-cert-path", certPath),
  331. zap.Int("peer-cert-length", len(certData)),
  332. zap.String("peer-key-path", keyPath),
  333. zap.Int("peer-key-length", len(keyData)),
  334. )
  335. }
  336. if srv.Member.Etcd.ClientAutoTLS {
  337. // in case of slow disk
  338. time.Sleep(time.Second)
  339. fdir := filepath.Join(srv.Member.Etcd.DataDir, "fixtures", "client")
  340. srv.lg.Info(
  341. "loading client TLS assets",
  342. zap.String("dir", fdir),
  343. zap.String("endpoint", srv.EtcdClientEndpoint),
  344. )
  345. certPath := filepath.Join(fdir, "cert.pem")
  346. if !fileutil.Exist(certPath) {
  347. return fmt.Errorf("cannot find %q", certPath)
  348. }
  349. certData, err := ioutil.ReadFile(certPath)
  350. if err != nil {
  351. return fmt.Errorf("cannot read %q (%v)", certPath, err)
  352. }
  353. srv.Member.ClientCertData = string(certData)
  354. keyPath := filepath.Join(fdir, "key.pem")
  355. if !fileutil.Exist(keyPath) {
  356. return fmt.Errorf("cannot find %q", keyPath)
  357. }
  358. keyData, err := ioutil.ReadFile(keyPath)
  359. if err != nil {
  360. return fmt.Errorf("cannot read %q (%v)", keyPath, err)
  361. }
  362. srv.Member.ClientKeyData = string(keyData)
  363. srv.lg.Info(
  364. "loaded client TLS assets",
  365. zap.String("peer-cert-path", certPath),
  366. zap.Int("peer-cert-length", len(certData)),
  367. zap.String("peer-key-path", keyPath),
  368. zap.Int("peer-key-length", len(keyData)),
  369. )
  370. }
  371. return nil
  372. }
  373. // start but do not wait for it to complete
  374. func (srv *Server) startEtcdCmd() error {
  375. return srv.etcdCmd.Start()
  376. }
  377. func (srv *Server) handle_RESTART_ETCD() (*rpcpb.Response, error) {
  378. var err error
  379. if !fileutil.Exist(srv.Member.BaseDir) {
  380. err = fileutil.TouchDirAll(srv.Member.BaseDir)
  381. if err != nil {
  382. return nil, err
  383. }
  384. }
  385. srv.creatEtcdCmd(false)
  386. if err = srv.saveTLSAssets(); err != nil {
  387. return nil, err
  388. }
  389. if err = srv.startEtcdCmd(); err != nil {
  390. return nil, err
  391. }
  392. srv.lg.Info("restarted etcd", zap.String("command-path", srv.etcdCmd.Path))
  393. if err = srv.loadAutoTLSAssets(); err != nil {
  394. return nil, err
  395. }
  396. // wait some time for etcd listener start
  397. // before setting up proxy
  398. // TODO: local tests should handle port conflicts
  399. // with clients on restart
  400. time.Sleep(time.Second)
  401. if err = srv.startProxy(); err != nil {
  402. return nil, err
  403. }
  404. return &rpcpb.Response{
  405. Success: true,
  406. Status: "restart etcd PASS",
  407. Member: srv.Member,
  408. }, nil
  409. }
  410. func (srv *Server) handle_SIGTERM_ETCD() (*rpcpb.Response, error) {
  411. srv.stopProxy()
  412. err := stopWithSig(srv.etcdCmd, syscall.SIGTERM)
  413. if err != nil {
  414. return nil, err
  415. }
  416. srv.lg.Info("killed etcd", zap.String("signal", syscall.SIGTERM.String()))
  417. return &rpcpb.Response{
  418. Success: true,
  419. Status: "killed etcd",
  420. }, nil
  421. }
  422. func (srv *Server) handle_SIGQUIT_ETCD_AND_REMOVE_DATA() (*rpcpb.Response, error) {
  423. srv.stopProxy()
  424. err := stopWithSig(srv.etcdCmd, syscall.SIGQUIT)
  425. if err != nil {
  426. return nil, err
  427. }
  428. srv.lg.Info("killed etcd", zap.String("signal", syscall.SIGQUIT.String()))
  429. srv.etcdLogFile.Sync()
  430. srv.etcdLogFile.Close()
  431. // for debugging purposes, rename instead of removing
  432. if err = os.RemoveAll(srv.Member.BaseDir + ".backup"); err != nil {
  433. return nil, err
  434. }
  435. if err = os.Rename(srv.Member.BaseDir, srv.Member.BaseDir+".backup"); err != nil {
  436. return nil, err
  437. }
  438. srv.lg.Info(
  439. "renamed",
  440. zap.String("base-dir", srv.Member.BaseDir),
  441. zap.String("new-dir", srv.Member.BaseDir+".backup"),
  442. )
  443. // create a new log file for next new member restart
  444. if !fileutil.Exist(srv.Member.BaseDir) {
  445. err = fileutil.TouchDirAll(srv.Member.BaseDir)
  446. if err != nil {
  447. return nil, err
  448. }
  449. }
  450. if err = srv.createEtcdLogFile(); err != nil {
  451. return nil, err
  452. }
  453. return &rpcpb.Response{
  454. Success: true,
  455. Status: "killed etcd and removed base directory",
  456. }, nil
  457. }
  458. func (srv *Server) handle_SAVE_SNAPSHOT() (*rpcpb.Response, error) {
  459. err := srv.Member.SaveSnapshot(srv.lg)
  460. if err != nil {
  461. return nil, err
  462. }
  463. return &rpcpb.Response{
  464. Success: true,
  465. Status: "saved snapshot",
  466. SnapshotInfo: srv.Member.SnapshotInfo,
  467. }, nil
  468. }
  469. func (srv *Server) handle_RESTORE_RESTART_FROM_SNAPSHOT() (resp *rpcpb.Response, err error) {
  470. err = srv.Member.RestoreSnapshot(srv.lg)
  471. if err != nil {
  472. return nil, err
  473. }
  474. resp, err = srv.handle_RESTART_FROM_SNAPSHOT()
  475. if resp != nil && err == nil {
  476. resp.Status = "restored snapshot and " + resp.Status
  477. }
  478. return resp, err
  479. }
  480. func (srv *Server) handle_RESTART_FROM_SNAPSHOT() (resp *rpcpb.Response, err error) {
  481. srv.creatEtcdCmd(true)
  482. if err = srv.saveTLSAssets(); err != nil {
  483. return nil, err
  484. }
  485. if err = srv.startEtcdCmd(); err != nil {
  486. return nil, err
  487. }
  488. srv.lg.Info("restarted etcd", zap.String("command-path", srv.etcdCmd.Path))
  489. if err = srv.loadAutoTLSAssets(); err != nil {
  490. return nil, err
  491. }
  492. // wait some time for etcd listener start
  493. // before setting up proxy
  494. // TODO: local tests should handle port conflicts
  495. // with clients on restart
  496. time.Sleep(time.Second)
  497. if err = srv.startProxy(); err != nil {
  498. return nil, err
  499. }
  500. return &rpcpb.Response{
  501. Success: true,
  502. Status: "restarted etcd from snapshot",
  503. SnapshotInfo: srv.Member.SnapshotInfo,
  504. }, nil
  505. }
  506. func (srv *Server) handle_SIGQUIT_ETCD_AND_ARCHIVE_DATA() (*rpcpb.Response, error) {
  507. srv.stopProxy()
  508. // exit with stackstrace
  509. err := stopWithSig(srv.etcdCmd, syscall.SIGQUIT)
  510. if err != nil {
  511. return nil, err
  512. }
  513. srv.lg.Info("killed etcd", zap.String("signal", syscall.SIGQUIT.String()))
  514. srv.etcdLogFile.Sync()
  515. srv.etcdLogFile.Close()
  516. // TODO: support separate WAL directory
  517. if err = archive(
  518. srv.Member.BaseDir,
  519. srv.Member.EtcdLogPath,
  520. srv.Member.Etcd.DataDir,
  521. ); err != nil {
  522. return nil, err
  523. }
  524. srv.lg.Info("archived data", zap.String("base-dir", srv.Member.BaseDir))
  525. if err = srv.createEtcdLogFile(); err != nil {
  526. return nil, err
  527. }
  528. srv.lg.Info("cleaning up page cache")
  529. if err := cleanPageCache(); err != nil {
  530. srv.lg.Warn("failed to clean up page cache", zap.String("error", err.Error()))
  531. }
  532. srv.lg.Info("cleaned up page cache")
  533. return &rpcpb.Response{
  534. Success: true,
  535. Status: "cleaned up etcd",
  536. }, nil
  537. }
  538. // stop proxy, etcd, delete data directory
  539. func (srv *Server) handle_SIGQUIT_ETCD_AND_REMOVE_DATA_AND_STOP_AGENT() (*rpcpb.Response, error) {
  540. srv.stopProxy()
  541. err := stopWithSig(srv.etcdCmd, syscall.SIGQUIT)
  542. if err != nil {
  543. return nil, err
  544. }
  545. srv.lg.Info("killed etcd", zap.String("signal", syscall.SIGQUIT.String()))
  546. srv.etcdLogFile.Sync()
  547. srv.etcdLogFile.Close()
  548. err = os.RemoveAll(srv.Member.BaseDir)
  549. if err != nil {
  550. return nil, err
  551. }
  552. srv.lg.Info("removed base directory", zap.String("dir", srv.Member.BaseDir))
  553. // stop agent server
  554. srv.Stop()
  555. return &rpcpb.Response{
  556. Success: true,
  557. Status: "destroyed etcd and agent",
  558. }, nil
  559. }
  560. func (srv *Server) handle_BLACKHOLE_PEER_PORT_TX_RX() (*rpcpb.Response, error) {
  561. for port, px := range srv.advertisePeerPortToProxy {
  562. srv.lg.Info("blackholing", zap.Int("peer-port", port))
  563. px.BlackholeTx()
  564. px.BlackholeRx()
  565. srv.lg.Info("blackholed", zap.Int("peer-port", port))
  566. }
  567. return &rpcpb.Response{
  568. Success: true,
  569. Status: "blackholed peer port tx/rx",
  570. }, nil
  571. }
  572. func (srv *Server) handle_UNBLACKHOLE_PEER_PORT_TX_RX() (*rpcpb.Response, error) {
  573. for port, px := range srv.advertisePeerPortToProxy {
  574. srv.lg.Info("unblackholing", zap.Int("peer-port", port))
  575. px.UnblackholeTx()
  576. px.UnblackholeRx()
  577. srv.lg.Info("unblackholed", zap.Int("peer-port", port))
  578. }
  579. return &rpcpb.Response{
  580. Success: true,
  581. Status: "unblackholed peer port tx/rx",
  582. }, nil
  583. }
  584. func (srv *Server) handle_DELAY_PEER_PORT_TX_RX() (*rpcpb.Response, error) {
  585. lat := time.Duration(srv.Tester.UpdatedDelayLatencyMs) * time.Millisecond
  586. rv := time.Duration(srv.Tester.DelayLatencyMsRv) * time.Millisecond
  587. for port, px := range srv.advertisePeerPortToProxy {
  588. srv.lg.Info("delaying",
  589. zap.Int("peer-port", port),
  590. zap.Duration("latency", lat),
  591. zap.Duration("random-variable", rv),
  592. )
  593. px.DelayTx(lat, rv)
  594. px.DelayRx(lat, rv)
  595. srv.lg.Info("delayed",
  596. zap.Int("peer-port", port),
  597. zap.Duration("latency", lat),
  598. zap.Duration("random-variable", rv),
  599. )
  600. }
  601. return &rpcpb.Response{
  602. Success: true,
  603. Status: "delayed peer port tx/rx",
  604. }, nil
  605. }
  606. func (srv *Server) handle_UNDELAY_PEER_PORT_TX_RX() (*rpcpb.Response, error) {
  607. for port, px := range srv.advertisePeerPortToProxy {
  608. srv.lg.Info("undelaying", zap.Int("peer-port", port))
  609. px.UndelayTx()
  610. px.UndelayRx()
  611. srv.lg.Info("undelayed", zap.Int("peer-port", port))
  612. }
  613. return &rpcpb.Response{
  614. Success: true,
  615. Status: "undelayed peer port tx/rx",
  616. }, nil
  617. }