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.

DhtInserterOptions.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2011, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.storage.dht;
  44. import static java.util.zip.Deflater.DEFAULT_COMPRESSION;
  45. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  46. import java.security.SecureRandom;
  47. import java.util.zip.Deflater;
  48. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.ChunkMeta;
  49. import org.eclipse.jgit.lib.Config;
  50. import org.eclipse.jgit.lib.CoreConfig;
  51. import org.eclipse.jgit.storage.dht.spi.WriteBuffer;
  52. /** Options controlling how objects are inserted into a DHT stored repository. */
  53. public class DhtInserterOptions {
  54. private static final SecureRandom prng = new SecureRandom();
  55. /** 1024 (number of bytes in one kibibyte/kilobyte) */
  56. public static final int KiB = 1024;
  57. /** 1024 {@link #KiB} (number of bytes in one mebibyte/megabyte) */
  58. public static final int MiB = 1024 * KiB;
  59. private int chunkSize;
  60. private int writeBufferSize;
  61. private int compression;
  62. private int prefetchDepth;
  63. private long parserCacheLimit;
  64. /** Create a default inserter configuration. */
  65. public DhtInserterOptions() {
  66. setChunkSize(1 * MiB);
  67. setWriteBufferSize(1 * MiB);
  68. setCompression(DEFAULT_COMPRESSION);
  69. setPrefetchDepth(50);
  70. setParserCacheLimit(512 * getChunkSize());
  71. }
  72. /** @return maximum size of a chunk, in bytes. */
  73. public int getChunkSize() {
  74. return chunkSize;
  75. }
  76. /**
  77. * Set the maximum size of a chunk, in bytes.
  78. *
  79. * @param sizeInBytes
  80. * the maximum size. A chunk's data segment won't exceed this.
  81. * @return {@code this}
  82. */
  83. public DhtInserterOptions setChunkSize(int sizeInBytes) {
  84. chunkSize = Math.max(1024, sizeInBytes);
  85. return this;
  86. }
  87. /** @return maximum number of outstanding write bytes. */
  88. public int getWriteBufferSize() {
  89. return writeBufferSize;
  90. }
  91. /**
  92. * Set the maximum number of outstanding bytes in a {@link WriteBuffer}.
  93. *
  94. * @param sizeInBytes
  95. * maximum number of bytes.
  96. * @return {@code this}
  97. */
  98. public DhtInserterOptions setWriteBufferSize(int sizeInBytes) {
  99. writeBufferSize = Math.max(1024, sizeInBytes);
  100. return this;
  101. }
  102. /** @return maximum number of objects to put into a chunk. */
  103. public int getMaxObjectCount() {
  104. // Do not allow the index to be larger than a chunk itself.
  105. return getChunkSize() / (OBJECT_ID_LENGTH + 4);
  106. }
  107. /** @return compression level used when writing new objects into chunks. */
  108. public int getCompression() {
  109. return compression;
  110. }
  111. /**
  112. * Set the compression level used when writing new objects.
  113. *
  114. * @param level
  115. * the compression level. Use
  116. * {@link Deflater#DEFAULT_COMPRESSION} to specify a default
  117. * compression setting.
  118. * @return {@code this}
  119. */
  120. public DhtInserterOptions setCompression(int level) {
  121. compression = level;
  122. return this;
  123. }
  124. /**
  125. * Maximum number of entries in a chunk's prefetch list.
  126. * <p>
  127. * Each commit or tree chunk stores an optional prefetch list containing the
  128. * next X chunk keys that a reader would need if they were traversing the
  129. * project history. This implies that chunk prefetch lists are overlapping.
  130. * <p>
  131. * The depth at insertion time needs to be deep enough to allow readers to
  132. * have sufficient parallel prefetch to keep themselves busy without waiting
  133. * on sequential loads. If the depth is not sufficient, readers will stall
  134. * while they sequentially look up the next chunk they need.
  135. *
  136. * @return maximum number of entries in a {@link ChunkMeta} list.
  137. */
  138. public int getPrefetchDepth() {
  139. return prefetchDepth;
  140. }
  141. /**
  142. * Maximum number of entries in a chunk's prefetch list.
  143. *
  144. * @param depth
  145. * maximum depth of the prefetch list.
  146. * @return {@code this}
  147. */
  148. public DhtInserterOptions setPrefetchDepth(int depth) {
  149. prefetchDepth = Math.max(0, depth);
  150. return this;
  151. }
  152. /**
  153. * Number of chunks the parser can cache for delta resolution support.
  154. *
  155. * @return chunks to hold in memory to support delta resolution.
  156. */
  157. public int getParserCacheSize() {
  158. return (int) (getParserCacheLimit() / getChunkSize());
  159. }
  160. /** @return number of bytes the PackParser can cache for delta resolution. */
  161. public long getParserCacheLimit() {
  162. return parserCacheLimit;
  163. }
  164. /**
  165. * Set the number of bytes the PackParser can cache.
  166. *
  167. * @param limit
  168. * number of bytes the parser can cache.
  169. * @return {@code this}
  170. */
  171. public DhtInserterOptions setParserCacheLimit(long limit) {
  172. parserCacheLimit = Math.max(0, limit);
  173. return this;
  174. }
  175. /** @return next random 32 bits to salt chunk keys. */
  176. int nextChunkSalt() {
  177. return prng.nextInt();
  178. }
  179. /**
  180. * Update properties by setting fields from the configuration.
  181. * <p>
  182. * If a property is not defined in the configuration, then it is left
  183. * unmodified.
  184. *
  185. * @param rc
  186. * configuration to read properties from.
  187. * @return {@code this}
  188. */
  189. public DhtInserterOptions fromConfig(Config rc) {
  190. setChunkSize(rc.getInt("core", "dht", "chunkSize", getChunkSize()));
  191. setWriteBufferSize(rc.getInt("core", "dht", "writeBufferSize", getWriteBufferSize()));
  192. setCompression(rc.get(CoreConfig.KEY).getCompression());
  193. setPrefetchDepth(rc.getInt("core", "dht", "packParserPrefetchDepth", getPrefetchDepth()));
  194. setParserCacheLimit(rc.getLong("core", "dht", "packParserCacheLimit", getParserCacheLimit()));
  195. return this;
  196. }
  197. }