You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IndexQueue.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.search;
  21. import org.elasticsearch.action.ActionRequest;
  22. import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
  23. import org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder;
  24. import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
  25. import org.elasticsearch.action.bulk.BulkRequestBuilder;
  26. import org.elasticsearch.action.bulk.BulkResponse;
  27. import org.elasticsearch.action.delete.DeleteRequest;
  28. import org.elasticsearch.action.index.IndexRequest;
  29. import org.elasticsearch.action.update.UpdateRequest;
  30. import org.sonar.api.ServerComponent;
  31. import org.sonar.core.platform.ComponentContainer;
  32. import org.sonar.api.utils.log.Logger;
  33. import org.sonar.api.utils.log.Loggers;
  34. import org.sonar.core.cluster.WorkQueue;
  35. import org.sonar.server.search.action.IndexAction;
  36. import java.util.HashMap;
  37. import java.util.HashSet;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.Set;
  41. import java.util.concurrent.ExecutorService;
  42. import java.util.concurrent.Executors;
  43. import java.util.concurrent.Future;
  44. import java.util.concurrent.TimeUnit;
  45. public class IndexQueue implements ServerComponent, WorkQueue<IndexAction<?>> {
  46. private final SearchClient searchClient;
  47. private final ComponentContainer container;
  48. private static final Logger LOGGER = Loggers.get(IndexQueue.class);
  49. private static final Integer CONCURRENT_NORMALIZATION_FACTOR = 1;
  50. public IndexQueue(SearchClient searchClient, ComponentContainer container) {
  51. this.searchClient = searchClient;
  52. this.container = container;
  53. }
  54. @Override
  55. public void enqueue(List<IndexAction<?>> actions) {
  56. if (actions.isEmpty()) {
  57. return;
  58. }
  59. boolean refreshRequired = false;
  60. Map<String, Index> indexes = getIndexMap();
  61. Set<String> indices = new HashSet<String>();
  62. for (IndexAction action : actions) {
  63. Index index = indexes.get(action.getIndexType());
  64. action.setIndex(index);
  65. if (action.needsRefresh()) {
  66. refreshRequired = true;
  67. indices.add(index.getIndexName());
  68. }
  69. }
  70. BulkRequestBuilder bulkRequestBuilder = searchClient.prepareBulk();
  71. processActionsIntoQueries(bulkRequestBuilder, actions);
  72. if (bulkRequestBuilder.numberOfActions() > 0) {
  73. // execute the request
  74. BulkResponse response = bulkRequestBuilder.setRefresh(false).get();
  75. if (refreshRequired) {
  76. this.refreshRequiredIndex(indices);
  77. }
  78. if (response.hasFailures()) {
  79. throw new IllegalStateException("Errors while indexing stack: " + response.buildFailureMessage());
  80. }
  81. }
  82. }
  83. private void refreshRequiredIndex(Set<String> indices) {
  84. if (!indices.isEmpty()) {
  85. RefreshRequestBuilder refreshRequest = searchClient.prepareRefresh(indices.toArray(new String[indices.size()]))
  86. .setForce(false);
  87. RefreshResponse refreshResponse = refreshRequest.get();
  88. if (refreshResponse.getFailedShards() > 0) {
  89. LOGGER.warn("{} Shard(s) did not refresh", refreshResponse.getFailedShards());
  90. }
  91. }
  92. }
  93. private void processActionsIntoQueries(BulkRequestBuilder bulkRequestBuilder, List<IndexAction<?>> actions) {
  94. try {
  95. boolean hasInlineRefreshRequest = false;
  96. ExecutorService executorService = Executors.newFixedThreadPool(CONCURRENT_NORMALIZATION_FACTOR);
  97. // invokeAll() blocks until ALL tasks submitted to executor complete
  98. List<Future<List<? extends ActionRequest>>> requests = (List) executorService.invokeAll(actions, 20, TimeUnit.MINUTES);
  99. for (Future<List<? extends ActionRequest>> updates : requests) {
  100. for (ActionRequest update : updates.get()) {
  101. if (IndexRequest.class.isAssignableFrom(update.getClass())) {
  102. bulkRequestBuilder.add((IndexRequest) update);
  103. } else if (UpdateRequest.class.isAssignableFrom(update.getClass())) {
  104. bulkRequestBuilder.add((UpdateRequest) update);
  105. } else if (DeleteRequest.class.isAssignableFrom(update.getClass())) {
  106. bulkRequestBuilder.add((DeleteRequest) update);
  107. } else if (RefreshRequest.class.isAssignableFrom(update.getClass())) {
  108. hasInlineRefreshRequest = true;
  109. } else {
  110. throw new IllegalStateException("Un-managed request type: " + update.getClass());
  111. }
  112. }
  113. }
  114. executorService.shutdown();
  115. bulkRequestBuilder.setRefresh(hasInlineRefreshRequest);
  116. } catch (Exception e) {
  117. throw new IllegalStateException("Could not execute normalization for stack", e);
  118. }
  119. }
  120. private Map<String, Index> getIndexMap() {
  121. Map<String, Index> indexes = new HashMap<String, Index>();
  122. for (Index index : container.getComponentsByType(Index.class)) {
  123. indexes.put(index.getIndexType(), index);
  124. }
  125. return indexes;
  126. }
  127. }