]> source.dussan.org Git - gitblit.git/commitdiff
Move GarbageCollector repository methods to the RepositoryManager
authorJames Moger <james.moger@gitblit.com>
Fri, 28 Feb 2014 19:04:37 +0000 (14:04 -0500)
committerJames Moger <james.moger@gitblit.com>
Fri, 28 Feb 2014 19:04:37 +0000 (14:04 -0500)
src/main/java/com/gitblit/manager/GitblitManager.java
src/main/java/com/gitblit/manager/IRepositoryManager.java
src/main/java/com/gitblit/manager/RepositoryManager.java
src/main/java/com/gitblit/service/GarbageCollectorService.java

index 08d64085307448a66d870c4f88bf5bf17b3aaf22..6eb6023640fa7f2109d37886a22e09dae2ed8b99 100644 (file)
@@ -1099,4 +1099,19 @@ public class GitblitManager implements IGitblit {
        public boolean deletePendingFederationProposal(FederationProposal proposal) {
                return federationManager.deletePendingFederationProposal(proposal);
        }
+
+       @Override
+       public void closeAll() {
+               repositoryManager.closeAll();
+       }
+
+       @Override
+       public void close(String repository) {
+               repositoryManager.close(repository);
+       }
+
+       @Override
+       public boolean isIdle(Repository repository) {
+               return repositoryManager.isIdle(repository);
+       }
 }
index a40019151539d259447bd9a28a99abefb9f4a307..23c61cb493da6384896595bdf2c80e13d5d2e33b 100644 (file)
@@ -393,4 +393,23 @@ public interface IRepositoryManager extends IManager {
         */
        boolean isCollectingGarbage(String repositoryName);
 
+       /**
+        * Ensures that all cached repositories are completely closed and their resources
+        * are properly released.
+        */
+       void closeAll();
+
+       /**
+        * Ensures that a cached repository is completely closed and it's resources
+        * are properly released.
+        */
+       void close(String repository);
+
+       /**
+        * Returns true if the repository is idle (not being accessed).
+        *
+        * @param repository
+        * @return true if the repository is idle
+        */
+       boolean isIdle(Repository repository);
 }
\ No newline at end of file
index 6b4fe97fa3e960cfa6e34c0356e66d54973a5f80..e412deba35fb4931b7958e248be63979d339ac66 100644 (file)
@@ -169,6 +169,7 @@ public class RepositoryManager implements IRepositoryManager {
                gcExecutor.close();
                mirrorExecutor.close();
 
+               closeAll();
                return this;
        }
 
@@ -1083,13 +1084,50 @@ public class RepositoryManager implements IRepositoryManager {
                return size;
        }
 
+       /**
+        * Returns true if the repository is idle (not being accessed).
+        *
+        * @param repository
+        * @return true if the repository is idle
+        */
+       @Override
+       public boolean isIdle(Repository repository) {
+               try {
+                       // Read the use count.
+                       // An idle use count is 2:
+                       // +1 for being in the cache
+                       // +1 for the repository parameter in this method
+                       Field useCnt = Repository.class.getDeclaredField("useCnt");
+                       useCnt.setAccessible(true);
+                       int useCount = ((AtomicInteger) useCnt.get(repository)).get();
+                       return useCount == 2;
+               } catch (Exception e) {
+                       logger.warn(MessageFormat
+                                       .format("Failed to reflectively determine use count for repository {0}",
+                                                       repository.getDirectory().getPath()), e);
+               }
+               return false;
+       }
+
+       /**
+        * Ensures that all cached repository are completely closed and their resources
+        * are properly released.
+        */
+       @Override
+       public void closeAll() {
+               for (String repository : getRepositoryList()) {
+                       close(repository);
+               }
+       }
+
        /**
         * Ensure that a cached repository is completely closed and its resources
         * are properly released.
         *
         * @param repositoryName
         */
