1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
|
/*
* Copyright (C) 2009-2010, Google Inc.
* Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com> and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.internal.storage.file;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.fail;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.time.Instant;
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
import org.eclipse.jgit.errors.MissingObjectException;
import org.eclipse.jgit.internal.storage.pack.PackExt;
import org.eclipse.jgit.internal.storage.pack.PackWriter;
import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.lib.AnyObjectId;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.NullProgressMonitor;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.ObjectLoader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevObject;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.WindowCacheConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class ConcurrentRepackTest extends RepositoryTestCase {
@Override
@Before
public void setUp() throws Exception {
WindowCacheConfig windowCacheConfig = new WindowCacheConfig();
windowCacheConfig.setPackedGitOpenFiles(1);
windowCacheConfig.install();
super.setUp();
}
@Override
@After
public void tearDown() throws Exception {
super.tearDown();
new WindowCacheConfig().install();
}
@Test
public void testObjectInNewPack() throws IncorrectObjectTypeException,
IOException {
// Create a new object in a new pack, and test that it is present.
//
final Repository eden = createBareRepository();
final RevObject o1 = writeBlob(eden, "o1");
pack(eden, o1);
assertEquals(o1.name(), parse(o1).name());
}
@Test
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 = createBareRepository();
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());
}
@Test
public void testObjectMovedWithinPack()
throws IncorrectObjectTypeException, IOException {
// Create an object and pack it.
//
final Repository eden = createBareRepository();
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");
try (PackWriter pw = new PackWriter(eden)) {
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());
}
@Test
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 = createBareRepository();
final RevObject o1 = writeBlob(eden, "o1");
final File[] out1 = pack(eden, o1);
assertEquals(o1.name(), parse(o1).name());
final ObjectLoader load1 = db.open(o1, Constants.OBJ_BLOB);
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.open(o1, Constants.OBJ_BLOB);
assertNotNull(load2);
assertNotSame(load1, load2);
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
assertArrayEquals(data1, data2);
assertEquals(load2.getType(), load1.getType());
}
private static void whackCache() {
final WindowCacheConfig config = new WindowCacheConfig();
config.setPackedGitOpenFiles(1);
config.install();
}
private RevObject parse(AnyObjectId id)
throws MissingObjectException, IOException {
try (RevWalk rw = new RevWalk(db)) {
return rw.parseAny(id);
}
}
private File[] pack(Repository src, RevObject... list)
throws IOException {
try (PackWriter pw = new PackWriter(src)) {
for (RevObject o : list) {
pw.addObject(o);
}
PackFile packFile = new PackFile(
db.getObjectDatabase().getPackDirectory(), pw.computeName(),
PackExt.PACK);
PackFile idxFile = packFile.create(PackExt.INDEX);
final File[] files = new File[] { packFile, idxFile };
write(files, pw);
return files;
}
}
private static void write(File[] files, PackWriter pw)
throws IOException {
final Instant begin = FS.DETECTED
.lastModifiedInstant(files[0].getParentFile());
NullProgressMonitor m = NullProgressMonitor.INSTANCE;
try (OutputStream out = new BufferedOutputStream(
new FileOutputStream(files[0]))) {
pw.writePack(m, m, out);
}
try (OutputStream out = new BufferedOutputStream(
new FileOutputStream(files[1]))) {
pw.writeIndex(out);
}
touch(begin, files[0].getParentFile());
}
private static void delete(File[] list) throws IOException {
final Instant begin = FS.DETECTED
.lastModifiedInstant(list[0].getParentFile());
for (File f : list) {
FileUtils.delete(f);
assertFalse(f + " was removed", f.exists());
}
touch(begin, list[0].getParentFile());
}
private static void touch(Instant begin, File dir) throws IOException {
while (begin.compareTo(FS.DETECTED.lastModifiedInstant(dir)) >= 0) {
try {
Thread.sleep(25);
} catch (InterruptedException ie) {
//
}
FS.DETECTED.setLastModified(dir.toPath(), Instant.now());
}
}
private RevObject writeBlob(Repository repo, String data)
throws IOException {
final byte[] bytes = Constants.encode(data);
final ObjectId id;
try (ObjectInserter inserter = repo.newObjectInserter()) {
id = inserter.insert(Constants.OBJ_BLOB, bytes);
inserter.flush();
}
try {
parse(id);
fail("Object " + id.name() + " should not exist in test repository");
} catch (MissingObjectException e) {
// Ok
}
try (RevWalk revWalk = new RevWalk(repo)) {
return revWalk.lookupBlob(id);
}
}
}
|