handler.go 20 KB

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