deletefile_test.cc 19.9 KB
Newer Older
1
//  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
S
Siying Dong 已提交
2 3 4
//  This source code is licensed under both the GPLv2 (found in the
//  COPYING file in the root directory) and Apache 2.0 License
//  (found in the LICENSE.Apache file in the root directory).
5
//
6 7 8 9
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. See the AUTHORS file for names of contributors.

10 11
#ifndef ROCKSDB_LITE

12 13 14 15
#include <stdlib.h>
#include <map>
#include <string>
#include <vector>
16
#include "db/db_impl/db_impl.h"
Y
Yanqin Jin 已提交
17
#include "db/db_test_util.h"
18 19
#include "db/version_set.h"
#include "db/write_batch_internal.h"
20
#include "file/filename.h"
Y
Yanqin Jin 已提交
21
#include "port/stack_trace.h"
22 23 24
#include "rocksdb/db.h"
#include "rocksdb/env.h"
#include "rocksdb/transaction_log.h"
25 26 27
#include "test_util/sync_point.h"
#include "test_util/testharness.h"
#include "test_util/testutil.h"
28
#include "util/string_util.h"
29

30
namespace ROCKSDB_NAMESPACE {
31

Y
Yanqin Jin 已提交
32
class DeleteFileTest : public DBTestBase {
33
 public:
Y
Yanqin Jin 已提交
34 35 36 37
  const int numlevels_;
  const std::string wal_dir_;

  DeleteFileTest()
38
      : DBTestBase("deletefile_test", /*env_do_fsync=*/true),
Y
Yanqin Jin 已提交
39 40 41 42
        numlevels_(7),
        wal_dir_(dbname_ + "/wal_files") {}

  void SetOptions(Options* options) {
43
    ASSERT_NE(options, nullptr);
Y
Yanqin Jin 已提交
44 45 46 47 48 49 50 51
    options->delete_obsolete_files_period_micros = 0;  // always do full purge
    options->enable_thread_tracking = true;
    options->write_buffer_size = 1024 * 1024 * 1000;
    options->target_file_size_base = 1024 * 1024 * 1000;
    options->max_bytes_for_level_base = 1024 * 1024 * 1000;
    options->WAL_ttl_seconds = 300;     // Used to test log files
    options->WAL_size_limit_MB = 1024;  // Used to test log files
    options->wal_dir = wal_dir_;
52 53 54 55 56 57 58
  }

  void AddKeys(int numkeys, int startkey = 0) {
    WriteOptions options;
    options.sync = false;
    ReadOptions roptions;
    for (int i = startkey; i < (numkeys + startkey) ; i++) {
59
      std::string temp = ToString(i);
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
      Slice key(temp);
      Slice value(temp);
      ASSERT_OK(db_->Put(options, key, value));
    }
  }

  int numKeysInLevels(
    std::vector<LiveFileMetaData> &metadata,
    std::vector<int> *keysperlevel = nullptr) {

    if (keysperlevel != nullptr) {
      keysperlevel->resize(numlevels_);
    }

    int numKeys = 0;
    for (size_t i = 0; i < metadata.size(); i++) {
      int startkey = atoi(metadata[i].smallestkey.c_str());
      int endkey = atoi(metadata[i].largestkey.c_str());
      int numkeysinfile = (endkey - startkey + 1);
      numKeys += numkeysinfile;
      if (keysperlevel != nullptr) {
        (*keysperlevel)[(int)metadata[i].level] += numkeysinfile;
      }
      fprintf(stderr, "level %d name %s smallest %s largest %s\n",
              metadata[i].level, metadata[i].name.c_str(),
              metadata[i].smallestkey.c_str(),
              metadata[i].largestkey.c_str());
    }
    return numKeys;
  }

