-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
grpc: Separate out grpc_init into its own class #8293
Merged
jmarantz
merged 10 commits into
envoyproxy:master
from
jmarantz:deflake-stats-mem-tests
Sep 23, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
080ea47
Separate out grpc init to a separate class.
jmarantz 80569b9
add new files
jmarantz a59d3f8
Remove extra grpc_init().
jmarantz 9625a48
Ensure we initiatlize GRPC in MainCommon and add format check to ensu…
jmarantz 8c8abc5
try using blocking grpc shutdown.
jmarantz af29f26
Merge branch 'master' into deflake-stats-mem-tests
jmarantz 00e2d77
Merge branch 'master' into deflake-stats-mem-tests
jmarantz b86d9e4
use a mutex rather than a std::atomic, and also CONSTRUCT_ON_FIRST_US…
jmarantz ffb477c
add another test file.
jmarantz 1a0fc54
Thread annotation and Google gRPC spelling.
jmarantz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include "common/grpc/google_grpc_context.h" | ||
|
||
#include <atomic> | ||
|
||
#include "common/common/assert.h" | ||
#include "common/common/lock_guard.h" | ||
#include "common/common/macros.h" | ||
#include "common/common/thread.h" | ||
|
||
#include "grpcpp/grpcpp.h" | ||
|
||
namespace Envoy { | ||
namespace Grpc { | ||
|
||
GoogleGrpcContext::GoogleGrpcContext() : instance_tracker_(instanceTracker()) { | ||
Thread::LockGuard lock(instance_tracker_.mutex_); | ||
if (++instance_tracker_.live_instances_ == 1) { | ||
#ifdef ENVOY_GOOGLE_GRPC | ||
grpc_init(); | ||
#endif | ||
} | ||
} | ||
|
||
GoogleGrpcContext::~GoogleGrpcContext() { | ||
// Per https://github.com/grpc/grpc/issues/20303 it is OK to call | ||
// grpc_shutdown_blocking() as long as no one can concurrently call | ||
// grpc_init(). We use check_format.py to ensure that this file contains the | ||
// only callers to grpc_init(), and the mutex to then make that guarantee | ||
// across users of this class. | ||
Thread::LockGuard lock(instance_tracker_.mutex_); | ||
ASSERT(instance_tracker_.live_instances_ > 0); | ||
if (--instance_tracker_.live_instances_ == 0) { | ||
#ifdef ENVOY_GOOGLE_GRPC | ||
grpc_shutdown_blocking(); // Waiting for quiescence avoids non-determinism in tests. | ||
#endif | ||
} | ||
} | ||
|
||
GoogleGrpcContext::InstanceTracker& GoogleGrpcContext::instanceTracker() { | ||
MUTABLE_CONSTRUCT_ON_FIRST_USE(InstanceTracker); | ||
} | ||
|
||
} // namespace Grpc | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
|
||
#include "common/common/thread.h" | ||
|
||
namespace Envoy { | ||
namespace Grpc { | ||
|
||
// Captures global grpc initialization and shutdown. Note that grpc | ||
// initialization starts several threads, so it is a little annoying to run them | ||
// alongside unrelated tests, particularly if they are trying to track memory | ||
// usage, or you are exploiting otherwise consistent run-to-run pointer values | ||
// during debug. | ||
// | ||
// Instantiating this class makes it easy to ensure classes that depend on grpc | ||
// libraries get them initialized. | ||
class GoogleGrpcContext { | ||
public: | ||
GoogleGrpcContext(); | ||
~GoogleGrpcContext(); | ||
|
||
private: | ||
struct InstanceTracker { | ||
Thread::MutexBasicLockable mutex_; | ||
uint64_t live_instances_ GUARDED_BY(mutex_) = 0; | ||
}; | ||
|
||
static InstanceTracker& instanceTracker(); | ||
|
||
InstanceTracker& instance_tracker_; | ||
}; | ||
|
||
} // namespace Grpc | ||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Envoy { | ||
|
||
void foo() { | ||
grpc_init(); | ||
} | ||
|
||
} // namespace Envoy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Envoy { | ||
|
||
void foo() { | ||
grpc_shutdown(); | ||
} | ||
|
||
} // namespace Envoy |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: is it possible for this to be stubbed when there is no Google-gRPC support compiled in? It's a little strange that we still have the context, but the actual calls inside it are compiled out?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK fair enough; can make that change. Do you want to consider merging this in first though and I will commit to cleaning it up in a follow-up, so we can de-flake CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure sounds good.