From 63dbdfda13daa78a26f1c2e77b0a4bfd5a35df8d Mon Sep 17 00:00:00 2001 From: Florian Zschocke Date: Sun, 5 Mar 2017 20:12:48 +0100 Subject: [PATCH] Reindex tickets on server start if no index exists 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`. --- .../gitblit/tickets/BranchTicketService.java | 3 +- .../gitblit/tickets/FileTicketService.java | 3 +- .../com/gitblit/tickets/ITicketService.java | 43 ++++++++++++++++--- .../gitblit/tickets/NullTicketService.java | 3 +- .../gitblit/tickets/RedisTicketService.java | 3 +- .../com/gitblit/tickets/TicketIndexer.java | 14 +++++- .../com/gitblit/utils/LuceneIndexStore.java | 2 +- .../tests/BranchTicketServiceTest.java | 2 +- .../gitblit/tests/FileTicketServiceTest.java | 2 +- .../gitblit/tests/RedisTicketServiceTest.java | 2 +- .../java/com/gitblit/tests/UITicketTest.java | 2 +- 11 files changed, 58 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/gitblit/tickets/BranchTicketService.java b/src/main/java/com/gitblit/tickets/BranchTicketService.java index 7bef435aa..8430c546b 100644 --- a/src/main/java/com/gitblit/tickets/BranchTicketService.java +++ b/src/main/java/com/gitblit/tickets/BranchTicketService.java @@ -110,9 +110,8 @@ public BranchTicketService( } @Override - public BranchTicketService start() { + public void onStart() { log.info("{} started", getClass().getSimpleName()); - return this; } @Override diff --git a/src/main/java/com/gitblit/tickets/FileTicketService.java b/src/main/java/com/gitblit/tickets/FileTicketService.java index 1e82f0de1..05670468a 100644 --- a/src/main/java/com/gitblit/tickets/FileTicketService.java +++ b/src/main/java/com/gitblit/tickets/FileTicketService.java @@ -80,9 +80,8 @@ public FileTicketService( } @Override - public FileTicketService start() { + public void onStart() { log.info("{} started", getClass().getSimpleName()); - return this; } @Override diff --git a/src/main/java/com/gitblit/tickets/ITicketService.java b/src/main/java/com/gitblit/tickets/ITicketService.java index 20b6505b4..3252a603f 100644 --- a/src/main/java/com/gitblit/tickets/ITicketService.java +++ b/src/main/java/com/gitblit/tickets/ITicketService.java @@ -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. @@ -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 @@ -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 @@ -1343,6 +1360,18 @@ public List 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. diff --git a/src/main/java/com/gitblit/tickets/NullTicketService.java b/src/main/java/com/gitblit/tickets/NullTicketService.java index 3947b945e..050c69905 100644 --- a/src/main/java/com/gitblit/tickets/NullTicketService.java +++ b/src/main/java/com/gitblit/tickets/NullTicketService.java @@ -61,9 +61,8 @@ public boolean isReady() { } @Override - public NullTicketService start() { + public void onStart() { log.info("{} started", getClass().getSimpleName()); - return this; } @Override diff --git a/src/main/java/com/gitblit/tickets/RedisTicketService.java b/src/main/java/com/gitblit/tickets/RedisTicketService.java index 0f9ad174e..4e632317f 100644 --- a/src/main/java/com/gitblit/tickets/RedisTicketService.java +++ b/src/main/java/com/gitblit/tickets/RedisTicketService.java @@ -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 diff --git a/src/main/java/com/gitblit/tickets/TicketIndexer.java b/src/main/java/com/gitblit/tickets/TicketIndexer.java index bc08fc889..7c1644872 100644 --- a/src/main/java/com/gitblit/tickets/TicketIndexer.java +++ b/src/main/java/com/gitblit/tickets/TicketIndexer.java @@ -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 * @@ -665,4 +677,4 @@ private int unpackInt(Document doc, Lucene lucene) { int i = Integer.parseInt(val); return i; } -} \ No newline at end of file +} diff --git a/src/main/java/com/gitblit/utils/LuceneIndexStore.java b/src/main/java/com/gitblit/utils/LuceneIndexStore.java index 0ccfd2e8c..c05e2019b 100644 --- a/src/main/java/com/gitblit/utils/LuceneIndexStore.java +++ b/src/main/java/com/gitblit/utils/LuceneIndexStore.java @@ -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' } } diff --git a/src/test/java/com/gitblit/tests/BranchTicketServiceTest.java b/src/test/java/com/gitblit/tests/BranchTicketServiceTest.java index 0a5de1961..0e9d88746 100644 --- a/src/test/java/com/gitblit/tests/BranchTicketServiceTest.java +++ b/src/test/java/com/gitblit/tests/BranchTicketServiceTest.java @@ -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, diff --git a/src/test/java/com/gitblit/tests/FileTicketServiceTest.java b/src/test/java/com/gitblit/tests/FileTicketServiceTest.java index 1fb2eed96..c4a63c410 100644 --- a/src/test/java/com/gitblit/tests/FileTicketServiceTest.java +++ b/src/test/java/com/gitblit/tests/FileTicketServiceTest.java @@ -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, diff --git a/src/test/java/com/gitblit/tests/RedisTicketServiceTest.java b/src/test/java/com/gitblit/tests/RedisTicketServiceTest.java index 48011ade2..5f3cb94fc 100644 --- a/src/test/java/com/gitblit/tests/RedisTicketServiceTest.java +++ b/src/test/java/com/gitblit/tests/RedisTicketServiceTest.java @@ -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, diff --git a/src/test/java/com/gitblit/tests/UITicketTest.java b/src/test/java/com/gitblit/tests/UITicketTest.java index 54aa1e1e7..e89c32fcf 100644 --- a/src/test/java/com/gitblit/tests/UITicketTest.java +++ b/src/test/java/com/gitblit/tests/UITicketTest.java @@ -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,