-
Notifications
You must be signed in to change notification settings - Fork 927
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
Set useOpenSsl in Flags lazily to reduce server startup time #2184
Conversation
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.
Way to go @mumgmangmange!
Let's initiate USE_EPOLL
lazily as we do for USE_OPENSSL
.
Also, if you let us know how much the server startup time decreases, that'll be much appreciated.
|
||
private static final boolean USE_OPENSSL = getBoolean("useOpenSsl", OpenSsl.isAvailable(), | ||
value -> OpenSsl.isAvailable() || !value); | ||
private static Boolean USE_OPENSSL = null; |
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.
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.
Because this is not a static final constant anymore, we should use camel case.
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.
Could you leave a comment about why we initialize this field lazily?
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.
Changed to Camel. Thank you.
Do not call OpenSsl.isAvailable() when not using SSL.
0.2 to 0.3 seconds shortened.
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.
// Netty epoll transport does not work with WSL (Windows Sybsystem for Linux) yet. | ||
// TODO(trustin): Re-enable on WSL if https://github.com/Microsoft/WSL/issues/1982 is resolved. |
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.
Please move this comment inside the if
block below for readability.
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.
I changed. Thank you. 😄
There are 3 Checkstyle violations:
Just in case it's unclear, the first violation means you do not need to assign |
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
/** | ||
* Return whether Linux is used. | ||
*/ | ||
public static boolean isLinux() { | ||
return Ascii.toLowerCase(System.getProperty("os.name", "")).startsWith("linux"); |
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.
Now that this is a public method, we should go ahead and optimize the method. Can you add private static final boolean IS_LINUX = Ascii.toLowerCase(System.getProperty("os.name", "")).startsWith("linux");
at the beginning and return that here?
Also, anyone have thoughts on making this public means we should instead return an enum of LINUX
, WINDOWS
, MAC_OS_X
, OTHER
?
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.
Yes, how about replacing isLinux()
with the following method?
private static final OsType OS_TYPE;
static {
// ... Detect OS type here ...
OS_TYPE = ...;
}
public static OsType osType() {
return OS_TYPE;
}
OsType
could look like:
public enum OsType {
LINUX,
WINDOWS,
// MAC_OS_X, FREEBSD, ... (Do we need them?)
OTHERS
}
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 😄 I understand.
I modified with reference to comment.
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.
It getting a little bit more effort than expected but I guess it will be useful to have SystemInfo.osType()
. PTAL, @mumgmangmange.
/** | ||
* Return whether Linux is used. | ||
*/ | ||
public static boolean isLinux() { | ||
return Ascii.toLowerCase(System.getProperty("os.name", "")).startsWith("linux"); |
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.
Yes, how about replacing isLinux()
with the following method?
private static final OsType OS_TYPE;
static {
// ... Detect OS type here ...
OS_TYPE = ...;
}
public static OsType osType() {
return OS_TYPE;
}
OsType
could look like:
public enum OsType {
LINUX,
WINDOWS,
// MAC_OS_X, FREEBSD, ... (Do we need them?)
OTHERS
}
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.
Oops, meant to request changes. Sorry. 😅
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.
maybe too much for this PR, but I would love to see a "Platform" like type that does the detection of things like this in a lazy way.
If single dimension doesn't cover parts we need..
Platform.get().isOpenSslAvailable()
We could instead do it like this..
Platform.get().os().isOpenSslAvailable()
where the .os()
looks deeper at the OS type and also caches it.
Don't let that slow this work, though (pun intended).
fyi this inspired me to notice and fix some things in brave. thanks! openzipkin/brave#1006 |
if (useOpenSsl != null) { | ||
return useOpenSsl; | ||
} | ||
useOpenSsl = getBoolean("useOpenSsl", OpenSsl.isAvailable(), value -> OpenSsl.isAvailable() || !value); |
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.
I think we can avoid isAvailable
if the user explicitly disabled OpenSSL.
final boolean useOpenSsl = getBoolean("useOpenSsl", true);
if (!useOpenSsl) {
// OpenSSL explicitly disabled
return Flags.useOpenSsl = false;
}
if (!OpenSsl.isAvailable(){
...
} else {
...
}
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.
Is this useOpenSsl variable unconditionally true?
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.
In this case, true
is the default value unless a user sets com.linecorp.armeria.useOpenSsl=false
.
At least I think it is let me know if I missed something :)
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.
Yes. I understand 😃
@@ -109,6 +111,12 @@ | |||
JETTY_ALPN_OPTIONAL_OR_AVAILABLE = false; | |||
} | |||
} | |||
|
|||
try { | |||
osType = OsType.valueOf(Ascii.toUpperCase(System.getProperty("os.name", "").split(" ")[0])); |
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.
OS detection is a bit more complicated unfortunately.
Can you use the if statements for the OsType
OS's from this famous OS detection logic?
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. 😃 Changed to an if statement.
WINDOWS, | ||
LINUX, | ||
MAC, | ||
FREEBSD, |
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.
I would lean towards leaving out FREEBSD
and SOLARIS
- it's unlikely Armeria servers would have logic specific to them anyways so they can go in OTHERS
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. 😄
I thought FREEBSD and SOLARIS as OTHERS and changed it.
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.
Just left some nits. 😄
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
@@ -422,7 +403,30 @@ public static boolean useEpoll() { | |||
* {@code -Dcom.linecorp.armeria.useOpenSsl=false} JVM option to disable it. | |||
*/ | |||
public static boolean useOpenSsl() { | |||
return USE_OPENSSL; |
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.
I think we still need the null check that returns the value after we set it once like we had before (can use a different name for the local variable if it makes it confusing).
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.
Thanks!
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.
Thanks a lot @mumgmangmange! 👍
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.
Could you move OsType to top level? LGTM except that.
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.
@mumgmangmange Thanks for your long efforts! LGTM please address @trustin review.
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
core/src/main/java/com/linecorp/armeria/common/util/SystemInfo.java
Outdated
Show resolved
Hide resolved
…ing system is Linux. (#2184)
@anuraaga, any other comments? |
Codecov Report
@@ Coverage Diff @@
## master #2184 +/- ##
===========================================
+ Coverage 73.64% 73.7% +0.05%
- Complexity 9620 9635 +15
===========================================
Files 839 840 +1
Lines 37022 37041 +19
Branches 4561 4566 +5
===========================================
+ Hits 27266 27302 +36
+ Misses 7432 7408 -24
- Partials 2324 2331 +7
Continue to review full report at Codecov.
|
@mumgmangmange Could you sign the CLA so that we can merge this PR? |
congratulations on your first PR, @mumgmangmange! |
Motivation: Related line#1645 `useOpenSsl` in `Flags` is used only when TLS is enabled. However, it takes a lot to instantiate `OpenSsl` class so it adds up a tremendous time to the server startup time. We should fix to instantiate it only when it needs. Modification: - Set `useOpenSsl` in `Flags` lazily - Check whether the OS is Linux before calling `Epoll.isAvailable()` - Add `OsType` Result: - Reduced server startup time to 80 percent (20% decreased)
Motivation: Related line#1645 `useOpenSsl` in `Flags` is used only when TLS is enabled. However, it takes a lot to instantiate `OpenSsl` class so it adds up a tremendous time to the server startup time. We should fix to instantiate it only when it needs. Modification: - Set `useOpenSsl` in `Flags` lazily - Check whether the OS is Linux before calling `Epoll.isAvailable()` - Add `OsType` Result: - Reduced server startup time to 80 percent (20% decreased)
Motivation:
Related #1645
useOpenSsl
inFlags
is used only when TLS is enabled. However, it takes a lot to instantiateOpenSsl
class so it adds up a tremendous time to the server startup time. We should fix to instantiate it only when it needs.Modification:
useOpenSsl
inFlags
lazilyEpoll.isAvailable()
OsType
Result: