5
5
// This file contains tests for the BucketIndex and higher-level operations
6
6
// concerning key-value lookup based on the BucketList.
7
7
8
+ #include " bucket/BucketInputIterator.h"
8
9
#include " bucket/BucketManager.h"
9
10
#include " bucket/BucketSnapshotManager.h"
10
11
#include " bucket/BucketUtils.h"
11
12
#include " bucket/LiveBucket.h"
12
13
#include " bucket/LiveBucketList.h"
13
14
#include " bucket/test/BucketTestUtils.h"
15
+ #include " ledger/LedgerTypeUtils.h"
14
16
#include " ledger/test/LedgerTestUtils.h"
15
17
#include " lib/catch.hpp"
16
18
#include " main/Application.h"
@@ -308,7 +310,7 @@ class BucketIndexTest
308
310
309
311
// Test bulk load lookup
310
312
auto loadResult =
311
- searchableBL->loadKeysWithLimits (mKeysToSearch , nullptr );
313
+ searchableBL->loadKeysWithLimits (mKeysToSearch , " test " , nullptr );
312
314
validateResults (mTestEntries , loadResult);
313
315
314
316
if (expectedHitRate)
@@ -356,8 +358,8 @@ class BucketIndexTest
356
358
mKeysToSearch .size ());
357
359
358
360
// Run bulk lookup again
359
- auto loadResult2 =
360
- searchableBL-> loadKeysWithLimits ( mKeysToSearch , nullptr );
361
+ auto loadResult2 = searchableBL-> loadKeysWithLimits (
362
+ mKeysToSearch , " test " , nullptr );
361
363
validateResults (mTestEntries , loadResult2);
362
364
363
365
checkHitRate (expectedHitRate, startingHitCount, startingMissCount,
@@ -401,7 +403,7 @@ class BucketIndexTest
401
403
}
402
404
403
405
auto blLoad =
404
- searchableBL->loadKeysWithLimits (searchSubset, nullptr );
406
+ searchableBL->loadKeysWithLimits (searchSubset, " test " , nullptr );
405
407
validateResults (testEntriesSubset, blLoad);
406
408
}
407
409
}
@@ -420,8 +422,8 @@ class BucketIndexTest
420
422
LedgerKeySet invalidKeys (keysNotInBL.begin (), keysNotInBL.end ());
421
423
422
424
// Test bulk load
423
- REQUIRE (searchableBL->loadKeysWithLimits (invalidKeys, nullptr ). size () ==
424
- 0 );
425
+ REQUIRE (searchableBL->loadKeysWithLimits (invalidKeys, " test " , nullptr )
426
+ . size () == 0 );
425
427
426
428
// Test individual load
427
429
for (auto const & key : invalidKeys)
@@ -745,6 +747,124 @@ TEST_CASE("do not load outdated values", "[bucket][bucketindex]")
745
747
testAllIndexTypes (f);
746
748
}
747
749
750
+ TEST_CASE (" bucket entry counters" , " [bucket][bucketindex]" )
751
+ {
752
+ // Initialize global counter for all of bucketlist
753
+ std::map<LedgerEntryTypeAndDurability, size_t > totalEntryTypeCounts;
754
+ std::map<LedgerEntryTypeAndDurability, size_t > totalEntryTypeSizes;
755
+ for (uint32_t type =
756
+ static_cast <uint32_t >(LedgerEntryTypeAndDurability::ACCOUNT);
757
+ type < static_cast <uint32_t >(LedgerEntryTypeAndDurability::NUM_TYPES);
758
+ ++type)
759
+ {
760
+ totalEntryTypeCounts[static_cast <LedgerEntryTypeAndDurability>(type)] =
761
+ 0 ;
762
+ totalEntryTypeSizes[static_cast <LedgerEntryTypeAndDurability>(type)] =
763
+ 0 ;
764
+ }
765
+
766
+ auto checkBucket = [&](auto bucket) {
767
+ if (bucket->isEmpty ())
768
+ {
769
+ return ;
770
+ }
771
+
772
+ // Local counter for each bucket
773
+ std::map<LedgerEntryTypeAndDurability, size_t > entryTypeCounts;
774
+ std::map<LedgerEntryTypeAndDurability, size_t > entryTypeSizes;
775
+ for (uint32_t type =
776
+ static_cast <uint32_t >(LedgerEntryTypeAndDurability::ACCOUNT);
777
+ type <
778
+ static_cast <uint32_t >(LedgerEntryTypeAndDurability::NUM_TYPES);
779
+ ++type)
780
+ {
781
+ entryTypeCounts[static_cast <LedgerEntryTypeAndDurability>(type)] =
782
+ 0 ;
783
+ entryTypeSizes[static_cast <LedgerEntryTypeAndDurability>(type)] = 0 ;
784
+ }
785
+
786
+ for (LiveBucketInputIterator iter (bucket); iter; ++iter)
787
+ {
788
+ auto be = *iter;
789
+ LedgerKey lk = getBucketLedgerKey (be);
790
+
791
+ auto count = [&](LedgerEntryTypeAndDurability type) {
792
+ entryTypeCounts[type]++;
793
+ entryTypeSizes[type] += xdr::xdr_size (be);
794
+ totalEntryTypeCounts[type]++;
795
+ totalEntryTypeSizes[type] += xdr::xdr_size (be);
796
+ };
797
+
798
+ switch (lk.type ())
799
+ {
800
+ case ACCOUNT:
801
+ count (LedgerEntryTypeAndDurability::ACCOUNT);
802
+ break ;
803
+ case TRUSTLINE:
804
+ count (LedgerEntryTypeAndDurability::TRUSTLINE);
805
+ break ;
806
+ case OFFER:
807
+ count (LedgerEntryTypeAndDurability::OFFER);
808
+ break ;
809
+ case DATA:
810
+ count (LedgerEntryTypeAndDurability::DATA);
811
+ break ;
812
+ case CLAIMABLE_BALANCE:
813
+ count (LedgerEntryTypeAndDurability::CLAIMABLE_BALANCE);
814
+ break ;
815
+ case LIQUIDITY_POOL:
816
+ count (LedgerEntryTypeAndDurability::LIQUIDITY_POOL);
817
+ break ;
818
+ case CONTRACT_DATA:
819
+ if (isPersistentEntry (lk))
820
+ {
821
+ count (
822
+ LedgerEntryTypeAndDurability::PERSISTENT_CONTRACT_DATA);
823
+ }
824
+ else
825
+ {
826
+ count (
827
+ LedgerEntryTypeAndDurability::TEMPORARY_CONTRACT_DATA);
828
+ }
829
+ break ;
830
+ case CONTRACT_CODE:
831
+ count (LedgerEntryTypeAndDurability::CONTRACT_CODE);
832
+ break ;
833
+ case CONFIG_SETTING:
834
+ count (LedgerEntryTypeAndDurability::CONFIG_SETTING);
835
+ break ;
836
+ case TTL:
837
+ count (LedgerEntryTypeAndDurability::TTL);
838
+ break ;
839
+ }
840
+ }
841
+
842
+ auto const & indexCounters =
843
+ bucket->getIndexForTesting ().getBucketEntryCounters ();
844
+ REQUIRE (indexCounters.entryTypeCounts == entryTypeCounts);
845
+ REQUIRE (indexCounters.entryTypeSizes == entryTypeSizes);
846
+ };
847
+
848
+ auto f = [&](Config& cfg) {
849
+ auto test = BucketIndexTest (cfg);
850
+ test.buildMultiVersionTest ();
851
+
852
+ for (auto i = 0 ; i < LiveBucketList::kNumLevels ; ++i)
853
+ {
854
+ auto level = test.getBM ().getLiveBucketList ().getLevel (i);
855
+ checkBucket (level.getCurr ());
856
+ checkBucket (level.getSnap ());
857
+ }
858
+
859
+ auto summedCounters =
860
+ test.getBM ().getLiveBucketList ().sumBucketEntryCounters ();
861
+ REQUIRE (summedCounters.entryTypeCounts == totalEntryTypeCounts);
862
+ REQUIRE (summedCounters.entryTypeSizes == totalEntryTypeSizes);
863
+ };
864
+
865
+ testAllIndexTypes (f);
866
+ }
867
+
748
868
TEST_CASE (" load from historical snapshots" , " [bucket][bucketindex]" )
749
869
{
750
870
auto f = [&](Config& cfg) {
@@ -839,6 +959,9 @@ TEST_CASE("serialize bucket indexes", "[bucket][bucketindex]")
839
959
840
960
auto & inMemoryIndex = b->getIndexForTesting ();
841
961
REQUIRE ((inMemoryIndex == *onDiskIndex));
962
+ auto inMemoryCounters = inMemoryIndex.getBucketEntryCounters ();
963
+ auto onDiskCounters = onDiskIndex->getBucketEntryCounters ();
964
+ REQUIRE (inMemoryCounters == onDiskCounters);
842
965
}
843
966
844
967
// Restart app with different config to test that indexes created with
@@ -862,17 +985,6 @@ TEST_CASE("serialize bucket indexes", "[bucket][bucketindex]")
862
985
863
986
auto & inMemoryIndex = b->getIndexForTesting ();
864
987
REQUIRE (inMemoryIndex.getPageSize () == (1UL << 10 ));
865
- auto inMemoryCoutners = inMemoryIndex.getBucketEntryCounters ();
866
- // Ensure the inMemoryIndex has some non-zero counters.
867
- REQUIRE (!inMemoryCoutners.entryTypeCounts .empty ());
868
- REQUIRE (!inMemoryCoutners.entryTypeSizes .empty ());
869
- bool allZero = true ;
870
- for (auto const & [k, v] : inMemoryCoutners.entryTypeCounts )
871
- {
872
- allZero = allZero && (v == 0 );
873
- allZero = allZero && (inMemoryCoutners.entryTypeSizes .at (k) == 0 );
874
- }
875
- REQUIRE (!allZero);
876
988
877
989
// Check if on-disk index rewritten with correct config params
878
990
auto indexFilename = test.getBM ().bucketIndexFilename (bucketHash);
0 commit comments