  void CreateTwoLevels() {
    AddKeys(50000, 10000);
Y
Yanqin Jin 已提交
93 94
    ASSERT_OK(dbfull()->TEST_FlushMemTable());
    ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
95
    for (int i = 0; i < 2; ++i) {
Y
Yanqin Jin 已提交
96
      ASSERT_OK(dbfull()->TEST_CompactRange(i, nullptr, nullptr));
97
    }
98 99

    AddKeys(50000, 10000);
Y
Yanqin Jin 已提交
100 101 102
    ASSERT_OK(dbfull()->TEST_FlushMemTable());
    ASSERT_OK(dbfull()->TEST_WaitForFlushMemTable());
    ASSERT_OK(dbfull()->TEST_CompactRange(0, nullptr, nullptr));
103 104
  }

Y
Yanqin Jin 已提交
105 106
  void CheckFileTypeCounts(const std::string& dir, int required_log,
                           int required_sst, int required_manifest) {
I
Igor Canadi 已提交
107
    std::vector<std::string> filenames;
108
    ASSERT_OK(env_->GetChildren(dir, &filenames));
I
Igor Canadi 已提交
109 110 111 112 113 114

    int log_cnt = 0, sst_cnt = 0, manifest_cnt = 0;
    for (auto file : filenames) {
      uint64_t number;
      FileType type;
      if (ParseFileName(file, &number, &type)) {
115
        log_cnt += (type == kWalFile);
I
Igor Canadi 已提交
116 117 118 119
        sst_cnt += (type == kTableFile);
        manifest_cnt += (type == kDescriptorFile);
      }
    }
120 121 122 123 124 125 126 127 128
    if (required_log >= 0) {
      ASSERT_EQ(required_log, log_cnt);
    }
    if (required_sst >= 0) {
      ASSERT_EQ(required_sst, sst_cnt);
    }
    if (required_manifest >= 0) {
      ASSERT_EQ(required_manifest, manifest_cnt);
    }
I
Igor Canadi 已提交
129 130
  }

131 132 133 134 135 136
  static void DoSleep(void* arg) {
    auto test = reinterpret_cast<DeleteFileTest*>(arg);
    test->env_->SleepForMicroseconds(2 * 1000 * 1000);
  }

