imply-cheddar commented on code in PR #13085:
URL: https://github.com/apache/druid/pull/13085#discussion_r1002843503
##########
core/src/main/java/org/apache/druid/guice/GuiceInjectableValues.java:
##########
@@ -50,7 +59,21 @@ public Object findInjectableValue(
// Currently we should only be dealing with `Key` instances, and anything
more advanced should be handled with
// great care
if (valueId instanceof Key) {
- return injector.getInstance((Key) valueId);
+ try {
+ return injector.getInstance((Key) valueId);
+ }
+ catch (ConfigurationException ce) {
+ // check if nullable annotation is present for this
+ if (nullables.get().contains((Key) valueId)) {
Review Comment:
The point of caching these is so that we only have Guice generate the
exception once. Putting this check inside of the catch means that we don't get
any benefit. This check needs to be done before we even ask the injector to
create the instance.
##########
core/src/main/java/org/apache/druid/guice/GuiceInjectableValues.java:
##########
@@ -50,7 +59,21 @@ public Object findInjectableValue(
// Currently we should only be dealing with `Key` instances, and anything
more advanced should be handled with
// great care
if (valueId instanceof Key) {
- return injector.getInstance((Key) valueId);
+ try {
+ return injector.getInstance((Key) valueId);
+ }
+ catch (ConfigurationException ce) {
+ // check if nullable annotation is present for this
+ if (nullables.get().contains((Key) valueId)) {
+ return null;
+ } else if (forProperty.getAnnotation(Nullable.class) != null) {
+ HashSet<Key> encounteredNullables = nullables.get();
+ encounteredNullables.add((Key) valueId);
Review Comment:
This is a non-thread-safe mutation of the HashSet. The goal of the
AtomicReference was to make thinds thread-safe. You must create a brand new
Set, add all of the old values, add the new value and then set the new
reference on the AtomicReference.
##########
core/src/main/java/org/apache/druid/guice/GuiceInjectableValues.java:
##########
@@ -22,19 +22,28 @@
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.InjectableValues;
+import com.google.inject.ConfigurationException;
import com.google.inject.Injector;
import com.google.inject.Key;
import org.apache.druid.java.util.common.IAE;
+import javax.annotation.Nullable;
+import java.util.HashSet;
+import java.util.concurrent.atomic.AtomicReference;
+
/**
+ *
*/
public class GuiceInjectableValues extends InjectableValues
{
private final Injector injector;
+ private final AtomicReference<HashSet<Key>> nullables;
public GuiceInjectableValues(Injector injector)
{
this.injector = injector;
+ this.nullables = new AtomicReference<>();
+ this.nullables.set(new HashSet<>());
Review Comment:
There's an `AtomicReference` constructor that takes an initial value. It's
a nit, but you can remove this line by using it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]