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 14KB

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