  // An empty job to guard all jobs are processed
A
Andrew Kryczka 已提交
137
  static void GuardFinish(void* /*arg*/) {
138 139
    TEST_SYNC_POINT("DeleteFileTest::GuardFinish");
  }
140 141
};

I
Igor Sugak 已提交
142
TEST_F(DeleteFileTest, AddKeysAndQueryLevels) {
Y
Yanqin Jin 已提交
143 144 145 146 147 148
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

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
  CreateTwoLevels();
  std::vector<LiveFileMetaData> metadata;
  db_->GetLiveFilesMetaData(&metadata);

  std::string level1file = "";
  int level1keycount = 0;
  std::string level2file = "";
  int level2keycount = 0;
  int level1index = 0;
  int level2index = 1;

  ASSERT_EQ((int)metadata.size(), 2);
  if (metadata[0].level == 2) {
    level1index = 1;
    level2index = 0;
  }

  level1file = metadata[level1index].name;
  int startkey = atoi(metadata[level1index].smallestkey.c_str());
  int endkey = atoi(metadata[level1index].largestkey.c_str());
  level1keycount = (endkey - startkey + 1);
  level2file = metadata[level2index].name;
  startkey = atoi(metadata[level2index].smallestkey.c_str());
  endkey = atoi(metadata[level2index].largestkey.c_str());
  level2keycount = (endkey - startkey + 1);

  // COntrolled setup. Levels 1 and 2 should both have 50K files.
  // This is a little fragile as it depends on the current
  // compaction heuristics.
  ASSERT_EQ(level1keycount, 50000);
  ASSERT_EQ(level2keycount, 50000);

  Status status = db_->DeleteFile("0.sst");
  ASSERT_TRUE(status.IsInvalidArgument());

  // intermediate level files cannot be deleted.
  status = db_->DeleteFile(level1file);
  ASSERT_TRUE(status.IsInvalidArgument());

  // Lowest level file deletion should succeed.
189 190
  status = db_->DeleteFile(level2file);
  ASSERT_OK(status);
191 192
}

I
Igor Sugak 已提交
193
TEST_F(DeleteFileTest, PurgeObsoleteFilesTest) {
Y
Yanqin Jin 已提交
194 195 196 197 198 199
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

I
Igor Canadi 已提交
200 201 202
  CreateTwoLevels();
  // there should be only one (empty) log file because CreateTwoLevels()
  // flushes the memtables to disk
Y
Yanqin Jin 已提交
203
  CheckFileTypeCounts(wal_dir_, 1, 0, 0);
I
Igor Canadi 已提交
204 205 206
  // 2 ssts, 1 manifest
  CheckFileTypeCounts(dbname_, 0, 2, 1);
  std::string first("0"), last("999999");
207 208 209
  CompactRangeOptions compact_options;
  compact_options.change_level = true;
  compact_options.target_level = 2;
I
Igor Canadi 已提交
210
  Slice first_slice(first), last_slice(last);
211
  ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
I
Igor Canadi 已提交
212 213 214 215
  // 1 sst after compaction
  CheckFileTypeCounts(dbname_, 0, 1, 1);

  // this time, we keep an iterator alive
Y
Yanqin Jin 已提交
216
  Reopen(options);
217
  Iterator *itr = nullptr;
I
Igor Canadi 已提交
218 219
  CreateTwoLevels();
  itr = db_->NewIterator(ReadOptions());
220 221 222
  ASSERT_OK(itr->status());
  ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
  ASSERT_OK(itr->status());
I
Igor Canadi 已提交
223 224 225 226 227 228 229
  // 3 sst after compaction with live iterator
  CheckFileTypeCounts(dbname_, 0, 3, 1);
  delete itr;
  // 1 sst after iterator deletion
  CheckFileTypeCounts(dbname_, 0, 1, 1);
}

230
TEST_F(DeleteFileTest, BackgroundPurgeIteratorTest) {
Y
Yanqin Jin 已提交
231 232 233 234 235 236
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

237 238 239 240 241 242 243
  std::string first("0"), last("999999");
  CompactRangeOptions compact_options;
  compact_options.change_level = true;
  compact_options.target_level = 2;
  Slice first_slice(first), last_slice(last);

  // We keep an iterator alive
244
  Iterator* itr = nullptr;
245
  CreateTwoLevels();
Y
Yanqin Jin 已提交
246 247 248
  ReadOptions read_options;
  read_options.background_purge_on_iterator_cleanup = true;
  itr = db_->NewIterator(read_options);
249 250
  ASSERT_OK(itr->status());
  ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  // 3 sst after compaction with live iterator
  CheckFileTypeCounts(dbname_, 0, 3, 1);
  test::SleepingBackgroundTask sleeping_task_before;
  env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
                 &sleeping_task_before, Env::Priority::HIGH);
  delete itr;
  test::SleepingBackgroundTask sleeping_task_after;
  env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
                 &sleeping_task_after, Env::Priority::HIGH);

  // Make sure no purges are executed foreground
  CheckFileTypeCounts(dbname_, 0, 3, 1);
  sleeping_task_before.WakeUp();
  sleeping_task_before.WaitUntilDone();

  // Make sure all background purges are executed
  sleeping_task_after.WakeUp();
  sleeping_task_after.WaitUntilDone();
  // 1 sst after iterator deletion
  CheckFileTypeCounts(dbname_, 0, 1, 1);
}

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
TEST_F(DeleteFileTest, PurgeDuringOpen) {
  Options options = CurrentOptions();
  CheckFileTypeCounts(dbname_, -1, 0, -1);
  Close();
  std::unique_ptr<WritableFile> file;
  ASSERT_OK(options.env->NewWritableFile(dbname_ + "/000002.sst", &file,
                                         EnvOptions()));
  ASSERT_OK(file->Close());
  CheckFileTypeCounts(dbname_, -1, 1, -1);
  options.avoid_unnecessary_blocking_io = false;
  options.create_if_missing = false;
  Reopen(options);
  CheckFileTypeCounts(dbname_, -1, 0, -1);
  Close();

  // test background purge
  options.avoid_unnecessary_blocking_io = true;
  options.create_if_missing = false;
  ASSERT_OK(options.env->NewWritableFile(dbname_ + "/000002.sst", &file,
                                         EnvOptions()));
  ASSERT_OK(file->Close());
  CheckFileTypeCounts(dbname_, -1, 1, -1);
  SyncPoint::GetInstance()->DisableProcessing();
  SyncPoint::GetInstance()->ClearAllCallBacks();
  SyncPoint::GetInstance()->LoadDependency(
      {{"DeleteFileTest::PurgeDuringOpen:1", "DBImpl::BGWorkPurge:start"}});
  SyncPoint::GetInstance()->EnableProcessing();
  Reopen(options);
  // the obsolete file is not deleted until the background purge job is ran
  CheckFileTypeCounts(dbname_, -1, 1, -1);
  TEST_SYNC_POINT("DeleteFileTest::PurgeDuringOpen:1");
  ASSERT_OK(dbfull()->TEST_WaitForPurge());
  CheckFileTypeCounts(dbname_, -1, 0, -1);
}

