Object access improvements
cover Object access improvements
This series tries to address issue 79[1] and GERRIT-81[2] by detecting when packs have been overwritten on disk, or when new packs have appeared and old packs have disappeared.
The first 5 patches are quite trivial and are just things I found while working on the real issue.
Patch 6 is rather intrusive. It starts a refactoring I have always wanted to do, which is to split the object database out of the Repository class, and also start to divorce it from the local filesystem. The library is in the same state after patch 5 as it was before patch 5; that is issue 79 is still an issue, but everything else is fine.
Patch 7 and 8 fix issue 79.
Patch 9 is a rewritten version of your 1/3 posted Sun 19 Apr, providing test cases for issue 79 / GERRIT-81.
Patch 10 is a broken test case. Basically as I explain in the commit message of 7, PackWriter can still crash randomly.
With 1-9 applied, issue 79 can be marked fixed, but GERRIT-81 is still a problem, as any concurrent PackWriter thread may randomly crash during a repack.
[1] http://code.google.com/p/egit/issues/detail?id=79
[2] http://jira.source.android.com/jira/browse/GERRIT-81
Shawn O. Pearce (10):
Safely handle closing an already closed WindowedFile Change empty tree test case to use a temporary repository Replace hand-coded read fully loop with NB.readFully Clear the reverse index when closing a PackFile Introduce a new exception type for an in-place pack modification Refactor object database access with new abstraction Rescan packs if a pack is modified in-place (part 1) Scan for new packs if GIT_DIR/objects/pack has been modified Add test cases for loading new (or replaced) pack files BROKEN TEST: ObjectLoader stays valid across repacks
.../org/spearce/jgit/lib/ConcurrentRepackTest.java | 252 +++++++++++ .../org/spearce/jgit/lib/RepositoryTestCase.java | 2 - .../tst/org/spearce/jgit/lib/T0003_Basic.java | 11 +- .../tst/org/spearce/jgit/lib/T0004_PackReader.java | 1 - .../spearce/jgit/errors/PackMismatchException.java | 55 +++ .../src/org/spearce/jgit/lib/ObjectDatabase.java | 367 ++++++++++++++++ .../src/org/spearce/jgit/lib/ObjectDirectory.java | 438 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/PackFile.java | 36 ++- .../src/org/spearce/jgit/lib/Repository.java | 174 +-------- .../org/spearce/jgit/lib/UnpackedObjectLoader.java | 34 +- .../src/org/spearce/jgit/lib/WindowedFile.java | 16 +- 11 files changed, 1183 insertions(+), 203 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConcurrentRepackTest.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/PackMismatchException.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDatabase.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java
PATCH 1/10 Safely handle closing an already closed WindowedFile
If cacheOpen throws an exception (e.g. FileNotFoundException) we may wind up in cacheClose, where the fd is null because we didn't successfully construct the RandomAccessFile. In such cases there is nothing to attempt to close.
--- .../src/org/spearce/jgit/lib/WindowedFile.java | 16 +++++++++------- 1 files changed, 9 insertions(+), 7 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/WindowedFile.java +9 -7
@@ -297,14 +297,16 @@ void cacheOpen() throws IOException { } void cacheClose() { - try { - fd.close(); - } catch (IOException err) { - // Ignore a close event. We had it open only for reading. - // There should not be errors related to network buffers - // not flushed, etc. + if (fd != null) { + try { + fd.close(); + } catch (IOException err) { + // Ignore a close event. We had it open only for reading. + // There should not be errors related to network buffers + // not flushed, etc. + } + fd = null; } - fd = null; } void allocWindow(final WindowCursor curs, final int windowId,
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 2/10 Change empty tree test case to use a temporary repository
This test case looks for a loose object of the empty tree, but our stock test repository contains the empty tree in a pack file. So the only way we can ensure it will be written is if we write to an empty repository.
--- .../tst/org/spearce/jgit/lib/T0003_Basic.java | 11 +++++------ 1 files changed, 5 insertions(+), 6 deletions(-)
org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0003_Basic.java +5 -6
@@ -75,14 +75,13 @@ public void test002_WriteEmptyTree() throws IOException { // open when we create it we won't write the object file out as a loose // object (as it already exists in the pack). // - db.closePacks(); - - final Tree t = new Tree(db); - t.accept(new WriteTree(trash, db), TreeEntry.MODIFIED_ONLY); + final Repository newdb = createNewEmptyRepo(); + final Tree t = new Tree(newdb); + t.accept(new WriteTree(trash, newdb), TreeEntry.MODIFIED_ONLY); assertEquals("4b825dc642cb6eb9a060e54bf8d69288fbee4904", t.getId() .name()); - final File o = new File(new File(new File(trash_git, "objects"), "4b"), - "825dc642cb6eb9a060e54bf8d69288fbee4904"); + final File o = new File(new File(new File(newdb.getDirectory(), + "objects"), "4b"), "825dc642cb6eb9a060e54bf8d69288fbee4904"); assertTrue("Exists " + o, o.isFile()); assertTrue("Read-only " + o, !o.canWrite()); }
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 3/10 Replace hand-coded read fully loop with NB.readFully
This code predates the NB utility class. I'd prefer to reuse the code over keeping this duplicate copy of the logic.
--- .../org/spearce/jgit/lib/UnpackedObjectLoader.java | 14 ++++++-------- 1 files changed, 6 insertions(+), 8 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectLoader.java +6 -8
@@ -46,6 +46,7 @@ import org.spearce.jgit.errors.CorruptObjectException; import org.spearce.jgit.util.MutableInteger; +import org.spearce.jgit.util.NB; import org.spearce.jgit.util.RawParseUtils; /**
@@ -74,17 +75,14 @@ public UnpackedObjectLoader(final Repository db, final AnyObjectId id) private static byte[] readCompressed(final Repository db, final AnyObjectId id) throws FileNotFoundException, IOException { - final FileInputStream objStream = new FileInputStream(db.toFile(id)); - final byte[] compressed; + final FileInputStream in = new FileInputStream(db.toFile(id)); try { - compressed = new byte[objStream.available()]; - int off = 0; - while (off < compressed.length) - off += objStream.read(compressed, off, compressed.length - off); + final byte[] compressed = new byte[(int) in.getChannel().size()]; + NB.readFully(in, compressed, 0, compressed.length); + return compressed; } finally { - objStream.close(); + in.close(); } - return compressed; } /**
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 4/10 Clear the reverse index when closing a PackFile
We clear the forward index (ObjectId -> offset) when we close the pack, so we might as well clear the reverse index (offset -> ObjectId).
The reverse index is protected by the lock on "this", just like the forward index is.
--- .../src/org/spearce/jgit/lib/PackFile.java | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/PackFile.java +1 -0
@@ -155,6 +155,7 @@ public void close() { pack.close(); synchronized (this) { loadedIdx = null; + reverseIdx = null; } }
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 5/10 Introduce a new exception type for an in-place pack modification
If a pack file is modified in place by "git gc" or "git repack" it means that the list of objects contained within the pack has not changed, but it is possible for the layout of the pack to be modified.
The layout can change when the compressed version of any object is changed, and usually happens because the packer has found a smaller way to represent a contained object. It also can change if an object's delta base encoding format is switched, such as from OBJ_REF to OBJ_OFS.
This new exception type can be caught by callers to recognize that the object contents of the pack is quite likely the same, but that any offsets and cached windows are wrong and need to be computed or loaded again.
--- .../spearce/jgit/errors/PackMismatchException.java | 55 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/PackFile.java | 15 +++-- 2 files changed, 64 insertions(+), 6 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/errors/PackMismatchException.java
org.spearce.jgit/src/org/spearce/jgit/errors/PackMismatchException.java new file +55 -0
@@ -0,0 +1,55 @@ +/* + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.errors; + +import java.io.IOException; + +/** Thrown when a PackFile no longer matches the PackIndex. */ +public class PackMismatchException extends IOException { + private static final long serialVersionUID = 1L; + + /** + * Construct a pack modification error. + * + * @param why + * description of the type of error. + */ + public PackMismatchException(final String why) { + super(why); + } +}
org.spearce.jgit/src/org/spearce/jgit/lib/PackFile.java +9 -6
@@ -51,6 +51,7 @@ import java.util.zip.DataFormatException; import org.spearce.jgit.errors.CorruptObjectException; +import org.spearce.jgit.errors.PackMismatchException; import org.spearce.jgit.util.NB; /**
@@ -292,16 +293,18 @@ private void onOpenPack() throws IOException { final long packCnt = NB.decodeUInt32(intbuf, 0); final long idxCnt = idx.getObjectCount(); if (idxCnt != packCnt) - throw new IOException("Pack index" - + " object count mismatch; expected " + packCnt - + " found " + idxCnt + ": " + pack.getName()); + throw new PackMismatchException("Pack object count mismatch:" + + " pack " + packCnt + + " index " + idxCnt + + ": " + pack.getName()); final byte[] csumbuf = new byte[20]; pack.readFully(pack.length() - 20, csumbuf, curs); if (!Arrays.equals(csumbuf, idx.packChecksum)) - throw new IOException("Pack index mismatch; pack SHA-1 is " - + ObjectId.fromRaw(csumbuf).name() + ", index expects " - + ObjectId.fromRaw(idx.packChecksum).name()); + throw new PackMismatchException("Pack checksum mismatch:" + + " pack " + ObjectId.fromRaw(csumbuf).name() + + " index " + ObjectId.fromRaw(idx.packChecksum).name() + + ": " + pack.getName()); } private PackedObjectLoader reader(final WindowCursor curs,
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 6/10 Refactor object database access with new abstraction
By moving object database access out of the Repository class we start to open the door for less-traditional databases, such as storing objects in a distributed hash table or in-memory.
This change doesn't introduce any new functionality, nor does it fix any existing bugs. A Repository instance still assumes that its object database is in "$GIT_DIR/objects", and there is no way to override that assumption.
Unlike the prior implementation, alternates are maintained as discrete instances, rather than being inlined into the same list as the repository's own object directory. This cleans up that search logic as its now more clear when we scan the alternate and when we scan the local repository. It also opens the door for sharing shared repositories, e.g. if an IDE were to open two repositories both forked from the same reference location we could reuse the same ObjectDatabase instance between them.
--- .../org/spearce/jgit/lib/RepositoryTestCase.java | 2 - .../tst/org/spearce/jgit/lib/T0004_PackReader.java | 1 - .../src/org/spearce/jgit/lib/ObjectDatabase.java | 354 +++++++++++++++++ .../src/org/spearce/jgit/lib/ObjectDirectory.java | 401 ++++++++++++++++++++ .../src/org/spearce/jgit/lib/Repository.java | 174 +-------- .../org/spearce/jgit/lib/UnpackedObjectLoader.java | 22 +- 6 files changed, 779 insertions(+), 175 deletions(-) create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDatabase.java create mode 100644 org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java
org.spearce.jgit.test/tst/org/spearce/jgit/lib/RepositoryTestCase.java +0 -2
@@ -297,8 +297,6 @@ copyFile(JGitTestUtil.getTestResourceFile(packs[k] + ".idx"), new File(packDir, copyFile(JGitTestUtil.getTestResourceFile("packed-refs"), new File(trash_git,"packed-refs")); - db.scanForPacks(); - fakeSystemReader.values.clear(); fakeSystemReader.values.put(Constants.OS_USER_NAME_KEY, Constants.OS_USER_NAME_KEY); fakeSystemReader.values.put(Constants.GIT_AUTHOR_NAME_KEY, Constants.GIT_AUTHOR_NAME_KEY);
org.spearce.jgit.test/tst/org/spearce/jgit/lib/T0004_PackReader.java +0 -1
@@ -89,7 +89,6 @@ copyFile(new File(todopack, packname + ".pack"), new File(packDir, packname + ".pack")); copyFile(new File(todopack, packname + ".idx"), new File(packDir, packname + ".idx")); - db.scanForPacks(); Tree t; t = db
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDatabase.java new file +354 -0
354 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java new file +401 -0
401 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +11 -163
257 lines not on this page · raw message
org.spearce.jgit/src/org/spearce/jgit/lib/UnpackedObjectLoader.java +13 -9
@@ -38,6 +38,7 @@ package org.spearce.jgit.lib; +import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;
@@ -60,22 +61,25 @@ private final byte[] bytes; /** - * Construct an ObjectLoader for the specified SHA-1 + * Construct an ObjectLoader to read from the file. * - * @param db - * repository + * @param path + * location of the loose object to read. * @param id - * SHA-1 + * expected identity of the object being loaded, if known. + * @throws FileNotFoundException + * the loose object file does not exist. * @throws IOException + * the loose object file exists, but is corrupt. */ - public UnpackedObjectLoader(final Repository db, final AnyObjectId id) + public UnpackedObjectLoader(final File path, final AnyObjectId id) throws IOException { - this(readCompressed(db, id), id); + this(readCompressed(path), id); } - private static byte[] readCompressed(final Repository db, - final AnyObjectId id) throws FileNotFoundException, IOException { - final FileInputStream in = new FileInputStream(db.toFile(id)); + private static byte[] readCompressed(final File path) + throws FileNotFoundException, IOException { + final FileInputStream in = new FileInputStream(path); try { final byte[] compressed = new byte[(int) in.getChannel().size()]; NB.readFully(in, compressed, 0, compressed.length);
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 7/10 Rescan packs if a pack is modified in-place (part 1)
If a pack file is modified in place it usually means that some other process has repacked this repository, but the contents of this one pack remained the same, but the object offsets may be different due to different pack creation settings. In such cases we can no longer use the existing PacKFile or PackIndex instances and instead we need to create new ones.
This isn't a full solution to the problem. It is possible for an application to obtain a PackedObjectLoader, see the PackFile get closed due to pressure on the WindowCache, then fail when the pack is reopened due to the pack being recreated on disk. PackWriter is very susceptible to this, as it caches PackedObjectLoaders for a long time before it uses them to copy data out of an existing pack file.
This particular solution only catches the failure where we had opened the index a long time ago, closed the pack, and are now opening it again in order to construct a PackedObjectLoader for the caller.
--- .../src/org/spearce/jgit/lib/ObjectDirectory.java | 66 +++++++++++++------ .../src/org/spearce/jgit/lib/PackFile.java | 20 +++++- 2 files changed, 63 insertions(+), 23 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java +45 -21
@@ -51,6 +51,7 @@ import java.util.Map; import java.util.concurrent.atomic.AtomicReference; +import org.spearce.jgit.errors.PackMismatchException; import org.spearce.jgit.util.FS; /**
@@ -190,38 +191,53 @@ protected boolean hasObject1(final AnyObjectId objectId) { @Override protected ObjectLoader openObject1(final WindowCursor curs, final AnyObjectId objectId) throws IOException { - for (final PackFile p : packs()) { - try { - final ObjectLoader ldr = p.get(curs, objectId); - if (ldr != null) { - return ldr; + PackFile[] pList = packs(); + SEARCH: for (;;) { + for (final PackFile p : pList) { + try { + final ObjectLoader ldr = p.get(curs, objectId); + if (ldr != null) { + return ldr; + } + } catch (PackMismatchException e) { + // Pack was modified; refresh the entire pack list. + // + pList = scanPacks(pList); + continue SEARCH; + } catch (IOException e) { + // Assume the pack is corrupted. + // + removePack(p); } - } catch (IOException e) { - // Assume the pack is corrupted. - // - removePack(p); - continue; } + return null; } - return null; } @Override void openObjectInAllPacks1(final Collection<PackedObjectLoader> out, final WindowCursor curs, final AnyObjectId objectId) throws IOException { - for (final PackFile p : packs()) { - try { - final PackedObjectLoader ldr = p.get(curs, objectId); - if (ldr != null) { - out.add(ldr); + PackFile[] pList = packs(); + SEARCH: for (;;) { + for (final PackFile p : pList) { + try { + final PackedObjectLoader ldr = p.get(curs, objectId); + if (ldr != null) { + out.add(ldr); + } + } catch (PackMismatchException e) { + // Pack was modified; refresh the entire pack list. + // + pList = scanPacks(pList); + continue SEARCH; + } catch (IOException e) { + // Assume the pack is corrupted. + // + removePack(p); } - } catch (IOException e) { - // Assume the pack is corrupted. - // - removePack(p); - continue; } + break SEARCH; } }
@@ -350,6 +366,14 @@ synchronized (packList) { private static Map<String, PackFile> reuseMap(final PackFile[] old) { final Map<String, PackFile> forReuse = new HashMap<String, PackFile>(); for (final PackFile p : old) { + if (p.invalid()) { + // The pack instance is corrupted, and cannot be safely used + // again. Do not include it in our reuse map. + // + p.close(); + continue; + } + final PackFile prior = forReuse.put(p.getPackFile().getName(), p); if (prior != null) { // This should never occur. It should be impossible for us
org.spearce.jgit/src/org/spearce/jgit/lib/PackFile.java +18 -2
@@ -73,6 +73,8 @@ public int compare(final PackFile a, final PackFile b) { private int packLastModified; + private volatile boolean invalid; + private PackIndex loadedIdx; private PackReverseIndex reverseIdx;
@@ -91,14 +93,24 @@ public PackFile(final File idxFile, final File packFile) { pack = new WindowedFile(packFile) { @Override protected void onOpen() throws IOException { - onOpenPack(); + try { + onOpenPack(); + } catch (IOException e) { + invalid = true; + throw e; + } } }; } private synchronized PackIndex idx() throws IOException { if (loadedIdx == null) { - loadedIdx = PackIndex.open(idxFile); + try { + loadedIdx = PackIndex.open(idxFile); + } catch (IOException e) { + invalid = true; + throw e; + } } return loadedIdx; }
@@ -267,6 +279,10 @@ boolean supportsFastCopyRawData() throws IOException { return idx().hasCRC32Support(); } + boolean invalid() { + return invalid; + } + private void onOpenPack() throws IOException { final PackIndex idx = idx(); final WindowCursor curs = new WindowCursor();
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 8/10 Scan for new packs if GIT_DIR/objects/pack has been modified
If GIT_DIR/objects/pack directory has changed then one or more packs may have been added to the directory, or removed from it, due to a concurrent application performing a repack. In such cases we need to reload the list of available packs before conducting a search.
As packs are infrequently created or modified relative to searches, we only scan for new packs if our search through existing packs has turned up no results.
Transports that create packs locally through IndexPack automatically register their new PackFile, so even those cases will typically not require scanning the object directory again.
As misses are fairly common during IndexPack (due to its automatic collision detection logic) we avoid scanning for packs unless the GIT_DIR/objects/pack directory has been modified since the last scan of it.
--- .../src/org/spearce/jgit/lib/ObjectDatabase.java | 15 ++++++++++++++- .../src/org/spearce/jgit/lib/ObjectDirectory.java | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDatabase.java +14 -1
@@ -137,7 +137,7 @@ private final boolean hasObjectImpl1(final AnyObjectId objectId) { return true; } } - return false; + return tryAgain1() && hasObject1(objectId); } private final boolean hasObjectImpl2(final String objectId) {
@@ -216,6 +216,12 @@ private ObjectLoader openObjectImpl1(final WindowCursor curs, return ldr; } } + if (tryAgain1()) { + ldr = openObject1(curs, objectId); + if (ldr != null) { + return ldr; + } + } return null; }
@@ -311,6 +317,13 @@ void openObjectInAllPacks1(Collection<PackedObjectLoader> out, } /** + * @return true if the fast-half search should be tried again. + */ + protected boolean tryAgain1() { + return false; + } + + /** * Get the alternate databases known to this database. * * @return the alternate list. Never null, but may be an empty array.
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java +13 -0
@@ -75,6 +75,8 @@ private final AtomicReference<PackFile[]> packList; + private volatile long packDirectoryLastModified; + /** * Initialize a reference to an on-disk object directory. *
@@ -257,6 +259,16 @@ protected ObjectLoader openObject2(final WindowCursor curs, } } + @Override + protected boolean tryAgain1() { + final PackFile[] old = packList.get(); + if (packDirectoryLastModified <= packDirectory.lastModified()) { + scanPacks(old); + return true; + } + return false; + } + private void insertPack(final PackFile pf) { PackFile[] o, n; do {
@@ -389,6 +401,7 @@ synchronized (packList) { } private String[] listPackIdx() { + packDirectoryLastModified = packDirectory.lastModified(); final String[] idxList = packDirectory.list(new FilenameFilter() { public boolean accept(final File baseDir, final String n) { // Must match "pack-[0-9a-f]{40}.idx" to be an index.
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 9/10 Add test cases for loading new (or replaced) pack files
--- .../org/spearce/jgit/lib/ConcurrentRepackTest.java | 206 ++++++++++++++++++++ 1 files changed, 206 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConcurrentRepackTest.java
org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConcurrentRepackTest.java new file +206 -0
@@ -0,0 +1,206 @@ +/* + * Copyright (C) 2009, Robin Rosenberg <[email protected]> + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.lib; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.spearce.jgit.errors.IncorrectObjectTypeException; +import org.spearce.jgit.errors.MissingObjectException; +import org.spearce.jgit.revwalk.RevObject; +import org.spearce.jgit.revwalk.RevWalk; + +public class ConcurrentRepackTest extends RepositoryTestCase { + public void setUp() throws Exception { + WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); + windowCacheConfig.setPackedGitOpenFiles(1); + WindowCache.reconfigure(windowCacheConfig); + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); + WindowCache.reconfigure(windowCacheConfig); + } + + public void testObjectInNewPack() throws IncorrectObjectTypeException, + IOException { + // Create a new object in a new pack, and test that it is present. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + } + + public void testObjectMovedToNewPack1() + throws IncorrectObjectTypeException, IOException { + // Create an object and pack it. Then remove that pack and put the + // object into a different pack file, with some other object. We + // still should be able to access the objects. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + final File[] out1 = pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + + final RevObject o2 = writeBlob(eden, "o2"); + pack(eden, o2, o1); + + // Force close, and then delete, the old pack. + // + whackCache(); + delete(out1); + + // Now here is the interesting thing. Will git figure the new + // object exists in the new pack, and not the old one. + // + assertEquals(o2.name(), parse(o2).name()); + assertEquals(o1.name(), parse(o1).name()); + } + + public void testObjectMovedWithinPack() + throws IncorrectObjectTypeException, IOException { + // Create an object and pack it. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + final File[] out1 = pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + + // Force close the old pack. + // + whackCache(); + + // Now overwrite the old pack in place. This method of creating a + // different pack under the same file name is partially broken. We + // should also have a different file name because the list of objects + // within the pack has been modified. + // + final RevObject o2 = writeBlob(eden, "o2"); + final PackWriter pw = new PackWriter(eden, NullProgressMonitor.INSTANCE); + pw.addObject(o2); + pw.addObject(o1); + write(out1, pw); + + // Try the old name, then the new name. The old name should cause the + // pack to reload when it opens and the index and pack mismatch. + // + assertEquals(o1.name(), parse(o1).name()); + assertEquals(o2.name(), parse(o2).name()); + } + + private static void whackCache() { + final WindowCacheConfig config = new WindowCacheConfig(); + + config.setPackedGitOpenFiles(0); + WindowCache.reconfigure(config); + + config.setPackedGitOpenFiles(1); + WindowCache.reconfigure(config); + } + + private RevObject parse(final AnyObjectId id) + throws MissingObjectException, IOException { + return new RevWalk(db).parseAny(id); + } + + private File[] pack(final Repository src, final RevObject... list) + throws IOException { + final PackWriter pw = new PackWriter(src, NullProgressMonitor.INSTANCE); + for (final RevObject o : list) { + pw.addObject(o); + } + + final ObjectId name = pw.computeName(); + final File packFile = fullPackFileName(name, ".pack"); + final File idxFile = fullPackFileName(name, ".idx"); + final File[] files = new File[] { packFile, idxFile }; + write(files, pw); + return files; + } + + private static void write(final File[] files, final PackWriter pw) + throws IOException { + FileOutputStream out; + + out = new FileOutputStream(files[0]); + try { + pw.writePack(out); + } finally { + out.close(); + } + + out = new FileOutputStream(files[1]); + try { + pw.writeIndex(out); + } finally { + out.close(); + } + } + + private static void delete(final File[] list) { + for (final File f : list) { + f.delete(); + assertFalse(f + " was removed", f.exists()); + } + } + + private File fullPackFileName(final ObjectId name, final String suffix) { + final File packdir = new File(db.getObjectsDirectory(), "pack"); + return new File(packdir, "pack-" + name.name() + suffix); + } + + private RevObject writeBlob(final Repository repo, final String data) + throws IOException { + final RevWalk revWalk = new RevWalk(repo); + final byte[] bytes = data.getBytes(Constants.CHARACTER_ENCODING); + final ObjectWriter ow = new ObjectWriter(repo); + final ObjectId id = ow.writeBlob(bytes); + try { + parse(id); + fail("Object " + id.name() + " should not exist in test repository"); + } catch (MissingObjectException e) { + // Ok + } + return revWalk.lookupBlob(id); + } +}
Signature · 1 line
1.6.3.rc1.188.ga02b
PATCH 10/10 BROKEN TEST: ObjectLoader stays valid across repacks
This doesn't doesn't work.
What we are trying to verify is that an ObjectLoader remains valid if the underlying storage for the object has moved, such as when a repository is repacked, the old pack was deleted, and the object is now in the new pack.
--- .../org/spearce/jgit/lib/ConcurrentRepackTest.java | 46 ++++++++++++++++++++ 1 files changed, 46 insertions(+), 0 deletions(-)
org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConcurrentRepackTest.java +46 -0
@@ -41,6 +41,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; +import java.util.Arrays; import org.spearce.jgit.errors.IncorrectObjectTypeException; import org.spearce.jgit.errors.MissingObjectException;
@@ -128,6 +129,51 @@ public void testObjectMovedWithinPack() assertEquals(o2.name(), parse(o2).name()); } + public void testObjectMovedToNewPack2() + throws IncorrectObjectTypeException, IOException { + // Create an object and pack it. Then remove that pack and put the + // object into a different pack file, with some other object. We + // still should be able to access the objects. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + final File[] out1 = pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + + final ObjectLoader load1 = db.openBlob(o1); + assertNotNull(load1); + + final RevObject o2 = writeBlob(eden, "o2"); + pack(eden, o2, o1); + + // Force close, and then delete, the old pack. + // + whackCache(); + delete(out1); + + // Now here is the interesting thing... can the loader we made + // earlier still resolve the object, even though its underlying + // pack is gone, but the object still exists. + // + final ObjectLoader load2 = db.openBlob(o1); + assertNotNull(load2); + assertNotSame(load1, load2); + + // Currently load1.getCachedBytes NPEs due to the openCount == 1, + // but fd == null. Aside from the underlying pack being gone, the + // WindowCache didn't reset the openCount to 0 when it closed the + // file during the mass cache eviction. Thus we never tried to do + // an subsequent open. + // + final byte[] data2 = load2.getCachedBytes(); + final byte[] data1 = load1.getCachedBytes(); + assertNotNull(data2); + assertNotNull(data1); + assertNotSame(data1, data2); // cache should be per-pack, not per object + assertTrue(Arrays.equals(data1, data2)); + assertEquals(load2.getType(), load1.getType()); + } + private static void whackCache() { final WindowCacheConfig config = new WindowCacheConfig();
Signature · 1 line
1.6.3.rc1.188.ga02b
Re: [JGIT PATCH 10/10] BROKEN TEST: ObjectLoader stays valid across repacks
tisdag 21 april 2009 03:21:12 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 6 lines
This doesn't doesn't work.
What we are trying to verify is that an ObjectLoader remains valid if the underlying storage for the object has moved, such as when a repository is repacked, the old pack was deleted, and the object is now in the new pack.
So, I had an idea and started hacking and suddenly the supposedly ok cases started crashing like this.
org.spearce.jgit.errors.MissingObjectException: Missing unknown 4a75554761c96be80602c05145d1ef41c77e1b72
at org.spearce.jgit.revwalk.RevWalk.parseAny(RevWalk.java:704)
at org.spearce.jgit.lib.ConcurrentRepackTest.parse(ConcurrentRepackTest.java:189) at org.spearce.jgit.lib.ConcurrentRepackTest.testObjectMovedWithinPack(ConcurrentRepackTest.java:129) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
After pruning the other changes, it still crashes , even with mvn clean test
HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack = 327983069 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack = 458811222 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745.pack = 331392325 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-546ff360fe3488adb20860ce3436a2d6373d2796.pack = 547821429 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa.pack = 536578194 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack = 885722359 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354367548.0/.git/objects/pack/pack-146aac1f6a11aed8e06af7d9f5cc1b4b8beedb36.pack = 830203467
The hash codes printed are the same everytime it crashes, removing the invalid flags will create these codes and the test succeeds.
HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-34be9032ac282b11fa9babdc2b2a93ca996c9c2f.pack = 327983069 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-df2982f284bbabb6bdb59ee3fcc6eb0983e20371.pack = 458811222 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-9fb5b411fe6dfa89cc2e6b89d2bd8e5de02b5745.pack = 331392325 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-546ff360fe3488adb20860ce3436a2d6373d2796.pack = 547821429 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-e6d07037cbcf13376308a0a995d1fa48f8f76aaa.pack = 536578194 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-3280af9c07ee18a87705ef50b0cc4cd20266cf12.pack = 885722359 HashCode /home/me/SW/EGIT.contrib/org.spearce.jgit.test/trash/trash1240354568171.0/.git/objects/pack/pack-146aac1f6a11aed8e06af7d9f5cc1b4b8beedb36.pack = 830203467
One cannot obviously assume they are the same, but the numbers might be a lead to why it crashes here. Looks like as hash collision and a failure of the "equals" part to distinguish different WindowedFile's.
-- robin
org.spearce.jgit/src/org/spearce/jgit/lib/WindowedFile.java +6 -0
@@ -80,6 +80,8 @@ /** Total number of windows actively in the associated cache. */ int openCount; + boolean invalid; + /** * Open a file for reading through window caching. *
@@ -93,6 +95,10 @@ public WindowedFile(final File file) { // value in WindowCache.hash(), without doing the multiply there. // hash = System.identityHashCode(this) * 31; + System.out.print("HashCode "); + System.out.print(file.getPath()); + System.out.print(" = "); + System.out.println(hash); length = Long.MAX_VALUE; }
Re: [JGIT PATCH 10/10] BROKEN TEST: ObjectLoader stays valid across repacks
Quoted 4 lines · Robin Rosenberg
So, I had an idea and started hacking and suddenly the supposedly ok cases started crashing like this.
org.spearce.jgit.errors.MissingObjectException: Missing unknown
4a75554761c96be80602c05145d1ef41c77e1b72
You broke it! :-)
Quoted 2 lines
The hash codes printed are the same everytime it crashes, removing the invalid flags will create these codes and the test succeeds.
I am not surprised. The hash codes are derived from the system identity hash code for the object, which are assigned using a rather deterministic algorithm. Given the same sequence of tests through the JVM and the same heap state, you are likely going to have the same hash code for the same objects every run.
Actually, I think you missed it, but the two sets of hash codes you printed here in the email are identical.
Quoted 2 lines
One cannot obviously assume they are the same, but the numbers might be a lead to why it crashes here. Looks like as hash collision and a failure of the "equals" part to distinguish different WindowedFile's.
Nope, that's not it. We never use .equals() on WindowedFile, and we never use that hash field you were printing as any part of an equality computation. That hash field is mixed with the offset of a window to hash it into the WindowCache. We *always* use reference equality ("foo.wp == wp") to test a WindowedFile.
org.spearce.jgit/src/org/spearce/jgit/lib/WindowedFile.java line 83
Quoted 9 lines
org.spearce.jgit/src/org/spearce/jgit/lib/WindowedFile.java +1
-0@@ -80,6 +80,8 @@ /** Total number of windows actively in the associated cache. */ int openCount; + boolean invalid;
This shadowed the "invalid" field in PackFile, causing the anonymous inner class on l.93-103 that subclasses WindowedFile to write to this new field, while PackFile still read from its own field.
If you had called this "invalid2", or marked it private, you wouldn't have seen the test failure.
FWIW, I've decided to merge PackFile and WindowedFile together. There is no point in keeping them separate anymore. We used to use WindowedFile for both the .pack and the .idx, but long ago you showed it was faster to load the entire .idx into memory in the PackIndex structure. And this split is causing confusion, like the one you just stepped in.
Signature · 1 line
Shawn.
Re: [JGIT PATCH 10/10] BROKEN TEST: ObjectLoader stays valid across repacks
In reply to Robin RosenbergQuoted 9 lines · Robin Rosenberg
tisdag 21 april 2009 03:21:12 skrev "Shawn O. Pearce" <[email protected]>:
Quoted 6 lines
This doesn't doesn't work.
What we are trying to verify is that an ObjectLoader remains valid if the underlying storage for the object has moved, such as when a repository is repacked, the old pack was deleted, and the object is now in the new pack.
So, I had an idea and started hacking [...]
Yea, I also have an idea. I'm working up a patch series that should fix this bug. I'll post it later this evening once its complete and passes tests.
Signature · 1 line
Shawn.
PATCH v2 9/10 Add test cases for loading new (or replaced) pack files
In reply to Shawn O. Pearce---
Matches with v2 08/10. Tests take a few seconds longer now as we have to delay at least 1 second on most filesystems; longer if their modification timestamp granularity is larger than 1 second.
.../org/spearce/jgit/lib/ConcurrentRepackTest.java | 222 ++++++++++++++++++++ 1 files changed, 222 insertions(+), 0 deletions(-) create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConcurrentRepackTest.java
org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConcurrentRepackTest.java new file +222 -0
@@ -0,0 +1,222 @@ +/* + * Copyright (C) 2009, Robin Rosenberg <[email protected]> + * Copyright (C) 2009, Google Inc. + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * - Neither the name of the Git Development Community nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +package org.spearce.jgit.lib; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; + +import org.spearce.jgit.errors.IncorrectObjectTypeException; +import org.spearce.jgit.errors.MissingObjectException; +import org.spearce.jgit.revwalk.RevObject; +import org.spearce.jgit.revwalk.RevWalk; + +public class ConcurrentRepackTest extends RepositoryTestCase { + public void setUp() throws Exception { + WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); + windowCacheConfig.setPackedGitOpenFiles(1); + WindowCache.reconfigure(windowCacheConfig); + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + WindowCacheConfig windowCacheConfig = new WindowCacheConfig(); + WindowCache.reconfigure(windowCacheConfig); + } + + public void testObjectInNewPack() throws IncorrectObjectTypeException, + IOException { + // Create a new object in a new pack, and test that it is present. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + } + + public void testObjectMovedToNewPack1() + throws IncorrectObjectTypeException, IOException { + // Create an object and pack it. Then remove that pack and put the + // object into a different pack file, with some other object. We + // still should be able to access the objects. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + final File[] out1 = pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + + final RevObject o2 = writeBlob(eden, "o2"); + pack(eden, o2, o1); + + // Force close, and then delete, the old pack. + // + whackCache(); + delete(out1); + + // Now here is the interesting thing. Will git figure the new + // object exists in the new pack, and not the old one. + // + assertEquals(o2.name(), parse(o2).name()); + assertEquals(o1.name(), parse(o1).name()); + } + + public void testObjectMovedWithinPack() + throws IncorrectObjectTypeException, IOException { + // Create an object and pack it. + // + final Repository eden = createNewEmptyRepo(); + final RevObject o1 = writeBlob(eden, "o1"); + final File[] out1 = pack(eden, o1); + assertEquals(o1.name(), parse(o1).name()); + + // Force close the old pack. + // + whackCache(); + + // Now overwrite the old pack in place. This method of creating a + // different pack under the same file name is partially broken. We + // should also have a different file name because the list of objects + // within the pack has been modified. + // + final RevObject o2 = writeBlob(eden, "o2"); + final PackWriter pw = new PackWriter(eden, NullProgressMonitor.INSTANCE); + pw.addObject(o2); + pw.addObject(o1); + write(out1, pw); + + // Try the old name, then the new name. The old name should cause the + // pack to reload when it opens and the index and pack mismatch. + // + assertEquals(o1.name(), parse(o1).name()); + assertEquals(o2.name(), parse(o2).name()); + } + + private static void whackCache() { + final WindowCacheConfig config = new WindowCacheConfig(); + + config.setPackedGitOpenFiles(0); + WindowCache.reconfigure(config); + + config.setPackedGitOpenFiles(1); + WindowCache.reconfigure(config); + } + + private RevObject parse(final AnyObjectId id) + throws MissingObjectException, IOException { + return new RevWalk(db).parseAny(id); + } + + private File[] pack(final Repository src, final RevObject... list) + throws IOException { + final PackWriter pw = new PackWriter(src, NullProgressMonitor.INSTANCE); + for (final RevObject o : list) { + pw.addObject(o); + } + + final ObjectId name = pw.computeName(); + final File packFile = fullPackFileName(name, ".pack"); + final File idxFile = fullPackFileName(name, ".idx"); + final File[] files = new File[] { packFile, idxFile }; + write(files, pw); + return files; + } + + private static void write(final File[] files, final PackWriter pw) + throws IOException { + final long begin = files[0].getParentFile().lastModified(); + FileOutputStream out; + + out = new FileOutputStream(files[0]); + try { + pw.writePack(out); + } finally { + out.close(); + } + + out = new FileOutputStream(files[1]); + try { + pw.writeIndex(out); + } finally { + out.close(); + } + + touch(begin, files[0].getParentFile()); + } + + private static void delete(final File[] list) { + final long begin = list[0].getParentFile().lastModified(); + for (final File f : list) { + f.delete(); + assertFalse(f + " was removed", f.exists()); + } + touch(begin, list[0].getParentFile()); + } + + private static void touch(final long begin, final File dir) { + while (begin >= dir.lastModified()) { + try { + Thread.sleep(25); + } catch (InterruptedException ie) { + // + } + dir.setLastModified(System.currentTimeMillis()); + } + } + + private File fullPackFileName(final ObjectId name, final String suffix) { + final File packdir = new File(db.getObjectsDirectory(), "pack"); + return new File(packdir, "pack-" + name.name() + suffix); + } + + private RevObject writeBlob(final Repository repo, final String data) + throws IOException { + final RevWalk revWalk = new RevWalk(repo); + final byte[] bytes = data.getBytes(Constants.CHARACTER_ENCODING); + final ObjectWriter ow = new ObjectWriter(repo); + final ObjectId id = ow.writeBlob(bytes); + try { + parse(id); + fail("Object " + id.name() + " should not exist in test repository"); + } catch (MissingObjectException e) { + // Ok + } + return revWalk.lookupBlob(id); + } +}
Signature · 1 line
1.6.3.rc1.188.ga02b
Re: [JGIT PATCH 08/10] Scan for new packs if GIT_DIR/objects/pack has been modified
In reply to Shawn O. PearceQuoted 1 line · Shawn O. Pearce
If GIT_DIR/objects/pack directory has changed then one or more packs
...
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java protected ObjectLoader openObject2(final WindowCursor curs, · line 265
Quoted 4 lines
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java +4
-0+ @Override + protected boolean tryAgain1() { + final PackFile[] old = packList.get(); + if (packDirectoryLastModified <= packDirectory.lastModified()) {
Sadly we needed to use <= here to avoid racy-git cases. Worse, unlike with the DirCache we don't have another file system inode we can use to compare timestamps against.
Consequently, I just realized my commit message isn't entirely accurate. In reality we rescan the #F*@!*! directory every time we have a miss, because the time stamp hasn't changed.
I can't think of another method to use here. I'd love to avoid the rescan on a miss if the aren't any new packs in the directory, but I just don't see how we can do that and avoid the racy-git case at the same time.
We could try to fudge it by saying < here, but the test cases in 9/10 would then require a Thread.sleep(2) or something to force enough time to pass for the directory modification time to advance.
I think in real applications, its "good enough" to allow this amount of fudging. We shouldn't see more than one repack per repository per FS clock tick. And if we do, the admin should be shot on sight.
git-repack is careful to ensure the new pack is linked into the directory before the old packs are unlinked, so we really shouldn't ever scan and miss a pack.
Signature · 1 line
Shawn.
PATCH v2 8/10 Scan for new packs if GIT_DIR/objects/pack has been modified
If GIT_DIR/objects/pack directory has changed then one or more packs may have been added to the directory, or removed from it, due to a concurrent application performing a repack. In such cases we need to reload the list of available packs before conducting a search.
As packs are infrequently created or modified relative to searches, we only scan for new packs if our search through existing packs has turned up no results.
Transports that create packs locally through IndexPack automatically register their new PackFile, so even those cases will typically not require scanning the object directory again.
As misses are fairly common during IndexPack (due to its automatic collision detection logic) we avoid scanning for packs unless the GIT_DIR/objects/pack directory has been modified since the last scan of it.
---
Quoted 4 lines · Shawn O. Pearce
We could try to fudge it by saying < here, [...]
I think in real applications, its "good enough" to allow this amount of fudging.
And this version makes it so. The interdiff is just <= changed to <.
.../src/org/spearce/jgit/lib/ObjectDatabase.java | 15 ++++++++++++++- .../src/org/spearce/jgit/lib/ObjectDirectory.java | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletions(-)
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDatabase.java +14 -1
@@ -137,7 +137,7 @@ private final boolean hasObjectImpl1(final AnyObjectId objectId) { return true; } } - return false; + return tryAgain1() && hasObject1(objectId); } private final boolean hasObjectImpl2(final String objectId) {
@@ -216,6 +216,12 @@ private ObjectLoader openObjectImpl1(final WindowCursor curs, return ldr; } } + if (tryAgain1()) { + ldr = openObject1(curs, objectId); + if (ldr != null) { + return ldr; + } + } return null; }
@@ -311,6 +317,13 @@ void openObjectInAllPacks1(Collection<PackedObjectLoader> out, } /** + * @return true if the fast-half search should be tried again. + */ + protected boolean tryAgain1() { + return false; + } + + /** * Get the alternate databases known to this database. * * @return the alternate list. Never null, but may be an empty array.
org.spearce.jgit/src/org/spearce/jgit/lib/ObjectDirectory.java +13 -0
@@ -75,6 +75,8 @@ private final AtomicReference<PackFile[]> packList; + private volatile long packDirectoryLastModified; + /** * Initialize a reference to an on-disk object directory. *
@@ -257,6 +259,16 @@ protected ObjectLoader openObject2(final WindowCursor curs, } } + @Override + protected boolean tryAgain1() { + final PackFile[] old = packList.get(); + if (packDirectoryLastModified < packDirectory.lastModified()) { + scanPacks(old); + return true; + } + return false; + } + private void insertPack(final PackFile pf) { PackFile[] o, n; do {
@@ -389,6 +401,7 @@ synchronized (packList) { } private String[] listPackIdx() { + packDirectoryLastModified = packDirectory.lastModified(); final String[] idxList = packDirectory.list(new FilenameFilter() { public boolean accept(final File baseDir, final String n) { // Must match "pack-[0-9a-f]{40}.idx" to be an index.
Signature · 1 line
1.6.3.rc1.188.ga02b