From e26d9338ddc5f0f0440947e60013a57b82043783 Mon Sep 17 00:00:00 2001 From: James Moger Date: Tue, 30 Oct 2012 17:01:57 -0400 Subject: [PATCH] Refactor GC period into an integer for simpler translations Also hooked-up GC settings in the Manager. --- distrib/gitblit.properties | 10 +++---- src/com/gitblit/GCExecutor.java | 4 +-- src/com/gitblit/GitBlit.java | 25 ++++++++++++++-- .../gitblit/client/EditRepositoryDialog.java | 15 ++++++++++ src/com/gitblit/models/RepositoryModel.java | 2 +- .../wicket/pages/EditRepositoryPage.java | 30 +++++++++++++++++-- 6 files changed, 72 insertions(+), 14 deletions(-) diff --git a/distrib/gitblit.properties b/distrib/gitblit.properties index 43432294..411699ff 100644 --- a/distrib/gitblit.properties +++ b/distrib/gitblit.properties @@ -110,6 +110,8 @@ git.defaultAuthorizationControl = NAMED # Enable JGit-based garbage collection. (!!EXPERIMENTAL!!) # +# USE AT YOUR OWN RISK! +# # If enabled, the garbage collection executor scans all repositories once a day # at the hour of your choosing. The GC executor will take each repository "offline", # one-at-a-time, to check if the repository satisfies it's GC trigger requirements. @@ -121,8 +123,6 @@ git.defaultAuthorizationControl = NAMED # especially on Windows systems, so if you are using other tools please coordinate # their usage with your GC Executor schedule or do not use this feature. # -# Use this feature at your own risk! -# # The GC algorithm complex and the JGit team advises caution when using their # young implementation of GC. # @@ -148,8 +148,8 @@ git.garbageCollectionHour = 0 # SINCE 1.2.0 git.defaultGarbageCollectionThreshold = 500k -# The default period between GCs for a repository. If the total filesize of the -# loose object exceeds *git.garbageCollectionThreshold* or the repository's +# The default period, in days, between GCs for a repository. If the total filesize +# of the loose object exceeds *git.garbageCollectionThreshold* or the repository's # custom threshold, this period will be short-circuited. # # e.g. if a repository collects 100KB of loose objects every day with a 500KB @@ -167,7 +167,7 @@ git.defaultGarbageCollectionThreshold = 500k # The minimum value is 1 day since the GC Executor only runs once a day. # # SINCE 1.2.0 -git.defaultGarbageCollectionPeriod = 7 days +git.defaultGarbageCollectionPeriod = 7 # Number of bytes of a pack file to load into memory in a single read operation. # This is the "page size" of the JGit buffer cache, used for all pack access diff --git a/src/com/gitblit/GCExecutor.java b/src/com/gitblit/GCExecutor.java index c5fe43b4..243cbb92 100644 --- a/src/com/gitblit/GCExecutor.java +++ b/src/com/gitblit/GCExecutor.java @@ -33,7 +33,6 @@ import org.slf4j.LoggerFactory; import com.gitblit.models.RepositoryModel; import com.gitblit.utils.FileUtils; -import com.gitblit.utils.TimeUtils; /** * The GC executor handles periodic garbage collection in repositories. @@ -162,14 +161,13 @@ public class GCExecutor implements Runnable { RepoStatistics stats = gc.getStatistics(); // determine if this is a scheduled GC - int gcPeriodInDays = TimeUtils.convertFrequencyToMinutes(model.gcPeriod)/(60*24); Calendar cal = Calendar.getInstance(); cal.setTime(model.lastGC); cal.set(Calendar.HOUR_OF_DAY, 0); cal.set(Calendar.MINUTE, 0); cal.set(Calendar.SECOND, 0); cal.set(Calendar.MILLISECOND, 0); - cal.add(Calendar.DATE, gcPeriodInDays); + cal.add(Calendar.DATE, model.gcPeriod); Date gcDate = cal.getTime(); boolean shouldCollectGarbage = now.after(gcDate); diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java index 6e587caa..e7b7bb9b 100644 --- a/src/com/gitblit/GitBlit.java +++ b/src/com/gitblit/GitBlit.java @@ -1445,7 +1445,7 @@ public class GitBlit implements ServletContextListener { Constants.CONFIG_GITBLIT, null, "federationSets"))); model.isFederated = getConfig(config, "isFederated", false); model.gcThreshold = getConfig(config, "gcThreshold", settings.getString(Keys.git.defaultGarbageCollectionThreshold, "500KB")); - model.gcPeriod = getConfig(config, "gcPeriod", settings.getString(Keys.git.defaultGarbageCollectionPeriod, "7 days")); + model.gcPeriod = getConfig(config, "gcPeriod", settings.getInteger(Keys.git.defaultGarbageCollectionPeriod, 7)); try { model.lastGC = new SimpleDateFormat(Constants.ISO8601).parse(getConfig(config, "lastGC", "1970-01-01'T'00:00:00Z")); } catch (Exception e) { @@ -1730,6 +1730,27 @@ public class GitBlit implements ServletContextListener { private boolean getConfig(StoredConfig config, String field, boolean defaultValue) { return config.getBoolean(Constants.CONFIG_GITBLIT, field, defaultValue); } + + /** + * Returns the gitblit string value for the specified key. If key is not + * set, returns defaultValue. + * + * @param config + * @param field + * @param defaultValue + * @return field value or defaultValue + */ + private int getConfig(StoredConfig config, String field, int defaultValue) { + String value = config.getString(Constants.CONFIG_GITBLIT, null, field); + if (StringUtils.isEmpty(value)) { + return defaultValue; + } + try { + return Integer.parseInt(value); + } catch (Exception e) { + } + return defaultValue; + } /** * Creates/updates the repository model keyed by reopsitoryName. Saves all @@ -1896,7 +1917,7 @@ public class GitBlit implements ServletContextListener { repository.federationStrategy.name()); config.setBoolean(Constants.CONFIG_GITBLIT, null, "isFederated", repository.isFederated); config.setString(Constants.CONFIG_GITBLIT, null, "gcThreshold", repository.gcThreshold); - config.setString(Constants.CONFIG_GITBLIT, null, "gcPeriod", repository.gcPeriod); + config.setInt(Constants.CONFIG_GITBLIT, null, "gcPeriod", repository.gcPeriod); if (repository.lastGC != null) { config.setString(Constants.CONFIG_GITBLIT, null, "lastGC", new SimpleDateFormat(Constants.ISO8601).format(repository.lastGC)); } diff --git a/src/com/gitblit/client/EditRepositoryDialog.java b/src/com/gitblit/client/EditRepositoryDialog.java index 06621c21..d91d18dd 100644 --- a/src/com/gitblit/client/EditRepositoryDialog.java +++ b/src/com/gitblit/client/EditRepositoryDialog.java @@ -120,6 +120,10 @@ public class EditRepositoryDialog extends JDialog { private JComboBox ownerField; private JComboBox headRefField; + + private JComboBox gcPeriod; + + private JTextField gcThreshold; private RegistrantPermissionsPanel usersPalette; @@ -193,6 +197,13 @@ public class EditRepositoryDialog extends JDialog { anRepository.availableRefs.toArray()); headRefField.setSelectedItem(anRepository.HEAD); } + + Integer [] gcPeriods = { 1, 2, 3, 4, 5, 7, 10, 14 }; + gcPeriod = new JComboBox(gcPeriods); + gcPeriod.setSelectedItem(anRepository.gcPeriod); + + gcThreshold = new JTextField(8); + gcThreshold.setText(anRepository.gcThreshold); ownerField = new JComboBox(); @@ -288,6 +299,8 @@ public class EditRepositoryDialog extends JDialog { .add(newFieldPanel(Translation.get("gb.origin"), originField)); fieldsPanel.add(newFieldPanel(Translation.get("gb.headRef"), headRefField)); fieldsPanel.add(newFieldPanel(Translation.get("gb.owner"), ownerField)); + fieldsPanel.add(newFieldPanel(Translation.get("gb.gcPeriod"), gcPeriod)); + fieldsPanel.add(newFieldPanel(Translation.get("gb.gcThreshold"), gcThreshold)); fieldsPanel.add(newFieldPanel(Translation.get("gb.enableTickets"), useTickets)); @@ -534,6 +547,8 @@ public class EditRepositoryDialog extends JDialog { : ownerField.getSelectedItem().toString(); repository.HEAD = headRefField.getSelectedItem() == null ? null : headRefField.getSelectedItem().toString(); + repository.gcPeriod = (Integer) gcPeriod.getSelectedItem(); + repository.gcThreshold = gcThreshold.getText(); repository.useTickets = useTickets.isSelected(); repository.useDocs = useDocs.isSelected(); repository.showRemoteBranches = showRemoteBranches.isSelected(); diff --git a/src/com/gitblit/models/RepositoryModel.java b/src/com/gitblit/models/RepositoryModel.java index 23ce9e3b..ed9e7188 100644 --- a/src/com/gitblit/models/RepositoryModel.java +++ b/src/com/gitblit/models/RepositoryModel.java @@ -77,7 +77,7 @@ public class RepositoryModel implements Serializable, Comparable("HEAD", availableRefs).setEnabled(availableRefs.size() > 0)); - List gcPeriods = Arrays.asList("1 day", "2 days", "3 days", "4 days", "5 days", "7 days", "10 days", "14 days"); - form.add(new DropDownChoice("gcPeriod", gcPeriods)); - form.add(new TextField("gcThreshold")); + boolean gcEnabled = GitBlit.getBoolean(Keys.git.enableGarbageCollection, false); + List gcPeriods = Arrays.asList(1, 2, 3, 4, 5, 7, 10, 14 ); + form.add(new DropDownChoice("gcPeriod", gcPeriods, new GCPeriodRenderer()).setEnabled(gcEnabled)); + form.add(new TextField("gcThreshold").setEnabled(gcEnabled)); // federation strategies - remove ORIGIN choice if this repository has // no origin. @@ -619,4 +620,27 @@ public class EditRepositoryPage extends RootSubPage { return Integer.toString(index); } } + + private class GCPeriodRenderer implements IChoiceRenderer { + + private static final long serialVersionUID = 1L; + + public GCPeriodRenderer() { + } + + @Override + public String getDisplayValue(Integer value) { + if (value == 1) { + return getString("gb.duration.oneDay"); + } else { + return MessageFormat.format(getString("gb.duration.days"), value); + } + } + + @Override + public String getIdValue(Integer value, int index) { + return Integer.toString(index); + } + } + } -- 2.39.5