308
TEST_F(DeleteFileTest, BackgroundPurgeCFDropTest) {
Y
Yanqin Jin 已提交
309 310 311 312 313 314
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

315 316
  auto do_test = [&](bool bg_purge) {
    ColumnFamilyOptions co;
317 318
    co.max_write_buffer_size_to_maintain =
        static_cast<int64_t>(co.write_buffer_size);
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    WriteOptions wo;
    FlushOptions fo;
    ColumnFamilyHandle* cfh = nullptr;

    ASSERT_OK(db_->CreateColumnFamily(co, "dropme", &cfh));

    ASSERT_OK(db_->Put(wo, cfh, "pika", "chu"));
    ASSERT_OK(db_->Flush(fo, cfh));
    // Expect 1 sst file.
    CheckFileTypeCounts(dbname_, 0, 1, 1);

    ASSERT_OK(db_->DropColumnFamily(cfh));
    // Still 1 file, it won't be deleted while ColumnFamilyHandle is alive.
    CheckFileTypeCounts(dbname_, 0, 1, 1);

    delete cfh;
    test::SleepingBackgroundTask sleeping_task_after;
    env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
                   &sleeping_task_after, Env::Priority::HIGH);
    // If background purge is enabled, the file should still be there.
    CheckFileTypeCounts(dbname_, 0, bg_purge ? 1 : 0, 1);
340
    TEST_SYNC_POINT("DeleteFileTest::BackgroundPurgeCFDropTest:1");
341 342 343 344 345 346 347 348 349 350 351 352 353

    // Execute background purges.
    sleeping_task_after.WakeUp();
    sleeping_task_after.WaitUntilDone();
    // The file should have been deleted.
    CheckFileTypeCounts(dbname_, 0, 0, 1);
  };

  {
    SCOPED_TRACE("avoid_unnecessary_blocking_io = false");
    do_test(false);
  }

354 355 356 357 358
  options.avoid_unnecessary_blocking_io = true;
  options.create_if_missing = false;
  Reopen(options);
  ASSERT_OK(dbfull()->TEST_WaitForPurge());

359 360 361 362 363 364 365
  SyncPoint::GetInstance()->DisableProcessing();
  SyncPoint::GetInstance()->ClearAllCallBacks();
  SyncPoint::GetInstance()->LoadDependency(
      {{"DeleteFileTest::BackgroundPurgeCFDropTest:1",
        "DBImpl::BGWorkPurge:start"}});
  SyncPoint::GetInstance()->EnableProcessing();

