Skip to content

Commit

Permalink
Merge branch 'main' into googleapisgh-1375/custom-write-timeout-executor
Browse files Browse the repository at this point in the history
  • Loading branch information
dzikoysk committed Dec 9, 2021
2 parents e622b5e + 198e0d5 commit 94e762c
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .github/.OwlBot.lock.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
docker:
image: gcr.io/repo-automation-bots/owlbot-java:latest
digest: sha256:a3ac08d167454718ff057b97a1950d3cb5e16fc39fb3f355d90276285a6cac75
digest: sha256:a4d7b2cfc6a9d6b378a6b2458740eae15fcab28854bd23dad3a15102d2e47c87
18 changes: 2 additions & 16 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ jobs:
units:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
java: [8, 11, 17]
steps:
- uses: actions/checkout@v2
- uses: stCarolas/setup-maven@v4
with:
maven-version: 3.8.1
- uses: actions/setup-java@v2
with:
distribution: zulu
Expand All @@ -27,9 +25,6 @@ jobs:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: stCarolas/setup-maven@v4
with:
maven-version: 3.8.1
- uses: actions/setup-java@v2
with:
distribution: zulu
Expand All @@ -45,9 +40,6 @@ jobs:
java: [8, 11, 17]
steps:
- uses: actions/checkout@v2
- uses: stCarolas/setup-maven@v4
with:
maven-version: 3.8.1
- uses: actions/setup-java@v2
with:
distribution: zulu
Expand All @@ -58,13 +50,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: stCarolas/setup-maven@v4
with:
maven-version: 3.8.1
- uses: actions/setup-java@v2
with:
distribution: zulu
java-version: 8
java-version: 11
- run: java -version
- run: .kokoro/build.sh
env:
Expand All @@ -73,9 +62,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: stCarolas/setup-maven@v4
with:
maven-version: 3.8.1
- uses: actions/setup-java@v2
with:
distribution: zulu
Expand Down
3 changes: 1 addition & 2 deletions .kokoro/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ integration)
;;
graalvm)
# Run Unit and Integration Tests with Native Image
mvn test -Pnative -Penable-integration-tests
mvn -ntp -Pnative -Penable-integration-tests test
RETURN_CODE=$?
;;
samples)
Expand All @@ -91,7 +91,6 @@ samples)

pushd ${SAMPLES_DIR}
mvn -B \
-Penable-samples \
-ntp \
-DtrimStackTrace=false \
-Dclirr.skip=true \
Expand Down
10 changes: 3 additions & 7 deletions .kokoro/release/publish_javadoc11.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@ mvn clean install -B -q -DskipTests=true
export NAME=google-http-client
export VERSION=$(grep ${NAME}: versions.txt | cut -d: -f3)

# V3 generates docfx yml from javadoc
# generate yml
mvn clean site -B -q -P docFX

# copy README to docfx-yml dir and rename index.md
cp README.md target/docfx-yml/index.md
# copy CHANGELOG to docfx-yml dir and rename history.md
# cloud RAD generation
mvn clean javadoc:aggregate -B -q -P docFX
# include CHANGELOG
cp CHANGELOG.md target/docfx-yml/history.md

pushd target/docfx-yml
Expand Down
61 changes: 7 additions & 54 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ mvn -Penable-integration-tests clean verify

## Code Samples

Code Samples must be bundled in separate Maven modules, and guarded by a
Maven profile with the name `enable-samples`.
All code samples must be in compliance with the [java sample formatting guide][3].
Code Samples must be bundled in separate Maven modules.

The samples must be separate from the primary project for a few reasons:
1. Primary projects have a minimum Java version of Java 7 whereas samples have
a minimum Java version of Java 8. Due to this we need the ability to
1. Primary projects have a minimum Java version of Java 8 whereas samples can have
Java version of Java 11. Due to this we need the ability to
selectively exclude samples from a build run.
2. Many code samples depend on external GCP services and need
credentials to access the service.
Expand All @@ -68,39 +68,16 @@ The samples must be separate from the primary project for a few reasons:
### Building

```bash
mvn -Penable-samples clean verify
mvn clean verify
```

Some samples require access to GCP services and require a service account:

```bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account.json
mvn -Penable-samples clean verify
mvn clean verify
```

### Profile Config

1. To add samples in a profile to your Maven project, add the following to your
`pom.xml`

```xml
<project>
[...]
<profiles>
<profile>
<id>enable-samples</id>
<modules>
<module>sample</module>
</modules>
</profile>
</profiles>
[...]
</project>
```

