failure_killmaj.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // Copyright 2015 CoreOS, Inc.
  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 main
  15. import "math/rand"
  16. type failureKillMajority struct {
  17. description
  18. }
  19. func newFailureKillMajority() *failureKillMajority {
  20. return &failureKillMajority{
  21. description: "kill majority of the cluster",
  22. }
  23. }
  24. func (f *failureKillMajority) Inject(c *cluster, round int) error {
  25. for i := range getToKillMap(c.Size, round) {
  26. if err := c.Agents[i].Stop(); err != nil {
  27. return err
  28. }
  29. }
  30. return nil
  31. }
  32. func (f *failureKillMajority) Recover(c *cluster, round int) error {
  33. for i := range getToKillMap(c.Size, round) {
  34. if _, err := c.Agents[i].Restart(); err != nil {
  35. return err
  36. }
  37. }
  38. return c.WaitHealth()
  39. }
  40. func getToKillMap(size int, seed int) map[int]bool {
  41. m := make(map[int]bool)
  42. r := rand.New(rand.NewSource(int64(seed)))
  43. majority := size/2 + 1
  44. for {
  45. m[r.Intn(size)] = true
  46. if len(m) >= majority {
  47. return m
  48. }
  49. }
  50. }