@@ -1884,6 +1884,8 @@ struct RocksDBKeyValueStore : IKeyValueStore {
18841884 Future<Void> openFuture;
18851885 std::unique_ptr<rocksdb::WriteBatch> writeBatch;
18861886 std::set<Key> keysSet;
1887+ // maximum number of single key deletes in a commit, if ROCKSDB_SINGLEKEY_DELETES_ON_CLEARRANGE is enabled.
1888+ int maxDeletes;
18871889 Optional<Future<Void>> metrics;
18881890 FlowLock readSemaphore;
18891891 int numReadWaiters;
@@ -2072,6 +2074,7 @@ struct RocksDBKeyValueStore : IKeyValueStore {
20722074 if (writeBatch == nullptr ) {
20732075 writeBatch.reset (new rocksdb::WriteBatch ());
20742076 keysSet.clear ();
2077+ maxDeletes = SERVER_KNOBS->ROCKSDB_SINGLEKEY_DELETES_MAX ;
20752078 }
20762079 ASSERT (defaultFdbCF != nullptr );
20772080 writeBatch->Put (defaultFdbCF, toSlice (kv.key ), toSlice (kv.value ));
@@ -2080,23 +2083,24 @@ struct RocksDBKeyValueStore : IKeyValueStore {
20802083 }
20812084 }
20822085
2083- void clear (KeyRangeRef keyRange, const StorageServerMetrics* storageMetrics, const Arena*) override {
2086+ void clear (KeyRangeRef keyRange, const Arena*) override {
20842087 if (writeBatch == nullptr ) {
20852088 writeBatch.reset (new rocksdb::WriteBatch ());
20862089 keysSet.clear ();
2090+ maxDeletes = SERVER_KNOBS->ROCKSDB_SINGLEKEY_DELETES_MAX ;
20872091 }
20882092
20892093 ASSERT (defaultFdbCF != nullptr );
20902094 // Number of deletes to rocksdb = counters.deleteKeyReqs + convertedDeleteKeyReqs;
20912095 // Number of deleteRanges to rocksdb = counters.deleteRangeReqs - counters.convertedDeleteRangeReqs;
2092- if (keyRange.singleKeyRange ()) {
2096+ if (keyRange.singleKeyRange () && !SERVER_KNOBS-> ROCKSDB_FORCE_DELETERANGE_FOR_CLEARRANGE ) {
20932097 writeBatch->Delete (defaultFdbCF, toSlice (keyRange.begin ));
20942098 ++counters.deleteKeyReqs ;
2099+ --maxDeletes;
20952100 } else {
20962101 ++counters.deleteRangeReqs ;
2097- if (SERVER_KNOBS->ROCKSDB_SINGLEKEY_DELETES_ON_CLEARRANGE && storageMetrics != nullptr &&
2098- storageMetrics->byteSample .getEstimate (keyRange) <
2099- SERVER_KNOBS->ROCKSDB_SINGLEKEY_DELETES_BYTES_LIMIT ) {
2102+ if (SERVER_KNOBS->ROCKSDB_SINGLEKEY_DELETES_ON_CLEARRANGE &&
2103+ !SERVER_KNOBS->ROCKSDB_FORCE_DELETERANGE_FOR_CLEARRANGE && maxDeletes > 0 ) {
21002104 ++counters.convertedDeleteRangeReqs ;
21012105 rocksdb::ReadOptions options = getReadOptions ();
21022106 auto beginSlice = toSlice (keyRange.begin );
@@ -2105,19 +2109,21 @@ struct RocksDBKeyValueStore : IKeyValueStore {
21052109 options.iterate_upper_bound = &endSlice;
21062110 auto cursor = std::unique_ptr<rocksdb::Iterator>(db->NewIterator (options, defaultFdbCF));
21072111 cursor->Seek (toSlice (keyRange.begin ));
2108- while (cursor->Valid () && toStringRef (cursor->key ()) < keyRange.end ) {
2112+ while (cursor->Valid () && toStringRef (cursor->key ()) < keyRange.end && maxDeletes > 0 ) {
21092113 writeBatch->Delete (defaultFdbCF, cursor->key ());
21102114 ++counters.convertedDeleteKeyReqs ;
2115+ --maxDeletes;
21112116 cursor->Next ();
21122117 }
2113- if (!cursor->status ().ok ()) {
2118+ if (!cursor->status ().ok () || maxDeletes <= 0 ) {
21142119 // if readrange iteration fails, then do a deleteRange.
21152120 writeBatch->DeleteRange (defaultFdbCF, toSlice (keyRange.begin ), toSlice (keyRange.end ));
21162121 } else {
21172122 auto it = keysSet.lower_bound (keyRange.begin );
21182123 while (it != keysSet.end () && *it < keyRange.end ) {
21192124 writeBatch->Delete (defaultFdbCF, toSlice (*it));
21202125 ++counters.convertedDeleteKeyReqs ;
2126+ --maxDeletes;
21212127 it++;
21222128 }
21232129 }
@@ -2156,6 +2162,7 @@ struct RocksDBKeyValueStore : IKeyValueStore {
21562162 auto a = new Writer::CommitAction ();
21572163 a->batchToCommit = std::move (writeBatch);
21582164 keysSet.clear ();
2165+ maxDeletes = SERVER_KNOBS->ROCKSDB_SINGLEKEY_DELETES_MAX ;
21592166 auto res = a->done .getFuture ();
21602167 writeThread->post (a);
21612168 return res;
0 commit comments