366 367 368 369 370 371
  {
    SCOPED_TRACE("avoid_unnecessary_blocking_io = true");
    do_test(true);
  }
}

372 373 374
// This test is to reproduce a bug that read invalid ReadOption in iterator
// cleanup function
TEST_F(DeleteFileTest, BackgroundPurgeCopyOptions) {
Y
Yanqin Jin 已提交
375 376 377 378 379 380
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

381 382 383 384 385 386 387
  std::string first("0"), last("999999");
  CompactRangeOptions compact_options;
  compact_options.change_level = true;
  compact_options.target_level = 2;
  Slice first_slice(first), last_slice(last);

  // We keep an iterator alive
388
  Iterator* itr = nullptr;
389
  CreateTwoLevels();
Y
Yanqin Jin 已提交
390 391 392 393
  {
    ReadOptions read_options;
    read_options.background_purge_on_iterator_cleanup = true;
    itr = db_->NewIterator(read_options);
394
    ASSERT_OK(itr->status());
Y
Yanqin Jin 已提交
395 396 397
    // ReadOptions is deleted, but iterator cleanup function should not be
    // affected
  }
398

399
  ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
  // 3 sst after compaction with live iterator
  CheckFileTypeCounts(dbname_, 0, 3, 1);
  delete itr;

  test::SleepingBackgroundTask sleeping_task_after;
  env_->Schedule(&test::SleepingBackgroundTask::DoSleepTask,
                 &sleeping_task_after, Env::Priority::HIGH);

  // Make sure all background purges are executed
  sleeping_task_after.WakeUp();
  sleeping_task_after.WaitUntilDone();
  // 1 sst after iterator deletion
  CheckFileTypeCounts(dbname_, 0, 1, 1);
}

415
TEST_F(DeleteFileTest, BackgroundPurgeTestMultipleJobs) {
Y
Yanqin Jin 已提交
416 417 418 419 420 421
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

422 423 424 425 426 427 428 429
  std::string first("0"), last("999999");
  CompactRangeOptions compact_options;
  compact_options.change_level = true;
  compact_options.target_level = 2;
  Slice first_slice(first), last_slice(last);

  // We keep an iterator alive
  CreateTwoLevels();
Y
Yanqin Jin 已提交
430 431 432
  ReadOptions read_options;
  read_options.background_purge_on_iterator_cleanup = true;
  Iterator* itr1 = db_->NewIterator(read_options);
433
  ASSERT_OK(itr1->status());
434
  CreateTwoLevels();
Y
Yanqin Jin 已提交
435
  Iterator* itr2 = db_->NewIterator(read_options);
436 437
  ASSERT_OK(itr2->status());
  ASSERT_OK(db_->CompactRange(compact_options, &first_slice, &last_slice));
438 439 440
  // 5 sst files after 2 compactions with 2 live iterators
  CheckFileTypeCounts(dbname_, 0, 5, 1);

441
  ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->DisableProcessing();
442
  // ~DBImpl should wait until all BGWorkPurge are finished
443
  ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->LoadDependency(
444 445 446
      {{"DBImpl::~DBImpl:WaitJob", "DBImpl::BGWorkPurge"},
       {"DeleteFileTest::GuardFinish",
        "DeleteFileTest::BackgroundPurgeTestMultipleJobs:DBClose"}});
447
  ROCKSDB_NAMESPACE::SyncPoint::GetInstance()->EnableProcessing();
448 449 450 451 452

  delete itr1;
  env_->Schedule(&DeleteFileTest::DoSleep, this, Env::Priority::HIGH);
  delete itr2;
  env_->Schedule(&DeleteFileTest::GuardFinish, nullptr, Env::Priority::HIGH);
Y
Yanqin Jin 已提交
453
  Close();
454 455 456 457 458 459

  TEST_SYNC_POINT("DeleteFileTest::BackgroundPurgeTestMultipleJobs:DBClose");
  // 1 sst after iterator deletion
  CheckFileTypeCounts(dbname_, 0, 1, 1);
}