-       private void closeRepository(String repositoryName) {
+       @Override
+       public void close(String repositoryName) {
                Repository repository = getRepository(repositoryName);
                if (repository == null) {
                        return;
@@ -1114,7 +1152,7 @@ public class RepositoryManager implements IRepositoryManager {
                                                        repositoryName), e);
                }
                if (uses > 0) {
-                       logger.info(MessageFormat
+                       logger.debug(MessageFormat
                                        .format("{0}.useCnt={1}, calling close() {2} time(s) to close object and ref databases",
                                                        repositoryName, uses, uses));
                        for (int i = 0; i < uses; i++) {
@@ -1252,7 +1290,7 @@ public class RepositoryManager implements IRepositoryManager {
                                                        "Failed to rename ''{0}'' because ''{1}'' already exists.",
                                                        repositoryName, repository.name));
                                }
-                               closeRepository(repositoryName);
+                               close(repositoryName);
                                File folder = new File(repositoriesFolder, repositoryName);
                                File destFolder = new File(repositoriesFolder, repository.name);
                                if (destFolder.exists()) {
@@ -1477,7 +1515,7 @@ public class RepositoryManager implements IRepositoryManager {
        @Override
        public boolean deleteRepository(String repositoryName) {
                try {
-                       closeRepository(repositoryName);
+                       close(repositoryName);
                        // clear the repository cache
                        clearRepositoryMetadataCache(repositoryName);
 
index 8dbd8d83810e813f67072c4e09b1fcf9607166d5..b98560fd95e16ef0bf9274f9dc7df4fd2efd317e 100644 (file)
@@ -15,7 +15,6 @@
  */\r
 package com.gitblit.service;\r
 \r
-import java.lang.reflect.Field;\r
 import java.text.MessageFormat;\r
 import java.util.Calendar;\r
 import java.util.Date;\r
@@ -23,7 +22,6 @@ import java.util.Map;
 import java.util.Properties;\r
 import java.util.concurrent.ConcurrentHashMap;\r
 import java.util.concurrent.atomic.AtomicBoolean;\r
-import java.util.concurrent.atomic.AtomicInteger;\r
 \r
 import org.eclipse.jgit.api.GarbageCollectCommand;\r
 import org.eclipse.jgit.api.Git;\r
@@ -33,7 +31,6 @@ import org.slf4j.LoggerFactory;
 \r
 import com.gitblit.IStoredSettings;\r
 import com.gitblit.Keys;\r
-import com.gitblit.Keys.git;\r
 import com.gitblit.manager.IRepositoryManager;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.utils.FileUtils;\r
@@ -160,7 +157,7 @@ public class GarbageCollectorService implements Runnable {
                                        continue;\r
                                }\r
 \r
-                               if (!isRepositoryIdle(repository)) {\r
+                               if (!repositoryManager.isIdle(repository)) {\r
                                        logger.debug(MessageFormat.format("GCExecutor is skipping {0} because it is not idle", repositoryName));\r
                                        continue;\r
                                }\r
@@ -228,22 +225,4 @@ public class GarbageCollectorService implements Runnable {
 \r
                running.set(false);\r
        }\r
-\r
-       private boolean isRepositoryIdle(Repository repository) {\r
-               try {\r
-                       // Read the use count.\r
-                       // An idle use count is 2:\r
-                       // +1 for being in the cache\r
-                       // +1 for the repository parameter in this method\r
-                       Field useCnt = Repository.class.getDeclaredField("useCnt");\r
-                       useCnt.setAccessible(true);\r
-                       int useCount = ((AtomicInteger) useCnt.get(repository)).get();\r
-                       return useCount == 2;\r
-               } catch (Exception e) {\r
-                       logger.warn(MessageFormat\r
-                                       .format("Failed to reflectively determine use count for repository {0}",\r
-                                                       repository.getDirectory().getPath()), e);\r
-               }\r
-               return false;\r
-       }\r
 }\r