Skip to content

Commit

Permalink
all tests pass!
Browse files Browse the repository at this point in the history
  • Loading branch information
Shane Brinkman-Davis authored and Shane Brinkman-Davis committed Jun 30, 2012
1 parent 7501f46 commit ee4eb7b
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/monotable/server/global_index.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def chunk_servers(internal_key,work_log=nil)
end end
def chunk_master(internal_key,work_log=nil) def chunk_master(internal_key,work_log=nil)
chunk_servers(internal_key).master find(internal_key,work_log).master
end end
# same as #chunk_servers, only cached # same as #chunk_servers, only cached
Expand Down
31 changes: 2 additions & 29 deletions lib/monotable/server/load_balancer.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -36,39 +36,12 @@ def balance
while chunks.length > local_store.chunks.length+1 while chunks.length > local_store.chunks.length+1
chunk_key = chunks.pop chunk_key = chunks.pop


master_client = # find master server, then just call "move_chunk" on its client master_client = cluster_manager[global_index.chunk_master(chunk_key)]

master_client.move_chunk(chunk_key,client,cluster_manager.local_server)
chunk_data = client.chunk chunk_key
chunk = local_store.add_chunk MemoryChunk.new(:data=>chunk_data)
global_index.add_local_replica(chunk)

client.delete_chunk chunk_key
chunks_moved[chunk_key]=client.to_s chunks_moved[chunk_key]=client.to_s
end end
chunks_moved chunks_moved
end end


private
# takes a neighbor and its list of chunks, and moves one chunk at a time to us until balanced
def async_balance_neighbor(neighbor,chunks,chunks_moved={},&block)

# if the most_loaded_neighbor has at most 1 or more chunks than we do, we're balanced enough
return yield chunks_moved if chunks.length+1 <= local_store.chunks.length

chunk_key = chunks.pop
neighbor.up_replicate_chunk chunk_key do
local_store.add_chunk chunk_data
client.down_replicate_chunk chunk_key do
chunks_moved[chunk_key]=client.to_s
async_balance_neighbor(neighbor,chunks,chunks_moved,&block) # async recursion
end
end
end
public

def async_balance(&block)
most_loaded_neighbor {|n,c| async_balance_neighbor(n,c,&block)}
end

end end
end end
11 changes: 6 additions & 5 deletions spec/server_spec/2daemon_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -62,10 +62,11 @@ def get_record(key)


def validate_index_records_for_chunks_on_server(client,internal_client=nil) def validate_index_records_for_chunks_on_server(client,internal_client=nil)
internal_client||=client.internal internal_client||=client.internal
server_name = client.server.split("http://")[1] server_name = client.server
client.chunks.each do |chunk| client.chunks.each do |chunk|
next if chunk=="" next if chunk==""
index_record = Monotable::GlobalIndex.index_record(chunk,internal_client) index_record = Monotable::GlobalIndex.index_record(chunk,internal_client)
# Monotable::Tools.debug :chunk => chunk, :index_record => index_record
index_record.servers.should == [server_name] index_record.servers.should == [server_name]
end end
end end
Expand Down Expand Up @@ -140,14 +141,14 @@ def test_records
end end


server_client(0).chunk_keys("u/bret").should == ["u/bret"] server_client(0).chunk_keys("u/bret").should == ["u/bret"]
server_client(1).chunk_keys("u/bret").should == [] server_client(1).chunk_keys("u/bret").should == nil


res = server_client(1).balance res = server_client(1).balance
server_client(0).chunks.should == ["", "++0", "+0", "0", "u/bret"] server_client(0).chunks.should == ["", "++0", "+0", "0", "u/bret"]
server_client(1).chunks.should == ["u/craig", "u/dan", "u/evan", "u/frank"] server_client(1).chunks.should == ["u/craig", "u/dan", "u/evan", "u/frank"]


server_client.chunk_keys("u/bret").should == ["u/bret"] server_client.chunk_keys("u/bret").should == ["u/bret"]
server_client(1).chunk_keys("u/bret").should == [] server_client(1).chunk_keys("u/bret").should == nil


test_records.each do |key,fields| test_records.each do |key,fields|
r0 = server_client(0).get(key) r0 = server_client(0).get(key)
Expand Down Expand Up @@ -188,8 +189,8 @@ def populate_balanced_store


# verify one record is on server-0 and another is on server-1 # verify one record is on server-0 and another is on server-1
server_client(0).chunk_keys("u/bret").should == ["u/bret"] server_client(0).chunk_keys("u/bret").should == ["u/bret"]
server_client(1).chunk_keys("u/bret").should == [] server_client(1).chunk_keys("u/bret").should == nil
server_client(0).chunk_keys("u/craig").should == [] server_client(0).chunk_keys("u/craig").should == nil
server_client(1).chunk_keys("u/craig").should == ["u/craig"] server_client(1).chunk_keys("u/craig").should == ["u/craig"]


# validate we can read we can read each record from either server # validate we can read we can read each record from either server
Expand Down
6 changes: 3 additions & 3 deletions spec/server_spec/n_daemon_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
end end


# verify records can be read from the correct servers # verify records can be read from the correct servers
server_clients.collect{|c|c.chunk_keys("u/bret")}.should == [[],[],["u/bret"],[]] server_clients.collect{|c|c.chunk_keys("u/bret")}.should == [nil,nil,["u/bret"],nil]
server_clients.collect{|c|c.chunk_keys("u/dan")}.should == [[],["u/dan"],[],[]] server_clients.collect{|c|c.chunk_keys("u/dan")}.should == [nil,["u/dan"],nil,nil]
server_clients.collect{|c|c.chunk_keys("u/frank")}.should == [[],[],[],["u/frank"]] server_clients.collect{|c|c.chunk_keys("u/frank")}.should == [nil,nil,nil,["u/frank"]]
end end
end end
8 changes: 4 additions & 4 deletions spec/server_spec/server_client_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Monotable
end end


before(:each) do before(:each) do
@client=ServerClient.new(daemon_uri) @client=nil
end end
attr_accessor :client attr_accessor :client


Expand All @@ -26,12 +26,12 @@ module Monotable
end end


def client def client
ServerClient.new(daemon_uri) @client ||= ServerClient.new daemon_address
end end


def blank_store def blank_store
clear_store clear_store
ServerClient.new(daemon_uri) client
end end


it "should fail locally to set with invalid fields" do it "should fail locally to set with invalid fields" do
Expand All @@ -41,7 +41,7 @@ def blank_store


it "should be comparable" do it "should be comparable" do
client.should == client client.should == client
client.should_not == ServerClient.new(daemon_uri(1)) client.should_not == ServerClient.new(daemon_address(1))
end end


it "should be accessible via HTTP" do it "should be accessible via HTTP" do
Expand Down

0 comments on commit ee4eb7b

Please sign in to comment.