distributedstorage_root_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package cluster
  11. import (
  12. "math"
  13. "testing"
  14. "devt.de/krotik/eliasdb/cluster/manager"
  15. )
  16. func TestSimpleDataReplicationRoot(t *testing.T) {
  17. // Set a low distribution range
  18. defaultDistributionRange = 10
  19. defer func() { defaultDistributionRange = math.MaxUint64 }()
  20. // Setup a cluster
  21. manager.FreqHousekeeping = 5
  22. defer func() { manager.FreqHousekeeping = 1000 }()
  23. // Disable the transfer worker for this test
  24. runTransferWorker = false
  25. defer func() { runTransferWorker = true }()
  26. // Create a cluster with 3 members and a replication factor of 2
  27. cluster3, ms := createCluster(3, 2)
  28. // Debug output
  29. // manager.LogDebug = manager.LogInfo
  30. // log.SetOutput(os.Stderr)
  31. // defer func() { log.SetOutput(ioutil.Discard) }()
  32. for i, dd := range cluster3 {
  33. dd.Start()
  34. defer dd.Close()
  35. if i > 0 {
  36. err := dd.MemberManager.JoinCluster(cluster3[0].MemberManager.Name(), cluster3[0].MemberManager.NetAddr())
  37. if err != nil {
  38. t.Error(err)
  39. return
  40. }
  41. }
  42. }
  43. // Main DB should be empty at this point
  44. sm := cluster3[1].StorageManager("test", true)
  45. sm.SetRoot(5, 10)
  46. if res := clusterLayout(ms, "test"); res != `
  47. TestClusterMember-0 MemberStorageManager mgs1/ls_test
  48. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=0 9=0
  49. transfer: [TestClusterMember-1] - SetRoot {"Root":5,"StoreName":"test"} "10"
  50. `[1:] {
  51. t.Error("Unexpected cluster storage layout: ", res)
  52. return
  53. }
  54. // Run the transfer worker
  55. runTransferWorker = true
  56. ms[0].transferWorker()
  57. runTransferWorker = false
  58. if res := clusterLayout(ms, "test"); res != `
  59. TestClusterMember-0 MemberStorageManager mgs1/ls_test
  60. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=0 9=0
  61. TestClusterMember-1 MemberStorageManager mgs2/ls_test
  62. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=0 9=0
  63. `[1:] {
  64. t.Error("Unexpected cluster storage layout: ", res)
  65. return
  66. }
  67. // Simulate a failure on member 0
  68. manager.MemberErrors = make(map[string]error)
  69. defer func() { manager.MemberErrors = nil }()
  70. manager.MemberErrors[cluster3[0].MemberManager.Name()] = &testNetError{}
  71. sm = cluster3[2].StorageManager("test", true)
  72. sm.SetRoot(8, 14)
  73. if res := clusterLayout(ms, "test"); res != `
  74. TestClusterMember-0 MemberStorageManager mgs1/ls_test
  75. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=0 9=0
  76. TestClusterMember-1 MemberStorageManager mgs2/ls_test
  77. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=14 9=0
  78. transfer: [TestClusterMember-0] - SetRoot {"Root":8,"StoreName":"test"} "14"
  79. `[1:] {
  80. t.Error("Unexpected cluster storage layout: ", res)
  81. return
  82. }
  83. sm = cluster3[2].StorageManager("test", true)
  84. if rv := sm.Root(8); rv != 14 {
  85. t.Error("Unexpected root value returned:", rv)
  86. return
  87. }
  88. // Simulate a member 0 is working again
  89. manager.MemberErrors = make(map[string]error)
  90. defer func() { manager.MemberErrors = nil }()
  91. delete(manager.MemberErrors, cluster3[0].MemberManager.Name())
  92. // Run the transfer worker
  93. runTransferWorker = true
  94. ms[0].transferWorker()
  95. ms[1].transferWorker()
  96. ms[2].transferWorker()
  97. runTransferWorker = false
  98. if res := clusterLayout(ms, "test"); res != `
  99. TestClusterMember-0 MemberStorageManager mgs1/ls_test
  100. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=14 9=0
  101. TestClusterMember-1 MemberStorageManager mgs2/ls_test
  102. Roots: 0=0 1=0 2=0 3=0 4=0 5=10 6=0 7=0 8=14 9=0
  103. `[1:] {
  104. t.Error("Unexpected cluster storage layout: ", res)
  105. return
  106. }
  107. }