Skip to content

Commit feffad5

Browse files
Splitting core into core, core-http, and core-grpc (googleapis#1968)
1 parent f0c51d9 commit feffad5

22 files changed

Lines changed: 946 additions & 307 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<artifactId>google-cloud-core-grpc</artifactId>
5+
<version>1.0.0-rc2-SNAPSHOT</version>
6+
<packaging>jar</packaging>
7+
<name>Google Cloud Core gRPC</name>
8+
<url>https://github.com/GoogleCloudPlatform/google-cloud-java/tree/master/google-cloud-core-grpc</url>
9+
<description>
10+
Core gRPC module for the google-cloud.
11+
</description>
12+
<parent>
13+
<groupId>com.google.cloud</groupId>
14+
<artifactId>google-cloud-pom</artifactId>
15+
<version>0.14.1-alpha-SNAPSHOT</version>
16+
</parent>
17+
<properties>
18+
<site.installationModule>google-cloud-core-grpc</site.installationModule>
19+
<artifact.version>${project.version}</artifact.version>
20+
</properties>
21+
<dependencies>
22+
<dependency>
23+
<groupId>com.google.cloud</groupId>
24+
<artifactId>google-cloud-core</artifactId>
25+
<version>${core.version}</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.google.guava</groupId>
29+
<artifactId>guava</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>junit</groupId>
33+
<artifactId>junit</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.easymock</groupId>
38+
<artifactId>easymock</artifactId>
39+
<version>3.4</version>
40+
<scope>test</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>com.google.protobuf</groupId>
44+
<artifactId>protobuf-java</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>com.google.protobuf</groupId>
48+
<artifactId>protobuf-java-util</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.grpc</groupId>
52+
<artifactId>grpc-protobuf</artifactId>
53+
<version>${grpc.version}</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>com.google.api</groupId>
57+
<artifactId>gax-grpc</artifactId>
58+
</dependency>
59+
<dependency>
60+
<groupId>com.google.truth</groupId>
61+
<artifactId>truth</artifactId>
62+
<version>0.30</version>
63+
<scope>test</scope>
64+
</dependency>
65+
<dependency>
66+
<groupId>com.google.guava</groupId>
67+
<artifactId>guava-testlib</artifactId>
68+
<version>19.0</version>
69+
<scope>test</scope>
70+
</dependency>
71+
</dependencies>
72+
<build>
73+
<plugins>
74+
<plugin>
75+
<groupId>org.codehaus.mojo</groupId>
76+
<artifactId>properties-maven-plugin</artifactId>
77+
<version>1.0-alpha-2</version>
78+
<executions>
79+
<execution>
80+
<phase>generate-resources</phase>
81+
<goals>
82+
<goal>write-project-properties</goal>
83+
</goals>
84+
<configuration>
85+
<outputFile>${project.build.outputDirectory}/com/google/cloud/project.properties</outputFile>
86+
</configuration>
87+
</execution>
88+
</executions>
89+
</plugin>
90+
</plugins>
91+
</build>
92+
</project>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.grpc;
18+
19+
import com.google.api.client.http.HttpResponseException;
20+
import com.google.api.gax.grpc.ApiException;
21+
import com.google.cloud.BaseServiceException;
22+
import com.google.common.base.MoreObjects;
23+
import java.io.IOException;
24+
import java.util.Collections;
25+
26+
/**
27+
* Base class for all exceptions from grpc-based services.
28+
*/
29+
public class BaseGrpcServiceException extends BaseServiceException {
30+
31+
private static final long serialVersionUID = -2685197215731335549L;
32+
33+
public BaseGrpcServiceException(String message, Throwable cause, int code, boolean retryable) {
34+
super(ExceptionData.newBuilder().setMessage(message).setCause(cause).setCode(code)
35+
.setRetryable(retryable).build());
36+
}
37+
38+
public BaseGrpcServiceException(IOException exception, boolean idempotent) {
39+
super(makeExceptionData(exception, idempotent));
40+
}
41+
42+
private static ExceptionData makeExceptionData(IOException exception, boolean idempotent) {
43+
int code = UNKNOWN_CODE;
44+
Boolean retryable = null;
45+
if (exception instanceof HttpResponseException) {
46+
// In cases where an exception is an instance of HttpResponseException,
47+
// check the status code to determine whether it's retryable
48+
code = ((HttpResponseException) exception).getStatusCode();
49+
retryable = BaseServiceException
50+
.isRetryable(code, null, idempotent, Collections.<Error>emptySet());
51+
}
52+
return ExceptionData.newBuilder()
53+
.setMessage(exception.getMessage())
54+
.setCause(exception)
55+
.setRetryable(MoreObjects
56+
.firstNonNull(retryable, BaseServiceException.isRetryable(idempotent, exception)))
57+
.setCode(code)
58+
.setReason(null)
59+
.setLocation(null)
60+
.setDebugInfo(null)
61+
.build();
62+
}
63+
64+
public BaseGrpcServiceException(ApiException apiException) {
65+
super(ExceptionData.newBuilder()
66+
.setMessage(apiException.getMessage())
67+
.setCause(apiException)
68+
.setRetryable(apiException.isRetryable())
69+
.setCode(apiException.getStatusCode().value())
70+
.setReason(apiException.getStatusCode().name())
71+
.setLocation(null)
72+
.setDebugInfo(null)
73+
.build());
74+
}
75+
}

java-core/google-cloud-core/src/main/java/com/google/cloud/GrpcTransportOptions.java renamed to java-core/google-cloud-core-grpc/src/main/java/com/google/cloud/grpc/GrpcTransportOptions.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud;
17+
package com.google.cloud.grpc;
1818

1919
import static com.google.common.base.MoreObjects.firstNonNull;
2020

21+
import com.google.api.core.InternalApi;
2122
import com.google.api.gax.core.FixedCredentialsProvider;
22-
import com.google.api.gax.core.RetrySettings;
2323
import com.google.api.gax.grpc.ChannelProvider;
2424
import com.google.api.gax.grpc.InstantiatingChannelProvider;
2525
import com.google.api.gax.grpc.UnaryCallSettings;
26+
import com.google.api.gax.retrying.RetrySettings;
2627
import com.google.auth.Credentials;
27-
import com.google.common.annotations.VisibleForTesting;
28-
import com.google.common.base.Preconditions;
28+
import com.google.cloud.NoCredentials;
29+
import com.google.cloud.ServiceOptions;
30+
import com.google.cloud.TransportOptions;
2931
import io.grpc.internal.SharedResourceHolder;
3032
import io.grpc.internal.SharedResourceHolder.Resource;
3133
import java.io.IOException;
@@ -92,8 +94,8 @@ public interface ExecutorFactory<T extends ExecutorService> {
9294
void release(T executor);
9395
}
9496

95-
@VisibleForTesting
96-
static class DefaultExecutorFactory implements ExecutorFactory<ScheduledExecutorService> {
97+
@InternalApi
98+
public static class DefaultExecutorFactory implements ExecutorFactory<ScheduledExecutorService> {
9799

98100
private static final DefaultExecutorFactory INSTANCE = new DefaultExecutorFactory();
99101

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2017 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.grpc;
18+
19+
import static org.easymock.EasyMock.createMock;
20+
import static org.easymock.EasyMock.expect;
21+
import static org.easymock.EasyMock.replay;
22+
import static org.easymock.EasyMock.verify;
23+
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.assertFalse;
25+
import static org.junit.Assert.assertNull;
26+
import static org.junit.Assert.assertTrue;
27+
28+
import com.google.api.gax.grpc.ApiException;
29+
import com.google.cloud.BaseServiceException;
30+
import com.google.cloud.RetryHelper;
31+
import io.grpc.Status.Code;
32+
import java.io.IOException;
33+
import java.net.SocketException;
34+
import java.net.SocketTimeoutException;
35+
import org.junit.Test;
36+
37+
public class BaseGrpcServiceExceptionTest {
38+
39+
private static final int CODE = 1;
40+
private static final String MESSAGE = "some message";
41+
private static final boolean NOT_RETRYABLE = false;
42+
private static final boolean IDEMPOTENT = true;
43+
44+
@Test
45+
public void testBaseServiceException() {
46+
BaseGrpcServiceException serviceException = null;
47+
48+
IOException exception = new SocketTimeoutException();
49+
serviceException = new BaseGrpcServiceException(exception, IDEMPOTENT);
50+
assertTrue(serviceException.isRetryable());
51+
assertNull(serviceException.getMessage());
52+
assertEquals(exception, serviceException.getCause());
53+
assertNull(serviceException.getReason());
54+
assertNull(serviceException.getLocation());
55+
assertNull(serviceException.getDebugInfo());
56+
57+
exception = new SocketException();
58+
serviceException = new BaseGrpcServiceException(exception, IDEMPOTENT);
59+
assertTrue(serviceException.isRetryable());
60+
assertNull(serviceException.getMessage());
61+
assertEquals(exception, serviceException.getCause());
62+
assertNull(serviceException.getReason());
63+
assertNull(serviceException.getLocation());
64+
assertNull(serviceException.getDebugInfo());
65+
66+
exception = new IOException("insufficient data written");
67+
serviceException = new BaseGrpcServiceException(exception, IDEMPOTENT);
68+
assertTrue(serviceException.isRetryable());
69+
assertEquals("insufficient data written", serviceException.getMessage());
70+
assertEquals(exception, serviceException.getCause());
71+
assertNull(serviceException.getReason());
72+
assertNull(serviceException.getLocation());
73+
assertNull(serviceException.getDebugInfo());
74+
75+
Exception cause = new IllegalArgumentException("bad arg");
76+
ApiException apiException = new ApiException(MESSAGE, cause, Code.INTERNAL, NOT_RETRYABLE);
77+
serviceException = new BaseGrpcServiceException(apiException);
78+
assertFalse(serviceException.isRetryable());
79+
assertEquals(MESSAGE, serviceException.getMessage());
80+
assertEquals(apiException, serviceException.getCause());
81+
assertEquals(Code.INTERNAL.value(), serviceException.getCode());
82+
assertEquals(Code.INTERNAL.name(), serviceException.getReason());
83+
assertNull(serviceException.getLocation());
84+
assertNull(serviceException.getDebugInfo());
85+
}
86+
87+
@Test
88+
public void testTranslateAndThrow() throws Exception {
89+
IOException exception = new SocketTimeoutException();
90+
BaseGrpcServiceException cause = new BaseGrpcServiceException(exception, IDEMPOTENT);
91+
RetryHelper.RetryHelperException exceptionMock =
92+
createMock(RetryHelper.RetryHelperException.class);
93+
expect(exceptionMock.getCause()).andReturn(cause).times(2);
94+
replay(exceptionMock);
95+
try {
96+
BaseServiceException.translate(exceptionMock);
97+
} catch (BaseServiceException ex) {
98+
assertEquals(0, ex.getCode());
99+
assertNull(ex.getMessage());
100+
assertTrue(ex.isRetryable());
101+
} finally {
102+
verify(exceptionMock);
103+
}
104+
}
105+
106+
}

java-core/google-cloud-core/src/test/java/com/google/cloud/GrpcTransportOptionsTest.java renamed to java-core/google-cloud-core-grpc/src/test/java/com/google/cloud/grpc/GrpcTransportOptionsTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.cloud;
17+
package com.google.cloud.grpc;
1818

1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertNotEquals;
2121
import static org.junit.Assert.assertSame;
2222
import static org.junit.Assert.assertTrue;
23-
import static org.junit.Assert.fail;
2423

25-
import com.google.cloud.GrpcTransportOptions.DefaultExecutorFactory;
26-
import com.google.cloud.GrpcTransportOptions.ExecutorFactory;
24+
import com.google.cloud.grpc.GrpcTransportOptions.DefaultExecutorFactory;
25+
import com.google.cloud.grpc.GrpcTransportOptions.ExecutorFactory;
2726
import java.util.concurrent.ScheduledExecutorService;
2827
import org.easymock.EasyMock;
2928
import org.junit.Test;

0 commit comments

Comments
 (0)