Use NotificationQueue from folly

Summary:
This is one of the last changes to make folly and
thrift portable. The old thrift SpinLock implementation is exactly we
want here so port this into folly. If someone later makes efficeint
versions of MicroSpinLock for platforms other than x64 they can easily
be plugged in without changing the calling code.

Test Plan: compiled on multiple platforms

Reviewed By: alandau@fb.com

Subscribers: folly-diffs@, ldbrandy, trunkagent, alandau, bmatheny, njormrod, mshneer, shikong, benyluo, kmdent, fma, ranjeeth, subodh

FB internal diff: D1621717

Tasks: 5183325
Make freezeToTempFile work like it used to

Test Plan: Unit tests

Reviewed By: ashishsharma@fb.com

Subscribers: alandau, bmatheny, mshneer

FB internal diff: D1624820
fix issues with handling saslErrors

Summary:
two problems
1. bug when converting old rethrow pattern to new exception wrappers i.e.
try { rethrow; } catch (TTEx& ex) { ...; return; }
has become
ex.with_exception<TTex>([] { .... ; return; });
which executes a 'return' statement inside lambda and is a noop

2. if user callback ends up desctructing the channel, we can endup with "use-after-free". DestructorGuard prevents this from happening.

Test Plan: unit tests

Reviewed By: andrei.bajenov@fb.com

Subscribers: bmatheny, mshneer, andrewcox, alandau, hitesh

FB internal diff: D1624483

Tasks: 5371697
Minor fixes

Summary: this caused issues with the preprocessor.

Test Plan: build it

Reviewed By: robbert@fb.com, andrei.alexandrescu@fb.com

Subscribers: fbcode-common-diffs@, alandau, bmatheny, anca, mshneer, trunkagent, ruibalp, andreib, simpkins, fugalh, agallagher, sdwilsh

FB internal diff: D1623030

Tasks: 5399083
Include wangle::Future only when needed

Summary: include wangle/Future by default

Test Plan: Buiid it

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, fbcode-common-diffs@, ruibalp, fugalh, alandau, mshneer

FB internal diff: D1627942
move asyncserversocket to folly

Summary:
Changes:
* namespace to folly
* some std::chrono replacesments
* Moves shutdownSocketSet stuff to thriftserver instead of in the socket itself.
* Changes exception types.  I don't see anywhere that uses the TAsync*SERVER*socket exceptions depending on the TTransport type, but I could be wrong? I don't really know what to do about the exception types

Test Plan: build thrift and folly

Reviewed By: dcsommer@fb.com

Subscribers: hphp-diffs@, ps, folly-diffs@, anca, trunkagent, jsedgwick, fugalh, doug, alandau, bmatheny, njormrod

FB internal diff: D1579187
set container/string size limit for ThriftSerializer

Summary:
The current thrift serialization/deserialization is done through static
functions in ThriftSerializer. This diff make the change inside that class.

Test Plan: build it, unit tests

Reviewed By: grantguo@fb.com

Subscribers: trunkagent, lars, varunk, dfong, sumeet, alandau, shilin, bmatheny, maoy, zhe, xie, mshneer

FB internal diff: D1628220

Tasks: 5406353
Remove hostname lookup from SecureThrift

Summary:
This way we have more control over what the principal is, and we don't have to add complicated code to thrift.

Test Plan: run with custom principals.

Reviewed By: mshneer@fb.com

Subscribers: trunkagent, dihde, mcduff, marccelani, ps, alandau, bmatheny, mshneer, sandeepkk, wstefancik

FB internal diff: D1627886
Remove unused FBString.h includes from neutronium.

Summary:
[Thrift] Remove unused FBString.h includes from neutronium.

These includes appear to be unused. But we can let contbuild tell us for sure.

Test Plan: Continuous build.

Reviewed By: alandau@fb.com

Subscribers: alandau, bmatheny, mshneer, dougw

FB internal diff: D1631478
Fallback to insecure (and not fatal) in case of kerberos config parse errors

Summary:
Modified Krb5Context ctor so that it doesn't fatal if context cannot be
initialized due to a bad config. Instead throw and exception.

Modified Krb5credentialsCacheManager ctor to catch these exceptions and ignore.
The ccache manager thread tries to initialize the context / store if they are
not already initialized. The exceptions are caught and propagated from the
thread in the same way.

Test Plan: unit tests

Tested manually by building a client locally which talks to a test service.

Reviewed By: andrei.bajenov@fb.com

Subscribers: trunkagent, alandau, bmatheny, mshneer, shamdor

FB internal diff: D1630742

Tasks: 4212569
Revert "[thrift] fix issues with handling saslErrors"

Summary: This reverts commit cc741c264097ecea428f719f53ded5fbb4e076eb.

Test Plan: @build-break

Subscribers: alandau, bmatheny, mshneer

FB internal diff: D1634213

