memberaddresstable_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. "errors"
  13. "math"
  14. "testing"
  15. "devt.de/krotik/eliasdb/hash"
  16. "devt.de/krotik/eliasdb/storage"
  17. )
  18. func TestAddressTableClusterLoc(t *testing.T) {
  19. // Set a low distribution range
  20. defaultDistributionRange = 3
  21. defer func() { defaultDistributionRange = math.MaxUint64 }()
  22. // Test an inoperable cluster
  23. cluster1, ms1 := createCluster(1, 1)
  24. cluster1[0].distributionTable = nil
  25. cluster1[0].distributionTableError = errors.New("testerror")
  26. _, err := ms1[0].at.NewClusterLoc("test1")
  27. if err.Error() != "Storage is currently disabled on member: TestClusterMember-0 (testerror)" {
  28. t.Error("Unexpected result:", err)
  29. return
  30. }
  31. // Test normal number sequence
  32. cluster1, ms1 = createCluster(1, 1)
  33. loc, err := ms1[0].at.NewClusterLoc("test1")
  34. if loc != 1 || err != nil {
  35. t.Error("Unexpected result:", loc, err)
  36. return
  37. }
  38. ms1[0].at.SetTransClusterLoc("test1", loc, 123, 1)
  39. // Starting an unrelated counter should have no effect
  40. loc, err = ms1[0].at.NewClusterLoc("test2")
  41. if loc != 1 || err != nil {
  42. t.Error("Unexpected result:", loc, err)
  43. return
  44. }
  45. delete(ms1[0].at.newlocCounters, "test1")
  46. // Advance the counter
  47. loc, err = ms1[0].at.NewClusterLoc("test1")
  48. if loc != 2 || err != nil {
  49. t.Error("Unexpected result:", loc, err)
  50. return
  51. }
  52. ms1[0].at.SetTransClusterLoc("test1", loc, 123, 1)
  53. loc, err = ms1[0].at.NewClusterLoc("test1")
  54. if loc != 3 || err != nil {
  55. t.Error("Unexpected result:", loc, err)
  56. return
  57. }
  58. // Not filling the location 3
  59. // The next call should find the free location 3
  60. loc, err = ms1[0].at.NewClusterLoc("test1")
  61. if loc != 3 || err != nil {
  62. t.Error("Unexpected result:", loc, err)
  63. return
  64. }
  65. ms1[0].at.SetTransClusterLoc("test1", loc, 123, 1)
  66. // Now we are full the next call should error
  67. loc, err = ms1[0].at.NewClusterLoc("test1")
  68. if err.Error() != "Could not find any free storage location on this member" {
  69. t.Error("Unexpected result:", loc, err)
  70. return
  71. }
  72. // Change distribution table - test member which is in the middle of the cluster
  73. defaultDistributionRange = 120
  74. dd, _ := NewDistributionTable([]string{"aa", cluster1[0].MemberManager.Name(), "bb"}, 1)
  75. cluster1[0].distributionTable = dd
  76. cluster1[0].distributionTableError = nil
  77. start, end := cluster1[0].distributionTable.MemberRange(cluster1[0].MemberManager.Name())
  78. if start != 40 || end != 79 {
  79. t.Error("Unexpected range:", start, end)
  80. return
  81. }
  82. loc, err = ms1[0].at.NewClusterLoc("test1")
  83. if loc != 40 || err != nil {
  84. t.Error("Unexpected result:", loc, err)
  85. return
  86. }
  87. ms1[0].at.SetTransClusterLoc("test1", loc, 123, 1)
  88. // Simulate a lookup failure
  89. for i := 40; i < 70; i++ {
  90. ms1[0].at.SetTransClusterLoc("test1", uint64(i), 123, 1)
  91. }
  92. // Get the translation location for cluster location 6
  93. _, loc, _ = ms1[0].at.translation.GetValueAndLocation(transKey("test1", 69))
  94. msm := ms1[0].at.sm.(*storage.MemoryStorageManager)
  95. msm.AccessMap[loc] = storage.AccessCacheAndFetchSeriousError
  96. loc, err = ms1[0].at.NewClusterLoc("test1")
  97. if err.Error() != "Record is already in-use (<memory> - )" {
  98. t.Error("Unexpected result:", loc, err)
  99. return
  100. }
  101. delete(msm.AccessMap, loc)
  102. // Recreate the member address table
  103. ms1[0].at, err = newMemberAddressTable(cluster1[0], ms1[0].at.sm)
  104. if err != nil {
  105. t.Error("Could not recreate MemberAddressTable:", err)
  106. return
  107. }
  108. // Now check the translation lookup
  109. if tr, ok, err := ms1[0].at.TransClusterLoc("test1", 50); tr.Loc != 123 || tr.Ver != 1 || !ok || err != nil {
  110. t.Error("Unexpected translation:", tr, ok, err)
  111. return
  112. }
  113. if tr, ok, err := ms1[0].at.TransClusterLoc("test1", 150); tr != nil || ok || err != nil {
  114. t.Error("Unexpected translation:", tr, ok, err)
  115. return
  116. }
  117. if tr, ok, err := ms1[0].at.SetTransClusterLoc("test1", 50, 555, 2); tr.Loc != 123 || tr.Ver != 1 || !ok || err != nil {
  118. t.Error("Unexpected translation:", tr, ok, err)
  119. return
  120. }
  121. if tr, ok, err := ms1[0].at.TransClusterLoc("test1", 50); tr.Loc != 555 || tr.Ver != 2 || !ok || err != nil {
  122. t.Error("Unexpected translation:", tr, ok, err)
  123. return
  124. }
  125. if tr, ok, err := ms1[0].at.RemoveTransClusterLoc("test1", 50); tr.Loc != 555 || tr.Ver != 2 || !ok || err != nil {
  126. t.Error("Unexpected translation:", tr, ok, err)
  127. return
  128. }
  129. if tr, ok, err := ms1[0].at.TransClusterLoc("test1", 50); tr != nil || ok || err != nil {
  130. t.Error("Unexpected translation:", tr, ok, err)
  131. return
  132. }
  133. if tr, ok, err := ms1[0].at.RemoveTransClusterLoc("test1", 50); tr != nil || ok || err != nil {
  134. t.Error("Unexpected translation:", tr, ok, err)
  135. return
  136. }
  137. }
  138. func TestAddressTableTransfer(t *testing.T) {
  139. _, ms1 := createCluster(1, 1)
  140. // Test storing transfer requests
  141. ms1[0].at.AddTransferRequest([]string{"a,b"}, nil)
  142. ms1[0].at.AddTransferRequest([]string{"c,d"}, nil)
  143. ms1[0].at.AddTransferRequest([]string{"e,f"}, nil)
  144. counter := 0
  145. it := hash.NewHTreeIterator(ms1[0].at.transfer)
  146. for ; it.HasNext(); it.Next() {
  147. counter++
  148. }
  149. // Check that we have 3 entries
  150. if counter != 3 {
  151. t.Error("Unexpected counter value:", counter)
  152. return
  153. }
  154. }