Ejemplo n.º 1
0
TEST_F(MVCCTest, VersionChainTest) {
  LOG_INFO("VersionChainTest");

  for (auto protocol : TEST_TYPES) {
    LOG_INFO("Validating %d", protocol);
    concurrency::TransactionManagerFactory::Configure(
        protocol, ISOLATION_LEVEL_TYPE_FULL);

    const int num_txn = 2;    // 5
    const int scale = 1;      // 20
    const int num_key = 2;    // 256
    srand(15721);

    std::unique_ptr<storage::DataTable> table(
        TransactionTestsUtil::CreateTable(num_key));
    auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();

    TransactionScheduler scheduler(num_txn, table.get(), &txn_manager);
    scheduler.SetConcurrent(true);
    for (int i = 0; i < num_txn; i++) {
      for (int j = 0; j < scale; j++) {
        // randomly select two uniq keys
        int key1 = rand() % num_key;
        int key2 = rand() % num_key;
        int delta = rand() % 1000;
        // Store substracted value
        scheduler.Txn(i).ReadStore(key1, -delta);
        scheduler.Txn(i).Update(key1, TXN_STORED_VALUE);
        // Store increased value
        scheduler.Txn(i).ReadStore(key2, delta);
        scheduler.Txn(i).Update(key2, TXN_STORED_VALUE);
      }
      scheduler.Txn(i).Commit();
    }
    scheduler.Run();

    // Read all values
    TransactionScheduler scheduler2(1, table.get(), &txn_manager);
    for (int i = 0; i < num_key; i++) {
      scheduler2.Txn(0).Read(i);
    }
    scheduler2.Txn(0).Commit();
    scheduler2.Run();

    ValidateMVCC_OldToNew(table.get());
  }
}
TEST_F(IsolationLevelTest, StressTest) {

  const int num_txn = 2;    // 16
  const int scale = 1;      // 20
  const int num_key = 2;    // 256
  srand(15721);

  for (auto test_type : TEST_TYPES) {
    concurrency::TransactionManagerFactory::Configure(
        test_type, ISOLATION_LEVEL_TYPE_FULL);
    std::unique_ptr<storage::DataTable> table(
        TransactionTestsUtil::CreateTable(num_key));
    auto &txn_manager = concurrency::TransactionManagerFactory::GetInstance();

    TransactionScheduler scheduler(num_txn, table.get(), &txn_manager);
    scheduler.SetConcurrent(true);
    for (int i = 0; i < num_txn; i++) {
      for (int j = 0; j < scale; j++) {
        // randomly select two uniq keys
        int key1 = rand() % num_key;
        int key2 = rand() % num_key;
        int delta = rand() % 1000;
        // Store substracted value
        scheduler.Txn(i).ReadStore(key1, -delta);
        scheduler.Txn(i).Update(key1, TXN_STORED_VALUE);
        LOG_INFO("Txn %d deducts %d from %d", i, delta, key1);
        // Store increased value
        scheduler.Txn(i).ReadStore(key2, delta);
        scheduler.Txn(i).Update(key2, TXN_STORED_VALUE);
        LOG_INFO("Txn %d adds %d to %d", i, delta, key2);
      }
      scheduler.Txn(i).Commit();
    }
    scheduler.Run();

    // Read all values
    TransactionScheduler scheduler2(1, table.get(), &txn_manager);
    for (int i = 0; i < num_key; i++) {
      scheduler2.Txn(0).Read(i);
    }
    scheduler2.Txn(0).Commit();
    scheduler2.Run();

    EXPECT_EQ(RESULT_SUCCESS, scheduler2.schedules[0].txn_result);
    // The sum should be zero
    int sum = 0;
    for (auto result : scheduler2.schedules[0].results) {
      LOG_INFO("Table has tuple value: %d", result);
      sum += result;
    }

    EXPECT_EQ(0, sum);

    // stats
    int nabort = 0;
    for (auto &schedule : scheduler.schedules) {
      if (schedule.txn_result == RESULT_ABORTED) nabort += 1;
    }
    LOG_INFO("Abort: %d out of %d", nabort, num_txn);
  }
}