]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8985 SONAR-9040 add CE_ACTIVITY.WORKER_UUID and EXECUTION_COUNT
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 27 Mar 2017 14:54:13 +0000 (16:54 +0200)
committerEric Hartmann <hartmann.eric@gmail.Com>
Thu, 27 Apr 2017 07:23:18 +0000 (09:23 +0200)
and populate them

13 files changed:
server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeActivityDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeActivityMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/ce/CeActivityDaoTest.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCount.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullable.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCountTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/DbVersion64Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullableTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCountTest/ce_activity.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullableTest/ce_activity.sql [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/computation/queue/InternalCeQueueImplTest.java

index d721494387df2c18e03e59f351c2c7dc6896b9b5..b24a231643a942ed0a5265028bebfd602d5acade 100644 (file)
@@ -583,6 +583,8 @@ CREATE TABLE "CE_ACTIVITY" (
   "IS_LAST" BOOLEAN NOT NULL,
   "IS_LAST_KEY" VARCHAR(55) NOT NULL,
   "SUBMITTER_LOGIN" VARCHAR(255) NULL,
+  "WORKER_UUID" VARCHAR(40) NULL,
+  "EXECUTION_COUNT" INTEGER NOT NULL,
   "SUBMITTED_AT" BIGINT NOT NULL,
   "STARTED_AT" BIGINT NULL,
   "EXECUTED_AT" BIGINT NULL,
index 67dcc2b376c7680d33cf0844d6edd09f949f3e87..0183618a348fbe195da8382872fe0172377b1ad7 100644 (file)
@@ -19,7 +19,6 @@
  */
 package org.sonar.db.ce;
 
-import com.google.common.base.MoreObjects;
 import com.google.common.base.Strings;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
@@ -44,6 +43,8 @@ public class CeActivityDto {
   private boolean isLast;
   private String isLastKey;
   private String submitterLogin;
+  private String workerUuid;
+  private int executionCount;
   private long submittedAt;
   private Long startedAt;
   private Long executedAt;
@@ -86,6 +87,8 @@ public class CeActivityDto {
     this.componentUuid = queueDto.getComponentUuid();
     this.isLastKey = format("%s%s", taskType, Strings.nullToEmpty(componentUuid));
     this.submitterLogin = queueDto.getSubmitterLogin();
+    this.workerUuid = queueDto.getWorkerUuid();
+    this.executionCount = queueDto.getExecutionCount();
     this.submittedAt = queueDto.getCreatedAt();
     this.startedAt = queueDto.getStartedAt();
   }
@@ -215,6 +218,25 @@ public class CeActivityDto {
     return this;
   }
 
+  @CheckForNull
+  public String getWorkerUuid() {
+    return workerUuid;
+  }
+
+  public CeActivityDto setWorkerUuid(String workerUuid) {
+    this.workerUuid = workerUuid;
+    return this;
+  }
+
+  public int getExecutionCount() {
+    return executionCount;
+  }
+
+  public CeActivityDto setExecutionCount(int executionCount) {
+    this.executionCount = executionCount;
+    return this;
+  }
+
   @CheckForNull
   public String getErrorMessage() {
     return errorMessage;
@@ -257,24 +279,26 @@ public class CeActivityDto {
 
   @Override
   public String toString() {
-    return MoreObjects.toStringHelper(this)
-      .add("uuid", uuid)
-      .add("taskType", taskType)
-      .add("componentUuid", componentUuid)
-      .add("analysisUuid", analysisUuid)
-      .add("status", status)
-      .add("isLast", isLast)
-      .add("isLastKey", isLastKey)
-      .add("submitterLogin", submitterLogin)
-      .add("submittedAt", submittedAt)
-      .add("startedAt", startedAt)
-      .add("executedAt", executedAt)
-      .add("createdAt", createdAt)
-      .add("updatedAt", updatedAt)
-      .add("executionTimeMs", executionTimeMs)
-      .add("errorMessage", errorMessage)
-      .add("errorStacktrace", errorStacktrace)
-      .add("hasScannerContext", hasScannerContext)
-      .toString();
+    return "CeActivityDto{" +
+      "uuid='" + uuid + '\'' +
+      ", componentUuid='" + componentUuid + '\'' +
+      ", analysisUuid='" + analysisUuid + '\'' +
+      ", status=" + status +
+      ", taskType='" + taskType + '\'' +
+      ", isLast=" + isLast +
+      ", isLastKey='" + isLastKey + '\'' +
+      ", submitterLogin='" + submitterLogin + '\'' +
+      ", workerUuid='" + workerUuid + '\'' +
+      ", executionCount=" + executionCount +
+      ", submittedAt=" + submittedAt +
+      ", startedAt=" + startedAt +
+      ", executedAt=" + executedAt +
+      ", createdAt=" + createdAt +
+      ", updatedAt=" + updatedAt +
+      ", executionTimeMs=" + executionTimeMs +
+      ", errorMessage='" + errorMessage + '\'' +
+      ", errorStacktrace='" + errorStacktrace + '\'' +
+      ", hasScannerContext=" + hasScannerContext +
+      '}';
   }
 }
index 6324e2fca4367de1f94e77a707bb9a7feabce017..4722e7be458dd18f349127bc963ee6d614a2fac4 100644 (file)
@@ -24,6 +24,8 @@
     ca.status as status,
     ca.submitter_login as submitterLogin,
     ca.submitted_at as submittedAt,
+    ca.worker_uuid as workerUuid,
+    ca.execution_count as executionCount,
     ca.started_at as startedAt,
     ca.executed_at as executedAt,
     ca.created_at as createdAt,
       is_last_key,
       submitter_login,
       submitted_at,
+      worker_uuid,
+      execution_count,
       started_at,
       executed_at,
       created_at,
       #{isLastKey,jdbcType=VARCHAR},
       #{submitterLogin,jdbcType=VARCHAR},
       #{submittedAt,jdbcType=BIGINT},
+      #{workerUuid,jdbcType=VARCHAR},
+      #{executionCount,jdbcType=INTEGER},
       #{startedAt,jdbcType=BIGINT},
       #{executedAt,jdbcType=BIGINT},
       #{createdAt,jdbcType=BIGINT},
index 050043eab6a9a185891f9edfa5d2913359b7ab01..2bc2840a7c3df39f32572e53345703752527e018 100644 (file)
@@ -64,9 +64,11 @@ public class CeActivityDaoTest {
     assertThat(dto.getComponentUuid()).isEqualTo("PROJECT_1");
     assertThat(dto.getStatus()).isEqualTo(CeActivityDto.Status.SUCCESS);
     assertThat(dto.getSubmitterLogin()).isEqualTo("henri");
+    assertThat(dto.getSubmittedAt()).isEqualTo(1_300_000_000_000L);
+    assertThat(dto.getWorkerUuid()).isEqualTo("worker uuid");
+    assertThat(dto.getExecutionCount()).isEqualTo(42);
     assertThat(dto.getIsLast()).isTrue();
     assertThat(dto.getIsLastKey()).isEqualTo("REPORTPROJECT_1");
-    assertThat(dto.getSubmittedAt()).isEqualTo(1_300_000_000_000L);
     assertThat(dto.getCreatedAt()).isEqualTo(1_450_000_000_000L);
     assertThat(dto.getStartedAt()).isEqualTo(1_500_000_000_000L);
     assertThat(dto.getExecutedAt()).isEqualTo(1_500_000_000_500L);
@@ -346,6 +348,8 @@ public class CeActivityDaoTest {
     queueDto.setTaskType(type);
     queueDto.setComponentUuid(componentUuid);
     queueDto.setSubmitterLogin("henri");
+    queueDto.setWorkerUuid("worker uuid");
+    queueDto.setExecutionCount(42);
     queueDto.setCreatedAt(1_300_000_000_000L);
 
     CeActivityDto dto = new CeActivityDto(queueDto);
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCount.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCount.java
new file mode 100644 (file)
index 0000000..9211c93
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.platform.db.migration.version.v64;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.IntegerColumnDef.newIntegerColumnDefBuilder;
+
+public class AddCeActivityWorkerUuidAndExecutionCount extends DdlChange {
+
+  private static final String TABLE_CE_ACTIVITY = "ce_activity";
+
+  public AddCeActivityWorkerUuidAndExecutionCount(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute(new AddColumnsBuilder(getDialect(), TABLE_CE_ACTIVITY)
+      .addColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
+        .setColumnName("worker_uuid")
+        .setLimit(VarcharColumnDef.UUID_SIZE)
+        .setIsNullable(true)
+        .build())
+      .addColumn(newIntegerColumnDefBuilder()
+        .setColumnName("execution_count")
+        .setIsNullable(true)
+        .build())
+      .build());
+  }
+}
index 4c697448cef2bb9318413f9a2f0a6cec54c25366..d2fa0263fd8bba9c7c866fbbe6251526f9cd19f5 100644 (file)
@@ -60,5 +60,7 @@ public class DbVersion64 implements DbVersion {
     ;
       .add(1628, "Add columns CE_QUEUE.WORKER_UUID and EXECUTION_COUNT", AddCeQueueWorkerUuidAndExecutionCount.class);
       .add(1629, "Make CE_QUEUE.EXECUTION_COUNT not nullable", MakeCeQueueExecutionCountNotNullable.class);
+      .add(1630, "Add columns CE_ACTIVITY.WORKER_UUID and EXECUTION_COUNT", AddCeActivityWorkerUuidAndExecutionCount.class)
+      .add(1631, "Make columns CE_ACTIVITY.EXECUTION_COUNT not nullable", MakeCeActivityExecutionCountNotNullable.class);
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullable.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullable.java
new file mode 100644 (file)
index 0000000..f3cad04
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.platform.db.migration.version.v64;
+
+import java.sql.SQLException;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.sql.AlterColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import static org.sonar.server.platform.db.migration.def.IntegerColumnDef.newIntegerColumnDefBuilder;
+
+public class MakeCeActivityExecutionCountNotNullable extends DdlChange {
+
+  private static final String TABLE_CE_ACTIVITY = "ce_activity";
+
+  public MakeCeActivityExecutionCountNotNullable(Database db) {
+    super(db);
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    context.execute("update ce_activity set execution_count = 0 where execution_count is null and status = 'CANCELED'");
+    context.execute("update ce_activity set execution_count = 1 where execution_count is null and status in ('SUCCESS', 'FAILED')");
+
+    context.execute(new AlterColumnsBuilder(getDialect(), TABLE_CE_ACTIVITY)
+      .updateColumn(newIntegerColumnDefBuilder()
+        .setColumnName("execution_count")
+        .setIsNullable(false)
+        .build())
+      .build());
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCountTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCountTest.java
new file mode 100644 (file)
index 0000000..4b49583
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.platform.db.migration.version.v64;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+public class AddCeActivityWorkerUuidAndExecutionCountTest {
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(AddCeActivityWorkerUuidAndExecutionCountTest.class, "ce_activity.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private AddCeActivityWorkerUuidAndExecutionCount underTest = new AddCeActivityWorkerUuidAndExecutionCount(db.database());
+
+  @Test
+  public void execute_adds_columns_worker_uuid_and_processing_count() throws SQLException {
+    underTest.execute();
+
+    db.assertColumnDefinition("ce_activity", "worker_uuid", Types.VARCHAR, 40, true);
+    db.assertColumnDefinition("ce_activity", "execution_count", Types.INTEGER, null, true);
+  }
+
+  @Test
+  public void execute_is_not_reentreant() throws SQLException {
+    underTest.execute();
+
+    expectedException.expect(IllegalStateException.class);
+    expectedException.expectMessage("Fail to execute");
+
+    underTest.execute();
+  }
+}
index e847aad7636f3a02e5af0a1500f0cd1af368458d..cd0e721c77b7597d6ad84cb638e809584688f393 100644 (file)
@@ -35,7 +35,7 @@ public class DbVersion64Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 29);
+    verifyMigrationCount(underTest, 31);
   }
 }
 
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullableTest.java
new file mode 100644 (file)
index 0000000..770627d
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2017 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program 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.
+ *
+ * This program 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.platform.db.migration.version.v64;
+
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.List;
+import java.util.Random;
+import java.util.stream.Stream;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.core.util.stream.MoreCollectors;
+import org.sonar.db.CoreDbTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class MakeCeActivityExecutionCountNotNullableTest {
+
+  private static final String TABLE_CE_ACTIVITY = "ce_activity";
+
+  @Rule
+  public CoreDbTester db = CoreDbTester.createForSchema(MakeCeActivityExecutionCountNotNullableTest.class, "ce_activity.sql");
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  private MakeCeActivityExecutionCountNotNullable underTest = new MakeCeActivityExecutionCountNotNullable(db.database());
+
+  @Test
+  public void execute_makes_column_execution_count_not_nullable_when_table_is_empty() throws SQLException {
+    underTest.execute();
+
+    verifyColumnDefinition();
+  }
+
+  @Test
+  public void execute_set_column_execution_count_to_0_or_1_and_not_nullable_depending_on_status_of_the_task() throws SQLException {
+    insertCeActivity("u1", Status.SUCCESS);
+    insertCeActivity("u2", Status.FAILED);
+    insertCeActivity("u3", Status.CANCELED);
+
+    underTest.execute();
+
+    verifyColumnDefinition();
+    assertThat(getUuidsForExecutionCount(0)).containsOnly("u3");
+    assertThat(getUuidsForExecutionCount(1)).containsOnly("u1", "u2");
+  }
+
+  private List<Object> getUuidsForExecutionCount(int executionCount) {
+    return db.select("select uuid as \"UUID\" from ce_activity where execution_count=" + executionCount)
+      .stream()
+      .flatMap(row -> Stream.of(row.get("UUID")))
+      .collect(MoreCollectors.toList());
+  }
+
+  private void verifyColumnDefinition() {
+    db.assertColumnDefinition(TABLE_CE_ACTIVITY, "execution_count", Types.INTEGER, null, false);
+  }
+
+  private void insertCeActivity(String uuid, Status status) {
+    db.executeInsert(TABLE_CE_ACTIVITY,
+      "UUID", uuid,
+      "TASK_TYPE", uuid + "_type",
+      "STATUS", status.name(),
+      "IS_LAST", new Random().nextBoolean() + "",
+      "IS_LAST_KEY", "key",
+      "SUBMITTED_AT", new Random().nextLong() + "",
+      "CREATED_AT", new Random().nextLong() + "",
+      "UPDATED_AT", new Random().nextLong() + "");
+  }
+
+  public enum Status {
+    SUCCESS, FAILED, CANCELED
+  }
+
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCountTest/ce_activity.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/AddCeActivityWorkerUuidAndExecutionCountTest/ce_activity.sql
new file mode 100644 (file)
index 0000000..512e00b
--- /dev/null
@@ -0,0 +1,23 @@
+CREATE TABLE "CE_ACTIVITY" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "UUID" VARCHAR(40) NOT NULL,
+  "TASK_TYPE" VARCHAR(15) NOT NULL,
+  "COMPONENT_UUID" VARCHAR(40) NULL,
+  "ANALYSIS_UUID" VARCHAR(50) NULL,
+  "STATUS" VARCHAR(15) NOT NULL,
+  "IS_LAST" BOOLEAN NOT NULL,
+  "IS_LAST_KEY" VARCHAR(55) NOT NULL,
+  "SUBMITTER_LOGIN" VARCHAR(255) NULL,
+  "SUBMITTED_AT" BIGINT NOT NULL,
+  "STARTED_AT" BIGINT NULL,
+  "EXECUTED_AT" BIGINT NULL,
+  "CREATED_AT" BIGINT NOT NULL,
+  "UPDATED_AT" BIGINT NOT NULL,
+  "EXECUTION_TIME_MS" BIGINT NULL,
+  "ERROR_MESSAGE" VARCHAR(1000),
+  "ERROR_STACKTRACE" CLOB(2147483647)
+);
+CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID");
+CREATE INDEX "CE_ACTIVITY_COMPONENT_UUID" ON "CE_ACTIVITY" ("COMPONENT_UUID");
+CREATE INDEX "CE_ACTIVITY_ISLASTKEY" ON "CE_ACTIVITY" ("IS_LAST_KEY");
+CREATE INDEX "CE_ACTIVITY_ISLAST_STATUS" ON "CE_ACTIVITY" ("IS_LAST", "STATUS");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullableTest/ce_activity.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v64/MakeCeActivityExecutionCountNotNullableTest/ce_activity.sql
new file mode 100644 (file)
index 0000000..02a14de
--- /dev/null
@@ -0,0 +1,25 @@
+CREATE TABLE "CE_ACTIVITY" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "UUID" VARCHAR(40) NOT NULL,
+  "TASK_TYPE" VARCHAR(15) NOT NULL,
+  "COMPONENT_UUID" VARCHAR(40) NULL,
+  "ANALYSIS_UUID" VARCHAR(50) NULL,
+  "STATUS" VARCHAR(15) NOT NULL,
+  "IS_LAST" BOOLEAN NOT NULL,
+  "IS_LAST_KEY" VARCHAR(55) NOT NULL,
+  "SUBMITTER_LOGIN" VARCHAR(255) NULL,
+  "WORKER_UUID" VARCHAR(40) NULL,
+  "EXECUTION_COUNT" INTEGER NULL,
+  "SUBMITTED_AT" BIGINT NOT NULL,
+  "STARTED_AT" BIGINT NULL,
+  "EXECUTED_AT" BIGINT NULL,
+  "CREATED_AT" BIGINT NOT NULL,
+  "UPDATED_AT" BIGINT NOT NULL,
+  "EXECUTION_TIME_MS" BIGINT NULL,
+  "ERROR_MESSAGE" VARCHAR(1000),
+  "ERROR_STACKTRACE" CLOB(2147483647)
+);
+CREATE UNIQUE INDEX "CE_ACTIVITY_UUID" ON "CE_ACTIVITY" ("UUID");
+CREATE INDEX "CE_ACTIVITY_COMPONENT_UUID" ON "CE_ACTIVITY" ("COMPONENT_UUID");
+CREATE INDEX "CE_ACTIVITY_ISLASTKEY" ON "CE_ACTIVITY" ("IS_LAST_KEY");
+CREATE INDEX "CE_ACTIVITY_ISLAST_STATUS" ON "CE_ACTIVITY" ("IS_LAST", "STATUS");
index 519cb5a752a1f60028379b29b4f9e7e8500de476..6b4c2c29cb70db9cca78e798bcf6a4c1a430ce34 100644 (file)
@@ -210,6 +210,27 @@ public class InternalCeQueueImplTest {
     assertThat(activityDto.get().getErrorStacktrace()).isEqualToIgnoringWhitespace(stacktraceToString(error));
   }
 
+  @Test
+  public void remove_copies_executionCount_and_workerUuid() {
+    dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+      .setUuid("uuid")
+      .setTaskType("foo")
+      .setStatus(CeQueueDto.Status.PENDING)
+      .setWorkerUuid("Dustin")
+      .setExecutionCount(2));
+    dbTester.commit();
+
+    underTest.remove(new CeTask.Builder()
+      .setOrganizationUuid("foo")
+      .setUuid("uuid")
+      .setType("bar")
+      .build(), CeActivityDto.Status.SUCCESS, null, null);
+
+    CeActivityDto dto = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "uuid").get();
+    assertThat(dto.getExecutionCount()).isEqualTo(2);
+    assertThat(dto.getWorkerUuid()).isEqualTo("Dustin");
+  }
+
   private static String stacktraceToString(Throwable error) {
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     error.printStackTrace(new PrintStream(out));
@@ -241,6 +262,23 @@ public class InternalCeQueueImplTest {
     assertThat(peek.isPresent()).isFalse();
   }
 
+  @Test
+  public void peek_increases_executionCount_and_override_workerUuid_to_argument() {
+    dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+      .setUuid("uuid")
+      .setTaskType("foo")
+      .setStatus(CeQueueDto.Status.PENDING)
+      .setWorkerUuid("must be overriden")
+      .setExecutionCount(2));
+    dbTester.commit();
+
+    underTest.peek(WORKER_UUID_1);
+
+    CeQueueDto ceQueueDto = dbTester.getDbClient().ceQueueDao().selectByUuid(session, "uuid").get();
+    assertThat(ceQueueDto.getWorkerUuid()).isEqualTo(WORKER_UUID_1);
+    assertThat(ceQueueDto.getExecutionCount()).isEqualTo(3);
+  }
+
   @Test
   public void peek_nothing_if_paused() throws Exception {
     submit(CeTaskTypes.REPORT, "PROJECT_1");
@@ -265,6 +303,23 @@ public class InternalCeQueueImplTest {
     assertThat(activity.get().getStatus()).isEqualTo(CeActivityDto.Status.CANCELED);
   }
 
+  @Test
+  public void cancel_copies_executionCount_and_workerUuid() {
+    dbTester.getDbClient().ceQueueDao().insert(session, new CeQueueDto()
+      .setUuid("uuid")
+      .setTaskType("foo")
+      .setStatus(CeQueueDto.Status.PENDING)
+      .setWorkerUuid("Dustin")
+      .setExecutionCount(2));
+    dbTester.commit();
+
+    underTest.cancel("uuid");
+
+    CeActivityDto dto = dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), "uuid").get();
+    assertThat(dto.getExecutionCount()).isEqualTo(2);
+    assertThat(dto.getWorkerUuid()).isEqualTo("Dustin");
+  }
+
   @Test
   public void fail_to_cancel_if_in_progress() throws Exception {
     expectedException.expect(IllegalStateException.class);