Class: N::Cluster::CHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/n/app/cluster.rb

Overview

CHash ClusterHash

Direct Known Subclasses

Clm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ldrb_uri = "druby://:8000") ⇒ CHash

Returns a new instance of CHash.



46
47
48
49
50
# File 'lib/n/app/cluster.rb', line 46

def initialize(ldrb_uri = "druby://:8000")
	@mon = Monitor.new
	@ldrb = DRb.start_service(ldrb_uri, self)
	@cluster = {}
end

Instance Attribute Details

#clusterObject

the cluster, use a cluster to implement a set (one server per drb_uri)



42
43
44
# File 'lib/n/app/cluster.rb', line 42

def cluster
  @cluster
end

#ldrbObject

drbobject for this hash (local)



38
39
40
# File 'lib/n/app/cluster.rb', line 38

def ldrb
  @ldrb
end

#monObject (readonly)

Returns the value of attribute mon.



35
36
37
# File 'lib/n/app/cluster.rb', line 35

def mon
  @mon
end

Instance Method Details

#[](key) ⇒ Object



103
104
105
106
107
108
# File 'lib/n/app/cluster.rb', line 103

def [](key)
	@mon.synchronize {
		puts "LOOKUP #{key}"
		return super
	}
end

#[]=(key, value) ⇒ Object

Not really usefull



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/n/app/cluster.rb', line 64

def []=(key, value)
	# store the value (useful on server restarts)
	@mon.synchronize {
		old_set(key, value)

		puts "CLUSTER #{key} = #{value}"

		cluster.each { |uri, sdrb|
			begin
				sdrb.server_sync(key, value)
			rescue => ex
				$log.error "Server at #{uri} is down, removing from cluster"
				cluster.delete(uri)
			end
		}
	}
end

#cluster_sync(key, value, server_uri) ⇒ Object

Use this, avoids syncing the original server, and avoids a nasty deadlock.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/n/app/cluster.rb', line 85

def cluster_sync(key, value, server_uri)
	# store the value (useful on server restarts)
	@mon.synchronize {
		old_set(key, value)

		puts "CLUSTER #{key} = #{value}"

		cluster.each { |uri, sdrb|
			begin
				sdrb.server_sync(key, value) unless uri == server_uri
			rescue => ex
				$log.error "Server at #{uri} is down, removing from cluster"
				cluster.delete(uri)
			end
		}
	}		
end

#join(sdrb_uri) ⇒ Object



54
55
56
57
58
# File 'lib/n/app/cluster.rb', line 54

def join(sdrb_uri)
	@mon.synchronize {
		cluster[sdrb_uri] = DRbObject.new(nil, sdrb_uri)
	}
end

#old_setObject



60
# File 'lib/n/app/cluster.rb', line 60

alias_method :old_set, :[]=