Tasks: 5388156
Reduce the default number of threads for test servers.

Summary:
[Thrift] Reduce the default number of threads for test servers.

Reduce to 1 worker thread. The reason is that, in test cases, we usually just want a convenient way to run a server in the background and send it one or two requests. We mostly don't need to do stress-testing in our unit/module-level tests. In my trials, this trims the taken time per test that but instantiates a `ScopedServerInterfaceThread` from 45-50ms to 25-30ms.

Provide an alternative constructor for people who want to customize their own `ThriftServer`. It's probably not terribly needed since basically ends up being a wrapper around `ScopedServerThread`, but, for consistency.

Test Plan: Unit tests in `thrift/lib/cpp2/test/util/ScopedServerInterfaceThreadTest.cpp` and continuous build.

Reviewed By: njormrod@fb.com

Subscribers: alandau, bmatheny, mshneer, dougw

FB internal diff: D1631258
Don't fire unexpected callbacks from SASL threads

Summary:
So I think here is the problem:
- Request #1 fires, SASL times out, we fall back the channel to insecure mode and send a request.
- The request comes back, we detach the event base from the channel and put it in the idle pool.
- At this point the SASL thread could still be chugging along trying to talk to the KDC, etc.
- Request #2 fires, takes the channel off the idle pool, attaches another evb to it. This request is now in flight
- The SASL threads finally completes, and since we now have a valid evb on the channel it fires a saslSendServer (which schedules another timeout on the channel)
- Response #2 comes back, we try to detach from the channel but can't since there is a request in progress and a timeout set.

Test Plan: unit tests

Reviewed By: mshneer@fb.com

Subscribers: alandau, bmatheny, mshneer, wstefancik

FB internal diff: D1635252
Hack assertIsValid => assert

Summary: title

Test Plan: Generate thrift hack code

Reviewed By: mcduff@fb.com

Subscribers: trunkagent, alandau, mshneer

FB internal diff: D1633172
Add UNSAFE comment for mapconstruct hack

Summary:
This will supress errors in struct constructors until we do proper cast
checking for non-basic typed fields (containers and structs).

Test Plan: Thrift still compiles

Reviewed By: jwatzman@fb.com

Subscribers: alandau, mshneer

FB internal diff: D1636028
Fixing crashes

Summary:
The fix in header client channel prevents the situation when both saslError and saslComplete
are called in same EventBase iteration.

Test Plan: unit tests

Reviewed By: andrei.bajenov@fb.com

Subscribers: trunkagent, mcduff, marccelani, hitesh, alandau, bmatheny, mshneer

FB internal diff: D1634188

Tasks: 5388156
Fix the thrift server behavior when security kill switch is removed / turned off

Summary:
Updated the logic which processes the kill-switch while creating a
connection. Make sure that when kill switch is enabled, a thrift server running
on the host:
- Doesn't upgrade to permitted from disabled.
- Downgrades to permitted from required.
- Doesn't change if it was already permitted.

Make sure that the behavior of the server reverts to the same as specified by
the sasl_policy once the kill switch expires or is turned off.

Improve server logs by logging the exception if SASL fallback to insecure
fails.

Test Plan: unit tests

Reviewed By: mhorowitz@fb.com

Subscribers: trunkagent, alandau, bmatheny, mshneer, shamdor

FB internal diff: D1635557

Tasks: 5448400
remove ThreadGate from thrift

Summary: `GenericThreadGate` was being (ab)used in a thrift test, and we had some code generation for a `future_gate` variant, but it doesn't seem to be used anywhere (and given the lack of an include of appropriate header files, or tests, probably wouldn't work anyway).

Test Plan:
I built and ran the edited test.

Reviewed By: davejwatson@fb.com

Subscribers: trunkagent, net-systems@, ruibalp, fugalh, alandau, bmatheny, exa, mshneer

FB internal diff: D1637557

Tasks: 5409538
Add UNSAFE comment for rest services in hack

Summary:
This will supress errors in struct constructors until we do proper cast
checking for non-basic typed fields (containers and structs).

Test Plan: Thrift still compiles

Reviewed By: alandau@fb.com

Subscribers: alandau, mshneer

FB internal diff: D1641151
AsyncSocket

Summary:
Move async socket to folly.

Changes:
* Made an AsyncSocketException type instead of TTransportException: Some of the exceptions didn't fit nicely in to std::exception types (like TIMED_OUT).  There are some wrappers in thrift/lib/cpp/async to convert back to TTransportException, so all existing code still compiles.
* Moved read/write callbacks out of AsyncTransport: filters are going to want to do the read/write stuff separately (see revproxy/tunnel/filters, and discussions in D1483148).

Test Plan: build thrift and folly

Reviewed By: dcsommer@fb.com

Subscribers: mshneer, folly-diffs@, trunkagent, doug, alandau, bmatheny, njormrod, fugalh, jsedgwick

FB internal diff: D1587625