Skip to content

Commit 42bd301

Browse files
author
Ajay Kannan
committed
Add retryable exceptions and fix checkstyle complaints
1 parent 2650901 commit 42bd301

File tree

6 files changed

+30
-21
lines changed

6 files changed

+30
-21
lines changed

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Option.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import java.util.Objects;
2626

2727
/**
28-
* Base class for Resource Manager operation options
28+
* Base class for Resource Manager operation options.
2929
*/
3030
class Option implements Serializable {
3131

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/Project.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**
2222
* A Google Cloud Resource Manager project object.
2323
*
24-
* A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
24+
* <p>A Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs,
2525
* AppEngine Apps, VMs, and other Google Cloud Platform resources. This class' member variables are
2626
* immutable. Methods that change or update the underlying Project information return a new Project
2727
* instance.
@@ -77,7 +77,7 @@ public Project reload() {
7777
/**
7878
* Marks the project identified by the specified project ID for deletion.
7979
*
80-
* This method will only affect the project if the following criteria are met:
80+
* <p>This method will only affect the project if the following criteria are met:
8181
* <ul>
8282
* <li>The project does not have a billing account associated with it.
8383
* <li>The project has a lifecycle state of {@link ProjectInfo.State#ACTIVE}.
@@ -103,7 +103,7 @@ public void delete() {
103103
/**
104104
* Restores the project identified by the specified project ID.
105105
*
106-
* You can only use this method for a project that has a lifecycle state of
106+
* <p>You can only use this method for a project that has a lifecycle state of
107107
* {@link ProjectInfo.State#DELETE_REQUESTED}. After deletion starts, as indicated by a lifecycle
108108
* state of {@link ProjectInfo.State#DELETE_IN_PROGRESS}, the project cannot be restored. The
109109
* caller must have modify permissions for this project.
@@ -120,7 +120,7 @@ public void undelete() {
120120
/**
121121
* Replaces the attributes of the project.
122122
*
123-
* The caller must have modify permissions for this project.
123+
* <p>The caller must have modify permissions for this project.
124124
*
125125
* @see <a
126126
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/update">Cloud

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManager.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import com.google.gcloud.Service;
2323
import com.google.gcloud.spi.ResourceManagerRpc;
2424

25-
import java.util.HashSet;
25+
import java.util.Set;
2626

2727
/**
2828
* An interface for Google Cloud Resource Manager.
@@ -31,7 +31,7 @@
3131
*/
3232
public interface ResourceManager extends Service<ResourceManagerOptions> {
3333

34-
public static final String DEFAULT_CONTENT_TYPE = "application/octet-stream";
34+
String DEFAULT_CONTENT_TYPE = "application/octet-stream";
3535

3636
/**
3737
* The fields of a project.
@@ -59,7 +59,7 @@ public String selector() {
5959
}
6060

6161
static String selector(ProjectField... fields) {
62-
HashSet<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
62+
Set<String> fieldStrings = Sets.newHashSetWithExpectedSize(fields.length + 1);
6363
fieldStrings.add(PROJECT_ID.selector());
6464
for (ProjectField field : fields) {
6565
fieldStrings.add(field.selector());
@@ -71,7 +71,7 @@ static String selector(ProjectField... fields) {
7171
/**
7272
* Class for specifying project get options.
7373
*/
74-
public class ProjectGetOption extends Option {
74+
class ProjectGetOption extends Option {
7575

7676
private static final long serialVersionUID = 270185129961146874L;
7777

@@ -95,7 +95,7 @@ public static ProjectGetOption fields(ProjectField... fields) {
9595
/**
9696
* Class for specifying project list options.
9797
*/
98-
public class ProjectListOption extends Option {
98+
class ProjectListOption extends Option {
9999

100100
private static final long serialVersionUID = 7888768979702012328L;
101101

@@ -227,10 +227,9 @@ public static ProjectListOption fields(ProjectField... fields) {
227227
* Lists the projects visible to the current user.
228228
*
229229
* <p>This method returns projects in an unspecified order. New projects do not necessarily appear
230-
* at
231-
* the end of the list. Use {@link ProjectListOption} to filter this list, set page size, and set
232-
* page tokens. Note that pagination is currently not implemented by the Cloud Resource Manager
233-
* API.
230+
* at the end of the list. Use {@link ProjectListOption} to filter this list, set page size, and
231+
* set page tokens. Note that pagination is currently not implemented by the Cloud Resource
232+
* Manager API.
234233
*
235234
* @see <a
236235
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/list">Cloud
@@ -264,7 +263,7 @@ public static ProjectListOption fields(ProjectField... fields) {
264263
* @see <a
265264
* href="https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/undelete">Cloud
266265
* Resource Manager undelete</a>
267-
* @throws ResourceManagerException
266+
* @throws ResourceManagerException upon failure
268267
*/
269268
void undelete(String projectId);
270269
}

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/ResourceManagerOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ public boolean equals(Object obj) {
107107
return obj instanceof ResourceManagerOptions && baseEquals((ResourceManagerOptions) obj);
108108
}
109109

110+
@Override
111+
public int hashCode() {
112+
return baseHashCode();
113+
}
114+
110115
@Override
111116
public Builder toBuilder() {
112117
return new Builder(this);

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/resourcemanager/testing/LocalResourceManagerHelper.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public class LocalResourceManagerHelper {
7070
private static final Set<Character> PERMISSIBLE_PROJECT_NAME_PUNCTUATION =
7171
ImmutableSet.of('-', '\'', '"', ' ', '!');
7272

73-
private HttpServer server;
73+
private final HttpServer server;
7474
private final ConcurrentHashMap<String, Project> projects = new ConcurrentHashMap<>();
7575
private final int port;
7676

@@ -257,7 +257,7 @@ private static Map<String, Object> parseListOptions(String query) {
257257
return options;
258258
}
259259

260-
private static final String checkForProjectErrors(Project project) {
260+
private static String checkForProjectErrors(Project project) {
261261
if (project.getProjectId() == null) {
262262
return "Project ID cannot be empty.";
263263
}
@@ -291,9 +291,9 @@ private static final String checkForProjectErrors(Project project) {
291291
return null;
292292
}
293293

294-
private static final boolean isValidIdOrLabel(String value, int minLength, int maxLength) {
294+
private static boolean isValidIdOrLabel(String value, int minLength, int maxLength) {
295295
for (char c : value.toCharArray()) {
296-
if (c != '-' && !Character.isDigit(c) && (!Character.isLowerCase(c))) {
296+
if (c != '-' && !Character.isDigit(c) && !Character.isLowerCase(c)) {
297297
return false;
298298
}
299299
}
@@ -367,7 +367,7 @@ Response list(Map<String, Object> options) {
367367
}
368368
String[] fields = (String[]) options.get("fields");
369369
for (Project p : projects.values()) {
370-
Boolean includeProject = includeProject(p, filters);
370+
boolean includeProject = includeProject(p, filters);
371371
if (includeProject) {
372372
try {
373373
projectsSerialized.add(jsonFactory.toString(extractFields(p, fields)));

gcloud-java-resourcemanager/src/main/java/com/google/gcloud/spi/DefaultResourceManagerRpc.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public class DefaultResourceManagerRpc implements ResourceManagerRpc {
2626

2727
// see https://cloud.google.com/resource-manager/v1/errors/core_errors
2828
private static final Set<Integer> RETRYABLE_CODES = ImmutableSet.of(503, 500, 429);
29+
private static final Set<String> RETRYABLE_REASONS = ImmutableSet.of("concurrentLimitExceeded",
30+
"limitExceeded", "rateLimitExceeded", "rateLimitExceededUnreg", "servingLimitExceeded",
31+
"userRateLimitExceeded", "userRateLimitExceededUnreg", "variableTermLimitExceeded");
2932

3033
private final Cloudresourcemanager resourceManager;
3134

@@ -51,7 +54,9 @@ private static ResourceManagerException translate(IOException exception) {
5154
}
5255

5356
private static ResourceManagerException translate(GoogleJsonError exception) {
54-
boolean retryable = RETRYABLE_CODES.contains(exception.getCode());
57+
boolean retryable =
58+
RETRYABLE_CODES.contains(exception.getCode()) || (!exception.getErrors().isEmpty()
59+
&& RETRYABLE_REASONS.contains(exception.getErrors().get(0).getReason()));
5560
return new ResourceManagerException(exception.getCode(), exception.getMessage(), retryable);
5661
}
5762

0 commit comments

Comments
 (0)