Skip to content

Commit

Permalink
SONAR-6834 refactor CeQueue.submit()
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Brandhof committed Sep 21, 2015
1 parent f6b8882 commit a47f15d
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 230 deletions.
Expand Up @@ -31,18 +31,18 @@
*/ */
public interface CeQueue { 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 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. * 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. * Submits a task to the queue. The task is processed asynchronously.
* If submits are paused (see {@link #isSubmitPaused()}, then an * If submits are paused (see {@link #isSubmitPaused()}, then an
* unchecked exception is thrown. * 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}. * Peek the oldest task in status {@link org.sonar.db.ce.CeQueueDto.Status#PENDING}.
Expand Down
Expand Up @@ -20,19 +20,17 @@
package org.sonar.server.computation; package org.sonar.server.computation;


import com.google.common.base.Optional; import com.google.common.base.Optional;
import com.google.common.base.Strings;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nullable;
import org.sonar.api.server.ServerSide; import org.sonar.api.server.ServerSide;
import org.sonar.api.utils.System2; import org.sonar.api.utils.System2;
import org.sonar.core.util.UuidFactory; import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbClient; import org.sonar.db.DbClient;
import org.sonar.db.DbSession; import org.sonar.db.DbSession;
import org.sonar.db.ce.CeActivityDto; import org.sonar.db.ce.CeActivityDto;
import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeQueueDto;
import org.sonar.db.component.ComponentDto;
import org.sonar.server.computation.monitoring.CEQueueStatus; 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 com.google.common.base.Preconditions.checkState;
import static java.lang.String.format; import static java.lang.String.format;


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


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


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


CeTask task = new CeTask(submission);
DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
try { 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); dbClient.ceQueueDao().insert(dbSession, dto);
CeTask task = loadTask(dbSession, dto);
dbSession.commit(); dbSession.commit();
queueStatus.addReceived(); queueStatus.addReceived();
return task; return task;

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


Expand All @@ -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 @Override
public boolean cancel(String taskUuid) { public boolean cancel(String taskUuid) {
DbSession dbSession = dbClient.openSession(false); DbSession dbSession = dbClient.openSession(false);
Expand All @@ -131,9 +139,10 @@ public boolean cancel(String taskUuid) {
} }


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


@Override @Override
Expand Down Expand Up @@ -233,53 +242,4 @@ public void resumePeek() {
public boolean isPeekPaused() { public boolean isPeekPaused() {
return peekPaused.get(); 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;
}
}
} }
Expand Up @@ -23,8 +23,8 @@
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable; 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; import static java.util.Objects.requireNonNull;


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


public CeTask(String uuid, String type, @Nullable String componentUuid, @Nullable String submitterLogin) { private CeTask(Builder builder) {
this.uuid = requireNonNull(uuid); this.uuid = requireNonNull(emptyToNull(builder.uuid));
this.type = requireNonNull(type); this.type = requireNonNull(emptyToNull(builder.type));
this.componentUuid = componentUuid; this.componentUuid = emptyToNull(builder.componentUuid);
this.submitterLogin = submitterLogin; this.componentKey = emptyToNull(builder.componentKey);
} this.componentName = emptyToNull(builder.componentName);

this.submitterLogin = emptyToNull(builder.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());
} }


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


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

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

@CheckForNull @CheckForNull
public String getSubmitterLogin() { public String getSubmitterLogin() {
return submitterLogin; return submitterLogin;
Expand Down Expand Up @@ -94,4 +100,47 @@ public boolean equals(@Nullable Object o) {
public int hashCode() { public int hashCode() {
return uuid.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);
}
}
} }
@@ -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);
}
}
}

0 comments on commit a47f15d

Please sign in to comment.