選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

QueueObjectLookup.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 java.io.IOException;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.Collections;
  48. import java.util.HashMap;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Map;
  52. import org.eclipse.jgit.errors.MissingObjectException;
  53. import org.eclipse.jgit.lib.AsyncOperation;
  54. import org.eclipse.jgit.lib.ObjectId;
  55. import org.eclipse.jgit.storage.dht.spi.Context;
  56. import org.eclipse.jgit.storage.dht.spi.Database;
  57. class QueueObjectLookup<T extends ObjectId> implements AsyncOperation {
  58. protected final RepositoryKey repo;
  59. protected final Database db;
  60. protected final DhtReader reader;
  61. private final DhtReaderOptions options;
  62. private final boolean reportMissing;
  63. private final ArrayList<ObjectInfo> tmp;
  64. private final int concurrentBatches;
  65. private int runningBatches;
  66. private Context context;
  67. private Iterator<T> toFind;
  68. private List<T> toRetry;
  69. private ObjectWithInfo<T> nextResult;
  70. private DhtException error;
  71. private boolean needChunkOnly;
  72. private boolean cacheLoadedInfo;
  73. QueueObjectLookup(DhtReader reader, boolean reportMissing) {
  74. this.repo = reader.getRepositoryKey();
  75. this.db = reader.getDatabase();
  76. this.reader = reader;
  77. this.options = reader.getOptions();
  78. this.reportMissing = reportMissing;
  79. this.tmp = new ArrayList<ObjectInfo>(4);
  80. this.context = Context.FAST_MISSING_OK;
  81. this.toRetry = new ArrayList<T>();
  82. this.concurrentBatches = options.getObjectIndexConcurrentBatches();
  83. }
  84. void setCacheLoadedInfo(boolean on) {
  85. cacheLoadedInfo = on;
  86. }
  87. void setNeedChunkOnly(boolean on) {
  88. needChunkOnly = on;
  89. }
  90. void init(Iterable<T> objectIds) {
  91. toFind = lookInCache(objectIds).iterator();
  92. }
  93. private Iterable<T> lookInCache(Iterable<T> objects) {
  94. RecentInfoCache infoCache = reader.getRecentInfoCache();
  95. List<T> missing = null;
  96. for (T obj : objects) {
  97. if (needChunkOnly && obj instanceof RefDataUtil.IdWithChunk) {
  98. push(obj, ((RefDataUtil.IdWithChunk) obj).getChunkKey());
  99. continue;
  100. }
  101. List<ObjectInfo> info = infoCache.get(obj);
  102. if (info != null && !info.isEmpty()) {
  103. push(obj, info.get(0));
  104. } else {
  105. if (missing == null) {
  106. if (objects instanceof List<?>)
  107. missing = new ArrayList<T>(((List<?>) objects).size());
  108. else
  109. missing = new ArrayList<T>();
  110. }
  111. missing.add(obj);
  112. }
  113. }
  114. if (missing != null)
  115. return missing;
  116. return Collections.emptyList();
  117. }
  118. synchronized ObjectWithInfo<T> nextObjectWithInfo()
  119. throws MissingObjectException, IOException {
  120. for (;;) {
  121. if (error != null)
  122. throw error;
  123. // Consider starting another batch before popping a result.
  124. // This ensures lookup is running while results are being
  125. // consumed by the calling application.
  126. //
  127. while (runningBatches < concurrentBatches) {
  128. if (!toFind.hasNext() // reached end of original input
  129. && runningBatches == 0 // all batches finished
  130. && toRetry != null // haven't yet retried
  131. && !toRetry.isEmpty()) {
  132. toFind = toRetry.iterator();
  133. toRetry = null;
  134. context = Context.READ_REPAIR;
  135. }
  136. if (toFind.hasNext())
  137. startBatch(context);
  138. else
  139. break;
  140. }
  141. ObjectWithInfo<T> c = pop();
  142. if (c != null) {
  143. if (c.chunkKey != null)
  144. return c;
  145. else
  146. throw missing(c.object);
  147. } else if (!toFind.hasNext() && runningBatches == 0)
  148. return null;
  149. try {
  150. wait();
  151. } catch (InterruptedException e) {
  152. throw new DhtTimeoutException(e);
  153. }
  154. }
  155. }
  156. private synchronized void startBatch(final Context ctx) {
  157. final int batchSize = options.getObjectIndexBatchSize();
  158. final Map<ObjectIndexKey, T> batch = new HashMap<ObjectIndexKey, T>();
  159. while (toFind.hasNext() && batch.size() < batchSize) {
  160. T obj = toFind.next();
  161. batch.put(ObjectIndexKey.create(repo, obj), obj);
  162. }
  163. final AsyncCallback<Map<ObjectIndexKey, Collection<ObjectInfo>>> cb;
  164. cb = new AsyncCallback<Map<ObjectIndexKey, Collection<ObjectInfo>>>() {
  165. public void onSuccess(Map<ObjectIndexKey, Collection<ObjectInfo>> r) {
  166. processResults(ctx, batch, r);
  167. }
  168. public void onFailure(DhtException e) {
  169. processFailure(e);
  170. }
  171. };
  172. db.objectIndex().get(ctx, batch.keySet(), cb);
  173. runningBatches++;
  174. }
  175. private synchronized void processResults(Context ctx,
  176. Map<ObjectIndexKey, T> batch,
  177. Map<ObjectIndexKey, Collection<ObjectInfo>> objects) {
  178. for (T obj : batch.values()) {
  179. Collection<ObjectInfo> matches = objects.get(obj);
  180. if (matches == null || matches.isEmpty()) {
  181. if (ctx == Context.FAST_MISSING_OK)
  182. toRetry.add(obj);
  183. else if (reportMissing)
  184. push(obj, (ChunkKey) null);
  185. continue;
  186. }
  187. tmp.clear();
  188. tmp.addAll(matches);
  189. ObjectInfo.sort(tmp);
  190. if (cacheLoadedInfo)
  191. reader.getRecentInfoCache().put(obj, tmp);
  192. push(obj, tmp.get(0));
  193. }
  194. runningBatches--;
  195. notify();
  196. }
  197. private synchronized void processFailure(DhtException e) {
  198. runningBatches--;
  199. error = e;
  200. notify();
  201. }
  202. private void push(T obj, ChunkKey chunkKey) {
  203. nextResult = new ObjectWithInfo<T>(obj, chunkKey, nextResult);
  204. }
  205. private void push(T obj, ObjectInfo info) {
  206. nextResult = new ObjectWithInfo<T>(obj, info, nextResult);
  207. }
  208. private ObjectWithInfo<T> pop() {
  209. ObjectWithInfo<T> r = nextResult;
  210. if (r == null)
  211. return null;
  212. nextResult = r.next;
  213. return r;
  214. }
  215. public boolean cancel(boolean mayInterruptIfRunning) {
  216. return true;
  217. }
  218. public void release() {
  219. // Do nothing, there is nothing to abort or discard.
  220. }
  221. private static <T extends ObjectId> MissingObjectException missing(T id) {
  222. return new MissingObjectException(id, DhtText.get().objectTypeUnknown);
  223. }
  224. static class ObjectWithInfo<T extends ObjectId> {
  225. final T object;
  226. final ObjectInfo info;
  227. final ChunkKey chunkKey;
  228. final ObjectWithInfo<T> next;
  229. ObjectWithInfo(T object, ObjectInfo info, ObjectWithInfo<T> next) {
  230. this.object = object;
  231. this.info = info;
  232. this.chunkKey = info.getChunkKey();
  233. this.next = next;
  234. }
  235. ObjectWithInfo(T object, ChunkKey chunkKey, ObjectWithInfo<T> next) {
  236. this.object = object;
  237. this.info = null;
  238. this.chunkKey = chunkKey;
  239. this.next = next;
  240. }
  241. }
  242. }