miyazakiresistance 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +164 -19
- data/initializers/rdb.rb +28 -3
- data/lib/miyazaki_resistance/tokyo_connection.rb +21 -11
- data/lib/miyazakiresistance.rb +1 -1
- metadata +1 -1
data/README.rdoc
CHANGED
@@ -5,6 +5,8 @@
|
|
5
5
|
== DESCRIPTION:
|
6
6
|
|
7
7
|
MiyazakiResistance is a library like ActiveRecord to use Tokyo Tyrant.
|
8
|
+
MiyazakiResistance support Master/Slave. If Slave server down, it separates automatically.
|
9
|
+
MiyazakiResistance support Dual Master(Active/Standby). If Active server down, Standby server become Active.
|
8
10
|
|
9
11
|
== SYNOPSIS:
|
10
12
|
|
@@ -12,7 +14,7 @@ MiyazakiResistance is a library like ActiveRecord to use Tokyo Tyrant.
|
|
12
14
|
class Example < MiyazakiResistance::Base
|
13
15
|
set_server "localhost", 1975, :write
|
14
16
|
set_server "slave", 1975, :standby
|
15
|
-
set_timeout
|
17
|
+
set_timeout 1
|
16
18
|
set_column :name, :string
|
17
19
|
set_column :age, :integer
|
18
20
|
set_column :birthday, :date
|
@@ -71,25 +73,168 @@ Tokyo Tyrant
|
|
71
73
|
|
72
74
|
== LICENSE:
|
73
75
|
|
74
|
-
|
76
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
77
|
+
Version 3, 29 June 2007
|
78
|
+
|
79
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
80
|
+
Everyone is permitted to copy and distribute verbatim copies
|
81
|
+
of this license document, but changing it is not allowed.
|
82
|
+
|
83
|
+
|
84
|
+
This version of the GNU Lesser General Public License incorporates
|
85
|
+
the terms and conditions of version 3 of the GNU General Public
|
86
|
+
License, supplemented by the additional permissions listed below.
|
87
|
+
|
88
|
+
0. Additional Definitions.
|
75
89
|
|
76
|
-
|
90
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
91
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
92
|
+
General Public License.
|
77
93
|
|
78
|
-
|
79
|
-
|
80
|
-
'Software'), to deal in the Software without restriction, including
|
81
|
-
without limitation the rights to use, copy, modify, merge, publish,
|
82
|
-
distribute, sublicense, and/or sell copies of the Software, and to
|
83
|
-
permit persons to whom the Software is furnished to do so, subject to
|
84
|
-
the following conditions:
|
94
|
+
"The Library" refers to a covered work governed by this License,
|
95
|
+
other than an Application or a Combined Work as defined below.
|
85
96
|
|
86
|
-
|
87
|
-
|
97
|
+
An "Application" is any work that makes use of an interface provided
|
98
|
+
by the Library, but which is not otherwise based on the Library.
|
99
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
100
|
+
of using an interface provided by the Library.
|
101
|
+
|
102
|
+
A "Combined Work" is a work produced by combining or linking an
|
103
|
+
Application with the Library. The particular version of the Library
|
104
|
+
with which the Combined Work was made is also called the "Linked
|
105
|
+
Version".
|
106
|
+
|
107
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
108
|
+
Corresponding Source for the Combined Work, excluding any source code
|
109
|
+
for portions of the Combined Work that, considered in isolation, are
|
110
|
+
based on the Application, and not on the Linked Version.
|
111
|
+
|
112
|
+
The "Corresponding Application Code" for a Combined Work means the
|
113
|
+
object code and/or source code for the Application, including any data
|
114
|
+
and utility programs needed for reproducing the Combined Work from the
|
115
|
+
Application, but excluding the System Libraries of the Combined Work.
|
116
|
+
|
117
|
+
1. Exception to Section 3 of the GNU GPL.
|
118
|
+
|
119
|
+
You may convey a covered work under sections 3 and 4 of this License
|
120
|
+
without being bound by section 3 of the GNU GPL.
|
88
121
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
122
|
+
2. Conveying Modified Versions.
|
123
|
+
|
124
|
+
If you modify a copy of the Library, and, in your modifications, a
|
125
|
+
facility refers to a function or data to be supplied by an Application
|
126
|
+
that uses the facility (other than as an argument passed when the
|
127
|
+
facility is invoked), then you may convey a copy of the modified
|
128
|
+
version:
|
129
|
+
|
130
|
+
a) under this License, provided that you make a good faith effort to
|
131
|
+
ensure that, in the event an Application does not supply the
|
132
|
+
function or data, the facility still operates, and performs
|
133
|
+
whatever part of its purpose remains meaningful, or
|
134
|
+
|
135
|
+
b) under the GNU GPL, with none of the additional permissions of
|
136
|
+
this License applicable to that copy.
|
137
|
+
|
138
|
+
3. Object Code Incorporating Material from Library Header Files.
|
139
|
+
|
140
|
+
The object code form of an Application may incorporate material from
|
141
|
+
a header file that is part of the Library. You may convey such object
|
142
|
+
code under terms of your choice, provided that, if the incorporated
|
143
|
+
material is not limited to numerical parameters, data structure
|
144
|
+
layouts and accessors, or small macros, inline functions and templates
|
145
|
+
(ten or fewer lines in length), you do both of the following:
|
146
|
+
|
147
|
+
a) Give prominent notice with each copy of the object code that the
|
148
|
+
Library is used in it and that the Library and its use are
|
149
|
+
covered by this License.
|
150
|
+
|
151
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
152
|
+
document.
|
153
|
+
|
154
|
+
4. Combined Works.
|
155
|
+
|
156
|
+
You may convey a Combined Work under terms of your choice that,
|
157
|
+
taken together, effectively do not restrict modification of the
|
158
|
+
portions of the Library contained in the Combined Work and reverse
|
159
|
+
engineering for debugging such modifications, if you also do each of
|
160
|
+
the following:
|
161
|
+
|
162
|
+
a) Give prominent notice with each copy of the Combined Work that
|
163
|
+
the Library is used in it and that the Library and its use are
|
164
|
+
covered by this License.
|
165
|
+
|
166
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
167
|
+
document.
|
168
|
+
|
169
|
+
c) For a Combined Work that displays copyright notices during
|
170
|
+
execution, include the copyright notice for the Library among
|
171
|
+
these notices, as well as a reference directing the user to the
|
172
|
+
copies of the GNU GPL and this license document.
|
173
|
+
|
174
|
+
d) Do one of the following:
|
175
|
+
|
176
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
177
|
+
License, and the Corresponding Application Code in a form
|
178
|
+
suitable for, and under terms that permit, the user to
|
179
|
+
recombine or relink the Application with a modified version of
|
180
|
+
the Linked Version to produce a modified Combined Work, in the
|
181
|
+
manner specified by section 6 of the GNU GPL for conveying
|
182
|
+
Corresponding Source.
|
183
|
+
|
184
|
+
1) Use a suitable shared library mechanism for linking with the
|
185
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
186
|
+
a copy of the Library already present on the user's computer
|
187
|
+
system, and (b) will operate properly with a modified version
|
188
|
+
of the Library that is interface-compatible with the Linked
|
189
|
+
Version.
|
190
|
+
|
191
|
+
e) Provide Installation Information, but only if you would otherwise
|
192
|
+
be required to provide such information under section 6 of the
|
193
|
+
GNU GPL, and only to the extent that such information is
|
194
|
+
necessary to install and execute a modified version of the
|
195
|
+
Combined Work produced by recombining or relinking the
|
196
|
+
Application with a modified version of the Linked Version. (If
|
197
|
+
you use option 4d0, the Installation Information must accompany
|
198
|
+
the Minimal Corresponding Source and Corresponding Application
|
199
|
+
Code. If you use option 4d1, you must provide the Installation
|
200
|
+
Information in the manner specified by section 6 of the GNU GPL
|
201
|
+
for conveying Corresponding Source.)
|
202
|
+
|
203
|
+
5. Combined Libraries.
|
204
|
+
|
205
|
+
You may place library facilities that are a work based on the
|
206
|
+
Library side by side in a single library together with other library
|
207
|
+
facilities that are not Applications and are not covered by this
|
208
|
+
License, and convey such a combined library under terms of your
|
209
|
+
choice, if you do both of the following:
|
210
|
+
|
211
|
+
a) Accompany the combined library with a copy of the same work based
|
212
|
+
on the Library, uncombined with any other library facilities,
|
213
|
+
conveyed under the terms of this License.
|
214
|
+
|
215
|
+
b) Give prominent notice with the combined library that part of it
|
216
|
+
is a work based on the Library, and explaining where to find the
|
217
|
+
accompanying uncombined form of the same work.
|
218
|
+
|
219
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
220
|
+
|
221
|
+
The Free Software Foundation may publish revised and/or new versions
|
222
|
+
of the GNU Lesser General Public License from time to time. Such new
|
223
|
+
versions will be similar in spirit to the present version, but may
|
224
|
+
differ in detail to address new problems or concerns.
|
225
|
+
|
226
|
+
Each version is given a distinguishing version number. If the
|
227
|
+
Library as you received it specifies that a certain numbered version
|
228
|
+
of the GNU Lesser General Public License "or any later version"
|
229
|
+
applies to it, you have the option of following the terms and
|
230
|
+
conditions either of that published version or of any later version
|
231
|
+
published by the Free Software Foundation. If the Library as you
|
232
|
+
received it does not specify a version number of the GNU Lesser
|
233
|
+
General Public License, you may choose any version of the GNU Lesser
|
234
|
+
General Public License ever published by the Free Software Foundation.
|
235
|
+
|
236
|
+
If the Library as you received it specifies that a proxy can decide
|
237
|
+
whether future versions of the GNU Lesser General Public License shall
|
238
|
+
apply, that proxy's public statement of acceptance of any version is
|
239
|
+
permanent authorization for you to choose that version for the
|
240
|
+
Library.
|
data/initializers/rdb.rb
CHANGED
@@ -1,13 +1,38 @@
|
|
1
1
|
module TokyoTyrant
|
2
2
|
class RDB
|
3
|
-
attr_accessor :host, :port
|
3
|
+
attr_accessor :host, :port, :open_flag
|
4
4
|
|
5
|
-
def
|
5
|
+
def set_server(host, port = 0)
|
6
6
|
@host = host
|
7
7
|
@port = port
|
8
|
-
|
8
|
+
end
|
9
|
+
|
10
|
+
def open_with_save_params(host = nil, port = 0)
|
11
|
+
@host ||= host
|
12
|
+
@port ||= port
|
13
|
+
|
14
|
+
if open_without_save_params(@host, @port)
|
15
|
+
@open_flag = true
|
16
|
+
else
|
17
|
+
false
|
18
|
+
end
|
9
19
|
end
|
10
20
|
alias_method :open_without_save_params, :open
|
11
21
|
alias_method :open, :open_with_save_params
|
22
|
+
|
23
|
+
def close_with_change_flag
|
24
|
+
if close_without_change_flag
|
25
|
+
@open_flag = false
|
26
|
+
true
|
27
|
+
else
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
alias_method :close_without_change_flag, :close
|
32
|
+
alias_method :close, :close_with_change_flag
|
33
|
+
|
34
|
+
def open?
|
35
|
+
@open_flag
|
36
|
+
end
|
12
37
|
end
|
13
38
|
end
|
@@ -12,7 +12,7 @@ module MiyazakiResistance
|
|
12
12
|
attr_accessor :timeout_time
|
13
13
|
|
14
14
|
DEFAULT = {
|
15
|
-
:timeout =>
|
15
|
+
:timeout => 5,
|
16
16
|
:config => "miyazakiresistance.yml",
|
17
17
|
:port => 1978,
|
18
18
|
:role => :readonly
|
@@ -39,14 +39,14 @@ module MiyazakiResistance
|
|
39
39
|
rdb = TokyoTyrant::RDBTBL.new
|
40
40
|
logger_info "set server host : #{host} port : #{port} role : #{role}"
|
41
41
|
|
42
|
-
|
43
|
-
logger_fatal "TokyoTyrantConnectError host : #{host} port : #{port} role : #{role}"
|
44
|
-
raise TokyoTyrantConnectError
|
45
|
-
end
|
42
|
+
rdb.set_server(host.to_s, port)
|
46
43
|
|
47
|
-
|
48
|
-
|
49
|
-
|
44
|
+
if role.to_sym == :standby
|
45
|
+
self.connection_pool[:standby] = rdb
|
46
|
+
else
|
47
|
+
self.connection_pool[:read] << rdb
|
48
|
+
self.connection_pool[:write] = rdb if role.to_sym == :write
|
49
|
+
end
|
50
50
|
end
|
51
51
|
|
52
52
|
def set_timeout(seconds)
|
@@ -60,13 +60,23 @@ module MiyazakiResistance
|
|
60
60
|
self.all_columns.update(name => type)
|
61
61
|
end
|
62
62
|
|
63
|
+
def connection(con)
|
64
|
+
unless con.open?
|
65
|
+
unless con.open
|
66
|
+
logger_fatal "TokyoTyrantConnectError host : #{con.host} port : #{con.port}"
|
67
|
+
raise TokyoTyrantConnectError
|
68
|
+
end
|
69
|
+
end
|
70
|
+
con
|
71
|
+
end
|
72
|
+
|
63
73
|
def read_connection
|
64
74
|
check_pool
|
65
|
-
self.connection_pool[:read].sort_by{rand}.first
|
75
|
+
connection(self.connection_pool[:read].sort_by{rand}.first)
|
66
76
|
end
|
67
77
|
|
68
78
|
def write_connection
|
69
|
-
self.connection_pool[:write]
|
79
|
+
connection(self.connection_pool[:write])
|
70
80
|
end
|
71
81
|
|
72
82
|
def remove_pool(rdb)
|
@@ -79,7 +89,6 @@ module MiyazakiResistance
|
|
79
89
|
self.connection_pool[:write] = new_rdb if rdb == self.connection_pool[:write]
|
80
90
|
else
|
81
91
|
logger_fatal "remove pool : host #{host} port : #{port}"
|
82
|
-
check_pool
|
83
92
|
fail_over if rdb == self.connection_pool[:write]
|
84
93
|
end
|
85
94
|
rdb.close
|
@@ -112,6 +121,7 @@ module MiyazakiResistance
|
|
112
121
|
|
113
122
|
logger_fatal "master server failover"
|
114
123
|
self.connection_pool[:write] = self.connection_pool[:standby]
|
124
|
+
self.connection_pool[:read] << self.connection_pool[:standby]
|
115
125
|
self.connection_pool[:standby] = nil
|
116
126
|
end
|
117
127
|
end
|
data/lib/miyazakiresistance.rb
CHANGED
@@ -9,7 +9,7 @@ Dir.glob("#{File.join(File.dirname(__FILE__), "../initializers")}/*.rb").each{|p
|
|
9
9
|
Dir.glob("#{File.join(File.dirname(__FILE__), "miyazaki_resistance")}/*.rb").each{|path| require path}
|
10
10
|
|
11
11
|
module MiyazakiResistance
|
12
|
-
VERSION = '0.1.
|
12
|
+
VERSION = '0.1.4'
|
13
13
|
end
|
14
14
|
|
15
15
|
MiyazakiResistance::Base.class_eval do
|