tem_mr_search 0.2 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.2.1. Check for cluster configuration on each query.
2
+
1
3
  v0.2. Implemented remote queries.
2
4
 
3
5
  v0.1. Initial release.
@@ -9,7 +9,7 @@ class Client
9
9
  def self.search(server_addr, client_query)
10
10
  output = issue_request server_addr, :type => :search, :root_tem => 0,
11
11
  :map_reduce => client_query.to_hash
12
- client_query.unpack_output output
12
+ output ? client_query.unpack_output(output) : nil
13
13
  end
14
14
 
15
15
  # Asks for an item in the server's database.
@@ -26,9 +26,17 @@ class Client
26
26
  issue_request server_addr, :type => :shutdown
27
27
  end
28
28
 
29
+ # Dumps the server database.
30
+ #
31
+ # In production, normal clients wouldn't have access to this.
32
+ def self.dump_database(server_addr)
33
+ issue_request server_addr, :type => :db_dump
34
+ end
35
+
29
36
  # Issues a request against a Map-Reduce server and returns the response.
30
37
  def self.issue_request(server_addr, request)
31
- socket = Zerg::Support::SocketFactory.socket :out_addr => server_addr
38
+ socket = Zerg::Support::SocketFactory.socket :out_addr => server_addr,
39
+ :out_port => Server::DEFAULT_PORT
32
40
  socket.extend OPAdapter
33
41
  begin
34
42
  socket.send_object request
@@ -5,6 +5,8 @@ require 'yaml'
5
5
  module Tem::Mr::Search
6
6
 
7
7
  class Server
8
+ DEFAULT_PORT = 9052
9
+
8
10
  OP = Zerg::Support::Protocols::ObjectProtocol
9
11
  OPAdapter = Zerg::Support::Sockets::ProtocolAdapter.adapter_module OP
10
12
 
@@ -12,8 +14,19 @@ class Server
12
14
  def initialize(db_file, cluster_file, port)
13
15
  @logger = Logger.new STDERR
14
16
  @db = Db.new db_file
15
- @tems = Server.tems_from_cluster_file cluster_file
16
- @port = port
17
+ @cluster_file = cluster_file
18
+ @tems = []
19
+ refresh_tems!
20
+ @port = port || DEFAULT_PORT
21
+ end
22
+
23
+ # Reinitializes the TEM cluster connections.
24
+ #
25
+ # This should be called reasonably often to be able to respond to cluster
26
+ # configuration changes.
27
+ def refresh_tems!
28
+ @tems.each { |tem| tem.disconnect }
29
+ @tems = Server.tems_from_cluster_file @cluster_file
17
30
  end
18
31
 
19
32
  # This server's loop.
@@ -37,7 +50,7 @@ class Server
37
50
  rescue Exception => e
38
51
  @logger.error e
39
52
  end
40
- client_socket.close rescue nil
53
+ client_socket.close
41
54
  end
42
55
  listen_socket.close
43
56
  end
@@ -46,6 +59,7 @@ class Server
46
59
  def process_request(request)
47
60
  case request[:type]
48
61
  when :search
62
+ refresh_tems!
49
63
  job = MapReduceJob.new request[:map_reduce]
50
64
  root_tem = request[:root_tem]
51
65
  executor = MapReduceExecutor.new job, @db, @tems, root_tem
@@ -54,6 +68,8 @@ class Server
54
68
  return @db.item_by_id(request[:id]) || :not_found
55
69
  when :shutdown
56
70
  return :shutdown
71
+ when :db_dump
72
+ return (0...@db.length).map { |i| @db.item(i) }
57
73
  else
58
74
  return :unknown
59
75
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{tem_mr_search}
5
- s.version = "0.2"
5
+ s.version = "0.2.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Victor Costan"]
@@ -8,7 +8,7 @@ class ClientServerTest < MrTestCase
8
8
 
9
9
  def setup
10
10
  super
11
- @server_port = 29552
11
+ @server_port = 29550
12
12
  end
13
13
 
14
14
  def _test_request
@@ -27,11 +27,24 @@ class ClientServerTest < MrTestCase
27
27
  end
28
28
  end
29
29
 
30
+ def test_dump_database
31
+ @server_port = 29557
32
+ _test_request do |server_addr|
33
+ items = Client.dump_database server_addr
34
+ assert_equal @db.length, items.length, 'Wrong number of items'
35
+ items.each_with_index do |item, i|
36
+ assert_equal @db.item(i), item, "Discrepancy in item #{i}"
37
+ end
38
+ end
39
+ end
40
+
30
41
  def test_query
31
- Tem.auto_conf
32
- @server_port = 29553
42
+ @server_port = 29551
33
43
  flexmock(Server).should_receive(:tems_from_cluster_file).
34
- with(@empty_cluster_file).and_return [$tem]
44
+ with(@empty_cluster_file).and_return do |file|
45
+ Tem.auto_conf
46
+ [$tem]
47
+ end
35
48
  _test_request do |server_addr|
36
49
  result = Client.search server_addr, @client_query
37
50
  gold_item = @db.item 5
@@ -9,6 +9,10 @@ class MapReduceExecutorTest < MrTestCase
9
9
  $tem.activate
10
10
  $tem.emit
11
11
  end
12
+
13
+ def teardown
14
+ $tem.disconnect
15
+ end
12
16
 
13
17
  def _test_executor(tems, root_tem)
14
18
  executor = MRExecutor.new @client_query, @db, tems, root_tem
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tem_mr_search
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.2"
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Costan