I
Igor Sugak 已提交
460
TEST_F(DeleteFileTest, DeleteFileWithIterator) {
Y
Yanqin Jin 已提交
461 462 463 464 465 466
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

467
  CreateTwoLevels();
Y
Yanqin Jin 已提交
468 469
  ReadOptions read_options;
  Iterator* it = db_->NewIterator(read_options);
470
  ASSERT_OK(it->status());
471 472 473
  std::vector<LiveFileMetaData> metadata;
  db_->GetLiveFilesMetaData(&metadata);

Y
Yanqin Jin 已提交
474
  std::string level2file;
475

Y
Yanqin Jin 已提交
476
  ASSERT_EQ(metadata.size(), static_cast<size_t>(2));
477 478 479 480 481 482 483
  if (metadata[0].level == 1) {
    level2file = metadata[1].name;
  } else {
    level2file = metadata[0].name;
  }

  Status status = db_->DeleteFile(level2file);
484
  fprintf(stdout, "Deletion status %s: %s\n",
485
          level2file.c_str(), status.ToString().c_str());
486
  ASSERT_OK(status);
487 488 489 490 491 492 493 494 495
  it->SeekToFirst();
  int numKeysIterated = 0;
  while(it->Valid()) {
    numKeysIterated++;
    it->Next();
  }
  ASSERT_EQ(numKeysIterated, 50000);
  delete it;
}
496

I
Igor Sugak 已提交
497
TEST_F(DeleteFileTest, DeleteLogFiles) {
Y
Yanqin Jin 已提交
498 499 500 501 502 503
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);

504 505
  AddKeys(10, 0);
  VectorLogPtr logfiles;
506
  ASSERT_OK(db_->GetSortedWalFiles(logfiles));
507 508 509 510 511
  ASSERT_GT(logfiles.size(), 0UL);
  // Take the last log file which is expected to be alive and try to delete it
  // Should not succeed because live logs are not allowed to be deleted
  std::unique_ptr<LogFile> alive_log = std::move(logfiles.back());
  ASSERT_EQ(alive_log->Type(), kAliveLogFile);
Y
Yanqin Jin 已提交
512
  ASSERT_OK(env_->FileExists(wal_dir_ + "/" + alive_log->PathName()));
513 514
  fprintf(stdout, "Deleting alive log file %s\n",
          alive_log->PathName().c_str());
515
  ASSERT_NOK(db_->DeleteFile(alive_log->PathName()));
Y
Yanqin Jin 已提交
516
  ASSERT_OK(env_->FileExists(wal_dir_ + "/" + alive_log->PathName()));
517 518 519 520 521 522
  logfiles.clear();

  // Call Flush to bring about a new working log file and add more keys
  // Call Flush again to flush out memtable and move alive log to archived log
  // and try to delete the archived log file
  FlushOptions fopts;
523
  ASSERT_OK(db_->Flush(fopts));
524
  AddKeys(10, 0);
525 526
  ASSERT_OK(db_->Flush(fopts));
  ASSERT_OK(db_->GetSortedWalFiles(logfiles));
527 528 529
  ASSERT_GT(logfiles.size(), 0UL);
  std::unique_ptr<LogFile> archived_log = std::move(logfiles.front());
  ASSERT_EQ(archived_log->Type(), kArchivedLogFile);
Y
Yanqin Jin 已提交
530
  ASSERT_OK(env_->FileExists(wal_dir_ + "/" + archived_log->PathName()));
531 532 533
  fprintf(stdout, "Deleting archived log file %s\n",
          archived_log->PathName().c_str());
  ASSERT_OK(db_->DeleteFile(archived_log->PathName()));
534 535
  ASSERT_TRUE(
      env_->FileExists(wal_dir_ + "/" + archived_log->PathName()).IsNotFound());
536 537
}

