]> source.dussan.org Git - gitblit.git/commitdiff
Reindex tickets on server start if no index exists 1168/head
authorFlorian Zschocke <florian.zschocke@devolo.de>
Sun, 5 Mar 2017 19:12:48 +0000 (20:12 +0100)
committerFlorian Zschocke <florian.zschocke@devolo.de>
Sun, 5 Mar 2017 19:27:40 +0000 (20:27 +0100)
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`.

src/main/java/com/gitblit/tickets/BranchTicketService.java
src/main/java/com/gitblit/tickets/FileTicketService.java
src/main/java/com/gitblit/tickets/ITicketService.java
src/main/java/com/gitblit/tickets/NullTicketService.java
src/main/java/com/gitblit/tickets/RedisTicketService.java
src/main/java/com/gitblit/tickets/TicketIndexer.java
src/main/java/com/gitblit/utils/LuceneIndexStore.java
src/test/java/com/gitblit/tests/BranchTicketServiceTest.java
src/test/java/com/gitblit/tests/FileTicketServiceTest.java
src/test/java/com/gitblit/tests/RedisTicketServiceTest.java
src/test/java/com/gitblit/tests/UITicketTest.java

index 7bef435aa8bf2a5784db013c9550bf8ac8f48592..8430c546b3a9ee274d8950f3cf2f637b9694ad11 100644 (file)
@@ -110,9 +110,8 @@ public class BranchTicketService extends ITicketService implements RefsChangedLi
        }
 
        @Override
-       public BranchTicketService start() {
+       public void onStart() {
                log.info("{} started", getClass().getSimpleName());
-               return this;
        }
 
        @Override
index 1e82f0de19a3ec6e916a6e1d8092be3dcec515c2..05670468a404a57f83022708eda2b784b501816a 100644 (file)
@@ -80,9 +80,8 @@ public class FileTicketService extends ITicketService {
        }
 
        @Override
-       public FileTicketService start() {
+       public void onStart() {
                log.info("{} started", getClass().getSimpleName());
-               return this;
        }
 
        @Override
index 20b6505b4bcd4f57c9aa401e3e514e4a48e292e0..3252a603f27f91b19dba8f30eef42eb91996e351 100644 (file)
@@ -181,7 +181,24 @@ public abstract class ITicketService implements IManager {
         * @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 abstract class ITicketService implements IManager {
                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 abstract class ITicketService implements IManager {
                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 abstract class ITicketService implements IManager {
                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.
index 3947b945e8ddf68fdd504a52f7fb35e90420953f..050c699051b05c42d9051373d2db46afe45c6b1a 100644 (file)
@@ -61,9 +61,8 @@ public class NullTicketService extends ITicketService {
        }
 
        @Override
-       public NullTicketService start() {
+       public void onStart() {
                log.info("{} started", getClass().getSimpleName());
-               return this;
        }
 
        @Override
index 0f9ad174e1664f776dc064e90dc7137c3adb6aed..4e632317f3d9d1d88b056a23ecc26eaad869a627 100644 (file)
@@ -83,12 +83,11 @@ public class RedisTicketService extends ITicketService {
        }
 
        @Override
-       public RedisTicketService start() {
+       public void onStart() {
                log.info("{} started", getClass().getSimpleName());
                if (!isReady()) {
                        log.warn("{} is not ready!", getClass().getSimpleName());
                }
-               return this;
        }
 
        @Override
index bc08fc8898cd347a34742d7e6bc2704aaa6a30af..7c1644872c721f30817e41e9775faaf9d16029d6 100644 (file)
@@ -226,6 +226,18 @@ public class TicketIndexer {
                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 @@ public class TicketIndexer {
                int i = Integer.parseInt(val);
                return i;
        }
-}
\ No newline at end of file
+}
index 0ccfd2e8c6b65458436dc1195c607cc7003027dc..c05e2019b4487bb6f18a6b704dd652b4685631b5 100644 (file)
@@ -92,7 +92,7 @@ public class LuceneIndexStore
        {
                return indexFolder.exists() &&
                                indexFolder.isDirectory() &&
-                               (indexFolder.list().length > 1);
+                               (indexFolder.list().length > 1);  // Must have more than 'write.lock'
        }
 
 }
index 0a5de1961d79fa3cd71c726f66899b2699d6264d..0e9d8874646e0920fde0808acae25195b4bf97fb 100644 (file)
@@ -59,7 +59,7 @@ public class BranchTicketServiceTest extends TicketServiceTest {
                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,
index 1fb2eed96f705580cbe418701a7ed6ce0d88ec30..c4a63c4101962e80591d13ac503313d6d163d54b 100644 (file)
@@ -58,7 +58,7 @@ public class FileTicketServiceTest extends TicketServiceTest {
                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,
index 48011ade259ad86c35c4b25ee865ebe8aa5f3a56..5f3cb94fcfba65ae99d8d2c854e325a8d67c92f5 100644 (file)
@@ -66,7 +66,7 @@ public class RedisTicketServiceTest extends TicketServiceTest {
                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,
index 54aa1e1e7d92cd62df56d84413243104fc63f20a..e89c32fcf153ce255efa049f0a57a17f933a470c 100644 (file)
@@ -83,7 +83,7 @@ public class UITicketTest extends GitblitUnitTest {
                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,