Skip to content

Commit

Permalink
Reindex tickets on server start if no index exists
Browse files Browse the repository at this point in the history
Check if tickets need to be reindexed when the server starts. This is the
case if no ticket index exists. In that case the ticket index is built.

This is done during the start of the `ITicketService`.

For this the interface of `ITicketService` needed to change. The `start`
method was defined abstract and the specific ticket services had to
implement it. None does any real starting stuff in it.
The `start` method is now final. It calls a new abstract method `onStart`
which the specific ticket services need to implement. In the existing
implementations I just changed `start` to `onStart`.
  • Loading branch information
fzs committed Mar 5, 2017
1 parent 197ddd2 commit 63dbdfd
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 21 deletions.
3 changes: 1 addition & 2 deletions src/main/java/com/gitblit/tickets/BranchTicketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,8 @@ public BranchTicketService(
}

@Override
public BranchTicketService start() {
public void onStart() {
log.info("{} started", getClass().getSimpleName());
return this;
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/gitblit/tickets/FileTicketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ public FileTicketService(
}

@Override
public FileTicketService start() {
public void onStart() {
log.info("{} started", getClass().getSimpleName());
return this;
}

@Override
Expand Down
43 changes: 36 additions & 7 deletions src/main/java/com/gitblit/tickets/ITicketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,24 @@ public ITicketService(
* @since 1.4.0
*/
@Override
public abstract ITicketService start();
public final ITicketService start() {
onStart();
if (shouldReindex()) {
log.info("Re-indexing all tickets...");
// long startTime = System.currentTimeMillis();
reindex();
// float duration = (System.currentTimeMillis() - startTime) / 1000f;
// log.info("Built Lucene index over all tickets in {} secs", duration);
}
return this;
}

/**
* Start the specific ticket service implementation.
*
* @since 1.9.0
*/
public abstract void onStart();

/**
* Stop the service.
Expand All @@ -196,6 +213,12 @@ public final ITicketService stop() {
return this;
}

/**
* Closes any open resources used by this service.
* @since 1.4.0
*/
protected abstract void close();

/**
* Creates a ticket notifier. The ticket notifier is not thread-safe!
* @since 1.4.0
Expand Down Expand Up @@ -273,12 +296,6 @@ public boolean hasTickets(RepositoryModel repository) {
return indexer.hasTickets(repository);
}

/**
* Closes any open resources used by this service.
* @since 1.4.0
*/
protected abstract void close();

/**
* Reset all caches in the service.
* @since 1.4.0
Expand Down Expand Up @@ -1343,6 +1360,18 @@ public List<QueryResult> queryFor(String query, int page, int pageSize, String s
return indexer.queryFor(query, page, pageSize, sortBy, descending);
}


/**
* Checks tickets should get re-indexed.
*
* @return true if tickets should get re-indexed, false otherwise.
*/
private boolean shouldReindex()
{
return indexer.shouldReindex();
}


/**
* Destroys an existing index and reindexes all tickets.
* This operation may be expensive and time-consuming.
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/gitblit/tickets/NullTicketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ public boolean isReady() {
}

@Override
public NullTicketService start() {
public void onStart() {
log.info("{} started", getClass().getSimpleName());
return this;
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/gitblit/tickets/RedisTicketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ public RedisTicketService(
}

@Override
public RedisTicketService start() {
public void onStart() {
log.info("{} started", getClass().getSimpleName());
if (!isReady()) {
log.warn("{} is not ready!", getClass().getSimpleName());
}
return this;
}

@Override
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/gitblit/tickets/TicketIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ public boolean deleteAll(RepositoryModel repository) {
return false;
}

/**
* Checks if a tickets index exists, that is compatible with Lucene.INDEX_VERSION
* and the Lucene codec version.
*
* @return true if no tickets index is found, false otherwise.
*
* @since 1.9.0
*/
boolean shouldReindex() {
return ! this.indexStore.hasIndex();
}

/**
* Bulk Add/Update tickets in the Lucene index
*
Expand Down Expand Up @@ -665,4 +677,4 @@ private int unpackInt(Document doc, Lucene lucene) {
int i = Integer.parseInt(val);
return i;
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/gitblit/utils/LuceneIndexStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public boolean hasIndex()
{
return indexFolder.exists() &&
indexFolder.isDirectory() &&
(indexFolder.list().length > 1);
(indexFolder.list().length > 1); // Must have more than 'write.lock'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ protected ITicketService getService(boolean deleteAll) throws Exception {
IUserManager userManager = new UserManager(runtimeManager, pluginManager).start();
IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, pluginManager, userManager).start();

BranchTicketService service = new BranchTicketService(
BranchTicketService service = (BranchTicketService) new BranchTicketService(
runtimeManager,
pluginManager,
notificationManager,
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/gitblit/tests/FileTicketServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected ITicketService getService(boolean deleteAll) throws Exception {
IUserManager userManager = new UserManager(runtimeManager, pluginManager).start();
IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, pluginManager, userManager).start();

FileTicketService service = new FileTicketService(
FileTicketService service = (FileTicketService) new FileTicketService(
runtimeManager,
pluginManager,
notificationManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected ITicketService getService(boolean deleteAll) throws Exception {
IUserManager userManager = new UserManager(runtimeManager, pluginManager).start();
IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, pluginManager, userManager).start();

RedisTicketService service = new RedisTicketService(
RedisTicketService service = (RedisTicketService) new RedisTicketService(
runtimeManager,
pluginManager,
notificationManager,
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/gitblit/tests/UITicketTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ protected ITicketService getService(boolean deleteAll) throws Exception {
IUserManager userManager = new UserManager(runtimeManager, pluginManager).start();
IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, pluginManager, userManager).start();

BranchTicketService service = new BranchTicketService(
BranchTicketService service = (BranchTicketService) new BranchTicketService(
runtimeManager,
pluginManager,
notificationManager,
Expand Down

0 comments on commit 63dbdfd

Please sign in to comment.