Skip to content

Commit e86a65c

Browse files
Knut Anders Hatlentrondn
authored andcommitted
Bug #14724: Incremental update of Bazaar history broken by instability in revision numbers
1 parent 3d35af9 commit e86a65c

File tree

6 files changed

+100
-6
lines changed

6 files changed

+100
-6
lines changed

src/org/opensolaris/opengrok/history/FileHistoryCache.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424

@@ -292,6 +292,13 @@ public String getLatestCachedRevision(Repository repository) {
292292
return null;
293293
}
294294

295+
@Override
296+
public void clear(Repository repository) {
297+
// We only expect this method to be called if the cache supports
298+
// incremental update, so it's not implemented here for now.
299+
throw new UnsupportedOperationException();
300+
}
301+
295302
@Override
296303
public String getInfo() {
297304
return getClass().getSimpleName();

src/org/opensolaris/opengrok/history/HistoryCache.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424
package org.opensolaris.opengrok.history;
@@ -105,6 +105,14 @@ boolean hasCacheForDirectory(File directory, Repository repository)
105105
String getLatestCachedRevision(Repository repository)
106106
throws HistoryException;
107107

108+
/**
109+
* Clear the history cache for a repository.
110+
*
111+
* @param repository the repository whose cache to clear
112+
* @throws HistoryException if the cache couldn't be cleared
113+
*/
114+
void clear(Repository repository) throws HistoryException;
115+
108116
/**
109117
* Get a string with information about the history cache.
110118
*

src/org/opensolaris/opengrok/history/JDBCHistoryCache.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424

@@ -1125,6 +1125,46 @@ private String getLatestRevisionForRepository(Repository repository)
11251125
}
11261126
}
11271127

1128+
@Override
1129+
public void clear(Repository repository) throws HistoryException {
1130+
try {
1131+
for (int i = 0;; i++) {
1132+
try {
1133+
clearHistoryForRepository(repository);
1134+
return;
1135+
} catch (SQLException sqle) {
1136+
handleSQLException(sqle, i);
1137+
}
1138+
}
1139+
} catch (SQLException sqle) {
1140+
throw new HistoryException(sqle);
1141+
}
1142+
}
1143+
1144+
/**
1145+
* Helper for {@link #clear(Repository)}.
1146+
*/
1147+
private void clearHistoryForRepository(Repository repository)
1148+
throws SQLException {
1149+
final ConnectionResource conn =
1150+
connectionManager.getConnectionResource();
1151+
try {
1152+
// This statement shouldn't be called very frequently, so don't
1153+
// care about caching it...
1154+
PreparedStatement ps = conn.prepareStatement(
1155+
getQuery("clearRepository"));
1156+
try {
1157+
ps.setInt(1, getRepositoryId(conn, repository));
1158+
ps.execute();
1159+
conn.commit();
1160+
} finally {
1161+
ps.close();
1162+
}
1163+
} finally {
1164+
connectionManager.releaseConnection(conn);
1165+
}
1166+
}
1167+
11281168
@Override
11291169
public String getInfo() throws HistoryException {
11301170
return info;

src/org/opensolaris/opengrok/history/JDBCHistoryCache_queries.properties

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#
1919

2020
#
21-
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
21+
# Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
# Use is subject to license terms.
2323
#
2424

@@ -168,3 +168,7 @@ getMaxDirId=SELECT MAX(ID) FROM OPENGROK.DIRECTORIES
168168
getMaxChangesetId=SELECT MAX(ID) FROM OPENGROK.CHANGESETS
169169

170170
getMaxAuthorId=SELECT MAX(ID) FROM OPENGROK.AUTHORS
171+
172+
# Cascading deletes will take care of deleting all data associated with the
173+
# repository from the other tables.
174+
clearRepository=DELETE FROM OPENGROK.REPOSITORIES WHERE ID=?

src/org/opensolaris/opengrok/history/Repository.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,33 @@ final void createCache(HistoryCache cache, String sinceRevision)
194194
}
195195

196196
File directory = new File(getDirectoryName());
197-
History history = getHistory(directory, sinceRevision);
197+
198+
History history;
199+
try {
200+
history = getHistory(directory, sinceRevision);
201+
} catch (HistoryException he) {
202+
if (sinceRevision == null) {
203+
// Failed to get full history, so fail.
204+
throw he;
205+
} else {
206+
// Failed to get partial history. This may have been caused
207+
// by changes in the revision numbers since the last update
208+
// (bug #14724) so we'll try to regenerate the cache from
209+
// scratch instead.
210+
OpenGrokLogger.getLogger().log(Level.INFO,
211+
"Failed to get partial history. Attempting to " +
212+
"recreate the history cache from scratch.", he);
213+
history = null;
214+
}
215+
}
216+
217+
if (sinceRevision != null && history == null) {
218+
// Failed to get partial history, now get full history instead.
219+
history = getHistory(directory);
220+
// Got full history successfully. Clear the history cache so that
221+
// we can recreate it from scratch.
222+
cache.clear(this);
223+
}
198224

199225
if (history != null) {
200226
cache.store(history, this);

test/org/opensolaris/opengrok/history/JDBCHistoryCacheTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
21+
* Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2222
* Use is subject to license terms.
2323
*/
2424

@@ -230,6 +230,15 @@ public void testStoreAndGet() throws Exception {
230230
updatedHistory.getHistoryEntries());
231231
assertSameEntry(newEntry, updatedEntries.removeFirst());
232232
assertSameEntries(historyToStore.getHistoryEntries(), updatedEntries);
233+
234+
// test clearing of cache
235+
cache.clear(repos);
236+
History clearedHistory = cache.get(reposRoot, repos, true);
237+
assertTrue("History should be empty",
238+
clearedHistory.getHistoryEntries().isEmpty());
239+
cache.store(historyToStore, repos);
240+
assertSameEntries(historyToStore.getHistoryEntries(),
241+
cache.get(reposRoot, repos, true).getHistoryEntries());
233242
}
234243

235244
/**

0 commit comments

Comments
 (0)