1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
|
/**
* @file CacheLRU_test.cpp
* @brief Unitary test for NodeManger cache LRU
*
* (c) 2013-2023 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega.h"
#include "utils.h"
#include <gtest/gtest.h>
#include <mega/megaapp.h>
#include <mega/megaclient.h>
#include <mega/user.h>
#include <mega/utils.h>
class CacheLRU: public testing::Test
{
protected:
uint32_t mLruSize = 0;
mega::MegaApp mApp;
mega::NodeManager::MissingParentNodes mMissingParentNodes;
uint64_t mIndex = 1;
static constexpr uint64_t mNumRootNodes{3}; // Root node + rubbish + vault
std::shared_ptr<mega::MegaClient> mClient;
std::shared_ptr<mega::Node> addRootNodes()
{
auto rootNode = addNode(mega::nodetype_t::ROOTNODE, nullptr, false, true);
addNode(mega::nodetype_t::VAULTNODE, nullptr, false, true);
addNode(mega::nodetype_t::RUBBISHNODE, nullptr, false, true);
return rootNode;
}
public:
std::shared_ptr<mega::Node> init(uint32_t lruSize)
{
mLruSize = lruSize;
auto dbAccess = new mega::SqliteDbAccess(mega::LocalPath::fromAbsolutePath("."));
mClient = mt::makeClient(mApp, dbAccess);
mClient->sid =
"AWA5YAbtb4JO-y2zWxmKZpSe5-6XM7CTEkA-3Nv7J4byQUpOazdfSC1ZUFlS-kah76gPKUEkTF9g7MeE";
mClient->opensctable();
mClient->mNodeManager.setCacheLRUMaxSize(mLruSize);
return addRootNodes();
}
~CacheLRU()
{
mClient.reset();
}
uint64_t numNodesInRam() const
{
return mClient->mNodeManager.getNumberNodesInRam();
}
uint64_t numNodesInCacheLru() const
{
return mClient->mNodeManager.getNumNodesAtCacheLRU();
}
uint64_t numNodesTotal() const
{
return mClient->mNodeManager.getNodeCount();
}
void setLruMaxSize(uint32_t size)
{
mClient->mNodeManager.setCacheLRUMaxSize(size);
mLruSize = size;
}
std::shared_ptr<mega::Node> addNode(mega::nodetype_t nodeType,
const std::shared_ptr<mega::Node>& parent,
bool notify,
bool isFetching,
std::function<void(mega::Node&)> nodeSetupCb = nullptr)
{
auto& nodeRef =
mt::makeNode(*mClient, nodeType, mega::NodeHandle().set6byte(mIndex++), parent.get());
std::shared_ptr<mega::Node> node(&nodeRef);
if (nodeSetupCb)
{
nodeSetupCb(nodeRef);
}
node->serializefingerprint(&node->attrs.map['c']);
node->setfingerprint();
mClient->mNodeManager.addNode(node, notify, isFetching, mMissingParentNodes);
mClient->mNodeManager.saveNodeInDb(node.get());
return node;
}
void checkNodesInCache(std::optional<uint64_t> expInRam,
std::optional<uint64_t> expTotal,
std::optional<uint64_t> expInLRU)
{
if (expInRam.has_value())
{
ASSERT_EQ(numNodesInRam(), expInRam) << "Unexpected num nodes in RAM";
}
if (expTotal.has_value())
{
ASSERT_EQ(numNodesTotal(), expTotal) << "Unexpected total num nodes";
}
if (expInLRU.has_value())
{
ASSERT_EQ(numNodesInCacheLru(), expInLRU) << "Unexpected num nodes in LRU cache";
}
}
void addNodeInCache(std::vector<std::string>& fingerprints,
std::shared_ptr<mega::Node> folderNode,
const m_off_t size,
const int64_t mtime,
const int32_t crcPart,
const unsigned int numNodesWithSameData = 1,
const mega::handle owner = 1,
std::optional<mega::attr_map> attrs = std::nullopt)
{
for (unsigned int i = 0; i < numNodesWithSameData; ++i)
{
addNode(mega::nodetype_t::FILENODE,
folderNode,
true,
false,
[&fingerprints, attrs, owner, size, mtime, crcPart](mega::Node& file)
{
file.size = static_cast<m_off_t>(size);
file.owner = owner;
file.ctime = mtime;
file.mtime = mtime;
file.crc[0] = crcPart;
file.crc[1] = crcPart;
file.crc[2] = crcPart;
file.crc[3] = crcPart;
file.isvalid = true;
file.attrs.map = attrs.value_or(
[]
{
mega::attr_map m;
m[101] = "foo";
m[102] = "bar";
return m;
}());
std::string fp;
file.mega::FileFingerprint::serialize(&fp);
fingerprints.push_back(fp);
});
}
}
void expectedNodeCountByFp(std::vector<std::string>& fingerprints,
size_t expectedNodeCount,
const size_t fpIdx,
const bool excludeMtime = true)
{
ASSERT_LT(fpIdx, fingerprints.size()) << "expectedNodeCountByFp Invalid Fp Index";
const char* fingerpritnString = fingerprints.at(fpIdx).data();
std::unique_ptr<mega::FileFingerprint> fp(
mega::FileFingerprint::unserialize(fingerpritnString,
fingerpritnString + fingerprints.front().size()));
ASSERT_TRUE(!!fp) << "expectedNodeCountByFp: fingerprint could not be unserialized";
mega::sharedNode_vector nodes(
mClient->mNodeManager.getNodesByFingerprint(*fp, excludeMtime));
ASSERT_EQ(nodes.size(), expectedNodeCount)
<< "expectedNodeCountByFp: Unexpected node count";
}
};
TEST_F(CacheLRU, checkNumNodes_higherLRUSize)
{
auto rootNode = init(8);
ASSERT_EQ(numNodesInRam(), 3);
for (uint32_t i = 0; i < mLruSize - 4; i++)
{
addNode(mega::nodetype_t::FILENODE, rootNode, false, true);
}
ASSERT_EQ(numNodesInRam(), numNodesInCacheLru());
ASSERT_EQ(numNodesTotal(), numNodesInRam());
for (uint32_t i = 0; i < mLruSize; i++)
{
addNode(mega::nodetype_t::FILENODE, rootNode, true, false);
}
// 2 (rubbis + vault) -> root node is load at LRU when getParent is called
ASSERT_EQ(numNodesInRam(), mLruSize + 2);
ASSERT_EQ(numNodesTotal(), mIndex - 1);
}
TEST_F(CacheLRU, checkNumNodes_LRUSize)
{
auto rootNode = init(8);
ASSERT_EQ(numNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
for (uint32_t i = 0; i < mLruSize - 4; i++)
{
addNode(mega::nodetype_t::FILENODE, folder, true, false);
}
ASSERT_EQ(numNodesInRam(), mLruSize);
ASSERT_EQ(numNodesTotal(), mLruSize);
for (uint32_t i = 0; i < 4; i++)
{
addNode(mega::nodetype_t::FILENODE, folder, true, false);
}
// 3 root nodes -> folder is at LRU cache, it accesed to set parent from new children
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
ASSERT_EQ(numNodesTotal(), mIndex - 1);
}
TEST_F(CacheLRU, removeNode)
{
auto rootNode = init(8);
ASSERT_EQ(numNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint64_t indexFromNodeAtLRU = mIndex;
uint32_t numNodes = 15;
for (uint32_t i = 0; i < numNodes; i++)
{
addNode(
mega::nodetype_t::FILENODE,
folder,
true,
false,
[this](mega::Node& file)
{
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
file.attrs.map = std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}};
});
}
// Root node + rubbish + vault
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
// 3 root nodes -> folder is at LRU cache, it's accesed to set parent from new children
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
ASSERT_EQ(numNodesTotal(), mIndex - 1);
auto& nodeMgr = mClient->mNodeManager;
auto nodeToRemove = nodeMgr.getNodeByHandle(mega::NodeHandle().set6byte(indexFromNodeAtLRU));
nodeToRemove->changed.removed = true;
nodeMgr.notifyNode(nodeToRemove);
nodeToRemove.reset();
nodeMgr.notifyPurge();
ASSERT_EQ(numNodesInRam(), mLruSize + 2);
ASSERT_EQ(numNodesInCacheLru(), mLruSize - 1);
ASSERT_EQ(numNodesTotal(), mIndex - 2);
}
TEST_F(CacheLRU, getNodebyFingerprint_RAM_NoLRU)
{
auto rootNode = init(8);
ASSERT_EQ(numNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint32_t numNodes = 15;
std::vector<std::string> fingerprints;
std::shared_ptr<mega::Node> nodeRemovedFromLRU;
for (uint32_t i = 0; i < numNodes; i++)
{
auto fileNode = addNode(
mega::nodetype_t::FILENODE,
folder,
true,
false,
[this, &fingerprints](mega::Node& file)
{
auto index = static_cast<int32_t>(mIndex);
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
// Modify fingerprint look nodes by fingerprint
file.crc[0] = index;
file.crc[1] = index;
file.crc[2] = index;
file.crc[3] = index;
file.isvalid = true;
file.attrs.map = std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}};
std::string fp;
file.mega::FileFingerprint::serialize(&fp);
fingerprints.push_back(fp);
});
if (i == 1)
{
nodeRemovedFromLRU = fileNode;
}
}
folder.reset();
// Root node + rubbish + vault + node with reference
ASSERT_EQ(numNodesInRam(), mLruSize + 4);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
ASSERT_GT(fingerprints.size(), 1);
// No found at LRU, fingerprint at DB
const char* fingerpritnString = fingerprints.front().data();
std::unique_ptr<mega::FileFingerprint> fp(mega::FileFingerprint::unserialize(fingerpritnString, fingerpritnString + fingerprints.front().size()));
std::shared_ptr<mega::Node> node(mClient->mNodeManager.getNodeByFingerprint(*fp));
ASSERT_NE(node.get(), nullptr);
// No found at LRU, fingerprint at DB but node is in RAM
fingerpritnString = fingerprints.at(1).data();
fp = mega::FileFingerprint::unserialize(fingerpritnString, fingerpritnString + fingerprints.front().size());
node = mClient->mNodeManager.getNodeByFingerprint(*fp);
ASSERT_NE(node.get(), nullptr);
ASSERT_EQ(node.get(), nodeRemovedFromLRU.get());
// Found at LRU, fingerprint at mFingerPrints
fingerpritnString = fingerprints.back().data();
fp = mega::FileFingerprint::unserialize(fingerpritnString, fingerpritnString + fingerprints.front().size());
node = mClient->mNodeManager.getNodeByFingerprint(*fp);
ASSERT_NE(node.get(), nullptr);
}
TEST_F(CacheLRU, getNodeByFingerprint_NoRAM_NoLRU)
{
auto rootNode = init(8);
ASSERT_EQ(mClient->mNodeManager.getNumberNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint32_t numNodes = 15;
std::vector<std::string> fingerprints;
for (uint32_t i = 0; i < numNodes; i++)
{
addNode(
mega::nodetype_t::FILENODE,
folder,
true,
false,
[this, &fingerprints](mega::Node& file)
{
auto index = static_cast<int32_t>(mIndex);
file.size = static_cast<m_off_t>(index);
file.owner = 88;
file.ctime = 44;
file.mtime = 44;
// Modify fingerprint look nodes by fingerprint
file.crc[0] = index;
file.crc[1] = index;
file.crc[2] = index;
file.crc[3] = index;
file.isvalid = true;
file.attrs.map = std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}};
std::string fp;
file.mega::FileFingerprint::serialize(&fp);
fingerprints.push_back(fp);
});
}
folder.reset();
// Root node + rubbish + vault
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
// 3 root nodes -> folder is at LRU cache, it's accesed to set parent from new children
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
ASSERT_EQ(numNodesTotal(), mIndex - 1);
ASSERT_GT(fingerprints.size(), 1);
// No found at LRU, fingerprint at DB
const char* fingerpritnString = fingerprints.front().data();
std::unique_ptr<mega::FileFingerprint> fp(mega::FileFingerprint::unserialize(fingerpritnString, fingerpritnString + fingerprints.front().size()));
mega::sharedNode_vector nodes(mClient->mNodeManager.getNodesByFingerprint(*fp));
ASSERT_EQ(nodes.size(), 1);
// Found at LRU, fingerprint at mFingerPrints
fingerpritnString = fingerprints.back().data();
fp = mega::FileFingerprint::unserialize(fingerpritnString, fingerpritnString + fingerprints.front().size());
nodes = mClient->mNodeManager.getNodesByFingerprint(*fp);
ASSERT_EQ(nodes.size(), 1);
}
/**
* @test CacheLRU.getNodesByFingerprintIgnoringMtime
* Test preconditions: Initialize the LRU cache with size (8) and create the root node.
*
* 1. Add multiple groups of nodes to the cache with different FPs (size, mtime, and CRC) between
* groups. All elements in the same group will have the same FP (size, mtime, and CRC).
* 2. Validate that the total number of nodes in the cache matches the expected count.
* 3. Test `getNodesByFingerprint` ignoring `mtime` to confirm that node search by FP behaves
* correctly.
* - This test case forces the SDK to search nodes in the DB and allocate them in the LRU cache and
* in the FingerprintContainer.
* - The LRU cache size (8) is small enough that this test case also exercises node unloading from
* the LRU cache when new ones need to be allocated.
* 4. Add more groups of nodes to the cache with different FPs (size, mtime, and CRC values) between
* groups. All elements in the same group will have the same FP (size, mtime, and CRC).
* 5. Validate that the total number of nodes in the cache matches the expected count.
* 6. Test `getNodesByFingerprint` both ignoring and including `mtime` to confirm that node search
* by FP behaves correctly in both cases.
* - This test case forces the SDK to search nodes in the DB and allocate them in the LRU cache and
* in the FingerprintContainer.
* - The LRU cache size (8) is small enough that this test case also exercises node unloading from
* the LRU cache when new ones need to be allocated.
*/
TEST_F(CacheLRU, getNodesByFingerprintIgnoringMtime)
{
constexpr uint32_t lruSize{8};
auto rootNode = init(lruSize);
ASSERT_EQ(mClient->mNodeManager.getNumberNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
std::vector<std::string> fingerprints;
uint64_t expectedNumNodes{0};
const std::vector<uint32_t> numNodes{5, 10, 15};
expectedNumNodes = numNodes[0] * 3;
addNodeInCache(fingerprints, folder, 10 /*size=*/, 20 /*mtime=*/, 30 /*crc*/, numNodes[0]);
addNodeInCache(fingerprints, folder, 20 /*size=*/, 20 /*mtime=*/, 40 /*crc*/, numNodes[0]);
addNodeInCache(fingerprints, folder, 30 /*size=*/, 40 /*mtime=*/, 50 /*crc*/, numNodes[0]);
ASSERT_EQ(fingerprints.size(), expectedNumNodes)
<< "Unexpected num nodes in fingerprints vector";
ASSERT_NO_FATAL_FAILURE(checkNodesInCache(mNumRootNodes + mLruSize,
mNumRootNodes + expectedNumNodes + +1 /*testFolder*/,
lruSize))
<< "TC1: Unexpected num nodes in cache";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 0 /*FpIndex*/, true /*excludeMtime*/))
<< "TC2: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 5 /*FpIndex*/, true /*excludeMtime*/))
<< "TC3: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 10 /*FpIndex*/, true /*excludeMtime*/))
<< "TC4: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 0 /*FpIndex*/, true /*excludeMtime*/))
<< "TC5: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 5 /*FpIndex*/, true /*excludeMtime*/))
<< "TC6: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 10 /*FpIndex*/, true /*excludeMtime*/))
<< "TC7: getNodesByFingerprint(ignoring mtime)";
addNodeInCache(fingerprints, folder, 40, 50, 60, numNodes[1]);
addNodeInCache(fingerprints, folder, 40, 80, 60, numNodes[2]);
folder.reset();
expectedNumNodes += numNodes[1] + numNodes[2];
ASSERT_NO_FATAL_FAILURE(checkNodesInCache(mNumRootNodes + mLruSize,
mNumRootNodes + expectedNumNodes + +1 /*testFolder*/,
lruSize))
<< "TC8: Unexpected num nodes in cache";
ASSERT_NO_FATAL_FAILURE(expectedNodeCountByFp(fingerprints,
numNodes[1] + numNodes[2],
15 /*FpIndex*/,
true /*excludeMtime*/))
<< "TC9: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(expectedNodeCountByFp(fingerprints,
numNodes[1] + numNodes[2],
15 /*FpIndex*/,
true /*excludeMtime*/))
<< "TC10: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[1], 15 /*FpIndex*/, false /*excludeMtime*/))
<< "TC11: getNodesByFingerprint(including mtime)";
}
/**
* @test CacheLRU.getNodesByFingerprintIgnoringMtimeSmallLRUCache
* Test preconditions: Initialize the LRU cache with a very small size (2) and create the root node.
*
* 1. Add multiple groups of nodes to the cache with different FPs (size, mtime, and CRC) between
* groups. All elements in the same group will have the same FP (size, mtime, and CRC).
* 2. Validate that the total number of nodes in the cache matches the expected count.
* 3. Test `getNodesByFingerprint` both ignoring and including `mtime` to confirm that node search
* by FP behaves correctly in both cases.
* - This test case forces the SDK to search nodes in the DB and allocate them in the LRU cache and
* in the FingerprintContainer.
* - As the LRU cache size (2) is very small, we search nodes from different groups (and multiple
* times), which causes the LRU to unload nodes to allocate new ones.
*/
TEST_F(CacheLRU, getNodesByFingerprintIgnoringMtimeSmallLRUCache)
{
constexpr uint32_t lruSize{2};
auto rootNode = init(lruSize);
uint64_t expectedNumNodes{0};
ASSERT_EQ(mClient->mNodeManager.getNumberNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
std::vector<std::string> fingerprints;
const std::vector<uint32_t> numNodes{2, 3};
addNodeInCache(fingerprints, folder, 40, 50, 60, numNodes[0]);
addNodeInCache(fingerprints, folder, 40, 80, 60, numNodes[1]);
expectedNumNodes = numNodes[0] + numNodes[1];
folder.reset();
ASSERT_NO_FATAL_FAILURE(checkNodesInCache(mNumRootNodes + mLruSize,
mNumRootNodes + expectedNumNodes + +1 /*testFolder*/,
lruSize))
<< "TC1: Unexpected num nodes in cache";
// Get nodes by Fingerprint (repeating use cases to force LRU cache to discard nodes)
// Also test both cases including/ignoring mtime in fingerprint comparison
ASSERT_NO_FATAL_FAILURE(expectedNodeCountByFp(fingerprints,
numNodes[0] + numNodes[1],
0 /*FpIndex*/,
true /*excludeMtime*/))
<< "TC2: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(expectedNodeCountByFp(fingerprints,
numNodes[0] + numNodes[1],
2 /*FpIndex*/,
true /*excludeMtime*/))
<< "TC3: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(expectedNodeCountByFp(fingerprints,
numNodes[0] + numNodes[1],
0 /*FpIndex*/,
true /*excludeMtime*/))
<< "TC4: getNodesByFingerprint(ignoring mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 0 /*FpIndex*/, false /*excludeMtime*/))
<< "TC5: getNodesByFingerprint(including mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[1], 2 /*FpIndex*/, false /*excludeMtime*/))
<< "TC6: getNodesByFingerprint(including mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[0], 0 /*FpIndex*/, false /*excludeMtime*/))
<< "TC7: getNodesByFingerprint(including mtime)";
ASSERT_NO_FATAL_FAILURE(
expectedNodeCountByFp(fingerprints, numNodes[1], 2 /*FpIndex*/, false /*excludeMtime*/))
<< "TC8: getNodesByFingerprint(including mtime)";
}
TEST_F(CacheLRU, searchNode) // processUnserializedNodes
{
auto rootNode = init(8);
ASSERT_EQ(mClient->mNodeManager.getNumberNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint32_t numNodes = 15;
std::vector<std::string> names;
std::shared_ptr<mega::Node> nodeInRAM;
std::string nameNodeInRam;
for (uint32_t i = 0; i < numNodes; i++)
{
std::string name = "name" + std::to_string(mIndex);
names.push_back(name);
auto fileNode = addNode(
mega::nodetype_t::FILENODE,
folder,
true,
false,
[this, &name](mega::Node& file)
{
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
file.attrs.map =
std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}, {110, name}};
});
if (i == 1)
{
nodeInRAM = fileNode;
nameNodeInRam = name;
}
}
// Root node + rubbish + vault + node in RAM
ASSERT_EQ(numNodesInRam(), mLruSize + 4);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
ASSERT_EQ(numNodesTotal(), mIndex - 1);
ASSERT_GT(names.size(), 1);
// No found at LRU
mega::NodeSearchFilter searchFilter;
searchFilter.byAncestors({rootNode->nodehandle, mega::UNDEF, mega::UNDEF});
searchFilter.byName(names.front());
auto& nodeMgr = mClient->mNodeManager;
mega::sharedNode_vector nodes(nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0}));
ASSERT_EQ(nodes.size(), 1);
// No found at LRU but in RAM
searchFilter.byName(nameNodeInRam);
nodes = nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0});
ASSERT_EQ(nodes.size(), 1);
// Found at LRU
searchFilter.byName(names.back());
nodes = nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0});
ASSERT_EQ(nodes.size(), 1);
// Search a not out shared folder by out share
searchFilter.byAncestors({mega::UNDEF, mega::UNDEF, mega::UNDEF});
searchFilter.byName("");
searchFilter.setIncludedShares(mega::OUT_SHARES);
nodes = nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0});
ASSERT_EQ(nodes.size(), 0);
// Set the folder as public link
folder->plink.reset(new mega::PublicLink{0x1, 0x1, 0x1, false});
nodeMgr.saveNodeInDb(folder.get());
// Search
searchFilter.setIncludedShares(mega::LINK);
nodes = nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0});
ASSERT_EQ(nodes.size(), 16);
// Set the folder as out shared as well
mega::User user{"name@name.com"};
folder->outshares.reset(new mega::share_map{});
folder->outshares->emplace(0x1ull, std::make_unique<mega::Share>(&user, mega::FULL, 0x1));
nodeMgr.saveNodeInDb(folder.get());
// Search by public link
searchFilter.setIncludedShares(mega::LINK);
nodes = nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0});
ASSERT_EQ(nodes.size(), 16);
// Search out shares with name
searchFilter.byName(names.back());
searchFilter.setIncludedShares(mega::OUT_SHARES);
nodes = nodeMgr.searchNodes(searchFilter,
0 /*order None*/,
mega::CancelToken(),
mega::NodeSearchPage{0, 0});
ASSERT_EQ(nodes.size(), 1);
}
TEST_F(CacheLRU, getChildren)
{
auto rootNode = init(8);
auto& nodeMgr = mClient->mNodeManager;
ASSERT_EQ(numNodesInRam(), 3);
std::array<uint32_t, 3> numNodesForFolder = {2, 10, 10};
std::array<std::shared_ptr<mega::Node>, 3> folders;
std::shared_ptr<mega::Node> nodeInRAM;
for (uint32_t f = 0; f < 3; f++)
{
auto& folder = folders[f] = addNode(mega::nodetype_t::FOLDERNODE,
rootNode,
false,
true,
[f](mega::Node& folderNode)
{
folderNode.attrs.map =
std::map<mega::nameid, std::string>{
{110, "Folder" + std::to_string(f + 1)}};
});
auto numNodes = numNodesForFolder[f];
for (uint32_t i = 0; i < numNodes; i++)
{
auto fileNode =
addNode(mega::nodetype_t::FILENODE,
folder,
true,
false,
[this](mega::Node& file)
{
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
file.attrs.map =
std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}};
});
if ((f == 2) && (i == numNodes / 2))
{
nodeInRAM = fileNode;
}
}
mega::sharedNode_list children = nodeMgr.getChildren(folder.get()); // All children in RAM
ASSERT_EQ(children.size(), numNodes);
}
auto children = nodeMgr.getChildren(folders[0].get()); // None node in RAM
ASSERT_EQ(children.size(), numNodesForFolder[0]);
}
TEST_F(CacheLRU, getNodeByHandle)
{
auto rootNode = init(8);
ASSERT_EQ(numNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint32_t numNodes = 15;
std::vector<mega::NodeHandle> handles;
std::shared_ptr<mega::Node> nodeInRAM;
for (uint32_t i = 0; i < numNodes; i++)
{
auto fileNode = addNode(
mega::nodetype_t::FILENODE,
folder,
true,
false,
[this, &handles](mega::Node& file)
{
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
std::string name = "name" + std::to_string(mIndex);
file.attrs.map =
std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}, {110, name}};
handles.push_back(file.nodeHandle());
});
if (i == numNodes / 2)
{
nodeInRAM = fileNode;
}
}
ASSERT_GT(handles.size(), 1);
mega::NodeHandle firstNodeHandle = handles.front();
mega::NodeHandle lasttNodeHandle = handles.front();
mega::NodeHandle nodeInRAMHandle = nodeInRAM->nodeHandle();
auto& nodeMgr = mClient->mNodeManager;
// No Node at RAM => no at LRU
// ASSERT_EQ(client->mNodeManager.getNodeInRAM(firstNodeHandle).get(), nullptr);
std::shared_ptr<mega::Node> node = nodeMgr.getNodeByHandle(firstNodeHandle);
// Node at RAM and LRU
auto auxiliarNode = nodeMgr.getNodeByHandle(lasttNodeHandle);
ASSERT_NE(auxiliarNode, nullptr);
ASSERT_NE(auxiliarNode->mNodePosition->second.mLRUPosition, nodeMgr.invalidCacheLRUPos());
node = nodeMgr.getNodeByHandle(lasttNodeHandle);
ASSERT_EQ(auxiliarNode.get(), node.get());
// Node at RAM, no at LRU
// ASSERT_NE(client->mNodeManager.getNodeInRAM(nodeInRAMHandle).get(), nullptr);
ASSERT_NE(nodeInRAM, nullptr);
ASSERT_EQ(nodeInRAM->mNodePosition->second.mLRUPosition, nodeMgr.invalidCacheLRUPos());
node = nodeMgr.getNodeByHandle(nodeInRAMHandle);
ASSERT_EQ(nodeInRAM.get(), node.get());
}
TEST_F(CacheLRU, childNodeByNameType)
{
auto rootNode = init(8);
ASSERT_EQ(numNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint32_t numNodes = 15;
std::vector<std::string> names;
std::shared_ptr<mega::Node> nodeInRAM;
std::string nameNodeInRam;
for (uint32_t i = 0; i < numNodes; i++)
{
std::string name = "name" + std::to_string(mIndex);
names.push_back(name);
auto fileNode = addNode(
mega::nodetype_t::FILENODE,
folder,
true,
false,
[this, &name](mega::Node& file)
{
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
file.attrs.map =
std::map<mega::nameid, std::string>{{101, "foo"}, {102, "bar"}, {110, name}};
});
if (i == 1)
{
nodeInRAM = fileNode;
nameNodeInRam = name;
}
}
// Root node + rubbish + vault + node in RAM
ASSERT_EQ(numNodesInRam(), mLruSize + 4);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
ASSERT_GT(names.size(), 1);
auto& nodeMgr = mClient->mNodeManager;
// No found at LRU
std::shared_ptr<mega::Node> node = nodeMgr.childNodeByNameType(folder.get(),
names.front().c_str(),
mega::nodetype_t::FILENODE);
ASSERT_NE(node, nullptr);
// No found at LRU but in RAM
node = nodeMgr.childNodeByNameType(folder.get(),
nameNodeInRam.c_str(),
mega::nodetype_t::FILENODE);
ASSERT_NE(node, nullptr);
// Found at LRU
node =
nodeMgr.childNodeByNameType(folder.get(), names.back().c_str(), mega::nodetype_t::FILENODE);
ASSERT_NE(node, nullptr);
}
TEST_F(CacheLRU, reduceCacheLRUSize)
{
auto rootNode = init(20);
ASSERT_EQ(numNodesInRam(), 3);
auto folder = addNode(mega::nodetype_t::FOLDERNODE, rootNode, false, true);
uint32_t numNodes = mLruSize;
for (uint32_t i = 0; i < numNodes; i++)
{
addNode(mega::nodetype_t::FILENODE,
folder,
true,
false,
[this](mega::Node& file)
{
file.size = static_cast<m_off_t>(mIndex);
file.owner = 88;
file.ctime = 44;
std::string name = "name" + std::to_string(mIndex);
file.attrs.map = std::map<mega::nameid, std::string>{{101, "foo"},
{102, "bar"},
{110, name}};
});
}
// Root node + rubbish + vault
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
setLruMaxSize(8);
// Root node + rubbish + vault
ASSERT_EQ(numNodesInRam(), mLruSize + 3);
// Root node + rubbish + vault + folder
ASSERT_EQ(numNodesTotal(), numNodes + 4);
}
|