]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5566 Fix issue when updating a lot of permissions
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 11 Sep 2014 11:51:17 +0000 (13:51 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 11 Sep 2014 11:51:23 +0000 (13:51 +0200)
server/sonar-server/src/main/java/org/sonar/server/db/BaseDao.java
server/sonar-server/src/main/java/org/sonar/server/issue/db/IssueAuthorizationDao.java
server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
server/sonar-server/src/main/java/org/sonar/server/search/DbSynchronizationHandler.java [new file with mode: 0644]

index 00489a934095d96bab5a165071cf1a7253424f1e..bceb86eb7b201a7b0c9eee2695535098e663c262 100644 (file)
@@ -23,12 +23,12 @@ import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import org.apache.ibatis.session.ResultContext;
-import org.apache.ibatis.session.ResultHandler;
 import org.sonar.api.utils.System2;
 import org.sonar.core.persistence.DaoComponent;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.persistence.Dto;
 import org.sonar.server.exceptions.NotFoundException;
+import org.sonar.server.search.DbSynchronizationHandler;
 import org.sonar.server.search.IndexDefinition;
 import org.sonar.server.search.action.DeleteKey;
 import org.sonar.server.search.action.DeleteNestedItem;
@@ -303,13 +303,18 @@ public abstract class BaseDao<MAPPER, DTO extends Dto<KEY>, KEY extends Serializ
 
   // Synchronization methods
 
-  protected ResultHandler getSynchronizationResultHandler(final DbSession session) {
-    return new ResultHandler() {
+  protected DbSynchronizationHandler getSynchronizationResultHandler(final DbSession session) {
+    return new DbSynchronizationHandler() {
       @Override
       public void handleResult(ResultContext resultContext) {
         DTO dto = (DTO) resultContext.getResultObject();
         session.enqueue(new UpsertDto<DTO>(getIndexType(), dto, true));
       }
+
+      @Override
+      public void enqueueCollected() {
+        // Do nothing in this case
+      }
     };
   }
 
@@ -322,7 +327,9 @@ public abstract class BaseDao<MAPPER, DTO extends Dto<KEY>, KEY extends Serializ
   @Override
   public void synchronizeAfter(final DbSession session, Date date) {
     try {
-      session.select(getSynchronizeStatementFQN(), getSynchronizationParams(date), getSynchronizationResultHandler(session));
+      DbSynchronizationHandler handler = getSynchronizationResultHandler(session);
+      session.select(getSynchronizeStatementFQN(), getSynchronizationParams(date), handler);
+      handler.enqueueCollected();
     } catch (Exception e) {
       throw new IllegalStateException(e);
     }
index 2a819600370ad9855dbdc312e0b3ceccbdc23a09..e3d3535163a8b2efb90ea103bc2dac0948fba35e 100644 (file)
@@ -23,7 +23,6 @@ package org.sonar.server.issue.db;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import org.apache.ibatis.session.ResultContext;
-import org.apache.ibatis.session.ResultHandler;
 import org.sonar.api.security.DefaultGroups;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
@@ -32,6 +31,7 @@ import org.sonar.core.issue.db.IssueAuthorizationMapper;
 import org.sonar.core.persistence.DaoComponent;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.server.db.BaseDao;
+import org.sonar.server.search.DbSynchronizationHandler;
 import org.sonar.server.search.IndexDefinition;
 import org.sonar.server.search.action.UpsertDto;
 
@@ -59,10 +59,9 @@ public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, Iss
   }
 
   @Override
-  protected ResultHandler getSynchronizationResultHandler(final DbSession session) {
-    return new ResultHandler() {
+  protected DbSynchronizationHandler getSynchronizationResultHandler(final DbSession session) {
+    return new DbSynchronizationHandler() {
       private final Map<String, IssueAuthorizationDto> authorizationDtoMap = new HashMap<String, IssueAuthorizationDto>();
-      private int count = 0;
 
       @Override
       public void handleResult(ResultContext context) {
@@ -85,14 +84,12 @@ public class IssueAuthorizationDao extends BaseDao<IssueAuthorizationMapper, Iss
           issueAuthorizationDto.addUser(user);
         }
         authorizationDtoMap.put(project, issueAuthorizationDto);
-        count++;
-
-        // TODO this sort of breaks the scrollable RS. Should be inline.
-        // Check if this is the last
-        if (count == context.getResultCount()) {
-          for (IssueAuthorizationDto authorization : authorizationDtoMap.values()) {
-            session.enqueue(new UpsertDto<IssueAuthorizationDto>(getIndexType(), authorization, true));
-          }
+      }
+
+      @Override
+      public void enqueueCollected() {
+        for (IssueAuthorizationDto authorization : authorizationDtoMap.values()) {
+          session.enqueue(new UpsertDto<IssueAuthorizationDto>(getIndexType(), authorization, true));
         }
       }
     };
index f51f45b523f5f346488e0092fbbbd04f5a47e0aa..ef7cbd7566dd6ce0895ea3b314078e717a9e627b 100644 (file)
@@ -275,7 +275,7 @@ public class InternalPermissionService implements ServerComponent {
   }
 
   private void synchronizePermissions() {
-    // The index synchronizer cannot use an existing session, otherwise it's failing with the error : org.apache.ibatis.executor.ExecutorException: Executor was closed
+    // The synchronisation cannot use an existing session, otherwise it's failing with the error : org.apache.ibatis.executor.ExecutorException: Executor was closed
     DbSession session = dbClient.openSession(false);
     try {
       dbClient.issueAuthorizationDao().synchronizeAfter(session, index.get(IssueAuthorizationIndex.class).getLastSynchronization());
diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/DbSynchronizationHandler.java b/server/sonar-server/src/main/java/org/sonar/server/search/DbSynchronizationHandler.java
new file mode 100644 (file)
index 0000000..8e56aa6
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * 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.search;
+
+import org.apache.ibatis.session.ResultContext;
+import org.apache.ibatis.session.ResultHandler;
+
+import javax.annotation.CheckForNull;
+
+public interface DbSynchronizationHandler extends ResultHandler {
+
+  @Override
+  void handleResult(ResultContext context);
+
+  @CheckForNull
+  void enqueueCollected();
+}