Remove dep on gflags for liger

Summary:
Remove the dep on gflags for liger.

This is only on the release branch,
but I'm open to master as well if people
are fine with this approach..

Test Plan: build, sync to fbandroid, builds.

Reviewed By: pgriess@fb.com

FB internal diff: D1185261
Augment Thrift INTERNAL_ERRORs with local and remote address and port

Summary: for easier debugging when such an error is thrown
@override-unit-failures

Test Plan: in prod

Reviewed By: andrewcox@fb.com

FB internal diff: D1197384
Move TAsyncSocketFactory and Mock* to Thrift

Summary:
- Create TAsyncSocketFactory and mocks

Facebook:
- Proxygen had a useful TAsyncSocketFactory (including mocks); move this
to Thrift so that others can benefit from it
- Change TAsyncSocketFactory to return a TAsyncSocket::UniquePtr since
that's the idiom that we use elsewhere to indicate transfer of
ownership

Test Plan: - Unit tests

Reviewed By: rajat@fb.com

FB internal diff: D1197579
remove ambiguity in FrozenRange operators

Summary: `operator{<,==}(const FrozenRange&, const FrozenRange&)` is ambiguous

Test Plan: fbconfig --clang unicorn/test && fbmake opt -j32

Reviewed By: tjackson@fb.com

FB internal diff: D1198817
Make onewayMethods and binary/compactProcessMap static

Summary:
These objects are being created on every single rpc call and for high qps services with lots of methods, that is expensive. This shows up in multifeed leaf perf reports.

A processor now looks like this:

