-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Use InetAddressResolverProvider
and add tests
#1353
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,9 @@ | |
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
Args = --initialize-at-run-time=com.mongodb.UnixServerAddress,com.mongodb.internal.connection.SnappyCompressor,org.bson.types.ObjectId,com.mongodb.internal.connection.ClientMetadataHelper | ||
Args =\ | ||
--initialize-at-run-time=\ | ||
jyemin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
com.mongodb.UnixServerAddress,\ | ||
com.mongodb.internal.connection.SnappyCompressor,\ | ||
com.mongodb.internal.connection.ClientMetadataHelper,\ | ||
com.mongodb.internal.connection.ServerAddressHelper | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I checked by looking at the reports generated by |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"resources":{ | ||
"includes":[{ | ||
"pattern":"\\QMETA-INF/services/com.mongodb.spi.dns.InetAddressResolverProvider\\E" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Weirdly, everything works even without this entry, but, if I collect reachability metadata, then this entry is added there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's strange. Did you find that for any other reachability metatada? Can you suggest a hypothesis for what might be going on? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I haven't checked everything, nor do I understand how some of the automatically collected resources are used, but removing the shared library resources, or the
I suspect, it's all about undocumented behavior that is intended to make things easier. I found the following:
Of the two experimental options mentioned in the PR comment linked above, only
P.S. Before I found the above, I did and wrote the following:
|
||
}]}, | ||
"bundles":[] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* 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 com.mongodb.connection; | ||
|
||
import com.mongodb.MongoClientSettings; | ||
import com.mongodb.internal.connection.DefaultInetAddressResolver; | ||
import com.mongodb.internal.connection.ServerAddressHelper; | ||
import com.mongodb.spi.dns.InetAddressResolver; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertAll; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
|
||
final class ServerAddressHelperTest { | ||
@Test | ||
void getInetAddressResolver() { | ||
assertAll( | ||
() -> assertEquals( | ||
DefaultInetAddressResolver.class, | ||
ServerAddressHelper.getInetAddressResolver(MongoClientSettings.builder().build()).getClass()), | ||
() -> { | ||
InetAddressResolver resolver = new DefaultInetAddressResolver(); | ||
assertSame( | ||
resolver, | ||
ServerAddressHelper.getInetAddressResolver(MongoClientSettings.builder().inetAddressResolver(resolver).build())); | ||
} | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -51,9 +51,8 @@ graalvmNative { | |
configureEach { | ||
buildArgs.add('--strict-image-heap') | ||
buildArgs.add('-H:+UnlockExperimentalVMOptions') | ||
// see class initialization report is generated at `graalvm/build/native/nativeCompile/reports`, | ||
// informing us on the kind of initialization for each Java class | ||
buildArgs.add('-H:+PrintClassInitialization') | ||
// see class initialization and other reports in `graalvm/build/native/nativeCompile/reports` | ||
buildArgs.add('--diagnostics-mode') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// see the "registerResource" entries in the `native-image` built-time output, | ||
// informing us on the resources included in the native image being built | ||
buildArgs.add('-H:Log=registerResource:5') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* 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 com.mongodb.internal.graalvm; | ||
|
||
import com.mongodb.internal.connection.DefaultInetAddressResolver; | ||
import com.mongodb.spi.dns.InetAddressResolver; | ||
import com.mongodb.spi.dns.InetAddressResolverProvider; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.util.List; | ||
|
||
import static java.lang.String.format; | ||
|
||
public final class CustomInetAddressResolverProvider implements InetAddressResolverProvider { | ||
private static volatile boolean used = false; | ||
|
||
public CustomInetAddressResolverProvider() { | ||
} | ||
|
||
@Override | ||
public InetAddressResolver create() { | ||
return new CustomInetAddressResolver(); | ||
} | ||
|
||
static void assertUsed() throws AssertionError { | ||
if (!used) { | ||
throw new AssertionError(format("%s is not used", CustomInetAddressResolverProvider.class.getSimpleName())); | ||
} | ||
} | ||
|
||
private static void markUsed() { | ||
used = true; | ||
} | ||
|
||
private static final class CustomInetAddressResolver implements InetAddressResolver { | ||
private final DefaultInetAddressResolver wrapped; | ||
|
||
CustomInetAddressResolver() { | ||
wrapped = new DefaultInetAddressResolver(); | ||
} | ||
|
||
@Override | ||
public List<InetAddress> lookupByName(final String host) throws UnknownHostException { | ||
markUsed(); | ||
return wrapped.lookupByName(host); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* 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 com.mongodb.internal.graalvm; | ||
|
||
import com.mongodb.client.MongoClient; | ||
import com.mongodb.client.MongoClients; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.ArrayList; | ||
|
||
final class DnsSpi { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we also have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My plan is to use a custom |
||
private static final Logger LOGGER = LoggerFactory.getLogger(DnsSpi.class); | ||
|
||
public static void main(final String... args) { | ||
LOGGER.info("Begin"); | ||
try (MongoClient client = args.length == 0 ? MongoClients.create() : MongoClients.create(args[0])) { | ||
LOGGER.info("Database names: {}", client.listDatabaseNames().into(new ArrayList<>())); | ||
} | ||
CustomInetAddressResolverProvider.assertUsed(); | ||
LOGGER.info("End"); | ||
} | ||
|
||
private DnsSpi() { | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2008-present MongoDB, Inc. | ||
* | ||
* 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. | ||
*/ | ||
@NonNullApi | ||
package com.mongodb.internal.graalvm; | ||
|
||
import com.mongodb.lang.NonNullApi; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.mongodb.internal.graalvm.CustomInetAddressResolverProvider |
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.
This was incorrect, as "Otherwise, ..." did not take into account
InetAddressResolverProvider
.Builder#inetAddressResolver
explains the behavior in full, so I added@see Builder#inetAddressResolver(InetAddressResolver)
.