I
Igor Sugak 已提交
538
TEST_F(DeleteFileTest, DeleteNonDefaultColumnFamily) {
Y
Yanqin Jin 已提交
539 540 541 542 543 544
  Options options = CurrentOptions();
  SetOptions(&options);
  Destroy(options);
  options.create_if_missing = true;
  Reopen(options);
  CreateAndReopenWithCF({"new_cf"}, options);
545 546 547

  Random rnd(5);
  for (int i = 0; i < 1000; ++i) {
Y
Yanqin Jin 已提交
548 549
    ASSERT_OK(db_->Put(WriteOptions(), handles_[1], test::RandomKey(&rnd, 10),
                       test::RandomKey(&rnd, 10)));
550
  }
Y
Yanqin Jin 已提交
551
  ASSERT_OK(db_->Flush(FlushOptions(), handles_[1]));
552
  for (int i = 0; i < 1000; ++i) {
Y
Yanqin Jin 已提交
553 554
    ASSERT_OK(db_->Put(WriteOptions(), handles_[1], test::RandomKey(&rnd, 10),
                       test::RandomKey(&rnd, 10)));
555
  }
Y
Yanqin Jin 已提交
556
  ASSERT_OK(db_->Flush(FlushOptions(), handles_[1]));
557 558

  std::vector<LiveFileMetaData> metadata;
Y
Yanqin Jin 已提交
559
  db_->GetLiveFilesMetaData(&metadata);
560 561 562 563 564 565 566 567 568
  ASSERT_EQ(2U, metadata.size());
  ASSERT_EQ("new_cf", metadata[0].column_family_name);
  ASSERT_EQ("new_cf", metadata[1].column_family_name);
  auto old_file = metadata[0].smallest_seqno < metadata[1].smallest_seqno
                      ? metadata[0].name
                      : metadata[1].name;
  auto new_file = metadata[0].smallest_seqno > metadata[1].smallest_seqno
                      ? metadata[0].name
                      : metadata[1].name;
Y
Yanqin Jin 已提交
569 570
  ASSERT_TRUE(db_->DeleteFile(new_file).IsInvalidArgument());
  ASSERT_OK(db_->DeleteFile(old_file));
571 572

  {
Y
Yanqin Jin 已提交
573
    std::unique_ptr<Iterator> itr(db_->NewIterator(ReadOptions(), handles_[1]));
574
    ASSERT_OK(itr->status());
575 576 577 578 579 580 581 582
    int count = 0;
    for (itr->SeekToFirst(); itr->Valid(); itr->Next()) {
      ASSERT_OK(itr->status());
      ++count;
    }
    ASSERT_EQ(count, 1000);
  }

Y
Yanqin Jin 已提交
583 584
  Close();
  ReopenWithColumnFamilies({kDefaultColumnFamilyName, "new_cf"}, options);
585 586

  {
Y
Yanqin Jin 已提交
587
    std::unique_ptr<Iterator> itr(db_->NewIterator(ReadOptions(), handles_[1]));
588 589 590 591 592 593 594 595 596
    int count = 0;
    for (itr->SeekToFirst(); itr->Valid(); itr->Next()) {
      ASSERT_OK(itr->status());
      ++count;
    }
    ASSERT_EQ(count, 1000);
  }
}

597
}  // namespace ROCKSDB_NAMESPACE
598 599

int main(int argc, char** argv) {
600
  ROCKSDB_NAMESPACE::port::InstallStackTraceHandler();
I
Igor Sugak 已提交
601
  ::testing::InitGoogleTest(&argc, argv);
Y
Yanqin Jin 已提交
602
  RegisterCustomObjects(argc, argv);
I
Igor Sugak 已提交
603
  return RUN_ALL_TESTS();
604 605
}

606 607 608
#else
#include <stdio.h>

609
int main(int /*argc*/, char** /*argv*/) {
610 611 612 613 614 615
  fprintf(stderr,
          "SKIPPED as DBImpl::DeleteFile is not supported in ROCKSDB_LITE\n");
  return 0;
}

#endif  // !ROCKSDB_LITE