Skip to content

Commit

Permalink
SONAR-6834 refactor CeQueue.submit()
Browse files Browse the repository at this point in the history
Simon Brandhof committed Sep 21, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent f6b8882 commit a47f15d
Showing 14 changed files with 224 additions and 230 deletions.
Original file line number Diff line number Diff line change
@@ -31,18 +31,18 @@
*/
public interface CeQueue {
/**
* Build an instance of {@link TaskSubmission} required for {@link #submit(TaskSubmission)}. It allows
* Build an instance of {@link CeTaskSubmit} required for {@link #submit(CeTaskSubmit)}. It allows
* to enforce that task ids are generated by the queue. It's used also for having access
* to the id before submitting the task to the queue.
*/
TaskSubmission prepareSubmit();
CeTaskSubmit.Builder prepareSubmit();

/**
* Submits a task to the queue. The task is processed asynchronously.
* If submits are paused (see {@link #isSubmitPaused()}, then an
* unchecked exception is thrown.
*/
CeTask submit(TaskSubmission submission);
CeTask submit(CeTaskSubmit submission);

/**
* Peek the oldest task in status {@link org.sonar.db.ce.CeQueueDto.Status#PENDING}.
Original file line number Diff line number Diff line change
@@ -20,19 +20,17 @@
package org.sonar.server.computation;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nullable;
import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.ce.CeActivityDto;
import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.computation.monitoring.CEQueueStatus;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;

@@ -59,41 +57,34 @@ public CeQueueImpl(System2 system2, DbClient dbClient, UuidFactory uuidFactory,
}

@Override
public TaskSubmission prepareSubmit() {
return new TaskSubmissionImpl(uuidFactory.create());
public CeTaskSubmit.Builder prepareSubmit() {
return new CeTaskSubmit.Builder(uuidFactory.create());
}

@Override
public CeTask submit(TaskSubmission submission) {
checkArgument(!Strings.isNullOrEmpty(submission.getUuid()));
checkArgument(!Strings.isNullOrEmpty(submission.getType()));
checkArgument(submission instanceof TaskSubmissionImpl);
public CeTask submit(CeTaskSubmit submission) {
checkState(!submitPaused.get(), "Compute Engine does not currently accept new tasks");

CeTask task = new CeTask(submission);
DbSession dbSession = dbClient.openSession(false);
try {
CeQueueDto dto = createQueueDtoForSubmit(task);
CeQueueDto dto = new CeQueueDto();
dto.setUuid(submission.getUuid());
dto.setTaskType(submission.getType());
dto.setComponentUuid(submission.getComponentUuid());
dto.setStatus(CeQueueDto.Status.PENDING);
dto.setSubmitterLogin(submission.getSubmitterLogin());
dto.setStartedAt(null);
dbClient.ceQueueDao().insert(dbSession, dto);
CeTask task = loadTask(dbSession, dto);
dbSession.commit();
queueStatus.addReceived();
return task;

} finally {
dbClient.closeSession(dbSession);
}
}

private CeQueueDto createQueueDtoForSubmit(CeTask task) {
CeQueueDto dto = new CeQueueDto();
dto.setUuid(task.getUuid());
dto.setTaskType(task.getType());
dto.setComponentUuid(task.getComponentUuid());
dto.setStatus(CeQueueDto.Status.PENDING);
dto.setSubmitterLogin(task.getSubmitterLogin());
dto.setStartedAt(null);
return dto;
}

@Override
public Optional<CeTask> peek() {
if (peekPaused.get()) {
@@ -104,8 +95,8 @@ public Optional<CeTask> peek() {
Optional<CeQueueDto> dto = dbClient.ceQueueDao().peek(dbSession);
CeTask task = null;
if (dto.isPresent()) {
task = loadTask(dbSession, dto.get());
queueStatus.addInProgress();
task = new CeTask(dto.get());
}
return Optional.fromNullable(task);

@@ -114,6 +105,23 @@ public Optional<CeTask> peek() {
}
}

private CeTask loadTask(DbSession dbSession, CeQueueDto dto) {
CeTask.Builder builder = new CeTask.Builder();
builder.setUuid(dto.getUuid());
builder.setType(dto.getTaskType());
builder.setSubmitterLogin(dto.getSubmitterLogin());
String componentUuid = dto.getComponentUuid();
if (componentUuid != null) {
builder.setComponentUuid(componentUuid);
Optional<ComponentDto> component = dbClient.componentDao().selectByUuid(dbSession, componentUuid);
if (component.isPresent()) {
builder.setComponentKey(component.get().getKey());
builder.setComponentName(component.get().name());
}
}
return builder.build();
}

@Override
public boolean cancel(String taskUuid) {
DbSession dbSession = dbClient.openSession(false);
@@ -131,9 +139,10 @@ public boolean cancel(String taskUuid) {
}

void cancel(DbSession dbSession, CeQueueDto q) {
CeTask task = loadTask(dbSession, q);
CeActivityDto activityDto = new CeActivityDto(q);
activityDto.setStatus(CeActivityDto.Status.CANCELED);
remove(dbSession, new CeTask(q), q, activityDto);
remove(dbSession, task, q, activityDto);
}

@Override
@@ -233,53 +242,4 @@ public void resumePeek() {
public boolean isPeekPaused() {
return peekPaused.get();
}

private static class TaskSubmissionImpl implements TaskSubmission {
private final String uuid;
private String type;
private String componentUuid;
private String submitterLogin;

private TaskSubmissionImpl(String uuid) {
this.uuid = uuid;
}

@Override
public String getUuid() {
return uuid;
}

@Override
public String getType() {
return type;
}

@Override
public TaskSubmission setType(@Nullable String s) {
this.type = s;
return this;
}

@Override
public String getComponentUuid() {
return componentUuid;
}

@Override
public TaskSubmission setComponentUuid(@Nullable String s) {
this.componentUuid = s;
return this;
}

@Override
public String getSubmitterLogin() {
return submitterLogin;
}

@Override
public TaskSubmission setSubmitterLogin(@Nullable String s) {
this.submitterLogin = s;
return this;
}
}
}
Original file line number Diff line number Diff line change
@@ -23,8 +23,8 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.sonar.db.ce.CeQueueDto;

import static com.google.common.base.Strings.emptyToNull;
import static java.util.Objects.requireNonNull;

@Immutable
@@ -33,21 +33,17 @@ public class CeTask {
private final String type;
private final String uuid;
private final String componentUuid;
private final String componentKey;
private final String componentName;
private final String submitterLogin;

public CeTask(String uuid, String type, @Nullable String componentUuid, @Nullable String submitterLogin) {
this.uuid = requireNonNull(uuid);
this.type = requireNonNull(type);
this.componentUuid = componentUuid;
this.submitterLogin = submitterLogin;
}

CeTask(TaskSubmission submit) {
this(submit.getUuid(), submit.getType(), submit.getComponentUuid(), submit.getSubmitterLogin());
}

CeTask(CeQueueDto dto) {
this(dto.getUuid(), dto.getTaskType(), dto.getComponentUuid(), dto.getSubmitterLogin());
private CeTask(Builder builder) {
this.uuid = requireNonNull(emptyToNull(builder.uuid));
this.type = requireNonNull(emptyToNull(builder.type));
this.componentUuid = emptyToNull(builder.componentUuid);
this.componentKey = emptyToNull(builder.componentKey);
this.componentName = emptyToNull(builder.componentName);
this.submitterLogin = emptyToNull(builder.submitterLogin);
}

public String getUuid() {
@@ -63,6 +59,16 @@ public String getComponentUuid() {
return componentUuid;
}

@CheckForNull
public String getComponentKey() {
return componentKey;
}

@CheckForNull
public String getComponentName() {
return componentName;
}

@CheckForNull
public String getSubmitterLogin() {
return submitterLogin;
@@ -94,4 +100,47 @@ public boolean equals(@Nullable Object o) {
public int hashCode() {
return uuid.hashCode();
}

public static final class Builder {
private String uuid;
private String type;
private String componentUuid;
private String componentKey;
private String componentName;
private String submitterLogin;

public Builder setUuid(String uuid) {
this.uuid = uuid;
return this;
}

public Builder setType(String type) {
this.type = type;
return this;
}

public Builder setComponentUuid(String componentUuid) {
this.componentUuid = componentUuid;
return this;
}

public Builder setComponentKey(@Nullable String s) {
this.componentKey = s;
return this;
}

public Builder setComponentName(@Nullable String s) {
this.componentName = s;
return this;
}

public Builder setSubmitterLogin(@Nullable String s) {
this.submitterLogin = s;
return this;
}

public CeTask build() {
return new CeTask(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation;

import java.util.Objects;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import static com.google.common.base.Strings.emptyToNull;

@Immutable
public final class CeTaskSubmit {

private final String uuid;
private final String type;
private final String componentUuid;
private final String submitterLogin;

private CeTaskSubmit(Builder builder) {
this.uuid = Objects.requireNonNull(emptyToNull(builder.uuid));
this.type = Objects.requireNonNull(emptyToNull(builder.type));
this.componentUuid = emptyToNull(builder.componentUuid);
this.submitterLogin = emptyToNull(builder.submitterLogin);
}

public String getType() {
return type;
}

public String getUuid() {
return uuid;
}

@CheckForNull
public String getComponentUuid() {
return componentUuid;
}

@CheckForNull
public String getSubmitterLogin() {
return submitterLogin;
}

public static final class Builder {
private final String uuid;
private String type;
private String componentUuid;
private String submitterLogin;

Builder(String uuid) {
this.uuid = uuid;
}

public String getUuid() {
return uuid;
}

public Builder setType(String s) {
this.type = s;
return this;
}

public Builder setComponentUuid(@Nullable String s) {
this.componentUuid = s;
return this;
}

public Builder setSubmitterLogin(@Nullable String s) {
this.submitterLogin = s;
return this;
}

public CeTaskSubmit build() {
return new CeTaskSubmit(this);
}
}
}
Loading

0 comments on commit a47f15d

Please sign in to comment.