On result2 resolution result have addresses or error (#11330) · grpc/grpc-java@1ded8af
@@ -20,13 +20,13 @@
2020import static com.google.common.base.Preconditions.checkNotNull;
21212222import com.google.common.base.MoreObjects;
23+import com.google.common.base.MoreObjects.ToStringHelper;
2324import com.google.common.base.Objects;
2425import com.google.errorprone.annotations.InlineMe;
2526import java.lang.annotation.Documented;
2627import java.lang.annotation.Retention;
2728import java.lang.annotation.RetentionPolicy;
2829import java.net.URI;
29-import java.util.ArrayList;
3030import java.util.Collections;
3131import java.util.List;
3232import java.util.Map;
@@ -95,7 +95,8 @@ public void onError(Status error) {
95959696@Override
9797public void onResult(ResolutionResult resolutionResult) {
98-listener.onAddresses(resolutionResult.getAddresses(), resolutionResult.getAttributes());
98+listener.onAddresses(resolutionResult.getAddressesOrError().getValue(),
99+resolutionResult.getAttributes());
99100 }
100101 });
101102 }
@@ -218,19 +219,21 @@ public abstract static class Listener2 implements Listener {
218219@Override
219220@Deprecated
220221@InlineMe(
221-replacement = "this.onResult(ResolutionResult.newBuilder().setAddresses(servers)"
222- + ".setAttributes(attributes).build())",
223-imports = "io.grpc.NameResolver.ResolutionResult")
222+replacement = "this.onResult2(ResolutionResult.newBuilder().setAddressesOrError("
223+ + "StatusOr.fromValue(servers)).setAttributes(attributes).build())",
224+imports = {"io.grpc.NameResolver.ResolutionResult", "io.grpc.StatusOr"})
224225public final void onAddresses(
225226List<EquivalentAddressGroup> servers, @ResolutionResultAttr Attributes attributes) {
226227// TODO(jihuncho) need to promote Listener2 if we want to use ConfigOrError
227-onResult(
228-ResolutionResult.newBuilder().setAddresses(servers).setAttributes(attributes).build());
228+onResult2(
229+ResolutionResult.newBuilder().setAddressesOrError(
230+StatusOr.fromValue(servers)).setAttributes(attributes).build());
229231 }
230232231233/**
232234 * Handles updates on resolved addresses and attributes. If
233- * {@link ResolutionResult#getAddresses()} is empty, {@link #onError(Status)} will be called.
235+ * {@link ResolutionResult#getAddressesOrError()} is empty, {@link #onError(Status)} will be
236+ * called.
234237 *
235238 * @param resolutionResult the resolved server addresses, attributes, and Service Config.
236239 * @since 1.21.0
@@ -584,17 +587,17 @@ public abstract static class ServiceConfigParser {
584587 */
585588@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
586589public static final class ResolutionResult {
587-private final List<EquivalentAddressGroup> addresses;
590+private final StatusOr<List<EquivalentAddressGroup>> addressesOrError;
588591@ResolutionResultAttr
589592private final Attributes attributes;
590593@Nullable
591594private final ConfigOrError serviceConfig;
592595593596ResolutionResult(
594-List<EquivalentAddressGroup> addresses,
597+StatusOr<List<EquivalentAddressGroup>> addressesOrError,
595598@ResolutionResultAttr Attributes attributes,
596599ConfigOrError serviceConfig) {
597-this.addresses = Collections.unmodifiableList(new ArrayList<>(addresses));
600+this.addressesOrError = addressesOrError;
598601this.attributes = checkNotNull(attributes, "attributes");
599602this.serviceConfig = serviceConfig;
600603 }
@@ -615,7 +618,7 @@ public static Builder newBuilder() {
615618 */
616619public Builder toBuilder() {
617620return newBuilder()
618- .setAddresses(addresses)
621+ .setAddressesOrError(addressesOrError)
619622 .setAttributes(attributes)
620623 .setServiceConfig(serviceConfig);
621624 }
@@ -624,9 +627,20 @@ public Builder toBuilder() {
624627 * Gets the addresses resolved by name resolution.
625628 *
626629 * @since 1.21.0
630+ * @deprecated Will be superseded by getAddressesOrError
627631 */
632+@Deprecated
628633public List<EquivalentAddressGroup> getAddresses() {
629-return addresses;
634+return addressesOrError.getValue();
635+ }
636+637+/**
638+ * Gets the addresses resolved by name resolution or the error in doing so.
639+ *
640+ * @since 1.65.0
641+ */
642+public StatusOr<List<EquivalentAddressGroup>> getAddressesOrError() {
643+return addressesOrError;
630644 }
631645632646/**
@@ -652,11 +666,11 @@ public ConfigOrError getServiceConfig() {
652666653667@Override
654668public String toString() {
655-return MoreObjects.toStringHelper(this)
656- .add("addresses", addresses)
657- .add("attributes", attributes)
658- .add("serviceConfig", serviceConfig)
659- .toString();
669+ToStringHelper stringHelper = MoreObjects.toStringHelper(this);
670+stringHelper.add("addressesOrError", addressesOrError.toString());
671+stringHelper.add("attributes", attributes);
672+stringHelper.add("serviceConfigOrError", serviceConfig);
673+return stringHelper.toString();
660674 }
661675662676/**
@@ -668,7 +682,7 @@ public boolean equals(Object obj) {
668682return false;
669683 }
670684ResolutionResult that = (ResolutionResult) obj;
671-return Objects.equal(this.addresses, that.addresses)
685+return Objects.equal(this.addressesOrError, that.addressesOrError)
672686&& Objects.equal(this.attributes, that.attributes)
673687&& Objects.equal(this.serviceConfig, that.serviceConfig);
674688 }
@@ -678,7 +692,7 @@ public boolean equals(Object obj) {
678692 */
679693@Override
680694public int hashCode() {
681-return Objects.hashCode(addresses, attributes, serviceConfig);
695+return Objects.hashCode(addressesOrError, attributes, serviceConfig);
682696 }
683697684698/**
@@ -688,7 +702,8 @@ public int hashCode() {
688702 */
689703@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
690704public static final class Builder {
691-private List<EquivalentAddressGroup> addresses = Collections.emptyList();
705+private StatusOr<List<EquivalentAddressGroup>> addresses =
706+StatusOr.fromValue(Collections.emptyList());
692707private Attributes attributes = Attributes.EMPTY;
693708@Nullable
694709private ConfigOrError serviceConfig;
@@ -700,9 +715,21 @@ public static final class Builder {
700715 * Sets the addresses resolved by name resolution. This field is required.
701716 *
702717 * @since 1.21.0
718+ * @deprecated Will be superseded by setAddressesOrError
703719 */
720+@Deprecated
704721public Builder setAddresses(List<EquivalentAddressGroup> addresses) {
705-this.addresses = addresses;
722+setAddressesOrError(StatusOr.fromValue(addresses));
723+return this;
724+ }
725+726+/**
727+ * Sets the addresses resolved by name resolution or the error in doing so. This field is
728+ * required.
729+ * @param addresses Resolved addresses or an error in resolving addresses
730+ */
731+public Builder setAddressesOrError(StatusOr<List<EquivalentAddressGroup>> addresses) {
732+this.addresses = checkNotNull(addresses, "StatusOr addresses cannot be null.");
706733return this;
707734 }
708735