Repository navigation
Using MT_hashtable for VC lookup. - #3055
Conversation
|
@shinrich @SolidWallOfCode @zizhong At the QUIC Hackathon in Tokyo, @JordyLiu26 suggested that The hashtable was originally used with congestion control feature that was removed on #2970, but according to @JordyLiu26, it has different characteristics from other ones we have. So even if we're going to use other hashtable for QUIC, we may want to bring back this hashtable. |
|
In this pull request, the current thread can be blocked by the ConnectionTable take the lock for access the data. If the ConnectionTable as a processor, it will improve the efficiency even more.
lookup
So, only take the lock when lookup value. |
| main(int /* argc ATS_UNUSED */, char ** /*argv ATS_UNUSED */) | ||
| { | ||
| int ret; | ||
| test_MT_HashTable(&ret); |
There was a problem hiding this comment.
Please use "Catch" for unit test framework. You can find some tests using Catch in lib/ts/unit-tests/
https://github.com/apache/trafficserver/tree/master/lib/ts/unit-tests
The header file is https://github.com/apache/trafficserver/blob/master/tests/include/catch.hpp
Docs are https://github.com/catchorg/Catch2/blob/master/docs/Readme.md#top
There was a problem hiding this comment.
Could you divide this PR like below? Because MTHashTable is data structure and it could be used by other component regardless QUIC use it or not.
- "Bring back MT_hashtable.h under lib/ts/" (for master branch)
- "using MT_hashtable for VC lookup" (for quic-latest branch)
|
Seems reasonable to me. I know that we have at times been looking for a "lockless" hash table. But if MTHashTable is better than what we have by default, we should not let the perfect future hash stop present progress. |
|
Agreed. Thanks for taking care of this issue. |
|
link to #3066 |
7243c94 to
3362520
Compare
|
@masaori335 I have update this pull request for using MT_hashtable for VC lookup. |
| @@ -66,6 +67,6 @@ class QUICConnectionTable | |||
|
|
|||
| private: | |||
| // FIXME Use another map impl. that has good support for concurrent access | |||
There was a problem hiding this comment.
This comment is not needed anymore.
| // To check whether the return value is nullptr in case memory leak. | ||
| // The return value isn't nullptr, the new value will take up the slot and return old value. | ||
| if (ret_connection) { | ||
| // TODO: free the old connection. |
There was a problem hiding this comment.
This shouldn't happen, right? Maybe we should return an error code instead of silently freeing it.
There was a problem hiding this comment.
Yes, It shouldn't happen(CID is unique value).
If the CID has duplicate value, the old value was be returned, it should be freed.
There was a problem hiding this comment.
The connection have to be freed somewhere but I think here is not the appropriate place because this table is just a container. Some another class should be responsible for freeing connections. Probably, the class that allocate/instantiate the connection.
How about escalating the odd situation to the caller side with an error code and freeing the connection on the caller side? Then we can do something more (Decrement/Increment some stats, Output log with other information).
There was a problem hiding this comment.
Fine, I can provide the pointer of QUICConnection as returning value in insert functions, like lookup functions. To determine if the returning value is nullptr by caller side. If the returning value isn't nullptr, it should be freed by caller.
There was a problem hiding this comment.
@JordyLiu26 maybe return a smart pointer since we are on modern c++?
There was a problem hiding this comment.
void
randomize()
{
std::random_device rnd;
this->_id = (static_cast<uint64_t>(rnd()) << 32) + rnd();
};
The rnd.min() is 0, rnd.max() is 4294967295 (unsigned int), but the rnd was shifted left 32 bits. So the CID should greater than 4294967296.(except 0)
There was a problem hiding this comment.
So if (static_cast<uint64_t>(rnd()) << 32) was 0, CID will be less than 4294967295. And more importantly, every 64 bits numbers should be valid CIDs. Using only 4294967296 to UINT64_MAX would be a kind of protocol violation.
There was a problem hiding this comment.
Maybe, modify the following,
void
randomize()
{
std::random_device rnd;
std::mt19937 gen(rnd); // to generate a single random number that is used as a seed for a pseudorandom number generator (MT19937 stands for "Mersenne Twister" based on the Mersenne prime 2^19937−1)
std::uniform_int_distribution<> dis(min, max); // set min and max number. [min, max]
this->_id = (static_cast<uint64_t>(dis(gen)) << 32) + rnd();
};
There was a problem hiding this comment.
If I understand correctly, it would ensure that upper 32 bits are not 0. However that's not what I really wanted to say. Probably, we need two hashtables unless a hashtable supports longer keys (maybe 224 bits at least).
There are tow reasons:
a) CID is unsigned 64 bits integer. Reserving some bits to not conflict with 5-tuple reduces actual ID range available.
b) Imagine IPv6 support. 5-tuple is obviously longer than CID. Even if we use only source address and source port, technically, we need 224 (64 + 128 + 32) bits space.
Anyway, the specification is not fixed, so the table doesn't need to be perfect. Let's revisit when we support omitting CID.
There was a problem hiding this comment.
Yes. If one table contains CID and 5-tuples, the key was divided two parts to ensure the table has unique key.
3362520 to
3f509e3
Compare
| // To check whether the return value is nullptr in case memory leak. | ||
| // The return value isn't nullptr, the new value will take up the slot and return old value. | ||
| if (ret_connection) { | ||
| // TODO: free the old connection. |
There was a problem hiding this comment.
@JordyLiu26 maybe return a smart pointer since we are on modern c++?
| // | ||
| QUICPacketHandlerIn::QUICPacketHandlerIn(const NetProcessor::AcceptOptions &opt, SSL_CTX *ctx) : NetAccept(opt), _ssl_ctx(ctx) | ||
| QUICPacketHandlerIn::QUICPacketHandlerIn(const NetProcessor::AcceptOptions &opt, SSL_CTX *ctx) | ||
| : NetAccept(opt), _ctable(65521), _ssl_ctx(ctx) |
There was a problem hiding this comment.
any reason to choose this number?
maybe make it configurable?
There was a problem hiding this comment.
65521 is primer number. I try to make the hash table size a prime number.
I think, to define a default when create the table. When the load factor > 4, the table will be resized.
OK, I add a configuration item for ConnectionTable.
| // this->_cids.put(connection->endpoint(), nullptr); | ||
| // } | ||
| this->_connections.put(cid, nullptr); | ||
| SCOPED_MUTEX_LOCK(lock, _connections.lock_for_key(cid), this_ethread()); |
There was a problem hiding this comment.
{
lock()
access()
}
please use a curly brackets to keep the lock thin.
918bc9f to
2a78a87
Compare
|
What is status of this PR? Ready for review again? |
| QUICPacketHandlerIn::~QUICPacketHandlerIn() | ||
| { | ||
| // TODO: clear all values before destory the table. | ||
| delete _ctable; |
There was a problem hiding this comment.
Rather than an explicit delete, why not use std::unique_ptr?
There was a problem hiding this comment.
It's a kind of historical reason. std::vec doesn't work correctly if you allocate a memory with THREAD_ALLOC or something like that for a object that has std::vec because it doesn't call constructors. Before I realize this, I used new/delete as a workaround. (and now QUIC code uses "placement new" after allocating memory).
So we can use std::unique_ptr, but actually we don't need to create it dynamically anymore. I'll fix it later.
Bring back MT_hashtable to lib/ts. The MT_hashtable have 64 locks, each lock protects 65535 buckets, so ideally, the table was accessed by 64 threads concurrently.
Using MT_hashtable as data structures of QUICConnectionTable.