@@ -33,8 +33,11 @@ class AtomicCounter {
3333 public:
3434 AtomicCounter () : count_(0 ) { }
3535 void Increment () {
36+ IncrementBy (1 );
37+ }
38+ void IncrementBy (int count) {
3639 MutexLock l (&mu_);
37- count_++ ;
40+ count_ += count ;
3841 }
3942 int Read () {
4043 MutexLock l (&mu_);
@@ -45,6 +48,10 @@ class AtomicCounter {
4548 count_ = 0 ;
4649 }
4750};
51+
52+ void DelayMilliseconds (int millis) {
53+ Env::Default ()->SleepForMicroseconds (millis * 1000 );
54+ }
4855}
4956
5057// Special Env used to delay background operations
@@ -69,6 +76,7 @@ class SpecialEnv : public EnvWrapper {
6976 AtomicCounter random_read_counter_;
7077
7178 AtomicCounter sleep_counter_;
79+ AtomicCounter sleep_time_counter_;
7280
7381 explicit SpecialEnv (Env* base) : EnvWrapper(base) {
7482 delay_sstable_sync_.Release_Store (NULL );
@@ -103,7 +111,7 @@ class SpecialEnv : public EnvWrapper {
103111 Status Flush () { return base_->Flush (); }
104112 Status Sync () {
105113 while (env_->delay_sstable_sync_ .Acquire_Load () != NULL ) {
106- env_-> SleepForMicroseconds ( 100000 );
114+ DelayMilliseconds ( 100 );
107115 }
108116 return base_->Sync ();
109117 }
@@ -174,8 +182,9 @@ class SpecialEnv : public EnvWrapper {
174182
175183 virtual void SleepForMicroseconds (int micros) {
176184 sleep_counter_.Increment ();
177- target ()-> SleepForMicroseconds (micros);
185+ sleep_time_counter_. IncrementBy (micros);
178186 }
187+
179188};
180189
181190class DBTest {
@@ -625,7 +634,7 @@ TEST(DBTest, GetEncountersEmptyLevel) {
625634 }
626635
627636 // Step 4: Wait for compaction to finish
628- env_-> SleepForMicroseconds ( 1000000 );
637+ DelayMilliseconds ( 1000 );
629638
630639 ASSERT_EQ (NumTableFilesAtLevel (0 ), 0 );
631640 } while (ChangeOptions ());
@@ -1309,7 +1318,7 @@ TEST(DBTest, L0_CompactionBug_Issue44_a) {
13091318 Reopen ();
13101319 Reopen ();
13111320 ASSERT_EQ (" (a->v)" , Contents ());
1312- env_-> SleepForMicroseconds ( 1000000 ); // Wait for compaction to finish
1321+ DelayMilliseconds ( 1000 ); // Wait for compaction to finish
13131322 ASSERT_EQ (" (a->v)" , Contents ());
13141323}
13151324
@@ -1325,7 +1334,7 @@ TEST(DBTest, L0_CompactionBug_Issue44_b) {
13251334 Put (" " ," " );
13261335 Reopen ();
13271336 Put (" " ," " );
1328- env_-> SleepForMicroseconds ( 1000000 ); // Wait for compaction to finish
1337+ DelayMilliseconds ( 1000 ); // Wait for compaction to finish
13291338 Reopen ();
13301339 Put (" d" ," dv" );
13311340 Reopen ();
@@ -1335,7 +1344,7 @@ TEST(DBTest, L0_CompactionBug_Issue44_b) {
13351344 Delete (" b" );
13361345 Reopen ();
13371346 ASSERT_EQ (" (->)(c->cv)" , Contents ());
1338- env_-> SleepForMicroseconds ( 1000000 ); // Wait for compaction to finish
1347+ DelayMilliseconds ( 1000 ); // Wait for compaction to finish
13391348 ASSERT_EQ (" (->)(c->cv)" , Contents ());
13401349}
13411350
@@ -1520,6 +1529,30 @@ TEST(DBTest, NoSpace) {
15201529 ASSERT_GE (env_->sleep_counter_ .Read (), 5 );
15211530}
15221531
1532+ TEST (DBTest, ExponentialBackoff) {
1533+ Options options = CurrentOptions ();
1534+ options.env = env_;
1535+ Reopen (&options);
1536+
1537+ ASSERT_OK (Put (" foo" , " v1" ));
1538+ ASSERT_EQ (" v1" , Get (" foo" ));
1539+ Compact (" a" , " z" );
1540+ env_->non_writable_ .Release_Store (env_); // Force errors for new files
1541+ env_->sleep_counter_ .Reset ();
1542+ env_->sleep_time_counter_ .Reset ();
1543+ for (int i = 0 ; i < 5 ; i++) {
1544+ dbfull ()->TEST_CompactRange (2 , NULL , NULL );
1545+ }
1546+ env_->non_writable_ .Release_Store (NULL );
1547+
1548+ // Wait for compaction to finish
1549+ DelayMilliseconds (1000 );
1550+
1551+ ASSERT_GE (env_->sleep_counter_ .Read (), 5 );
1552+ ASSERT_LT (env_->sleep_counter_ .Read (), 10 );
1553+ ASSERT_GE (env_->sleep_time_counter_ .Read (), 10e6 );
1554+ }
1555+
15231556TEST (DBTest, NonWritableFileSystem) {
15241557 Options options = CurrentOptions ();
15251558 options.write_buffer_size = 1000 ;
@@ -1533,7 +1566,7 @@ TEST(DBTest, NonWritableFileSystem) {
15331566 fprintf (stderr, " iter %d; errors %d\n " , i, errors);
15341567 if (!Put (" foo" , big).ok ()) {
15351568 errors++;
1536- env_-> SleepForMicroseconds ( 100000 );
1569+ DelayMilliseconds ( 100 );
15371570 }
15381571 }
15391572 ASSERT_GT (errors, 0 );
@@ -1589,6 +1622,7 @@ TEST(DBTest, MissingSSTFile) {
15891622 dbfull ()->TEST_CompactMemTable ();
15901623 ASSERT_EQ (" bar" , Get (" foo" ));
15911624
1625+ Close ();
15921626 ASSERT_TRUE (DeleteAnSSTFile ());
15931627 Options options = CurrentOptions ();
15941628 options.paranoid_checks = true ;
@@ -1742,13 +1776,13 @@ TEST(DBTest, MultiThreaded) {
17421776 }
17431777
17441778 // Let them run for a while
1745- env_-> SleepForMicroseconds (kTestSeconds * 1000000 );
1779+ DelayMilliseconds (kTestSeconds * 1000 );
17461780
17471781 // Stop the threads and wait for them to finish
17481782 mt.stop .Release_Store (&mt);
17491783 for (int id = 0 ; id < kNumThreads ; id++) {
17501784 while (mt.thread_done [id].Acquire_Load () == NULL ) {
1751- env_-> SleepForMicroseconds ( 100000 );
1785+ DelayMilliseconds ( 100 );
17521786 }
17531787 }
17541788 } while (ChangeOptions ());
0 commit comments