-
Notifications
You must be signed in to change notification settings - Fork 3.9k
core: combined epoch seconds and nanos changes in TimeProvider #11604
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
Merged
kannanjgithub
merged 29 commits into
grpc:master
from
vinodhabib:defect-5494-timeprovider-changes
Oct 29, 2024
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
fca2584
grpc-core: combined epoch seconds and nanos changes in TimeProvider
vinodhabib bd0a6c6
Merge remote-tracking branch 'origin/defect-5494-timeprovider-changes…
vinodhabib 12b9b1a
grpc-core: combined epoch seconds and nanos changes in TimeProvider
vinodhabib 3d538d8
grpc-core: Fixed checkstyle issue
vinodhabib 9d04a74
grpc-core: added the comment
vinodhabib 9c564dc
grpc-core: removed the comment
vinodhabib a64afcc
grpc-core: Fixed Review points along with junits
vinodhabib 34d7c1d
grpc-core: Fixed Review points
vinodhabib 32cd455
grpc-core: Fixed Review points
vinodhabib 71ce040
grpc-core: Fixed Review points
vinodhabib 58073d4
grpc-core: Reverted back to previous changes
vinodhabib 808b587
core: Fixed the UT as per Review point
vinodhabib 7eee75d
core: changed the implementation as per the review points
vinodhabib 6d59fa1
core: Fixed checkstyle issue
vinodhabib a43e9fc
core: Fixed checkstyle issue
vinodhabib 04f1c3d
core: Added junits for the changes
vinodhabib 7d09147
core: Added annotation for junit class
vinodhabib 19a44f5
okHttp: Code cleanup
vinodhabib 1757f99
core: Fixed the review points
vinodhabib b6a91b9
core: Fixed checkstyle issue
vinodhabib 5178e78
core: Added junit for exception flow
vinodhabib 133988b
core: Fixed checkstyle issues
vinodhabib ee69565
core: Fixed review points
vinodhabib 00fae97
core: Fixed review points
vinodhabib f4abffc
core: Fixed review points
vinodhabib 09fb474
core: Fixed checkstyle issues
vinodhabib b24dd90
core: Fixed recent review points
vinodhabib 72ecc01
Merge branch 'master' into defect-5494-timeprovider-changes
vinodhabib d637a36
core: Fixed recent review points
vinodhabib 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
32 changes: 32 additions & 0 deletions
32
core/src/main/java/io/grpc/internal/ConcurrentTimeProvider.java
This file contains hidden or 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,32 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.internal; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* {@link ConcurrentTimeProvider} resolves ConcurrentTimeProvider which implements | ||
* {@link TimeProvider}. | ||
*/ | ||
|
||
final class ConcurrentTimeProvider implements TimeProvider { | ||
|
||
@Override | ||
public long currentTimeNanos() { | ||
return TimeUnit.MILLISECONDS.toNanos(System.currentTimeMillis()); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
core/src/main/java/io/grpc/internal/InstantTimeProvider.java
This file contains hidden or 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,54 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.internal; | ||
|
||
import static com.google.common.math.LongMath.saturatedAdd; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* {@link InstantTimeProvider} resolves InstantTimeProvider which implements {@link TimeProvider}. | ||
*/ | ||
final class InstantTimeProvider implements TimeProvider { | ||
private Method now; | ||
private Method getNano; | ||
private Method getEpochSecond; | ||
|
||
public InstantTimeProvider(Class<?> instantClass) { | ||
try { | ||
this.now = instantClass.getMethod("now"); | ||
this.getNano = instantClass.getMethod("getNano"); | ||
this.getEpochSecond = instantClass.getMethod("getEpochSecond"); | ||
} catch (NoSuchMethodException ex) { | ||
throw new AssertionError(ex); | ||
} | ||
} | ||
|
||
@Override | ||
public long currentTimeNanos() { | ||
try { | ||
Object instant = now.invoke(null); | ||
int nanos = (int) getNano.invoke(instant); | ||
long epochSeconds = (long) getEpochSecond.invoke(instant); | ||
return saturatedAdd(TimeUnit.SECONDS.toNanos(epochSeconds), nanos); | ||
} catch (IllegalAccessException | InvocationTargetException ex) { | ||
throw new RuntimeException(ex); | ||
} | ||
} | ||
} |
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
core/src/main/java/io/grpc/internal/TimeProviderResolverFactory.java
This file contains hidden or 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,32 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.internal; | ||
|
||
/** | ||
* {@link TimeProviderResolverFactory} resolves Time providers. | ||
*/ | ||
|
||
final class TimeProviderResolverFactory { | ||
static TimeProvider resolveTimeProvider() { | ||
try { | ||
Class<?> instantClass = Class.forName("java.time.Instant"); | ||
return new InstantTimeProvider(instantClass); | ||
} catch (ClassNotFoundException ex) { | ||
return new ConcurrentTimeProvider(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
core/src/test/java/io/grpc/internal/ConcurrentTimeProviderTest.java
This file contains hidden or 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,48 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.internal; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.time.Instant; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Unit tests for {@link ConcurrentTimeProvider}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class ConcurrentTimeProviderTest { | ||
@Test | ||
public void testConcurrentCurrentTimeNanos() { | ||
|
||
ConcurrentTimeProvider concurrentTimeProvider = new ConcurrentTimeProvider(); | ||
// Get the current time from the ConcurrentTimeProvider | ||
long actualTimeNanos = concurrentTimeProvider.currentTimeNanos(); | ||
|
||
// Get the current time from Instant for comparison | ||
Instant instantNow = Instant.now(); | ||
long expectedTimeNanos = TimeUnit.SECONDS.toNanos(instantNow.getEpochSecond()) | ||
+ instantNow.getNano(); | ||
|
||
// Validate the time returned is close to the expected value within a tolerance | ||
// (i,e 10 millisecond tolerance in nanoseconds). | ||
assertThat(actualTimeNanos).isWithin(10_000_000L).of(expectedTimeNanos); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
core/src/test/java/io/grpc/internal/InstantTimeProviderTest.java
This file contains hidden or 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,50 @@ | ||
/* | ||
* Copyright 2024 The gRPC Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.grpc.internal; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import java.time.Instant; | ||
import java.util.concurrent.TimeUnit; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
/** | ||
* Unit tests for {@link InstantTimeProvider}. | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class InstantTimeProviderTest { | ||
@Test | ||
public void testInstantCurrentTimeNanos() throws Exception { | ||
|
||
InstantTimeProvider instantTimeProvider = new InstantTimeProvider( | ||
Class.forName("java.time.Instant")); | ||
|
||
// Get the current time from the InstantTimeProvider | ||
long actualTimeNanos = instantTimeProvider.currentTimeNanos(); | ||
|
||
// Get the current time from Instant for comparison | ||
Instant instantNow = Instant.now(); | ||
long expectedTimeNanos = TimeUnit.SECONDS.toNanos(instantNow.getEpochSecond()) | ||
+ instantNow.getNano(); | ||
|
||
// Validate the time returned is close to the expected value within a tolerance | ||
// (i,e 10 millisecond tolerance in nanoseconds). | ||
assertThat(actualTimeNanos).isWithin(10_000_000L).of(expectedTimeNanos); | ||
kannanjgithub marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.