diff options
author | Marius Mathiesen <marius@shortcut.no> | 2010-02-03 15:38:03 +0100 |
---|---|---|
committer | Marius Mathiesen <marius@shortcut.no> | 2010-02-03 15:38:03 +0100 |
commit | 2ba975497d9d1fa0014d0414631210726c7ef0f3 (patch) | |
tree | ec00b9e91ad2268b7366aaaf6dd9cfa443638831 | |
parent | 95b7efce99f7c405cb0a50185f467f8aba74f932 (diff) | |
download | gitorious-mainline-outdated-2ba975497d9d1fa0014d0414631210726c7ef0f3.zip gitorious-mainline-outdated-2ba975497d9d1fa0014d0414631210726c7ef0f3.tar.gz gitorious-mainline-outdated-2ba975497d9d1fa0014d0414631210726c7ef0f3.tar.bz2 |
Adding a counter of pushes since last garbage collection, incremented when pushed to and reset when gc-ed
The algorithm for finding gc candidates is unchanged, will run this in production and collect metrics for changing this the next few days.
- Also tracking disk usage for repositories, calculated when pushed to and stored in the database
-rw-r--r-- | app/models/repository.rb | 17 | ||||
-rw-r--r-- | app/processors/push_event_processor.rb | 4 | ||||
-rw-r--r-- | db/migrate/20100203132425_adding_disk_size_and_push_count_to_repositories.rb | 11 | ||||
-rw-r--r-- | db/schema.rb | 4 | ||||
-rw-r--r-- | test/unit/repository_test.rb | 47 |
5 files changed, 75 insertions, 8 deletions
diff --git a/app/models/repository.rb b/app/models/repository.rb index 4c61148..ee4f1d4 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -662,11 +662,26 @@ class Repository < ActiveRecord::Base def gc! Grit::Git.with_timeout(nil) do if self.git.gc_auto - return update_attribute(:last_gc_at, Time.now.utc) + self.last_gc_at = Time.now + self.push_count_since_gc = 0 + return save end end end + def register_push + self.last_pushed_at = Time.now.utc + self.push_count_since_gc = push_count_since_gc.to_i + 1 + end + + def update_disk_usage + self.disk_usage = calculate_disk_usage + end + + def calculate_disk_usage + @calculated_disk_usage ||= `du -sb #{full_repository_path} 2>/dev/null`.chomp.to_i + end + def matches_regexp?(term) return user.login =~ term || name =~ term || diff --git a/app/processors/push_event_processor.rb b/app/processors/push_event_processor.rb index 5ff15f5..a7d49ef 100644 --- a/app/processors/push_event_processor.rb +++ b/app/processors/push_event_processor.rb @@ -79,7 +79,9 @@ class PushEventProcessor < ApplicationProcessor r, name, @identifier = @revname.split("/", 3) @target = {'tags' => :tag, 'heads' => :head, 'merge-requests' => :review}[name] if @target != :review && @repository - @repository.update_attribute(:last_pushed_at, Time.now.utc) + @repository.update_disk_usage + @repository.register_push + @repository.save end process_push end diff --git a/db/migrate/20100203132425_adding_disk_size_and_push_count_to_repositories.rb b/db/migrate/20100203132425_adding_disk_size_and_push_count_to_repositories.rb new file mode 100644 index 0000000..2381f64 --- /dev/null +++ b/db/migrate/20100203132425_adding_disk_size_and_push_count_to_repositories.rb @@ -0,0 +1,11 @@ +class AddingDiskSizeAndPushCountToRepositories < ActiveRecord::Migration + def self.up + add_column :repositories, :disk_usage, :integer + add_column :repositories, :push_count_since_gc, :integer + end + + def self.down + remove_column :repositories, :disk_usage + remove_column :repositories, :push_count_since_gc + end +end diff --git a/db/schema.rb b/db/schema.rb index d0b5c2d..188607c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20100126094829) do +ActiveRecord::Schema.define(:version => 20100203132425) do create_table "cloners", :force => true do |t| t.string "ip" @@ -279,6 +279,8 @@ ActiveRecord::Schema.define(:version => 20100126094829) do t.boolean "notify_committers_on_new_merge_request", :default => true t.datetime "last_gc_at" t.boolean "merge_requests_enabled", :default => true + t.integer "disk_usage" + t.integer "push_count_since_gc" end add_index "repositories", ["hashed_path"], :name => "index_repositories_on_hashed_path", :unique => true diff --git a/test/unit/repository_test.rb b/test/unit/repository_test.rb index da1d938..50d7794 100644 --- a/test/unit/repository_test.rb +++ b/test/unit/repository_test.rb @@ -960,17 +960,23 @@ class RepositoryTest < ActiveSupport::TestCase context "garbage collection" do setup do @repository = repositories(:johans) + @now = Time.now + Time.stubs(:now).returns(@now) + @repository.stubs(:git).returns(stub()) + @repository.git.expects(:gc_auto).returns(true) end should "have a gc! method that updates last_gc_at" do - now = Time.now - Time.stubs(:now).returns(now) - @repository.stubs(:git).returns(stub()) - @repository.git.expects(:gc_auto).returns(true) assert_nil @repository.last_gc_at assert @repository.gc! assert_not_nil @repository.last_gc_at - assert_equal now, @repository.last_gc_at + assert_equal @now, @repository.last_gc_at + end + + should "set push_count_since_gc to 0 when doing gc" do + @repository.push_count_since_gc = 10 + @repository.gc! + assert_equal 0, @repository.push_count_since_gc end end @@ -1118,6 +1124,37 @@ class RepositoryTest < ActiveSupport::TestCase end end + context "Calculation of disk usage" do + setup do + @repository = repositories(:johans) + @bytes = 90129 + end + + should "save the bytes used" do + @repository.expects(:calculate_disk_usage).returns(@bytes) + @repository.update_disk_usage + assert_equal @bytes, @repository.disk_usage + end + end + + context "Pushing" do + setup do + @repository = repositories(:johans) + end + + should "update last_pushed_at" do + @repository.last_pushed_at = 1.hour.ago.utc + @repository.register_push + assert @repository.last_pushed_at > 1.hour.ago.utc + end + + should "increment the number of pushes" do + @repository.push_count_since_gc = 2 + @repository.register_push + assert_equal 3, @repository.push_count_since_gc + end + end + end |