Skip to content

Commit

Permalink
Merge branch 'grpc:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SreeramdasLavanya authored Oct 8, 2024
2 parents 4113845 + 2aae68e commit 09c3509
Show file tree
Hide file tree
Showing 92 changed files with 1,308 additions and 1,160 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ For a guided tour, take a look at the [quick start
guide](https://grpc.io/docs/languages/java/quickstart) or the more explanatory [gRPC
basics](https://grpc.io/docs/languages/java/basics).

The [examples](https://github.com/grpc/grpc-java/tree/v1.66.0/examples) and the
[Android example](https://github.com/grpc/grpc-java/tree/v1.66.0/examples/android)
The [examples](https://github.com/grpc/grpc-java/tree/v1.67.1/examples) and the
[Android example](https://github.com/grpc/grpc-java/tree/v1.67.1/examples/android)
are standalone projects that showcase the usage of gRPC.

Download
Expand All @@ -56,18 +56,18 @@ Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`:
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.66.0</version>
<version>1.67.1</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.66.0</version>
<version>1.67.1</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.66.0</version>
<version>1.67.1</version>
</dependency>
<dependency> <!-- necessary for Java 9+ -->
<groupId>org.apache.tomcat</groupId>
Expand All @@ -79,18 +79,18 @@ Download [the JARs][]. Or for Maven with non-Android, add to your `pom.xml`:

Or for Gradle with non-Android, add to your dependencies:
```gradle
runtimeOnly 'io.grpc:grpc-netty-shaded:1.66.0'
implementation 'io.grpc:grpc-protobuf:1.66.0'
implementation 'io.grpc:grpc-stub:1.66.0'
runtimeOnly 'io.grpc:grpc-netty-shaded:1.67.1'
implementation 'io.grpc:grpc-protobuf:1.67.1'
implementation 'io.grpc:grpc-stub:1.67.1'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+
```

For Android client, use `grpc-okhttp` instead of `grpc-netty-shaded` and
`grpc-protobuf-lite` instead of `grpc-protobuf`:
```gradle
implementation 'io.grpc:grpc-okhttp:1.66.0'
implementation 'io.grpc:grpc-protobuf-lite:1.66.0'
implementation 'io.grpc:grpc-stub:1.66.0'
implementation 'io.grpc:grpc-okhttp:1.67.1'
implementation 'io.grpc:grpc-protobuf-lite:1.67.1'
implementation 'io.grpc:grpc-stub:1.67.1'
compileOnly 'org.apache.tomcat:annotations-api:6.0.53' // necessary for Java 9+
```

Expand All @@ -99,7 +99,7 @@ For [Bazel](https://bazel.build), you can either
(with the GAVs from above), or use `@io_grpc_grpc_java//api` et al (see below).

[the JARs]:
https://search.maven.org/search?q=g:io.grpc%20AND%20v:1.66.0
https://search.maven.org/search?q=g:io.grpc%20AND%20v:1.67.1

Development snapshots are available in [Sonatypes's snapshot
repository](https://oss.sonatype.org/content/repositories/snapshots/).
Expand Down Expand Up @@ -131,7 +131,7 @@ For protobuf-based codegen integrated with the Maven build system, you can use
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.66.0:exe:${os.detected.classifier}</pluginArtifact>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.67.1:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
Expand Down Expand Up @@ -161,7 +161,7 @@ protobuf {
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.66.0'
artifact = 'io.grpc:protoc-gen-grpc-java:1.67.1'
}
}
generateProtoTasks {
Expand Down Expand Up @@ -194,7 +194,7 @@ protobuf {
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.66.0'
artifact = 'io.grpc:protoc-gen-grpc-java:1.67.1'
}
}
generateProtoTasks {
Expand Down
6 changes: 6 additions & 0 deletions api/src/main/java/io/grpc/LoadBalancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ public abstract class LoadBalancer {
HEALTH_CONSUMER_LISTENER_ARG_KEY =
LoadBalancer.CreateSubchannelArgs.Key.create("internal:health-check-consumer-listener");

@Internal
public static final LoadBalancer.CreateSubchannelArgs.Key<Boolean>
DISABLE_SUBCHANNEL_RECONNECT_KEY =
LoadBalancer.CreateSubchannelArgs.Key.createWithDefault(
"internal:disable-subchannel-reconnect", Boolean.FALSE);

@Internal
public static final Attributes.Key<Boolean>
HAS_HEALTH_PRODUCER_LISTENER_KEY =
Expand Down
71 changes: 49 additions & 22 deletions api/src/main/java/io/grpc/NameResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Objects;
import com.google.errorprone.annotations.InlineMe;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.net.URI;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -95,7 +95,8 @@ public void onError(Status error) {

@Override
public void onResult(ResolutionResult resolutionResult) {
listener.onAddresses(resolutionResult.getAddresses(), resolutionResult.getAttributes());
listener.onAddresses(resolutionResult.getAddressesOrError().getValue(),
resolutionResult.getAttributes());
}
});
}
Expand Down Expand Up @@ -218,19 +219,21 @@ public abstract static class Listener2 implements Listener {
@Override
@Deprecated
@InlineMe(
replacement = "this.onResult(ResolutionResult.newBuilder().setAddresses(servers)"
+ ".setAttributes(attributes).build())",
imports = "io.grpc.NameResolver.ResolutionResult")
replacement = "this.onResult2(ResolutionResult.newBuilder().setAddressesOrError("
+ "StatusOr.fromValue(servers)).setAttributes(attributes).build())",
imports = {"io.grpc.NameResolver.ResolutionResult", "io.grpc.StatusOr"})
public final void onAddresses(
List<EquivalentAddressGroup> servers, @ResolutionResultAttr Attributes attributes) {
// TODO(jihuncho) need to promote Listener2 if we want to use ConfigOrError
onResult(
ResolutionResult.newBuilder().setAddresses(servers).setAttributes(attributes).build());
onResult2(
ResolutionResult.newBuilder().setAddressesOrError(
StatusOr.fromValue(servers)).setAttributes(attributes).build());
}

/**
* Handles updates on resolved addresses and attributes. If
* {@link ResolutionResult#getAddresses()} is empty, {@link #onError(Status)} will be called.
* {@link ResolutionResult#getAddressesOrError()} is empty, {@link #onError(Status)} will be
* called.
*
* @param resolutionResult the resolved server addresses, attributes, and Service Config.
* @since 1.21.0
Expand Down Expand Up @@ -584,17 +587,17 @@ public abstract static class ServiceConfigParser {
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
public static final class ResolutionResult {
private final List<EquivalentAddressGroup> addresses;
private final StatusOr<List<EquivalentAddressGroup>> addressesOrError;
@ResolutionResultAttr
private final Attributes attributes;
@Nullable
private final ConfigOrError serviceConfig;

ResolutionResult(
List<EquivalentAddressGroup> addresses,
StatusOr<List<EquivalentAddressGroup>> addressesOrError,
@ResolutionResultAttr Attributes attributes,
ConfigOrError serviceConfig) {
this.addresses = Collections.unmodifiableList(new ArrayList<>(addresses));
this.addressesOrError = addressesOrError;
this.attributes = checkNotNull(attributes, "attributes");
this.serviceConfig = serviceConfig;
}
Expand All @@ -615,7 +618,7 @@ public static Builder newBuilder() {
*/
public Builder toBuilder() {
return newBuilder()
.setAddresses(addresses)
.setAddressesOrError(addressesOrError)
.setAttributes(attributes)
.setServiceConfig(serviceConfig);
}
Expand All @@ -624,9 +627,20 @@ public Builder toBuilder() {
* Gets the addresses resolved by name resolution.
*
* @since 1.21.0
* @deprecated Will be superseded by getAddressesOrError
*/
@Deprecated
public List<EquivalentAddressGroup> getAddresses() {
return addresses;
return addressesOrError.getValue();
}

/**
* Gets the addresses resolved by name resolution or the error in doing so.
*
* @since 1.65.0
*/
public StatusOr<List<EquivalentAddressGroup>> getAddressesOrError() {
return addressesOrError;
}

/**
Expand All @@ -652,11 +666,11 @@ public ConfigOrError getServiceConfig() {

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("addresses", addresses)
.add("attributes", attributes)
.add("serviceConfig", serviceConfig)
.toString();
ToStringHelper stringHelper = MoreObjects.toStringHelper(this);
stringHelper.add("addressesOrError", addressesOrError.toString());
stringHelper.add("attributes", attributes);
stringHelper.add("serviceConfigOrError", serviceConfig);
return stringHelper.toString();
}

/**
Expand All @@ -668,7 +682,7 @@ public boolean equals(Object obj) {
return false;
}
ResolutionResult that = (ResolutionResult) obj;
return Objects.equal(this.addresses, that.addresses)
return Objects.equal(this.addressesOrError, that.addressesOrError)
&& Objects.equal(this.attributes, that.attributes)
&& Objects.equal(this.serviceConfig, that.serviceConfig);
}
Expand All @@ -678,7 +692,7 @@ public boolean equals(Object obj) {
*/
@Override
public int hashCode() {
return Objects.hashCode(addresses, attributes, serviceConfig);
return Objects.hashCode(addressesOrError, attributes, serviceConfig);
}

/**
Expand All @@ -688,7 +702,8 @@ public int hashCode() {
*/
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/1770")
public static final class Builder {
private List<EquivalentAddressGroup> addresses = Collections.emptyList();
private StatusOr<List<EquivalentAddressGroup>> addresses =
StatusOr.fromValue(Collections.emptyList());
private Attributes attributes = Attributes.EMPTY;
@Nullable
private ConfigOrError serviceConfig;
Expand All @@ -700,9 +715,21 @@ public static final class Builder {
* Sets the addresses resolved by name resolution. This field is required.
*
* @since 1.21.0
* @deprecated Will be superseded by setAddressesOrError
*/
@Deprecated
public Builder setAddresses(List<EquivalentAddressGroup> addresses) {
this.addresses = addresses;
setAddressesOrError(StatusOr.fromValue(addresses));
return this;
}

/**
* Sets the addresses resolved by name resolution or the error in doing so. This field is
* required.
* @param addresses Resolved addresses or an error in resolving addresses
*/
public Builder setAddressesOrError(StatusOr<List<EquivalentAddressGroup>> addresses) {
this.addresses = checkNotNull(addresses, "StatusOr addresses cannot be null.");
return this;
}

Expand Down
102 changes: 102 additions & 0 deletions api/src/main/java/io/grpc/StatusOr.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* 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;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Objects;
import javax.annotation.Nullable;

/** Either a Status or a value. */
@ExperimentalApi("https://github.com/grpc/grpc-java/issues/11563")
public class StatusOr<T> {
private StatusOr(Status status, T value) {
this.status = status;
this.value = value;
}

/** Construct from a value. */
public static <T> StatusOr<T> fromValue(@Nullable T value) {
StatusOr<T> result = new StatusOr<T>(null, value);
return result;
}

/** Construct from a non-Ok status. */
public static <T> StatusOr<T> fromStatus(Status status) {
StatusOr<T> result = new StatusOr<T>(checkNotNull(status, "status"), null);
checkArgument(!status.isOk(), "cannot use OK status: %s", status);
return result;
}

/** Returns whether there is a value. */
public boolean hasValue() {
return status == null;
}

/**
* Returns the value if set or throws exception if there is no value set. This method is meant
* to be called after checking the return value of hasValue() first.
*/
public @Nullable T getValue() {
if (status != null) {
throw new IllegalStateException("No value present.");
}
return value;
}

/** Returns the status. If there is a value (which can be null), returns OK. */
public Status getStatus() {
return status == null ? Status.OK : status;
}

@Override
public boolean equals(Object other) {
if (!(other instanceof StatusOr)) {
return false;
}
StatusOr<?> otherStatus = (StatusOr<?>) other;
if (hasValue() != otherStatus.hasValue()) {
return false;
}
if (hasValue()) {
return Objects.equal(value, otherStatus.value);
}
return Objects.equal(status, otherStatus.status);
}

@Override
public int hashCode() {
return Objects.hashCode(status, value);
}

@Override
public String toString() {
ToStringHelper stringHelper = MoreObjects.toStringHelper(this);
if (status == null) {
stringHelper.add("value", value);
} else {
stringHelper.add("error", status);
}
return stringHelper.toString();
}

private final Status status;
private final T value;
}
Loading

0 comments on commit 09c3509

Please sign in to comment.