Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ruanhaishen
redis
Commits
744f34d8
Commit
744f34d8
authored
Apr 12, 2011
by
antirez
Browse files
redis-trib can now assign hash slots to initialize a cluster. Next step is to join the cluster.
parent
d415f9ff
Changes
1
Hide whitespace changes
Inline
Side-by-side
src/redis-trib.rb
View file @
744f34d8
...
@@ -3,6 +3,8 @@
...
@@ -3,6 +3,8 @@
require
'rubygems'
require
'rubygems'
require
'redis'
require
'redis'
ClusterHashSlots
=
4096
def
xputs
(
s
)
def
xputs
(
s
)
printf
s
printf
s
STDOUT
.
flush
STDOUT
.
flush
...
@@ -17,6 +19,8 @@ class ClusterNode
...
@@ -17,6 +19,8 @@ class ClusterNode
end
end
@host
=
s
[
0
]
@host
=
s
[
0
]
@port
=
s
[
1
]
@port
=
s
[
1
]
@slots
=
{}
@dirty
=
false
end
end
def
to_s
def
to_s
...
@@ -51,12 +55,55 @@ class ClusterNode
...
@@ -51,12 +55,55 @@ class ClusterNode
end
end
end
end
def
add_slots
(
slots
)
slots
.
each
{
|
s
|
@slots
[
s
]
=
:new
}
@dirty
=
true
end
def
flush_node_config
return
if
!
@dirty
new
=
[]
@slots
.
each
{
|
s
,
val
|
if
val
==
:new
new
<<
s
@slots
[
s
]
=
true
end
}
@r
.
cluster
(
"addslots"
,
*
new
)
@dirty
=
false
end
def
info
slots
=
@slots
.
map
{
|
k
,
v
|
k
}.
reduce
{
|
a
,
b
|
a
=
[(
a
..
a
)]
if
!
a
.
is_a?
(
Array
)
if
b
==
(
a
[
-
1
].
last
)
+
1
a
[
-
1
]
=
(
a
[
-
1
].
first
)
..
b
a
else
a
<<
(
b
..
b
)
end
}.
map
{
|
x
|
(
x
.
first
==
x
.
last
)
?
x
.
first
.
to_s
:
"
#{
x
.
first
}
-
#{
x
.
last
}
"
}.
join
(
","
)
"
#{
self
.
to_s
.
ljust
(
25
)
}
slots:
#{
slots
}
"
end
def
is_dirty?
@dirty
end
def
r
def
r
@r
@r
end
end
end
end
class
RedisTrib
class
RedisTrib
def
initialize
@nodes
=
[]
end
def
check_arity
(
req_args
,
num_args
)
def
check_arity
(
req_args
,
num_args
)
if
((
req_args
>
0
and
num_args
!=
req_args
)
||
if
((
req_args
>
0
and
num_args
!=
req_args
)
||
(
req_args
<
0
and
num_args
<
req_args
.
abs
))
(
req_args
<
0
and
num_args
<
req_args
.
abs
))
...
@@ -72,7 +119,52 @@ class RedisTrib
...
@@ -72,7 +119,52 @@ class RedisTrib
node
.
connect
node
.
connect
node
.
assert_cluster
node
.
assert_cluster
node
.
assert_empty
node
.
assert_empty
@nodes
<<
node
}
}
puts
"Performing hash slots allocation on
#{
@nodes
.
length
}
nodes..."
alloc_slots
show_nodes
yes_or_die
"Can I set the above configuration?"
flush_nodes_config
puts
"** Nodes configuration updated"
puts
"Sending CLUSTER MEET messages to join the cluster"
join_cluster
end
def
alloc_slots
slots_per_node
=
ClusterHashSlots
/
@nodes
.
length
i
=
0
@nodes
.
each
{
|
n
|
first
=
i
*
slots_per_node
last
=
first
+
slots_per_node
-
1
last
=
ClusterHashSlots
-
1
if
i
==
@nodes
.
length
-
1
n
.
add_slots
first
..
last
i
+=
1
}
end
def
flush_nodes_config
@nodes
.
each
{
|
n
|
n
.
flush_node_config
}
end
def
show_nodes
@nodes
.
each
{
|
n
|
puts
n
.
info
}
end
def
join_cluster
end
def
yes_or_die
(
msg
)
print
"
#{
msg
}
(type 'yes' to accept): "
STDOUT
.
flush
if
!
(
STDIN
.
gets
.
chomp
.
downcase
==
"yes"
)
puts
"Aborting..."
exit
1
end
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment