Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initialize lock file from config

* Service nodes can now configure the lock file as
  database_lock_file: /tmp/LOCK (or similar)
* This prevents failures in an environment where
  /var/vcap (the default) is not writable
* Added 2 new tests (one of which fails on master
  in such an environment)

Change-Id: I57710b1a34a04314e85613afdea3d71676dc0d05
  • Loading branch information...
commit e9b54c07be7f200b97b42fa96a5324e582f97f01 1 parent bbc1828
@dsyer dsyer authored
View
34 lib/base/datamapper_l.rb
@@ -2,14 +2,6 @@
require "monitor"
require "datamapper"
-LOCK_FILE = "/var/vcap/sys/run/LOCK"
-
-# Initialize lockfile
-FileUtils.mkdir_p(File.dirname(LOCK_FILE))
-File.open(LOCK_FILE, 'w') do |file|
- file.truncate(0)
-end
-
# Export Monitor's count
class Monitor
def count
@@ -41,7 +33,13 @@ def synchronize
end
end
- LOCK = GlobalMutex.new(LOCK_FILE)
+ def self.initialize_lock_file(lock_file)
+ FileUtils.mkdir_p(File.dirname(lock_file))
+ File.open(lock_file, 'w') do |file|
+ file.truncate(0)
+ end
+ @lock = GlobalMutex.new(lock_file)
+ end
# The following code will overwrite DataMapper's functions, and replace
# them with a synchronized version of the same function.
@@ -50,13 +48,13 @@ module Resource
alias original_destroy destroy
def save
- LOCK.synchronize do
+ @lock.synchronize do
original_save
end
end
def destroy
- LOCK.synchronize do
+ @lock.synchronize do
original_destroy
end
end
@@ -67,13 +65,13 @@ module Model
alias original_all all
def get(*args)
- LOCK.synchronize do
+ @lock.synchronize do
original_get(*args)
end
end
def all(*args)
- LOCK.synchronize do
+ @lock.synchronize do
original_all(*args)
end
end
@@ -87,7 +85,7 @@ class Collection
def each(&block)
instances = []
- LOCK.synchronize do
+ @lock.synchronize do
original_each do |instance|
instances << instance
end
@@ -96,19 +94,19 @@ def each(&block)
end
def [](*args)
- LOCK.synchronize do
+ @lock.synchronize do
original_at(*args)
end
end
def get(*args)
- LOCK.synchronize do
+ @lock.synchronize do
original_get(*args)
end
end
def empty?()
- LOCK.synchronize do
+ @lock.synchronize do
original_empty?()
end
end
@@ -120,7 +118,7 @@ module SingletonMethods
alias original_repository_execute repository_execute
def repository_execute(*args)
- LOCK.synchronize do
+ @lock.synchronize do
original_repository_execute(*args)
end
end
View
2  lib/base/node.rb
@@ -7,6 +7,7 @@
$:.unshift(File.dirname(__FILE__))
require 'base'
require 'service_message'
+require 'datamapper_l'
class VCAP::Services::Base::Node < VCAP::Services::Base::Base
include VCAP::Services::Internal
@@ -21,6 +22,7 @@ def initialize(options)
@migration_nfs = options[:migration_nfs]
@fqdn_hosts = options[:fqdn_hosts]
@op_time_limit = options[:op_time_limit]
+ DataMapper::initialize_lock_file(options[:database_lock_file])
# A default supported version
# *NOTE: All services *MUST* override this to provide the actual supported versions
View
1  lib/base/node_bin.rb
@@ -76,6 +76,7 @@ def start
:memory_overhead => parse_property(config, "memory_overhead", Numeric, :optional => true, :default => 0.0),
:max_disk => parse_property(config, "max_disk", Numeric, :optional => true, :default => 128.0),
:disk_overhead => parse_property(config, "disk_overhead", Numeric, :optional => true, :default => 0.0),
+ :database_lock_file => parse_property(config, "database_lock_file", String, :optional => true, :default => "/var/vcap/sys/run/LOCK")
}
# Workaround for services that support running the service both inside and outside warden
use_warden = parse_property(config, "use_warden", Boolean, :optional => true, :default => false)
View
4 spec/helper/base_spec_helper.rb
@@ -27,6 +27,7 @@ def self.nats_uri
NODE_TIMEOUT = 5
PLAN = "free"
CAPACITY = 200
+ DATABASE_LOCK_FILE = "/tmp/LOCK"
def self.default(more=nil)
options = {
@@ -35,7 +36,8 @@ def self.default(more=nil)
:capacity => CAPACITY,
:ip_route => IP_ROUTE,
:mbus => NATS_URI,
- :node_timeout => NODE_TIMEOUT
+ :node_timeout => NODE_TIMEOUT,
+ :database_lock_file => DATABASE_LOCK_FILE
}
more.each { |k,v| options[k] = v } if more
options
View
17 spec/unit_test/datamapper_spec.rb
@@ -0,0 +1,17 @@
+# Copyright (c) 2009-2011 VMware, Inc.
+require 'helper/spec_helper'
+
+LOCK_FILE = "/tmp/foo"
+
+describe "datamapper extensions" do
+
+ before :all do
+ File::delete(LOCK_FILE) if File::exists?(LOCK_FILE)
+ end
+
+ it "should be able to initialize lock" do
+ require 'base/datamapper_l'
+ DataMapper::initialize_lock_file(LOCK_FILE)
+ end
+
+end
View
8 spec/unit_test/top_level_spec.rb
@@ -0,0 +1,8 @@
+# Copyright (c) 2009-2011 VMware, Inc.
+require 'helper/spec_helper'
+
+describe "main entry point" do
+ it "should load cleanly" do
+ require 'vcap_services_base'
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.