added unset_zone_id context for clients
[voldemort/jeffpc.git] / bin / rebalance-zone-expansion.sh
blob66690e3aa6ccaf1e23839d5177aec746613de538
1 #!/bin/bash -e
4 # Copyright 2013 LinkedIn, Inc
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 # This script generates a cluster.xml and a plan for the zone expansion.
19 # The final cluster is placed in output_dir/
20 # Argument = -c current_cluster -s current_stores -i interim_cluster -f final_stores
21 # -o output dir
23 # This script uses getopts which means only single character switches are allowed.
24 # Using getopt would allow for multi charcter switch names but would come at a
25 # cost of not being cross compatible.
27 # Function to display usage
28 usage_and_exit() {
29 echo "ERROR: $1."
30 cat <<EOF
32 Usage: $0 options
33 OPTIONS:
34 -h Show this message
35 -c Current cluster that describes the cluster
36 -s Current stores that describes the store. If you do not have info about the stores yet, look
37 under 'voldemort_home/config/tools/' for some store examples.
38 -i Interim Cluster that corresponds to zone expansion.
39 -f Final Stores that corresponds to zone expansion.
40 -o Output dir where all interim and final files will be stored.
41 EOF
42 exit 1
45 # initialize variables to an empty string
46 current_cluster=""
47 current_stores=""
48 interim_cluster=""
49 final_stores=""
50 output_dir=""
52 # Figure out voldemort home directory
53 dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
54 vold_home="$(dirname "$dir")"
56 # Parse options
57 while getopts “hc:s:i:f:o:” OPTION
59 case $OPTION in
61 usage_and_exit
62 exit 1
65 current_cluster=$OPTARG
66 echo "[rebalance-zone-expansion] Will rebalance on the cluster described in '$current_cluster'."
69 current_stores=$OPTARG
70 echo "[rebalance-zone-expansion] Will rebalance on the stores described in '$current_stores'."
73 interim_cluster=$OPTARG
74 echo "[rebalance-zone-expansion] Will rebalance on the interim cluster described in '$interim_cluster'."
77 final_stores=$OPTARG
78 echo "[rebalance-zone-expansion] Will rebalance on the final stores described in '$final_stores'."
81 output_dir=$OPTARG
82 output_dir=$(readlink -m $output_dir)
83 mkdir -p $output_dir
84 echo "[rebalance-zone-expansion] Using '$output_dir' for all interim and final files generated."
87 usage_and_exit
89 esac
90 done
92 if [[ -z $current_cluster ]] || [[ -z $current_stores ]] || [[ -z $interim_cluster ]] \
93 || [[ -z $final_stores ]] || [[ -z $output_dir ]]
94 then
95 printf "\n"
96 echo "[rebalance-zone-expansion] Missing argument. Check again."
97 usage_and_exit
98 exit 1
101 if [ ! -e $current_cluster ]; then
102 usage_and_exit "File '$current_cluster' does not exist."
105 if [ ! -e $current_stores ]; then
106 usage_and_exit "File '$current_stores' does not exist."
109 if [ ! -e $interim_cluster ]; then
110 usage_and_exit "File '$interim_cluster' does not exist."
113 if [ ! -e $final_stores ]; then
114 usage_and_exit "File '$final_stores' does not exist."
118 # The final cluster.xml for zone expansion is generated in three steps.
119 # Step 1 : In step 1 1000 separate iterations of the repartitioner is executed and the one
120 # with the minimal utility value is chose for step 2.
121 # Step 2: In step 2, the cluster.xml from step 1 is fed to the repartitioner along with random swap
122 # attempts. The repartitioner randomly swaps the partitions only in zone 2
123 # and tries to balance the ring.
124 # Step 3: Finally, a plan is generated on how to reach from the orignal cluster topology to
125 # the one that is generated in step 2.
128 step2_zoneid=2
129 step2_swap_attempts=1000
130 step2_overall_iterations=5
133 # Step 1
134 for i in {1..1000}
136 mkdir -p $output_dir/step1/$i
137 $vold_home/bin/run-class.sh voldemort.tools.RepartitionerCLI \
138 --current-cluster $current_cluster \
139 --current-stores $current_stores \
140 --interim-cluster $interim_cluster \
141 --final-stores $final_stores \
142 --attempts 1 \
143 --output-dir $output_dir/step1/$i
144 done
146 #find the run with the best (minimal) utility value
147 bestUtil="$(grep "Utility" $output_dir/step1/*/final-cluster.xml.analysis | sort -nk 3 | head -n 1 | cut -f1 -d ' ')"
148 remove=".analysis:Utility"
149 path_to_bestUtil=${bestUtil%%$remove}
151 if [ ! -e $path_to_bestUtil ]; then
152 usage_and_exit "final cluster.xml from step1 does not exist"
155 # Step 2
156 $vold_home/bin/run-class.sh voldemort.tools.RepartitionerCLI \
157 --current-cluster $path_to_bestUtil \
158 --current-stores $final_stores \
159 --output-dir $output_dir/step2 \
160 --enable-random-swaps \
161 --random-swap-zoneids $step2_zoneid \
162 --attempts $step2_overall_iterations \
163 --random-swap-attempts $step2_swap_attempts \
165 # Step 3
166 mkdir -p $output_dir/step3/
167 $vold_home/bin/run-class.sh voldemort.tools.RebalancePlanCLI \
168 --current-cluster $current_cluster \
169 --current-stores $current_stores \
170 --final-cluster $output_dir/step2/final-cluster.xml \
171 --final-stores $final_stores \
172 --output-dir $output_dir/step3/
174 echo "[rebalance-zone-expansion] Placing final-cluster.xml in '$output_dir'"
175 cp $output_dir/step3/final-cluster.xml $output_dir/final-cluster.xml
176 echo "[rebalance-zone-expansion] Placing plan.out in '$output_dir'"
177 cp $output_dir/step3/plan.out $output_dir/plan.out
178 echo "[rebalance-zone-expansion] Placing final-cluster.xml.analysis in '$output_dir'"
179 cp $output_dir/step2/final-cluster.xml $output_dir/final-cluster.xml.analysis