Permalink
Browse files

Bring neo4j up-to-date

Change-Id: I9d028c7ee83248a4f43e021d1a591fab481acc89
  • Loading branch information...
1 parent 5a675c2 commit c9c50354d32b2fdb3cb45f721b9021044934d149 @anfernee anfernee committed Nov 4, 2011
View
2 neo4j/Gemfile
@@ -13,7 +13,7 @@ gem "thin"
gem "rest-client"
gem 'vcap_common', :require => ['vcap/common', 'vcap/component']
-gem 'vcap_logging', :require => ['vcap/logging']
+gem 'vcap_logging', '>=0.1.3', :require => ['vcap/logging']
group :test do
gem "rake"
View
4 neo4j/Gemfile.lock
@@ -102,7 +102,7 @@ GEM
posix-spawn
thin
yajl-ruby
- vcap_logging (0.1.1)
+ vcap_logging (0.1.3)
yajl-ruby (1.0.0)
PLATFORMS
@@ -125,4 +125,4 @@ DEPENDENCIES
thin
uuidtools
vcap_common
- vcap_logging
+ vcap_logging (>= 0.1.3)
View
103 neo4j/bin/neo4j_node
@@ -1,101 +1,38 @@
#!/usr/bin/env ruby
+# -*- mode: ruby -*-
# Copyright (c) 2009-2011 VMware, Inc.
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile", __FILE__)
-require "rubygems"
-require "bundler/setup"
+$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', '..', 'base', 'lib')
+require 'base/node_bin'
$LOAD_PATH.unshift(File.expand_path("../../lib", __FILE__))
-
require "neo4j_service/neo4j_node"
-neo4j_server_conf_template = File.expand_path("../../resources/neo4j-server.properties.erb", __FILE__)
-neo4j_conf_template = File.expand_path("../../resources/neo4j.properties.erb", __FILE__)
-log4j_conf_template = File.expand_path("../../resources/log4j.properties.erb", __FILE__)
-config_file = File.expand_path("../../config/neo4j_node.yml", __FILE__)
+class VCAP::Services::Neo4j::NodeBin < VCAP::Services::Base::NodeBin
-OptionParser.new do |opts|
- opts.banner = "Usage: neo4j_node [options]"
- opts.on("-c", "--config [ARG]", "Configuration File") do |opt|
- config_file = opt
- end
- opts.on("-h", "--help", "Help") do
- puts opts
- exit
+ def node_class
+ VCAP::Services::Neo4j::Node
end
-end.parse!
-def parse_property(hash, key, type, options = {})
- obj = hash[key]
- if obj.nil?
- raise "Missing required option: #{key}" unless options[:optional]
- nil
- elsif type == Range
- raise "Invalid Range object: #{obj}" unless obj.kind_of?(Hash)
- first, last = obj["first"], obj["last"]
- raise "Invalid Range object: #{obj}" unless first.kind_of?(Integer) and last.kind_of?(Integer)
- Range.new(first, last)
- else
- raise "Invalid #{type} object: #{obj}" unless obj.kind_of?(type)
- obj
+ def default_config_file
+ config_base_dir = ENV["CLOUD_FOUNDRY_CONFIG_PATH"] || File.join(File.dirname(__FILE__), '..', 'config')
+ File.join(config_base_dir, 'neo4j_node.yml')
end
-end
-
-begin
- config = YAML.load_file(config_file)
-rescue => e
- puts "Could not read configuration file: #{e}"
- exit
-end
-
-logger = Logger.new(parse_property(config, "log_file", String, :optional => true) || STDOUT, "daily")
-logger.level = case (parse_property(config, "log_level", String, :optional => true) || "INFO")
- when "DEBUG" then Logger::DEBUG
- when "INFO" then Logger::INFO
- when "WARN" then Logger::WARN
- when "ERROR" then Logger::ERROR
- when "FATAL" then Logger::FATAL
- else Logger::UNKNOWN
-end
-options = {
- :logger => logger,
- :base_dir => parse_property(config, "base_dir", String),
- :neo4j_path => parse_property(config, "neo4j_path", String),
- :ip_route => parse_property(config, "ip_route", String, :optional => true),
- :available_memory => parse_property(config, "available_memory", Integer),
- :node_id => parse_property(config, "node_id", String),
- :mbus => parse_property(config, "mbus", String),
- :config_template => neo4j_server_conf_template,
- :neo4j_template => neo4j_conf_template,
- :log4j_template => log4j_conf_template,
- :local_db => parse_property(config, "local_db", String),
- :port_range => parse_property(config, "port_range", Range),
- :max_memory => parse_property(config, "max_memory", Integer)
-}
-
-def shutdown(neo4j_node)
- neo4j_node.shutdown
- EM.stop
-end
-
-EM.error_handler do |e|
- logger.fatal("#{e}\n#{e.backtrace.join("\n")}")
- exit
-end
+ def additional_config(options, config)
+ options[:config_template] = File.expand_path("../../resources/neo4j-server.properties.erb", __FILE__)
+ options[:port_range] = parse_property(config, "port_range", Range)
+ options[:neo4j_path] = parse_property(config, "neo4j_path", String)
+ options[:available_memory] = parse_property(config, "available_memory", Integer)
+ options[:max_memory] = parse_property(config, "max_memory", Integer)
+ options[:neo4j_template] = File.expand_path("../../resources/neo4j.properties.erb", __FILE__)
+ options[:log4j_template] = File.expand_path("../../resources/log4j.properties.erb", __FILE__)
+ options
+ end
-pid_file = parse_property(config, "pid", String)
-begin
- FileUtils.mkdir_p(File.dirname(pid_file))
-rescue => e
- logger.fatal "Can't create pid directory, exiting: #{e}"
end
-File.open(pid_file, 'w') { |f| f.puts "#{Process.pid}" }
-EM.run do
- neo4j_node = VCAP::Services::Neo4j::Node.new(options)
- trap("INT") {shutdown(neo4j_node)}
- trap("TERM") {shutdown(neo4j_node)}
-end
+VCAP::Services::Neo4j::NodeBin.new.start
View
12 neo4j/config/neo4j_gateway.yml
@@ -6,12 +6,20 @@ service:
description: 'Neo4j NOSQL store'
plans: ['free']
tags: ['neo4j', 'neo4j-1.4', 'nosql']
-host: localhost
+ timeout: 20
+ip_route: localhost
+#proxy:
+# host: proxy
+# port: 8080
+# keepalive: true
index: 0
token: "0xdeadbeef"
logging:
level: debug
mbus: nats://localhost:4222
pid: /var/vcap/sys/run/neo4j_service.pid
-node_timeout: 10
+node_timeout: 20
# allow_over_provisioning: false
+# z_interval: 30
+# check_orphan_interval: 3600
+# double_check_orphan_interval: 300
View
8 neo4j/config/neo4j_node.yml
@@ -3,12 +3,16 @@ local_db: sqlite3:/var/vcap/services/neo4j/neo4j_node.db
mbus: nats://localhost:4222
index: 0
base_dir: /var/vcap/services/neo4j/instances
-log_level: DEBUG
+logging:
+ level: debug
pid: /var/vcap/sys/run/neo4j_node.pid
available_memory: 4096
max_memory: 128
node_id: neo4j_node_1
neo4j_path: /var/vcap/services/neo4j
port_range:
- first: 45000
+ first: 45001
last: 55000
+
+# z_interval: 30
+# max_nats_payload: 1048576
View
68 neo4j/lib/neo4j_service/neo4j_node.rb
@@ -81,14 +81,51 @@ def initialize(options)
@free_ports = Set.new
options[:port_range].each {|port| @free_ports << port}
+ @mutex = Mutex.new
+ end
+
+ def fetch_port(port=nil)
+ @mutex.synchronize do
+ port ||= @free_ports.first
+ raise "port #{port} is already taken!" unless @free_ports.include?(port)
+ @free_ports.delete(port)
+ port
+ end
+ end
+
+ def return_port(port)
+ @mutex.synchronize do
+ @free_ports << port
+ end
+ end
+
+ def delete_port(port)
+ @mutex.synchronize do
+ @free_ports.delete(port)
+ end
+ end
+ def inc_memory(memory)
+ @mutex.synchronize do
+ @available_memory += memory
+ end
+ end
+
+ def dec_memory(memory)
+ @mutex.synchronize do
+ @available_memory -= memory
+ end
+ end
+
+ def pre_send_announcement
ProvisionedService.all.each do |provisioned_service|
- @free_ports.delete(provisioned_service.port)
+ delete_port(provisioned_service.port)
if provisioned_service.listening?
@logger.info("Service #{provisioned_service.name} already listening on port #{provisioned_service.port}")
- @available_memory -= (provisioned_service.memory || @max_memory)
+ dec_memory(provisioned_service.memory || @max_memory)
next
end
+
begin
pid = start_instance(provisioned_service)
provisioned_service.pid = pid
@@ -97,7 +134,7 @@ def initialize(options)
raise "Couldn't save pid (#{pid})"
end
rescue => e
- @logger.warn("Error starting service #{provisioned_service.name}: #{e}")
+ @logger.error("Error starting service #{provisioned_service.name}: #{e}")
end
end
@@ -108,8 +145,6 @@ def shutdown
@logger.info("Shutting down instances..")
ProvisionedService.all.each do |service|
@logger.info("Shutting down #{service}")
- end
- ProvisionedService.all.each do |service|
stop_service(service)
end
end
@@ -135,16 +170,14 @@ def announcement
a
end
-
def provision(plan, credentials=nil)
- port = @free_ports.first
- @free_ports.delete(port)
+ port = fetch_port
provisioned_service = ProvisionedService.new
if credentials
provisioned_service.name = credentials["name"]
provisioned_service.username = credentials["username"]
- provisioned_service.password = credentials["username"]
+ provisioned_service.password = credentials["password"]
else
provisioned_service.name = "neo4j-#{UUIDTools::UUID.random_create.to_s}"
provisioned_service.username = UUIDTools::UUID.random_create.to_s
@@ -195,8 +228,8 @@ def cleanup_service(provisioned_service)
EM.defer { FileUtils.rm_rf(dir) }
- @available_memory += provisioned_service.memory
- @free_ports << provisioned_service.port
+ inc_memory(provisioned_service.memory)
+ return_port(provisioned_service.port)
true
rescue => e
@@ -221,6 +254,7 @@ def bind(name, bind_opts, credentials=nil)
ro = bind_opts == "ro"
r = RestClient.post "http://#{provisioned_service.username}:#{provisioned_service.password}@#{@local_ip}:#{provisioned_service.port}/admin/add-user-#{ro ? 'ro' : 'rw'}","user=#{username}:#{password}"
raise "Failed to add user: #{username} status: #{r.code} message: #{r.to_str}" unless r.code == 200
+
response = {
"hostname" => @local_ip,
"host" => @local_ip,
@@ -246,6 +280,7 @@ def unbind(credentials)
password = credentials['password']
r = RestClient.post "http://#{provisioned_service.username}:#{provisioned_service.password}@#{@local_ip}:#{provisioned_service.port}/admin/remove-user", "user=#{username}:#{password}"
raise "Failed to remove user: #{username} status: #{r.code} message: #{r.to_str}" unless r.code == 200
+
@logger.debug("Successfully unbound #{credentials}")
true
rescue => e
@@ -294,16 +329,15 @@ def start_instance(provisioned_service)
@logger.info("Calling #{init_script} start")
out = `cd #{dir} && #{init_script} start`
- started = $?
- @logger.send(started ? :error : :debug, "Init finished, started = #{started}: #{out}")
+ status = $?
+ @logger.send(status.success? ? :debug : :error, "Init finished, status = #{status}: #{out}")
+ dec_memory(memory) if status.success?
pidfile = File.join(dir,"data","neo4j-service.pid")
pid = `[ -f #{pidfile} ] && cat #{pidfile}`
- started = $?
-
- @logger.send(started ? :error : :debug, "Service #{name} running with pid #{pid} #{started}")
- @available_memory -= memory if started
+ status = $?
+ @logger.send(status.success? ? :debug : :error, "Service #{name} running with pid #{pid} #{status}")
return pid.to_i
end
View
BIN neo4j/vendor/cache/vcap_logging-0.1.1.gem
Binary file not shown.
View
BIN neo4j/vendor/cache/vcap_logging-0.1.3.gem
Binary file not shown.

0 comments on commit c9c5035

Please sign in to comment.