distributedstorage_maindb_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 TestSimpleDataReplicationMainDB(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. if res := clusterLayout(ms, ""); res != `
  45. TestClusterMember-0 MemberStorageManager MainDB
  46. TestClusterMember-1 MemberStorageManager MainDB
  47. TestClusterMember-2 MemberStorageManager MainDB
  48. `[1:] {
  49. t.Error("Unexpected cluster storage layout: ", res)
  50. return
  51. }
  52. // Insert a string
  53. main := cluster3[1].MainDB()
  54. main["test1"] = "123"
  55. cluster3[1].FlushMain()
  56. if res := clusterLayout(ms, ""); res != `
  57. TestClusterMember-0 MemberStorageManager MainDB
  58. test1 - "123"
  59. transfer: [TestClusterMember-1] - SetMain null "{\"test1\":\"123\"}"
  60. TestClusterMember-1 MemberStorageManager MainDB
  61. TestClusterMember-2 MemberStorageManager MainDB
  62. `[1:] {
  63. t.Error("Unexpected cluster storage layout: ", res)
  64. return
  65. }
  66. // Run the transfer worker
  67. runTransferWorker = true
  68. ms[0].transferWorker()
  69. runTransferWorker = false
  70. if res := clusterLayout(ms, ""); res != `
  71. TestClusterMember-0 MemberStorageManager MainDB
  72. test1 - "123"
  73. TestClusterMember-1 MemberStorageManager MainDB
  74. test1 - "123"
  75. TestClusterMember-2 MemberStorageManager MainDB
  76. `[1:] {
  77. t.Error("Unexpected cluster storage layout: ", res)
  78. return
  79. }
  80. // Simulate a failure on member 0
  81. manager.MemberErrors = make(map[string]error)
  82. defer func() { manager.MemberErrors = nil }()
  83. manager.MemberErrors[cluster3[0].MemberManager.Name()] = &testNetError{}
  84. // Insert another string
  85. main = cluster3[2].MainDB()
  86. if len(main) != 1 {
  87. t.Error("MainDB should have only one entry at this point:", main)
  88. return
  89. }
  90. main["test2"] = "456"
  91. cluster3[2].FlushMain()
  92. if res := clusterLayout(ms, ""); res != `
  93. TestClusterMember-0 MemberStorageManager MainDB
  94. test1 - "123"
  95. TestClusterMember-1 MemberStorageManager MainDB
  96. test1 - "123"
  97. test2 - "456"
  98. transfer: [TestClusterMember-0] - SetMain null "{\"test1\":\"123\",\"test2\":\"456\"}"
  99. TestClusterMember-2 MemberStorageManager MainDB
  100. `[1:] {
  101. t.Error("Unexpected cluster storage layout: ", res)
  102. return
  103. }
  104. // Simulate a member 0 is working again
  105. manager.MemberErrors = make(map[string]error)
  106. defer func() { manager.MemberErrors = nil }()
  107. delete(manager.MemberErrors, cluster3[0].MemberManager.Name())
  108. // Run the transfer worker
  109. runTransferWorker = true
  110. ms[0].transferWorker()
  111. ms[1].transferWorker()
  112. ms[2].transferWorker()
  113. runTransferWorker = false
  114. if res := clusterLayout(ms, ""); res != `
  115. TestClusterMember-0 MemberStorageManager MainDB
  116. test1 - "123"
  117. test2 - "456"
  118. TestClusterMember-1 MemberStorageManager MainDB
  119. test1 - "123"
  120. test2 - "456"
  121. TestClusterMember-2 MemberStorageManager MainDB
  122. `[1:] {
  123. t.Error("Unexpected cluster storage layout: ", res)
  124. return
  125. }
  126. main = cluster3[2].MainDB()
  127. if len(main) != 2 {
  128. t.Error("MainDB should have two entries at this point:", main)
  129. return
  130. }
  131. // Remove from main DB
  132. main = cluster3[1].MainDB()
  133. delete(main, "test1")
  134. cluster3[1].FlushMain()
  135. runTransferWorker = true
  136. ms[0].transferWorker()
  137. ms[1].transferWorker()
  138. ms[2].transferWorker()
  139. runTransferWorker = false
  140. if res := clusterLayout(ms, ""); res != `
  141. TestClusterMember-0 MemberStorageManager MainDB
  142. test2 - "456"
  143. TestClusterMember-1 MemberStorageManager MainDB
  144. test2 - "456"
  145. TestClusterMember-2 MemberStorageManager MainDB
  146. `[1:] {
  147. t.Error("Unexpected cluster storage layout: ", res)
  148. return
  149. }
  150. }