Eolisation de Redis

90-redis-init 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #!/bin/bash
  2. #
  3. # Cluster init !
  4. #
  5. #function ProgressBar {
  6. # # Process data
  7. # let _progress=(${1}*100/${2}*100)/100
  8. # let _done=(${_progress}*4)/10
  9. # let _left=40-$_done
  10. # # Build progressbar string lengths
  11. # _fill=$(printf "%${_done}s")
  12. # _empty=$(printf "%${_left}s")
  13. #
  14. # # 1.2 Build progressbar strings and print the ProgressBar line
  15. # # 1.2.1 Output example:
  16. # # 1.2.1.1 Progress : [########################################] 100%
  17. # printf "\rProgress : [${_fill// /#}${_empty// /-}] ${_progress}%%"
  18. #}
  19. function redisRun()
  20. {
  21. cmd="redis-cli"
  22. host=$1
  23. shift
  24. port=$1
  25. shift
  26. #pass=$1
  27. #shift
  28. #opt="-h ${host} -p ${port} -a ${pass}"
  29. opt="-h ${host} -p ${port}"
  30. act=${@}
  31. ${cmd} ${opt} ${act}
  32. return ${?}
  33. }
  34. #
  35. # Get Node ID with IP Adress and Port Number
  36. # Params :
  37. # - $1 => Leader IP
  38. # - $2 => Leader Port
  39. # - $3 => Cluster Password
  40. # - $4 => Node IP Adress
  41. # - $5 => Node Port
  42. #
  43. function getNodeID()
  44. {
  45. if [[ ${1} == ${3} ]]
  46. then
  47. searchCmd="awk '/myself,master/ { print \$1 }'"
  48. else
  49. searchCmd="awk '/${3}:${4}@/ { print \$1 }'"
  50. fi
  51. result=$(redisRun $1 $2 cluster nodes | eval ${searchCmd})
  52. rcode=${?}
  53. if [[ -z ${result} ]]
  54. then
  55. searchCmd="awk '/${3}:${4}@/ { print \$1 }'"
  56. result=$(redisRun $1 $2 cluster nodes | eval ${searchCmd})
  57. rcode=${?}
  58. fi
  59. echo ${result}
  60. return ${rcode}
  61. }
  62. function redisClusterForgetAll()
  63. {
  64. local res=0
  65. for node in $(redisRun ${1} ${2} ${3} cluster nodes | awk '!/myself,master/ {print $1}')
  66. do
  67. redisRun ${1} ${2} ${3} "cluster forget ${node}"
  68. res=$((res+${?}))
  69. done
  70. return ${res}
  71. }
  72. #
  73. # Assing redis slots to master Node
  74. # Params :
  75. # - $1 => Node IP
  76. # - $2 => Node Port
  77. # - $3 => Cluster Password
  78. # - $4 => First slot
  79. # - $5 => Last slot
  80. #
  81. function redisSlotAssign()
  82. {
  83. res=0
  84. for slot in $(seq ${3} ${4})
  85. do
  86. OUT=$(redisRun ${1} ${2} "cluster ADDSLOTS $slot")
  87. res=$((res+${?}))
  88. if [[ ${OUT} =~ ERR ]]
  89. then
  90. OUT=$(redisRun ${1} ${2} "cluster ADDSLOTS $slot")
  91. fi
  92. done
  93. return ${res}
  94. }
  95. function redisAddReplica()
  96. {
  97. redisRun ${1} ${2} "cluster replicate ${3}"
  98. return ${?}
  99. }
  100. function redisClusterIsOK()
  101. {
  102. out=$(redis-cli -h ${1} -p ${2} cluster info | grep "cluster_state:ok")
  103. return $?
  104. }
  105. CONF="/etc/redis/cluster.conf"
  106. INITFLAG="/usr/share/eole/redisinit.flg"
  107. sysConf=$(grep "vm.overcommit_memory" /etc/sysctl.conf 2>&1 > /dev/null)
  108. if [[ ${?} -ne 0 ]]
  109. then
  110. echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
  111. sysctl -p /etc/sysctl.conf
  112. fi
  113. [[ ! -e ${CONF} ]] && exit 0
  114. declare -A IPADDRS
  115. declare -A PORTS
  116. declare -A ROLES
  117. declare -A MASTERS
  118. declare -A NIDS
  119. declare LeaderIP
  120. declare LeaderPort
  121. index=0
  122. while read line
  123. do
  124. [[ ${line} =~ ^# ]] && continue
  125. line=${line//::/:none:}
  126. li=(${line//:/ })
  127. name=${li[0]}
  128. ip=${li[1]}
  129. port=${li[2]}
  130. role=${li[3]}
  131. master=${li[4]}
  132. ntype=${li[5]}
  133. if [[ ${ntype} == "Leader" ]]
  134. then
  135. LeaderIP=${ip}
  136. LeaderPort=${port}
  137. fi
  138. NAMES+=(${name})
  139. IPADDRS+=([${name}]=${ip})
  140. PORTS+=([${name}]=${port})
  141. ROLES+=([${name}]=${role})
  142. MASTERS+=([${name}]=${master})
  143. done < ${CONF}
  144. # Disable Transparent Huge Page support on kernel
  145. echo never > /sys/kernel/mm/transparent_hugepage/enabled
  146. if [[ ${1} == "forget" ]]
  147. then
  148. redisClusterForgetAll ${LeaderIP} ${LeaderPort}
  149. exit ${?}
  150. fi
  151. # If the cluster is "ok" don't do anything
  152. st=$(redisClusterIsOK ${LeaderIP} ${LeaderPort})
  153. [[ ${?} -eq 0 ]] && exit 0
  154. if [[ -e ${INITFLAG} ]]
  155. then
  156. echo "Init allready done ... nothing to do"
  157. exit 0
  158. fi
  159. echo " * Organise Cluster Meeting."
  160. for node in ${NAMES[@]}
  161. do
  162. act="cluster meet ${IPADDRS[${node}]} ${PORTS[${node}]}"
  163. echo " - ${node} - ${IPADDRS[${node}]}:${PORTS[${node}]} meeting with :"
  164. for oth in ${NAMES[@]}
  165. do
  166. [[ ${oth} == ${node} ]] && continue
  167. echo -en " -> ${oth} - ${IPADDRS[${oth}]} ${PORTS[${oth}]} "
  168. redisRun ${IPADDRS[${oth}]} ${PORTS[${oth}]} "${act}"
  169. done
  170. done
  171. echo " * Waiting for all members ."
  172. for node in ${NAMES[@]}
  173. do
  174. try=3
  175. scmd="awk '/${IPADDRS[$node]}:${PORTS[${node}]}/ {print \$8}'"
  176. while [[ $(redisRun ${LeaderIP} ${LeaderPort} "cluster nodes" | eval ${scmd}) != "connected" ]]
  177. do
  178. sleep 1
  179. try=$((try-1))
  180. [[ ${try} -eq 0 ]]
  181. break
  182. done
  183. done
  184. nbMaster=$(grep -c ":master" ${CONF})
  185. totalslots=16384
  186. perNodeSlots=$((totalslots/nbMaster))
  187. startSlot=0
  188. echo " * Assign Slots to the nodes."
  189. for node in ${NAMES[@]}
  190. do
  191. if [[ ${ROLES[$node]} == "master" ]]
  192. then
  193. if [[ ${startSlot} -eq 0 ]]
  194. then
  195. lastSlot=$(((perNodeSlots+startSlot)-1))
  196. else
  197. lastSlot=$((perNodeSlots+startSlot))
  198. fi
  199. [[ ${lastSlot} -ge ${totalslots} ]] && lastSlot=$((totalslots-1))
  200. echo " - Slots ${startSlot} to ${lastSlot} => ${node} - ${IPADDRS[${node}]} ${PORTS[${node}]}"
  201. redisSlotAssign ${IPADDRS[${node}]} ${PORTS[${node}]} ${startSlot} ${lastSlot}
  202. startSlot=$((lastSlot+1))
  203. fi
  204. done
  205. echo " * Configuring replication."
  206. NIDS+=()
  207. for node in ${NAMES[@]}
  208. do
  209. NIDS+=([${node}]=$(getNodeID ${LeaderIP} ${LeaderPort} ${IPADDRS[${node}]} ${PORTS[${node}]}))
  210. done
  211. for node in ${NAMES[@]}
  212. do
  213. [[ ${ROLES[$node]} == "master" ]] && continue
  214. echo -ne " - Replicate ${MASTERS[${node}]} to ${node}\t\t"
  215. redisAddReplica ${IPADDRS[${node}]} ${PORTS[${node}]} ${NIDS[${MASTERS[${node}]}]}
  216. done
  217. sleep 5
  218. echo
  219. echo "Cluster status :"
  220. echo
  221. redisRun ${LeaderIP} ${LeaderPort} cluster info
  222. echo
  223. touch ${INITFLAG}
  224. exit 0