class TestServiceAsyncProcessor : public ::apache::thrift::GeneratedAsyncProcessor {
public:
virtual std::string getServiceName();
protected:
TestServiceSvIf* iface_;
virtual void process(std::unique_ptr<apache::thrift::ResponseChannel::Request> req, std::unique_ptr<folly::IOBuf> buf, apache::thrift::protocol::PROTOCOL_TYPES protType,apache::thrift::Cpp2RequestContext* context,apache::thrift::async::TEventBase* eb, apache::thrift::concurrency::ThreadManager* tm);
virtual bool isOnewayMethod(const folly::IOBuf* buf, const apache::thrift::transport::THeader* header);
private:
static std::unordered_set<std::string> onewayMethods;
typedef void (TestServiceAsyncProcessor::*binaryProcessFunction)(std::unique_ptr<apache::thrift::ResponseChannel::Request> req, std::unique_ptr<folly::IOBuf> buf, std::unique_ptr<apache::thrift::BinaryProtocolReader> iprot, apache::thrift::Cpp2RequestContext* context, apache::thrift::async::TEventBase* eb, apache::thrift::concurrency::ThreadManager* tm);
typedef std::unordered_map<std::string, binaryProcessFunction> binaryProcessMap;
static TestServiceAsyncProcessor::binaryProcessMap binaryProcessMap_;
typedef void (TestServiceAsyncProcessor::*compactProcessFunction)(std::unique_ptr<apache::thrift::ResponseChannel::Request> req, std::unique_ptr<folly::IOBuf> buf, std::unique_ptr<apache::thrift::CompactProtocolReader> iprot, apache::thrift::Cpp2RequestContext* context, apache::thrift::async::TEventBase* eb, apache::thrift::concurrency::ThreadManager* tm);
typedef std::unordered_map<std::string, compactProcessFunction> compactProcessMap;
static TestServiceAsyncProcessor::compactProcessMap compactProcessMap_;

As you can see, the maps and onewayMethod set is now static.

In the cpp, it looks like this:

std::unordered_set<std::string> TestServiceAsyncProcessor::onewayMethods {
"noResponse"
};
TestServiceAsyncProcessor::binaryProcessMap TestServiceAsyncProcessor::binaryProcessMap_ {
{"sendResponse", &TestServiceAsyncProcessor::_processInThread_sendResponse<apache::thrift::BinaryProtocolReader, apache::thrift::BinaryProtocolWriter>},
{"noResponse", &TestServiceAsyncProcessor::_processInThread_noResponse<apache::thrift::BinaryProtocolReader, apache::thrift::BinaryProtocolWriter>},
{"echoRequest", &TestServiceAsyncProcessor::_processInThread_echoRequest<apache::thrift::BinaryProtocolReader, apache::thrift::BinaryProtocolWriter>},
{"serializationTest", &TestServiceAsyncProcessor::_processInThread_serializationTest<apache::thrift::BinaryProtocolReader, apache::thrift::BinaryProtocolWriter>},
{"eventBaseAsync", &TestServiceAsyncProcessor::process_eventBaseAsync<apache::thrift::BinaryProtocolReader, apache::thrift::BinaryProtocolWriter>}
};
TestServiceAsyncProcessor::compactProcessMap TestServiceAsyncProcessor::compactProcessMap_ {
{"sendResponse", &TestServiceAsyncProcessor::_processInThread_sendResponse<apache::thrift::CompactProtocolReader, apache::thrift::CompactProtocolWriter>},
{"noResponse", &TestServiceAsyncProcessor::_processInThread_noResponse<apache::thrift::CompactProtocolReader, apache::thrift::CompactProtocolWriter>},
{"echoRequest", &TestServiceAsyncProcessor::_processInThread_echoRequest<apache::thrift::CompactProtocolReader, apache::thrift::CompactProtocolWriter>},
{"serializationTest", &TestServiceAsyncProcessor::_processInThread_serializationTest<apache::thrift::CompactProtocolReader, apache::thrift::CompactProtocolWriter>},
{"eventBaseAsync", &TestServiceAsyncProcessor::process_eventBaseAsync<apache::thrift::CompactProtocolReader, apache::thrift::CompactProtocolWriter>}
};

Test Plan: reran unit tests and canary. Canary results are insignificant, but I still think this is worth fixing.

Reviewed By: davejwatson@fb.com

FB internal diff: D1197939
Fix use-after-free in D1156566 and D1151984

Summary:
timer_ was destroyed while still potentially being used by
saslClientCallback_.

Test Plan:
./ThriftServer -sasl_policy=required -num_threads=32
./loadgen -num_threads=1 -weight_noop=1 -cpp2 -ops_per_conn=1 -minloglevel=10 -sasl_policy_test=required

kill the client and make sure the server stays alive

Reviewed By: andrewcox@fb.com

FB internal diff: D1199609
Refactor ThreadManager to use std::chrono, prepare for observable ThreadManager interface

Summary: I plan to include an observer pattern on the ThreadManager similar to TServerObserver. For each item that is queued, I want to know the point in time that it is queued, the point in time it starts work, and the point in time it finishes work. I plan to include notifying the observer in reportStats(), but reportStats() takes in durations instead of points in time. While refactoring this, I figured that switching to more explicit units of time (std::chrono) is a good idea.

Test Plan: reran unit tests

Reviewed By: davejwatson@fb.com

FB internal diff: D1194700
Cancel timeout on HeaderClientChannel destruction

Summary:
This cancels a timeout if a channel is being destroyed. This fixes a set of errors that happen in the following scenario:
- timeout is scheduled
- channel is destroyed
- abort is thrown because we try to destroy an HHWheelTimer while it still has a timeout

Note that this doesn't fix the server-side crashes I was seeing.

Test Plan: loadgen

Reviewed By: mhorowitz@fb.com

FB internal diff: D1199543
Cancel timeout on HeaderServerChannel destruction

Summary: Similar to D1199543, but cancels the timeout server-side.

Test Plan: ThriftServer / loadgen

Reviewed By: mhorowitz@fb.com

FB internal diff: D1199575
fix close_wait on deadlock

Summary:
If the ThreadManager workers deadlock or take a long time, and the clients sends a close, the socket will be stuck in CLOSE_WAIT for a long time / forever.  This is because the socket/channel isn't closed until the shared_ptr in the Cpp2Request object is freed.

Instead, just reset() them manually - they won't be used again anyway since all the tasks are marked as cancelled.

The only minor change this may have is if a task is just taking a long time, we may miss sending a timeout response - instead the client would get a channel closed error, or the generic 'could not read 4 bytes from...'

Test Plan:
fbconfig -r thrift/perf/cpp
Thrift2Server
loadgen -cpp2 -async -weight_burn=1 -burn_avg=9999999999
(Control-C, wait a second)
lsof|grep -i close|wc -l - shows 0 close_wait after this fix, shows thousands in close wait before.

Reviewed By: alandau@fb.com

FB internal diff: D1200110
Fix unittest

Summary: Broken after D1197384

Test Plan: fbmake and run the test

Reviewed By: davejwatson@fb.com

FB internal diff: D1199909
Revert "[ThriftAuth] Revert build breaks"

Summary:
This reverts commit 8ce23054c85c83f2f18ca22d112cfcc41c7dc231.

This also fixes the build issues caused by that commit. I provide a stub dummy function for krb5_is_config_principal and gss_krb5_import_cred, so that we can compile on older platforms. Also I'm changing std::unordered_map<std::string, ServiceTimeSeries> serviceCountMap_ to be std::unordered_map<std::string, std::unique_ptr<ServiceTimeSeries>> serviceCountMap_, since some platforms removed a copy constructor on 'atomic' causing ServiceTimeSeries to be non-copyable / movable.

Test Plan: Will see if contbuild complains

Reviewed By: alandau@fb.com

FB internal diff: D1197500
revert D1200110

Summary: clean revert. Details in t3855556

Test Plan: none

Reviewed By: qiao@fb.com

FB internal diff: D1203200

@override-unit-failures
misc code cleanup

Summary:
1. Remove unused imports, unnecessary `@SuppressWarnings`, unused local fields.

2. Add generic annotations and replace Iterators with for-each loops

3. Add `@SuppressWarnings("serial")` or a serialVersionUID where relevant.

4. Two specific instances of dead code eclipse pointed out.

Test Plan: it compiles

Reviewed By: alandau@fb.com

FB internal diff: D1183945
Add TAsyncSSLSocketFactory

Summary: - Add TAsyncSSLSocketFactory, which subclasses TAsyncSSLSocketFactory

Test Plan: - N/A

Reviewed By: rajat@fb.com

FB internal diff: D1204805
Update CppClient.cpp

Summary:
Fixed initialization of the objects of type std::shared_ptr. Raw new results in a suboptimal allocation pattern (and is potentially exception-unsafe) -- std::make_shared is the recommended practice when using std::shared_ptr, see:

http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared
http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/STL11-Magic-Secrets
http://blog.emptycrate.com/node/474
http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-3-of-n

Test Plan: .

Reviewed By: alandau@fb.com

FB internal diff: D1183873
Log whether security is active to scuba

Summary:
For each call, I want to know whether it was authenticated and
encrypted. Log this info to scuba.

Test Plan: fbmake, run client and check scuba

Reviewed By: hitesh@fb.com

FB internal diff: D1204245
Fix C linkage for com_err.h for older platforms

Summary:
Missing C linkage in krb com_err headers makes newly built mta
packages fatal due to missing symbols on startup:

```
[Thu 06 Mar 2014 13:02:51] <(nil)>
dlopen("/opt/msys/ecelerity/libexec/fb/smtp_thrift.so") failed: /opt/msys/ecelerity/libexec/fb/lib/libthrift_lib_cpp_util_kerberos_krb5_util.so: undefined symbol: _Z13error_messagel
```

Test Plan: @mariocole built it and the mta started up.  I could build a modern platform and things work.

Reviewed By: macole@fb.com

FB internal diff: D1207421
Offload kerberos context cleanup to an async thread

Summary:
Previously, if the krb5 cleanup methods blocked, they would block the evb, and thus caused the request to take longer than it should. This diff offloads this cleanup to the thread manager. If the thread manager can't accept any new jobs, we cleanup inline to avoid leaking memory.

I'll add logging around all this stuff in a separate diff.

Test Plan: ThriftServer / loadgen

This is an update to a diff where all the tests ran, so no need to rerun.
@override-unit-failures

Reviewed By: alandau@fb.com

FB internal diff: D1207456
fix close_wait

Summary: original D1200110, revert, D1203200.  Change to call closeNow() instead of reset() on shared_ptr - THeader is still used in some codepaths after client closes the connection (setHeader() is called, it probably shouldn't be though).

Test Plan:
Same test as D1200110 to check for close_wait, plus
multifeed canary:

Canary URL: https://our.intern.facebook.com/intern/feedtools/mycanary?id=davejwatson_20140305-083828_local.1394037507_75cc828d0bb297e9dc10a74fac305db81bd3f1d2_

(12 dead hosts? )

adfinder canary:

https://our.intern.facebook.com/intern/services/fbsentry/counter_diff.php?service=adfinder&host_a=ads9075-9082.frc1&host_b=ads9083-9090.frc1&extra_counters=&dur=3600&snapshot=on

Reviewed By: varunk@fb.com

FB internal diff: D1203869

Blame Revision: D1200110