Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize LocalResourceManagerHelper #497

Merged
merged 1 commit into from
Dec 22, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@ String body() {
private enum Error {
ALREADY_EXISTS(409, "global", "alreadyExists", "ALREADY_EXISTS"),
PERMISSION_DENIED(403, "global", "forbidden", "PERMISSION_DENIED"),
// change failed precondition error code to 412 when #440 is fixed
FAILED_PRECONDITION(400, "global", "failedPrecondition", "FAILED_PRECONDITION"),
// change invalid argument error code to 412 when #440 is fixed
INVALID_ARGUMENT(400, "global", "badRequest", "INVALID_ARGUMENT"),
BAD_REQUEST(400, "global", "badRequest", "BAD_REQUEST"),
INTERNAL_ERROR(500, "global", "internalError", "INTERNAL_ERROR");
Expand Down Expand Up @@ -324,12 +322,9 @@ Response create(Project project) {
}
}

Response delete(String projectId) {
synchronized Response delete(String projectId) {

This comment was marked as spam.

This comment was marked as spam.

Project project = projects.get(projectId);
if (project == null) {
// Currently the service returns 403 Permission Denied when trying to delete a project that
// doesn't exist. Here we mimic this behavior, but this line should be changed to throw a
// 404 Not Found error when the service fixes this (#440).
return Error.PERMISSION_DENIED.response(
"Error when deleting " + projectId + " because the project was not found.");
}
Expand All @@ -343,18 +338,16 @@ Response delete(String projectId) {
}

Response get(String projectId, String[] fields) {
if (!projects.containsKey(projectId)) {
// Currently the service returns 403 Permission Denied when trying to get a project that
// doesn't exist. Here we mimic this behavior, but this line should be changed to throw a
// 404 Not Found error when the service fixes this (#440).
return Error.PERMISSION_DENIED.response("Project " + projectId + " not found.");
}
Project project = projects.get(projectId);
try {
return new Response(HTTP_OK, jsonFactory.toString(extractFields(project, fields)));
} catch (IOException e) {
return Error.INTERNAL_ERROR.response(
"Error when serializing project " + project.getProjectId());
if (project != null) {
try {
return new Response(HTTP_OK, jsonFactory.toString(extractFields(project, fields)));
} catch (IOException e) {
return Error.INTERNAL_ERROR.response(
"Error when serializing project " + project.getProjectId());
}
} else {
return Error.PERMISSION_DENIED.response("Project " + projectId + " not found.");
}
}

Expand Down Expand Up @@ -462,12 +455,9 @@ private static Project extractFields(Project fullProject, String[] fields) {
return project;
}

Response replace(String projectId, Project project) {
synchronized Response replace(String projectId, Project project) {
Project originalProject = projects.get(projectId);
if (originalProject == null) {
// Currently the service returns 403 Permission Denied when trying to replace a project that
// doesn't exist. Here we mimic this behavior, but this line should be changed to throw a
// 404 Not Found error when the service fixes this (#440).
return Error.PERMISSION_DENIED.response(
"Error when replacing " + projectId + " because the project was not found.");
} else if (!originalProject.getLifecycleState().equals("ACTIVE")) {
Expand All @@ -478,23 +468,23 @@ Response replace(String projectId, Project project) {
"The server currently only supports setting the parent once "
+ "and does not allow unsetting it.");
}
originalProject.setName(project.getName());
originalProject.setLabels(project.getLabels());
originalProject.setParent(project.getParent());
project.setProjectId(projectId);
project.setLifecycleState(originalProject.getLifecycleState());
project.setCreateTime(originalProject.getCreateTime());
project.setProjectNumber(originalProject.getProjectNumber());
// replace cannot fail because both this method and removeProject are synchronized
projects.replace(projectId, project);
try {
return new Response(HTTP_OK, jsonFactory.toString(originalProject));
return new Response(HTTP_OK, jsonFactory.toString(project));
} catch (IOException e) {
return Error.INTERNAL_ERROR.response("Error when serializing project " + projectId);
}
}

Response undelete(String projectId) {
synchronized Response undelete(String projectId) {
Project project = projects.get(projectId);
Response response;
if (project == null) {
// Currently the service returns 403 Permission Denied when trying to undelete a project that
// doesn't exist. Here we mimic this behavior, but this line should be changed to throw a
// 404 Not Found error when the service fixes this (#440).
response = Error.PERMISSION_DENIED.response(
"Error when undeleting " + projectId + " because the project was not found.");
} else if (!project.getLifecycleState().equals("DELETE_REQUESTED")) {
Expand Down Expand Up @@ -550,7 +540,7 @@ public void stop() {
*
* @return true if the lifecycle state was successfully updated, false otherwise.
*/
public boolean changeLifecycleState(String projectId, String lifecycleState) {
public synchronized boolean changeLifecycleState(String projectId, String lifecycleState) {
checkArgument(
"ACTIVE".equals(lifecycleState) || "DELETE_REQUESTED".equals(lifecycleState)
|| "DELETE_IN_PROGRESS".equals(lifecycleState),
Expand All @@ -571,7 +561,9 @@ public boolean changeLifecycleState(String projectId, String lifecycleState) {
*
* @return true if the project was successfully deleted, false if the project didn't exist.
*/
public boolean removeProject(String projectId) {
public synchronized boolean removeProject(String projectId) {
// Because this method is synchronized, any code that relies on non-atomic read/write operations
// should not fail if that code is also synchronized.
return projects.remove(checkNotNull(projectId)) != null;
}
}