@@ -33,8 +33,11 @@ class AtomicCounter {
33
33
public:
34
34
AtomicCounter () : count_(0 ) { }
35
35
void Increment () {
36
+ IncrementBy (1 );
37
+ }
38
+ void IncrementBy (int count) {
36
39
MutexLock l (&mu_);
37
- count_++ ;
40
+ count_ += count ;
38
41
}
39
42
int Read () {
40
43
MutexLock l (&mu_);
@@ -45,6 +48,10 @@ class AtomicCounter {
45
48
count_ = 0 ;
46
49
}
47
50
};
51
+
52
+ void DelayMilliseconds (int millis) {
53
+ Env::Default ()->SleepForMicroseconds (millis * 1000 );
54
+ }
48
55
}
49
56
50
57
// Special Env used to delay background operations
@@ -69,6 +76,7 @@ class SpecialEnv : public EnvWrapper {
69
76
AtomicCounter random_read_counter_;
70
77
71
78
AtomicCounter sleep_counter_;
79
+ AtomicCounter sleep_time_counter_;
72
80
73
81
explicit SpecialEnv (Env* base) : EnvWrapper(base) {
74
82
delay_sstable_sync_.Release_Store (NULL );
@@ -103,7 +111,7 @@ class SpecialEnv : public EnvWrapper {
103
111
Status Flush () { return base_->Flush (); }
104
112
Status Sync () {
105
113
while (env_->delay_sstable_sync_ .Acquire_Load () != NULL ) {
106
- env_-> SleepForMicroseconds ( 100000 );
114
+ DelayMilliseconds ( 100 );
107
115
}
108
116
return base_->Sync ();
109
117
}
@@ -174,8 +182,9 @@ class SpecialEnv : public EnvWrapper {
174
182
175
183
virtual void SleepForMicroseconds (int micros) {
176
184
sleep_counter_.Increment ();
177
- target ()-> SleepForMicroseconds (micros);
185
+ sleep_time_counter_. IncrementBy (micros);
178
186
}
187
+
179
188
};
180
189
181
190
class DBTest {
@@ -625,7 +634,7 @@ TEST(DBTest, GetEncountersEmptyLevel) {
625
634
}
626
635
627
636
// Step 4: Wait for compaction to finish
628
- env_-> SleepForMicroseconds ( 1000000 );
637
+ DelayMilliseconds ( 1000 );
629
638
630
639
ASSERT_EQ (NumTableFilesAtLevel (0 ), 0 );
631
640
} while (ChangeOptions ());
@@ -1309,7 +1318,7 @@ TEST(DBTest, L0_CompactionBug_Issue44_a) {
1309
1318
Reopen ();
1310
1319
Reopen ();
1311
1320
ASSERT_EQ (" (a->v)" , Contents ());
1312
- env_-> SleepForMicroseconds ( 1000000 ); // Wait for compaction to finish
1321
+ DelayMilliseconds ( 1000 ); // Wait for compaction to finish
1313
1322
ASSERT_EQ (" (a->v)" , Contents ());
1314
1323
}
1315
1324
@@ -1325,7 +1334,7 @@ TEST(DBTest, L0_CompactionBug_Issue44_b) {
1325
1334
Put (" " ," " );
1326
1335
Reopen ();
1327
1336
Put (" " ," " );
1328
- env_-> SleepForMicroseconds ( 1000000 ); // Wait for compaction to finish
1337
+ DelayMilliseconds ( 1000 ); // Wait for compaction to finish
1329
1338
Reopen ();
1330
1339
Put (" d" ," dv" );
1331
1340
Reopen ();
@@ -1335,7 +1344,7 @@ TEST(DBTest, L0_CompactionBug_Issue44_b) {
1335
1344
Delete (" b" );
1336
1345
Reopen ();
1337
1346
ASSERT_EQ (" (->)(c->cv)" , Contents ());
1338
- env_-> SleepForMicroseconds ( 1000000 ); // Wait for compaction to finish
1347
+ DelayMilliseconds ( 1000 ); // Wait for compaction to finish
1339
1348
ASSERT_EQ (" (->)(c->cv)" , Contents ());
1340
1349
}
1341
1350
@@ -1520,6 +1529,30 @@ TEST(DBTest, NoSpace) {
1520
1529
ASSERT_GE (env_->sleep_counter_ .Read (), 5 );
1521
1530
}
1522
1531
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
+
1523
1556
TEST (DBTest, NonWritableFileSystem) {
1524
1557
Options options = CurrentOptions ();
1525
1558
options.write_buffer_size = 1000 ;
@@ -1533,7 +1566,7 @@ TEST(DBTest, NonWritableFileSystem) {
1533
1566
fprintf (stderr, " iter %d; errors %d\n " , i, errors);
1534
1567
if (!Put (" foo" , big).ok ()) {
1535
1568
errors++;
1536
- env_-> SleepForMicroseconds ( 100000 );
1569
+ DelayMilliseconds ( 100 );
1537
1570
}
1538
1571
}
1539
1572
ASSERT_GT (errors, 0 );
@@ -1589,6 +1622,7 @@ TEST(DBTest, MissingSSTFile) {
1589
1622
dbfull ()->TEST_CompactMemTable ();
1590
1623
ASSERT_EQ (" bar" , Get (" foo" ));
1591
1624
1625
+ Close ();
1592
1626
ASSERT_TRUE (DeleteAnSSTFile ());
1593
1627
Options options = CurrentOptions ();
1594
1628
options.paranoid_checks = true ;
@@ -1742,13 +1776,13 @@ TEST(DBTest, MultiThreaded) {
1742
1776
}
1743
1777
1744
1778
// Let them run for a while
1745
- env_-> SleepForMicroseconds (kTestSeconds * 1000000 );
1779
+ DelayMilliseconds (kTestSeconds * 1000 );
1746
1780
1747
1781
// Stop the threads and wait for them to finish
1748
1782
mt.stop .Release_Store (&mt);
1749
1783
for (int id = 0 ; id < kNumThreads ; id++) {
1750
1784
while (mt.thread_done [id].Acquire_Load () == NULL ) {
1751
- env_-> SleepForMicroseconds ( 100000 );
1785
+ DelayMilliseconds ( 100 );
1752
1786
}
1753
1787
}
1754
1788
} while (ChangeOptions ());
0 commit comments