2. [Activate](#profile-activation) the profile.
3. Define your samples in a normal Maven project in the `samples/` directory.

### Code Formatting

Code in this repo is formatted with
Expand All @@ -110,30 +87,6 @@ To run formatting on your project, you can run:
mvn com.coveo:fmt-maven-plugin:format
```

### Profile Activation

To include code samples when building and testing the project, enable the
`enable-samples` Maven profile.

#### Command line

To activate the Maven profile on the command line add `-Penable-samples` to your
Maven command.

#### Maven `settings.xml`

To activate the Maven profile in your `~/.m2/settings.xml` add an entry of
`enable-samples` following the instructions in [Active Profiles][2].

This method has the benefit of applying to all projects you build (and is
respected by IntelliJ IDEA) and is recommended if you are going to be
contributing samples to several projects.

#### IntelliJ IDEA

To activate the Maven Profile inside IntelliJ IDEA, follow the instructions in
[Activate Maven profiles][3] to activate `enable-samples`.

[1]: https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account
[2]: https://maven.apache.org/settings.html#Active_Profiles
[3]: https://www.jetbrains.com/help/idea/work-with-maven-profiles.html#activate_maven_profiles
[3]: https://github.com/GoogleCloudPlatform/java-docs-samples/blob/main/SAMPLE_FORMAT.md
2 changes: 1 addition & 1 deletion docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ the `dependencyManagement` section of your `pom.xml`:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>libraries-bom</artifactId>
<version>23.1.0</version>
<version>24.1.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
Expand Down
10 changes: 1 addition & 9 deletions google-http-client-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -144,18 +144,10 @@
<plugin>
<groupId>com.coveo</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.9</version>
<version>2.13</version>
<configuration>
<style>google</style>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
<version>1.7</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,9 @@ public HttpResponse execute() throws IOException {
// throw an exception if unsuccessful response
if (throwExceptionOnExecuteError && !response.isSuccessStatusCode()) {
try {
throw new HttpResponseException(response);
throw new HttpResponseException.Builder(response)
.setAttemptCount(numRetries - retriesRemaining)
.build();
} finally {
response.disconnect();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public class HttpResponseException extends IOException {
/** HTTP response content or {@code null} for none. */
private final String content;

/** Number of attempts performed */
private final int attemptCount;

/**
* Constructor that constructs a detail message from the given HTTP response that includes the
* status code, status message and HTTP response content.
Expand Down Expand Up @@ -73,6 +76,7 @@ protected HttpResponseException(Builder builder) {
statusMessage = builder.statusMessage;
headers = builder.headers;
content = builder.content;
attemptCount = builder.attemptCount;
}

/**
Expand Down Expand Up @@ -121,6 +125,15 @@ public final String getContent() {
return content;
}

/**
* Returns the attempt count
*
* @since 1.41
*/
public final int getAttemptCount() {
return attemptCount;
}

/**
* Builder.
*
Expand All @@ -145,6 +158,9 @@ public static class Builder {
/** Detail message to use or {@code null} for none. */
String message;

/** Number of attempts performed */
int attemptCount;

/**
* @param statusCode HTTP status code
* @param statusMessage status message or {@code null}
Expand Down Expand Up @@ -260,6 +276,18 @@ public Builder setContent(String content) {
return this;
}

/** Returns the request attempt count */
public final int getAttemptCount() {
return attemptCount;
}

/** Sets the attempt count for the related HTTP request execution. */
public Builder setAttemptCount(int attemptCount) {
Preconditions.checkArgument(attemptCount >= 0);
this.attemptCount = attemptCount;
return this;
}

/** Returns a new instance of {@link HttpResponseException} based on this builder. */
public HttpResponseException build() {
return new HttpResponseException(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public class HttpRequestTracingTest {
public void setupTestTracer() {
Tracing.getExportComponent().getSpanExporter().registerHandler("test", testHandler);
TraceParams params =
Tracing.getTraceConfig()
.getActiveTraceParams()
.toBuilder()
Tracing.getTraceConfig().getActiveTraceParams().toBuilder()
.setSampler(Samplers.alwaysSample())
.build();
Tracing.getTraceConfig().updateActiveTraceParams(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
import com.google.api.client.testing.http.MockHttpTransport;
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
import com.google.api.client.util.ExponentialBackOff;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.function.ThrowingRunnable;

/**
Expand Down Expand Up @@ -208,6 +210,8 @@ public void run() throws Throwable {
+ SIMPLE_GENERIC_URL
+ LINE_SEPARATOR
+ "Unable to find resource");
// no retries expected
assertEquals(1, responseException.getAttemptCount());
}

public void testInvalidCharset() throws Exception {
Expand Down Expand Up @@ -245,6 +249,50 @@ public void run() throws Throwable {
.isEqualTo("404 Not Found\nGET " + SIMPLE_GENERIC_URL);
}

public void testAttemptCountWithBackOff() throws Exception {
HttpTransport fakeTransport =
new MockHttpTransport() {
@Override
public LowLevelHttpRequest buildRequest(String method, String url) throws IOException {
return new MockLowLevelHttpRequest() {
@Override
public LowLevelHttpResponse execute() throws IOException {
MockLowLevelHttpResponse result = new MockLowLevelHttpResponse();
result.setStatusCode(HttpStatusCodes.STATUS_CODE_SERVER_ERROR);
result.setReasonPhrase("Error");
result.setContent("Unknown Error");
return result;
}
};
}
};
ExponentialBackOff backoff = new ExponentialBackOff.Builder().build();
final HttpRequest request =
fakeTransport.createRequestFactory().buildGetRequest(new GenericUrl("http://not/used"));
request.setUnsuccessfulResponseHandler(
new HttpBackOffUnsuccessfulResponseHandler(backoff)
.setBackOffRequired(
new HttpBackOffUnsuccessfulResponseHandler.BackOffRequired() {
public boolean isRequired(HttpResponse response) {
return true;
}
}));
request.setNumberOfRetries(1);
HttpResponseException responseException =
assertThrows(
HttpResponseException.class,
new ThrowingRunnable() {
@Override
public void run() throws Throwable {
request.execute();
}
});

Assert.assertEquals(500, responseException.getStatusCode());
// original request and 1 retry - total 2
assertEquals(2, responseException.getAttemptCount());
}

public void testUnsupportedCharset() throws Exception {
HttpTransport transport =
new MockHttpTransport() {
Expand Down

0 comments on commit 94e762c

Please sign in to comment.