diff --git a/xds/src/main/java/io/grpc/xds/Bootstrapper.java b/xds/src/main/java/io/grpc/xds/Bootstrapper.java index 69c5e5858b..f1a86cc2a6 100644 --- a/xds/src/main/java/io/grpc/xds/Bootstrapper.java +++ b/xds/src/main/java/io/grpc/xds/Bootstrapper.java @@ -35,18 +35,29 @@ import java.util.List; import java.util.Map; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; -import javax.annotation.concurrent.ThreadSafe; /** * Loads configuration information to bootstrap gRPC's integration of xDS protocol. */ -@ThreadSafe abstract class Bootstrapper { private static final String BOOTSTRAP_PATH_SYS_ENV_VAR = "GRPC_XDS_BOOTSTRAP"; - static Bootstrapper newInsatnce() { - return new FileBasedBootstrapper(); + private static final Bootstrapper DEFAULT_INSTANCE = new Bootstrapper() { + @Override + BootstrapInfo readBootstrap() throws IOException { + String filePath = System.getenv(BOOTSTRAP_PATH_SYS_ENV_VAR); + if (filePath == null) { + throw + new IOException("Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR + " not defined."); + } + return parseConfig( + new String(Files.readAllBytes(Paths.get(filePath)), StandardCharsets.UTF_8)); + } + }; + + static Bootstrapper getInstance() { + return DEFAULT_INSTANCE; } /** @@ -54,39 +65,6 @@ abstract class Bootstrapper { */ abstract BootstrapInfo readBootstrap() throws Exception; - private static final class FileBasedBootstrapper extends Bootstrapper { - - private static volatile Exception failToBootstrapException; - private static volatile BootstrapInfo bootstrapInfo; - - @Override - BootstrapInfo readBootstrap() throws Exception { - if (bootstrapInfo == null && failToBootstrapException == null) { - synchronized (FileBasedBootstrapper.class) { - if (bootstrapInfo == null && failToBootstrapException == null) { - try { - String filePath = System.getenv(BOOTSTRAP_PATH_SYS_ENV_VAR); - if (filePath == null) { - throw - new IOException("Environment variable " + BOOTSTRAP_PATH_SYS_ENV_VAR - + " not found."); - } - bootstrapInfo = - parseConfig(new String(Files.readAllBytes(Paths.get(filePath)), - StandardCharsets.UTF_8)); - } catch (Exception e) { - failToBootstrapException = e; - } - } - } - } - if (failToBootstrapException != null) { - throw new IOException(failToBootstrapException); - } - return bootstrapInfo; - } - } - @VisibleForTesting static BootstrapInfo parseConfig(String rawData) throws IOException { @SuppressWarnings("unchecked") diff --git a/xds/src/main/java/io/grpc/xds/XdsNameResolver.java b/xds/src/main/java/io/grpc/xds/XdsNameResolver.java index f678787cd7..f4c69cffaf 100644 --- a/xds/src/main/java/io/grpc/xds/XdsNameResolver.java +++ b/xds/src/main/java/io/grpc/xds/XdsNameResolver.java @@ -60,7 +60,7 @@ final class XdsNameResolver extends NameResolver { private final Bootstrapper bootstrapper; XdsNameResolver(String name) { - this(name, Bootstrapper.newInsatnce()); + this(name, Bootstrapper.getInstance()); } @VisibleForTesting