kvrocks icon indicating copy to clipboard operation
kvrocks copied to clipboard

feat: replace worker connection's mutex with `tbb::concurrent_hash_map`

Open LindaSummer opened this issue 6 months ago • 11 comments

Issue

#1389

Proposed Changes

  • refactor worker's connection structure from std:::map to tbb::concurrent_hash_map
  • remove redundant mutex

Comment

During my testing and researching on tbb, I find that the tbb::concurrent_hash_map is not thread safe on traversing and erasing. Here is the related issue: https://github.com/oneapi-src/oneTBB/issues/183. ~So, I have to keep the lock on iteration and erasing.~

After reading the tbb code, I find that tbb::concurrent_hashmap should use range() with tbb::parallel_for and tbb::parallel_reduce for traversing.

    //------------------------------------------------------------------------
    // Parallel algorithm support
    //------------------------------------------------------------------------
    range_type range( size_type grainsize=1 ) {
        return range_type( *this, grainsize );
    }
    const_range_type range( size_type grainsize=1 ) const {
        return const_range_type( *this, grainsize );
    }

    //------------------------------------------------------------------------
    // STL support - not thread-safe methods
    //------------------------------------------------------------------------
    iterator begin() { return iterator( *this, 0, this->my_embedded_segment, this->my_embedded_segment->node_list.load(std::memory_order_relaxed) ); }
    const_iterator begin() const { return const_iterator( *this, 0, this->my_embedded_segment, this->my_embedded_segment->node_list.load(std::memory_order_relaxed) ); }
    const_iterator cbegin() const { return const_iterator( *this, 0, this->my_embedded_segment, this->my_embedded_segment->node_list.load(std::memory_order_relaxed) ); }
    iterator end() { return iterator( *this, 0, nullptr, nullptr ); }
    const_iterator end() const { return const_iterator( *this, 0, nullptr, nullptr ); }
    const_iterator cend() const { return const_iterator( *this, 0, nullptr, nullptr ); }

LindaSummer avatar Aug 01 '24 13:08 LindaSummer