Skip to content

Using MT_hashtable for VC lookup. - #3055

Merged
masaori335 merged 1 commit into
apache:quic-latestfrom
JordyLiu26:quic-latest
Mar 5, 2018
Merged

masaori335 merged 1 commit into
apache:quic-latestfrom
JordyLiu26:quic-latest

Conversation

@JordyLiu26

@JordyLiu26 JordyLiu26 commented Jan 30, 2018 •

Copy link
Copy Markdown
Contributor

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.

@oknet
oknet requested review from masaori335 and maskit January 30, 2018 08:49
@maskit maskit added the Core label Jan 30, 2018
@maskit maskit added this to the 8.0.0 milestone Jan 30, 2018
@maskit

maskit commented Jan 30, 2018

Copy link
Copy Markdown
Member

@shinrich @SolidWallOfCode @zizhong At the QUIC Hackathon in Tokyo, @JordyLiu26 suggested that MT_hashtable would suit for the QUIC NetVC table. So this is another candidate for #2478, and that means this conflict with #2555.

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.

@JordyLiu26

Copy link
Copy Markdown
Contributor Author

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.
Like this:
Create 64 todo-lists (todolist[64]) by InkAtomicList, each atomic list is corresponding to the lock in the ConnectionTableProcessor.
Insert/remove
If the value want to be inserted(removed) into TB by key, the ConnectionTableProcessor will try to take the lock.

  • insert the data after take the lock[x].( caculate x according to the key)
  • fail to take the lock, the data will be encapsulated as a todo-data and insert it into the todo-list[x].

lookup
lookup a value by key in the ConnectionTable.

  • Take the lock[x] that was caculated by key. Firstly, process all the data of todo-list[x].secondly, lookup value by key.
  • Failed to take the lock[x], create a continuation and set the key or other context in it, then put the continuation into EventProcessor, wait to callback next looping.

So, only take the lock when lookup value.
The thread isn't be blocked even when lookup value by key in the ConnectionTable .

Comment thread lib/ts/test_MT_hashtable.cc Outdated
main(int /* argc ATS_UNUSED */, char ** /*argv ATS_UNUSED */)
{
int ret;
test_MT_HashTable(&ret);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@masaori335 masaori335 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. "Bring back MT_hashtable.h under lib/ts/" (for master branch)
  2. "using MT_hashtable for VC lookup" (for quic-latest branch)

@shinrich

Copy link
Copy Markdown
Member

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.

@zizhong

zizhong commented Jan 31, 2018 •

Copy link
Copy Markdown
Member

Agreed. Thanks for taking care of this issue.

@oknet

oknet commented Feb 1, 2018

Copy link
Copy Markdown
Member

link to #3066

@JordyLiu26 JordyLiu26 changed the title Bring back MT_hashtable.h under lib/ts/ and using MT_hashtable for VC lookup Using MT_hashtable for VC lookup. Feb 5, 2018
@JordyLiu26

Copy link
Copy Markdown
Contributor Author

@masaori335 I have update this pull request for using MT_hashtable for VC lookup.
QUICConnectionTable have 64 locks, each lock protects 65521(prime number) buckets, so ideally, the table was accessed by 64 threads concurrently.

Comment thread iocore/net/quic/QUICConnectionTable.h Outdated
@@ -66,6 +67,6 @@ class QUICConnectionTable

private:
// FIXME Use another map impl. that has good support for concurrent access

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not needed anymore.

Comment thread iocore/net/quic/QUICConnectionTable.cc Outdated
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't happen, right? Maybe we should return an error code instead of silently freeing it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@JordyLiu26 JordyLiu26 Feb 5, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JordyLiu26 maybe return a smart pointer since we are on modern c++?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JordyLiu26 JordyLiu26 Feb 6, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. If one table contains CID and 5-tuples, the key was divided two parts to ensure the table has unique key.

Comment thread iocore/net/quic/QUICConnectionTable.cc Outdated
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JordyLiu26 maybe return a smart pointer since we are on modern c++?

Comment thread iocore/net/QUICPacketHandler.cc Outdated
//
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to choose this number?
maybe make it configurable?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
     lock()
     access()
}

please use a curly brackets to keep the lock thin.

@JordyLiu26
JordyLiu26 force-pushed the quic-latest branch 2 times, most recently from 918bc9f to 2a78a87 Compare February 8, 2018 07:06
@masaori335

Copy link
Copy Markdown
Contributor

What is status of this PR? Ready for review again?
Anyway, please rebase on latest "quic-latest" branch and resolve conflicts.

QUICPacketHandlerIn::~QUICPacketHandlerIn()
{
// TODO: clear all values before destory the table.
delete _ctable;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than an explicit delete, why not use std::unique_ptr?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@masaori335
masaori335 merged commit b3759a9 into apache:quic-latest Mar 5, 2018
@zwoop zwoop modified the milestones: 8.0.0, QUIC Mar 9, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

8 participants