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.

DhtRefDatabase.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 org.eclipse.jgit.lib.Ref.Storage.LOOSE;
  45. import static org.eclipse.jgit.lib.Ref.Storage.NEW;
  46. import java.io.IOException;
  47. import java.util.Collections;
  48. import java.util.List;
  49. import java.util.Map;
  50. import java.util.Set;
  51. import java.util.concurrent.TimeoutException;
  52. import java.util.concurrent.atomic.AtomicReference;
  53. import org.eclipse.jgit.errors.MissingObjectException;
  54. import org.eclipse.jgit.generated.storage.dht.proto.GitStore.RefData;
  55. import org.eclipse.jgit.lib.AnyObjectId;
  56. import org.eclipse.jgit.lib.ObjectId;
  57. import org.eclipse.jgit.lib.ObjectIdRef.PeeledNonTag;
  58. import org.eclipse.jgit.lib.ObjectIdRef.PeeledTag;
  59. import org.eclipse.jgit.lib.ObjectIdRef.Unpeeled;
  60. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  61. import org.eclipse.jgit.lib.Ref;
  62. import org.eclipse.jgit.lib.RefDatabase;
  63. import org.eclipse.jgit.lib.RefRename;
  64. import org.eclipse.jgit.lib.SymbolicRef;
  65. import org.eclipse.jgit.revwalk.RevObject;
  66. import org.eclipse.jgit.revwalk.RevTag;
  67. import org.eclipse.jgit.revwalk.RevWalk;
  68. import org.eclipse.jgit.storage.dht.RefDataUtil.IdWithChunk;
  69. import org.eclipse.jgit.storage.dht.spi.Context;
  70. import org.eclipse.jgit.storage.dht.spi.Database;
  71. import org.eclipse.jgit.util.RefList;
  72. import org.eclipse.jgit.util.RefMap;
  73. /** Repository references stored on top of a DHT database. */
  74. public class DhtRefDatabase extends RefDatabase {
  75. private final DhtRepository repository;
  76. private final Database db;
  77. private final AtomicReference<RefCache> cache;
  78. DhtRefDatabase(DhtRepository repository, Database db) {
  79. this.repository = repository;
  80. this.db = db;
  81. this.cache = new AtomicReference<RefCache>();
  82. }
  83. DhtRepository getRepository() {
  84. return repository;
  85. }
  86. ChunkKey findChunk(AnyObjectId id) {
  87. RefCache c = cache.get();
  88. if (c != null) {
  89. IdWithChunk i = c.hints.get(id);
  90. if (i != null)
  91. return i.getChunkKey();
  92. }
  93. return null;
  94. }
  95. @Override
  96. public Ref getRef(String needle) throws IOException {
  97. RefCache curr = readRefs();
  98. for (String prefix : SEARCH_PATH) {
  99. Ref ref = curr.ids.get(prefix + needle);
  100. if (ref != null) {
  101. ref = resolve(ref, 0, curr.ids);
  102. return ref;
  103. }
  104. }
  105. return null;
  106. }
  107. @Override
  108. public List<Ref> getAdditionalRefs() {
  109. return Collections.emptyList();
  110. }
  111. @Override
  112. public Map<String, Ref> getRefs(String prefix) throws IOException {
  113. RefCache curr = readRefs();
  114. RefList<Ref> packed = RefList.emptyList();
  115. RefList<Ref> loose = curr.ids;
  116. RefList.Builder<Ref> sym = new RefList.Builder<Ref>(curr.sym.size());
  117. for (int idx = 0; idx < curr.sym.size(); idx++) {
  118. Ref ref = curr.sym.get(idx);
  119. String name = ref.getName();
  120. ref = resolve(ref, 0, loose);
  121. if (ref != null && ref.getObjectId() != null) {
  122. sym.add(ref);
  123. } else {
  124. // A broken symbolic reference, we have to drop it from the
  125. // collections the client is about to receive. Should be a
  126. // rare occurrence so pay a copy penalty.
  127. int toRemove = loose.find(name);
  128. if (0 <= toRemove)
  129. loose = loose.remove(toRemove);
  130. }
  131. }
  132. return new RefMap(prefix, packed, loose, sym.toRefList());
  133. }
  134. private Ref resolve(Ref ref, int depth, RefList<Ref> loose)
  135. throws IOException {
  136. if (!ref.isSymbolic())
  137. return ref;
  138. Ref dst = ref.getTarget();
  139. if (MAX_SYMBOLIC_REF_DEPTH <= depth)
  140. return null; // claim it doesn't exist
  141. dst = loose.get(dst.getName());
  142. if (dst == null)
  143. return ref;
  144. dst = resolve(dst, depth + 1, loose);
  145. if (dst == null)
  146. return null;
  147. return new SymbolicRef(ref.getName(), dst);
  148. }
  149. @Override
  150. public Ref peel(Ref ref) throws IOException {
  151. final Ref oldLeaf = ref.getLeaf();
  152. if (oldLeaf.isPeeled() || oldLeaf.getObjectId() == null)
  153. return ref;
  154. Ref newLeaf = doPeel(oldLeaf);
  155. RefCache cur = readRefs();
  156. int idx = cur.ids.find(oldLeaf.getName());
  157. if (0 <= idx && cur.ids.get(idx) == oldLeaf) {
  158. RefList<Ref> newList = cur.ids.set(idx, newLeaf);
  159. if (cache.compareAndSet(cur, new RefCache(newList, cur)))
  160. cachePeeledState(oldLeaf, newLeaf);
  161. }
  162. return recreate(ref, newLeaf);
  163. }
  164. private void cachePeeledState(Ref oldLeaf, Ref newLeaf) {
  165. // TODO(spearce) Use an ExecutorService here
  166. try {
  167. RepositoryKey repo = repository.getRepositoryKey();
  168. RefKey key = RefKey.create(repo, newLeaf.getName());
  169. RefData oldData = RefDataUtil.fromRef(oldLeaf);
  170. RefData newData = RefDataUtil.fromRef(newLeaf);
  171. db.ref().compareAndPut(key, oldData, newData);
  172. } catch (TimeoutException e) {
  173. // Ignore a timeout here, we were only trying to update
  174. // a cached value to save peeling costs in the future.
  175. } catch (DhtException e) {
  176. // Ignore a database error, this was only an attempt to
  177. // fix a value that could be cached to save time later.
  178. }
  179. }
  180. private Ref doPeel(final Ref leaf) throws MissingObjectException,
  181. IOException {
  182. RevWalk rw = new RevWalk(getRepository());
  183. try {
  184. String name = leaf.getName();
  185. ObjectId oId = leaf.getObjectId();
  186. RevObject obj = rw.parseAny(oId);
  187. DhtReader ctx = (DhtReader) rw.getObjectReader();
  188. ChunkKey key = ctx.findChunk(oId);
  189. if (key != null)
  190. oId = new IdWithChunk(oId, key);
  191. if (obj instanceof RevTag) {
  192. ObjectId pId = rw.peel(obj);
  193. key = ctx.findChunk(pId);
  194. pId = key != null ? new IdWithChunk(pId, key) : pId.copy();
  195. return new PeeledTag(leaf.getStorage(), name, oId, pId);
  196. } else {
  197. return new PeeledNonTag(leaf.getStorage(), name, oId);
  198. }
  199. } finally {
  200. rw.release();
  201. }
  202. }
  203. private static Ref recreate(final Ref old, final Ref leaf) {
  204. if (old.isSymbolic()) {
  205. Ref dst = recreate(old.getTarget(), leaf);
  206. return new SymbolicRef(old.getName(), dst);
  207. }
  208. return leaf;
  209. }
  210. @Override
  211. public DhtRefUpdate newUpdate(String refName, boolean detach)
  212. throws IOException {
  213. Ref ref = getRefs(ALL).get(refName);
  214. if (ref == null)
  215. ref = new Unpeeled(NEW, refName, null);
  216. RepositoryKey repo = repository.getRepositoryKey();
  217. return new DhtRefUpdate(this, repo, db, ref);
  218. }
  219. @Override
  220. public RefRename newRename(String fromName, String toName)
  221. throws IOException {
  222. DhtRefUpdate src = newUpdate(fromName, true);
  223. DhtRefUpdate dst = newUpdate(toName, true);
  224. return new DhtRefRename(src, dst);
  225. }
  226. @Override
  227. public boolean isNameConflicting(String refName) throws IOException {
  228. RefList<Ref> all = readRefs().ids;
  229. // Cannot be nested within an existing reference.
  230. int lastSlash = refName.lastIndexOf('/');
  231. while (0 < lastSlash) {
  232. String needle = refName.substring(0, lastSlash);
  233. if (all.contains(needle))
  234. return true;
  235. lastSlash = refName.lastIndexOf('/', lastSlash - 1);
  236. }
  237. // Cannot be the container of an existing reference.
  238. String prefix = refName + '/';
  239. int idx = -(all.find(prefix) + 1);
  240. if (idx < all.size() && all.get(idx).getName().startsWith(prefix))
  241. return true;
  242. return false;
  243. }
  244. @Override
  245. public void create() {
  246. // Nothing to do.
  247. }
  248. @Override
  249. public void close() {
  250. clearCache();
  251. }
  252. void clearCache() {
  253. cache.set(null);
  254. }
  255. void stored(String refName, RefData newData) {
  256. Ref ref = fromData(refName, newData);
  257. RefCache oldCache, newCache;
  258. do {
  259. oldCache = cache.get();
  260. if (oldCache == null)
  261. return;
  262. RefList<Ref> ids = oldCache.ids.put(ref);
  263. RefList<Ref> sym = oldCache.sym;
  264. if (ref.isSymbolic()) {
  265. sym.put(ref);
  266. } else {
  267. int p = sym.find(refName);
  268. if (0 <= p)
  269. sym = sym.remove(p);
  270. }
  271. newCache = new RefCache(ids, sym, oldCache.hints);
  272. } while (!cache.compareAndSet(oldCache, newCache));
  273. }
  274. void removed(String refName) {
  275. RefCache oldCache, newCache;
  276. do {
  277. oldCache = cache.get();
  278. if (oldCache == null)
  279. return;
  280. int p;
  281. RefList<Ref> ids = oldCache.ids;
  282. p = ids.find(refName);
  283. if (0 <= p)
  284. ids = ids.remove(p);
  285. RefList<Ref> sym = oldCache.sym;
  286. p = sym.find(refName);
  287. if (0 <= p)
  288. sym = sym.remove(p);
  289. newCache = new RefCache(ids, sym, oldCache.hints);
  290. } while (!cache.compareAndSet(oldCache, newCache));
  291. }
  292. private RefCache readRefs() throws DhtException {
  293. RefCache c = cache.get();
  294. if (c == null) {
  295. try {
  296. c = read();
  297. } catch (TimeoutException e) {
  298. throw new DhtTimeoutException(e);
  299. }
  300. cache.set(c);
  301. }
  302. return c;
  303. }
  304. private RefCache read() throws DhtException, TimeoutException {
  305. RefList.Builder<Ref> id = new RefList.Builder<Ref>();
  306. RefList.Builder<Ref> sym = new RefList.Builder<Ref>();
  307. ObjectIdSubclassMap<IdWithChunk> hints = new ObjectIdSubclassMap<IdWithChunk>();
  308. for (Map.Entry<RefKey, RefData> e : scan()) {
  309. Ref ref = fromData(e.getKey().getName(), e.getValue());
  310. if (ref.isSymbolic())
  311. sym.add(ref);
  312. id.add(ref);
  313. if (ref.getObjectId() instanceof IdWithChunk
  314. && !hints.contains(ref.getObjectId()))
  315. hints.add((IdWithChunk) ref.getObjectId());
  316. if (ref.getPeeledObjectId() instanceof IdWithChunk
  317. && !hints.contains(ref.getPeeledObjectId()))
  318. hints.add((IdWithChunk) ref.getPeeledObjectId());
  319. }
  320. id.sort();
  321. sym.sort();
  322. return new RefCache(id.toRefList(), sym.toRefList(), hints);
  323. }
  324. private static Ref fromData(String name, RefData data) {
  325. if (data.hasSymref()) {
  326. Ref leaf = new Unpeeled(NEW, data.getSymref(), null);
  327. return new SymbolicRef(name, leaf);
  328. }
  329. if (!data.hasTarget())
  330. return new Unpeeled(LOOSE, name, null);
  331. ObjectId oId = IdWithChunk.create(data.getTarget());
  332. if (data.getIsPeeled() && data.hasPeeled()) {
  333. ObjectId pId = IdWithChunk.create(data.getPeeled());
  334. return new PeeledTag(LOOSE, name, oId, pId);
  335. }
  336. if (data.getIsPeeled())
  337. return new PeeledNonTag(LOOSE, name, oId);
  338. return new Unpeeled(LOOSE, name, oId);
  339. }
  340. private Set<Map.Entry<RefKey, RefData>> scan() throws DhtException,
  341. TimeoutException {
  342. // TODO(spearce) Do we need to perform READ_REPAIR here?
  343. RepositoryKey repo = repository.getRepositoryKey();
  344. return db.ref().getAll(Context.LOCAL, repo).entrySet();
  345. }
  346. private static class RefCache {
  347. final RefList<Ref> ids;
  348. final RefList<Ref> sym;
  349. final ObjectIdSubclassMap<IdWithChunk> hints;
  350. RefCache(RefList<Ref> ids, RefList<Ref> sym,
  351. ObjectIdSubclassMap<IdWithChunk> hints) {
  352. this.ids = ids;
  353. this.sym = sym;
  354. this.hints = hints;
  355. }
  356. RefCache(RefList<Ref> ids, RefCache old) {
  357. this(ids, old.sym, old.hints);
  358. }
  359. }
  360. }