PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/elasticsearch/src/main/java/org/elasticsearch/indices/memory/IndexingMemoryBufferController.java

https://github.com/medcl/elasticsearch
Java | 236 lines | 176 code | 33 blank | 27 comment | 39 complexity | ae00b04398b525ba201767153aba499b MD5 | raw file
  1. /*
  2. * Licensed to Elastic Search and Shay Banon under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. Elastic Search licenses this
  6. * file to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.elasticsearch.indices.memory;
  20. import org.elasticsearch.ElasticSearchException;
  21. import org.elasticsearch.common.collect.Maps;
  22. import org.elasticsearch.common.component.AbstractLifecycleComponent;
  23. import org.elasticsearch.common.inject.Inject;
  24. import org.elasticsearch.common.settings.Settings;
  25. import org.elasticsearch.common.unit.ByteSizeUnit;
  26. import org.elasticsearch.common.unit.ByteSizeValue;
  27. import org.elasticsearch.common.unit.TimeValue;
  28. import org.elasticsearch.index.engine.Engine;
  29. import org.elasticsearch.index.service.IndexService;
  30. import org.elasticsearch.index.shard.ShardId;
  31. import org.elasticsearch.index.shard.service.IndexShard;
  32. import org.elasticsearch.index.shard.service.InternalIndexShard;
  33. import org.elasticsearch.index.translog.Translog;
  34. import org.elasticsearch.indices.IndicesLifecycle;
  35. import org.elasticsearch.indices.IndicesService;
  36. import org.elasticsearch.monitor.jvm.JvmInfo;
  37. import org.elasticsearch.threadpool.ThreadPool;
  38. import java.util.Map;
  39. import java.util.concurrent.ScheduledFuture;
  40. /**
  41. * @author kimchy (shay.banon)
  42. */
  43. public class IndexingMemoryBufferController extends AbstractLifecycleComponent<IndexingMemoryBufferController> {
  44. private final ThreadPool threadPool;
  45. private final IndicesService indicesService;
  46. private final ByteSizeValue indexingBuffer;
  47. private final ByteSizeValue minShardIndexBufferSize;
  48. private final ByteSizeValue maxShardIndexBufferSize;
  49. private final TimeValue inactiveTime;
  50. private final TimeValue interval;
  51. private final Listener listener = new Listener();
  52. private final Map<ShardId, ShardIndexingStatus> shardsIndicesStatus = Maps.newHashMap();
  53. private volatile ScheduledFuture scheduler;
  54. private final Object mutex = new Object();
  55. @Inject public IndexingMemoryBufferController(Settings settings, ThreadPool threadPool, IndicesService indicesService) {
  56. super(settings);
  57. this.threadPool = threadPool;
  58. this.indicesService = indicesService;
  59. ByteSizeValue indexingBuffer;
  60. String indexingBufferSetting = componentSettings.get("index_buffer_size", "10%");
  61. if (indexingBufferSetting.endsWith("%")) {
  62. double percent = Double.parseDouble(indexingBufferSetting.substring(0, indexingBufferSetting.length() - 1));
  63. indexingBuffer = new ByteSizeValue((long) (((double) JvmInfo.jvmInfo().mem().heapMax().bytes()) * (percent / 100)));
  64. ByteSizeValue minIndexingBuffer = componentSettings.getAsBytesSize("min_index_buffer_size", new ByteSizeValue(48, ByteSizeUnit.MB));
  65. ByteSizeValue maxIndexingBuffer = componentSettings.getAsBytesSize("max_index_buffer_size", null);
  66. if (indexingBuffer.bytes() < minIndexingBuffer.bytes()) {
  67. indexingBuffer = minIndexingBuffer;
  68. }
  69. if (maxIndexingBuffer != null && indexingBuffer.bytes() > maxIndexingBuffer.bytes()) {
  70. indexingBuffer = maxIndexingBuffer;
  71. }
  72. } else {
  73. indexingBuffer = ByteSizeValue.parseBytesSizeValue(indexingBufferSetting, null);
  74. }
  75. this.indexingBuffer = indexingBuffer;
  76. this.minShardIndexBufferSize = componentSettings.getAsBytesSize("min_shard_index_buffer_size", new ByteSizeValue(4, ByteSizeUnit.MB));
  77. // LUCENE MONITOR: Based on this thread, currently (based on Mike), having a large buffer does not make a lot of sense: https://issues.apache.org/jira/browse/LUCENE-2324?focusedCommentId=13005155&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-13005155
  78. this.maxShardIndexBufferSize = componentSettings.getAsBytesSize("max_shard_index_buffer_size", new ByteSizeValue(512, ByteSizeUnit.MB));
  79. this.inactiveTime = componentSettings.getAsTime("shard_inactive_time", TimeValue.timeValueMinutes(30));
  80. // we need to have this relatively small to move a shard from inactive to active fast (enough)
  81. this.interval = componentSettings.getAsTime("interval", TimeValue.timeValueSeconds(30));
  82. logger.debug("using index_buffer_size [{}], with min_shard_index_buffer_size [{}], max_shard_index_buffer_size [{}], shard_inactive_time [{}]", this.indexingBuffer, this.minShardIndexBufferSize, this.maxShardIndexBufferSize, this.inactiveTime);
  83. }
  84. @Override protected void doStart() throws ElasticSearchException {
  85. indicesService.indicesLifecycle().addListener(listener);
  86. // its fine to run it on the scheduler thread, no busy work
  87. this.scheduler = threadPool.scheduleWithFixedDelay(new ShardsIndicesStatusChecker(), interval);
  88. }
  89. @Override protected void doStop() throws ElasticSearchException {
  90. indicesService.indicesLifecycle().removeListener(listener);
  91. if (scheduler != null) {
  92. scheduler.cancel(false);
  93. scheduler = null;
  94. }
  95. }
  96. @Override protected void doClose() throws ElasticSearchException {
  97. }
  98. class ShardsIndicesStatusChecker implements Runnable {
  99. @Override public void run() {
  100. synchronized (mutex) {
  101. boolean activeInactiveStatusChanges = false;
  102. long time = System.currentTimeMillis();
  103. for (IndexService indexService : indicesService) {
  104. for (IndexShard indexShard : indexService) {
  105. Translog translog = ((InternalIndexShard) indexShard).translog();
  106. ShardIndexingStatus status = shardsIndicesStatus.get(indexShard.shardId());
  107. if (status == null) { // not added yet
  108. continue;
  109. }
  110. // check if it is deemed to be inactive (sam translogId and numberOfOperations over a long period of time)
  111. if (status.translogId == translog.currentId() && translog.estimatedNumberOfOperations() == 0) {
  112. if (status.time == -1) { // first time
  113. status.time = time;
  114. }
  115. // inactive?
  116. if (!status.inactive) {
  117. if ((time - status.time) > inactiveTime.millis()) {
  118. // inactive for this amount of time, mark it
  119. status.inactive = true;
  120. activeInactiveStatusChanges = true;
  121. logger.debug("marking shard [{}][{}] as inactive (inactive_time[{}]), setting size to [{}]", indexShard.shardId().index().name(), indexShard.shardId().id(), inactiveTime, Engine.INACTIVE_SHARD_INDEXING_BUFFER);
  122. ((InternalIndexShard) indexShard).engine().updateIndexingBufferSize(Engine.INACTIVE_SHARD_INDEXING_BUFFER);
  123. }
  124. }
  125. } else {
  126. if (status.inactive) {
  127. status.inactive = false;
  128. activeInactiveStatusChanges = true;
  129. logger.debug("marking shard [{}][{}] as active", indexShard.shardId().index().name(), indexShard.shardId().id());
  130. }
  131. status.time = -1;
  132. }
  133. status.translogId = translog.currentId();
  134. status.translogNumberOfOperations = translog.estimatedNumberOfOperations();
  135. }
  136. }
  137. if (activeInactiveStatusChanges) {
  138. calcAndSetShardIndexingBuffer("shards became active/inactive");
  139. }
  140. }
  141. }
  142. }
  143. class Listener extends IndicesLifecycle.Listener {
  144. @Override public void afterIndexShardCreated(IndexShard indexShard) {
  145. synchronized (mutex) {
  146. calcAndSetShardIndexingBuffer("created_shard[" + indexShard.shardId().index().name() + "][" + indexShard.shardId().id() + "]");
  147. shardsIndicesStatus.put(indexShard.shardId(), new ShardIndexingStatus());
  148. }
  149. }
  150. @Override public void afterIndexShardClosed(ShardId shardId, boolean delete) {
  151. synchronized (mutex) {
  152. calcAndSetShardIndexingBuffer("removed_shard[" + shardId.index().name() + "][" + shardId.id() + "]");
  153. shardsIndicesStatus.remove(shardId);
  154. }
  155. }
  156. }
  157. private void calcAndSetShardIndexingBuffer(String reason) {
  158. int shardsCount = countShards();
  159. if (shardsCount == 0) {
  160. return;
  161. }
  162. ByteSizeValue shardIndexingBufferSize = calcShardIndexingBuffer(shardsCount);
  163. if (shardIndexingBufferSize == null) {
  164. return;
  165. }
  166. if (shardIndexingBufferSize.bytes() < minShardIndexBufferSize.bytes()) {
  167. shardIndexingBufferSize = minShardIndexBufferSize;
  168. }
  169. if (shardIndexingBufferSize.bytes() > maxShardIndexBufferSize.bytes()) {
  170. shardIndexingBufferSize = maxShardIndexBufferSize;
  171. }
  172. logger.debug("recalculating shard indexing buffer (reason={}), total is [{}] with [{}] active shards, each shard set to [{}]", reason, indexingBuffer, shardsCount, shardIndexingBufferSize);
  173. for (IndexService indexService : indicesService) {
  174. for (IndexShard indexShard : indexService) {
  175. ShardIndexingStatus status = shardsIndicesStatus.get(indexShard.shardId());
  176. if (status == null || !status.inactive) {
  177. ((InternalIndexShard) indexShard).engine().updateIndexingBufferSize(shardIndexingBufferSize);
  178. }
  179. }
  180. }
  181. }
  182. private ByteSizeValue calcShardIndexingBuffer(int shardsCount) {
  183. return new ByteSizeValue(indexingBuffer.bytes() / shardsCount);
  184. }
  185. private int countShards() {
  186. int shardsCount = 0;
  187. for (IndexService indexService : indicesService) {
  188. for (IndexShard indexShard : indexService) {
  189. ShardIndexingStatus status = shardsIndicesStatus.get(indexShard.shardId());
  190. if (status == null || !status.inactive) {
  191. shardsCount++;
  192. }
  193. }
  194. }
  195. return shardsCount;
  196. }
  197. static class ShardIndexingStatus {
  198. long translogId = -1;
  199. int translogNumberOfOperations = -1;
  200. boolean inactive = false;
  201. long time = -1; // contains the first time we saw this shard with no operations done on it
  202. }
  203. }