Skip to content

Commit 9377921

Browse files
committed
add readme
1 parent e39eeb5 commit 9377921

3 files changed

Lines changed: 931 additions & 0 deletions

File tree

tools/pika_migrate_3.5/README.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
2+
3+
## 适用版本
4+
5+
适用 PIKA 3.5.0及以上版本(4.0.x 版本不支持),单机模式且只使用了单 DB。若 PIKA 版本低于3.5.0,需将内核版本升级至 3.5.0。
6+
### 开发背景:
7+
之前Pika项目官方提供的pika\_to\_redis工具仅支持离线将Pika的DB中的数据迁移到Pika、Redis, 且无法增量同步, 该工具实际上就是一个特殊的Pika, 只不过成为从库之后, 内部会将从主库获取到的数据转发给Redis,同时并支持增量同步, 实现热迁功能.
8+
9+
## 迁移原理
10+
11+
将 PIKA 中的数据在线迁移到 Redis,并支持全量和增量同步。使用 pika-migrate 工具,将工具虚拟为 PIKA 的从库,然后从主库获取到数据转发给 Redis,同时支持增量同步,实现在线热迁的功能。
12+
1. pika-migrate 通过 dbsync 请求获取主库全量 DB 数据,以及当前 DB 数据所对应的 binlog 点位。
13+
2. 获取到主库当前全量 DB 数据之后,扫描 DB,将 DB 中的数据打包转发给 Redis。
14+
3. 通过之前获取的 binlog 的点位向主库进行增量同步, 在增量同步的过程中,将从主库获取到的 binlog 重组成 Redis 命令,转发给 Redis。
15+
16+
17+
## 注意事项
18+
19+
PIKA 支持不同数据结构采用同名 Key,但是 Redis 不⽀持,所以在有同 Key 数据的场景下,以第⼀个迁移到 Redis 数据结构为准,其他同名 Key 的数据结构会丢失。
20+
该工具只支持热迁移单机模式下,并且只采⽤单 DB 版本的 PIKA,如果是集群模式,或者是多 DB 场景,⼯具会报错并且退出。
21+
为了避免由于主库 binlog 被清理导致该⼯具触发多次全量同步向 Redis 写入脏数据,工具自身做了保护,在第⼆次触发全量同步时会报错退出。
22+
23+
## 编译步骤
24+
```shell
25+
# 若third目录中子仓库为空,需要进入工具根目录更新submodule
26+
git submodule update --init --recursive
27+
# 编译
28+
make
29+
```
30+
31+
### 编译备注
32+
33+
1.如果rocksdb编译失败,请先按照[此处](https://github.com/facebook/rocksdb/blob/004237e62790320d8e630456cbeb6f4a1f3579c2/INSTALL.md) 的步骤准备环境
34+
2.若类似为:
35+
```shell
36+
error: implicitly-declared 'constexpr rocksdb::FileDescriptor::FileDescriptor(const rocksdb::FileDescriptor&)' is deprecated [-Werror=deprecated-copy]
37+
```
38+
可以修改tools/pika_migrate/third/rocksdb目录下的makefile:
WARNING_FLAGS = -Wno-missing-field-initializers
39+
-Wno-unused-parameter
40+
41+
## 迁移步骤
42+
43+
1. 在 PIKA 主库上执行如下命令,让 PIKA 主库保留10000个 binlog 文件。
44+
45+
```shell
46+
config set expire-logs-nums 10000
47+
```
48+
49+
```text
50+
说明:
51+
pika-port 将全量数据写入到 Redis 这段时间可能耗时很长,而导致主库原先 binlog 点位被清理。需要在 PIKA 主库上保留10000个 binlog ⽂件,确保后续该⼯具请求增量同步的时候,对应的 binlog 文件还存在。
52+
binlog 文件占用磁盘空间,可以根据实际情况确定保留 binlog 的数量。
53+
```
54+
55+
2. 修改迁移工具的配置文件 pika.conf 中的如下参数。
56+
![img.png](img.png)
57+
58+
target-redis-host:指定 Redis 的 IP 地址。
59+
target-redis-port:指定 Redis 的端口号。
60+
target-redis-pwd:指定 Redis 默认账号的密码。
61+
sync-batch-num:指定 pika-migrate 接收到主库的 sync-batch-num 个数据⼀起打包发送给 Redis,提升转发效率。
62+
redis-sender-num:指定 redis-sender-num 个线程用于转发数据包。转发命令通过 Key 的哈希值将数据分配到不同的线程发送,无需担心多线程发送导致数据错乱的问题。
63+
3. 在工具包的路径下执行如下命令,启动 pika-migrate 工具,并查看回显信息。
64+
```shell
65+
pika -c pika.conf
66+
```
67+
68+
4. 执行如下命令,将迁移工具伪装成 Slave,向主库请求同步,并观察是否有报错信息。
69+
```shell
70+
slaveof ip port force
71+
```
72+
73+
5. 确认主从关系建立成功之后,pika-migrate 同时向目标 Redis 转发数据。执行如下命令,查看主从同步延迟。可在主库写入⼀个特殊的 Key,然后在 Redis 侧查看是否可立即获取到该 Key,判断数据同步完毕。
74+
```shell
75+
info Replication
76+
```

tools/pika_migrate_3.5/build.sh

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#!/bin/bash
2+
3+
set -x
4+
5+
#color code
6+
C_RED="\033[31m"
7+
C_GREEN="\033[32m"
8+
9+
C_END="\033[0m"
10+
11+
CMAKE_MIN_VERSION="3.18"
12+
TAR_MIN_VERSION="1.26"
13+
14+
BUILD_DIR=output
15+
16+
CLEAN_BUILD="false"
17+
ARGS=()
18+
19+
for i in "$@"; do
20+
case $i in
21+
-c*|--clean*)
22+
CLEAN_BUILD="true"
23+
;;
24+
-*|--*)
25+
echo "Unknown option $i"
26+
exit 1
27+
;;
28+
*)
29+
ARGS=("${ARGS[@]}" $i)
30+
;;
31+
esac
32+
done
33+
34+
if [ ! -f "/proc/cpuinfo" ];then
35+
CPU_CORE=$(sysctl -n hw.ncpu)
36+
else
37+
CPU_CORE=$(cat /proc/cpuinfo| grep "processor"| wc -l)
38+
fi
39+
if [ ${CPU_CORE} -eq 0 ]; then
40+
CPU_CORE=1
41+
fi
42+
43+
echo "cpu core ${CPU_CORE}"
44+
45+
if [[ "${CLEAN_BUILD}" = "true" ]]; then
46+
rm -rf "${BUILD_DIR}" buildtrees deps pkg
47+
fi
48+
49+
if [[ "${ARGS[0]}" = "clean" ]]; then
50+
rm -rf "${BUILD_DIR}" buildtrees deps pkg
51+
exit 0
52+
fi
53+
54+
if [[ "${ARGS[0]}" = "codis" ]]; then
55+
export GOPATH=${PWD}
56+
export CGO_ENABLED=1
57+
pushd codis
58+
if [[ "${CLEAN_BUILD}" = "true" ]]; then
59+
make -j ${CPU_CORE} clean
60+
fi
61+
make -j ${CPU_CORE} "${ARGS[@]:1}"
62+
popd
63+
exit 0
64+
fi
65+
66+
source ./utils/Get_OS_Version.sh
67+
68+
function version_compare() {
69+
if [[ "$1" == "$2" ]]; then
70+
return 0
71+
fi
72+
73+
if [[ "$(printf '%s\n' "$1" "$2" | sort -rV | head -n1)" == "$1" ]]; then
74+
#local version less min version
75+
echo -e "local ${C_GREEN} $3 ${C_END} version ${C_GREEN} $2 ${C_END} less min version ${C_GREEN} $1 ${C_END}"
76+
exit 1
77+
fi
78+
}
79+
80+
function check_program() {
81+
if ! type $1 >/dev/null 2>&1; then
82+
# not find
83+
echo -e "not find ${C_GREEN} $1 ${C_END} on localhost"
84+
return 1
85+
fi
86+
return 0
87+
}
88+
89+
function install_package() {
90+
if [ $PM == "unknow" ]; then
91+
echo -e "${C_RED} unknow package manager, please install $1 ${C_END}"
92+
exit 1
93+
fi
94+
if [ ${PM} == "apt" ]; then
95+
sudo ${PM} -y install $1
96+
elif [ ${PM} == "brew" ]; then
97+
${PM} install -d $1
98+
else
99+
sudo ${PM} install -y $1
100+
fi
101+
if [ $? -ne 0 ]; then
102+
echo -e "${C_RED} install $1 fail, install autoconf before compiling ${C_END}"
103+
exit 1;
104+
fi
105+
}
106+
107+
if ! check_program autoconf; then
108+
# not find autoconf,do install
109+
echo -e "not find ${C_GREEN} autoconf ${C_END} on localhost, now do install"
110+
install_package autoconf
111+
fi
112+
113+
if ! check_program tar; then
114+
echo -e "not find ${C_GREEN} tar ${C_END} on localhost, please install and min version ${C_GREEN} ${TAR_MIN_VERSION} ${C_END}"
115+
exit 1;
116+
fi
117+
118+
if ! check_program cmake; then
119+
if ! check_program cmake3; then
120+
echo -e "not find ${C_GREEN} cmake ${C_END}, please install cmake and min version ${C_GREEN} ${CMAKE_MIN_VERSION} ${C_END}"
121+
exit 1
122+
else
123+
CMAKE=cmake3
124+
fi
125+
else
126+
CMAKE=cmake
127+
fi
128+
129+
# get local cmake version
130+
LOCAL_CMAKE_VERSION=`${CMAKE} --version |grep version |grep -o '[0-9.]\+'`
131+
#compare cmake version
132+
version_compare ${CMAKE_MIN_VERSION} ${LOCAL_CMAKE_VERSION} 'cmake'
133+
134+
# get local tar version
135+
LOCAL_TAR_VERSION=`tar --version |head -n 1 |grep -o '[0-9.]\+'`
136+
#compare tar version
137+
version_compare ${TAR_MIN_VERSION} ${LOCAL_TAR_VERSION} 'tar'
138+
139+
if [ ! -d ${BUILD_DIR} ]; then
140+
mkdir ${BUILD_DIR}
141+
fi
142+
143+
cd ${BUILD_DIR}
144+
145+
use_pika_tools=""
146+
if [[ "${ARGS[0]}" = "tools" ]]; then
147+
use_pika_tools="-DUSE_PIKA_TOOLS=ON"
148+
fi
149+
150+
with_command_docs=""
151+
if [ "${WITH_COMMAND_DOCS}" = "ON" ]; then
152+
with_command_docs="-DWITH_COMMAND_DOCS=ON"
153+
fi
154+
155+
${CMAKE} ${use_pika_tools} ${with_command_docs} .. .
156+
157+
if [ $? -ne 0 ]; then
158+
echo -e "${C_RED} cmake execution error ${C_END}"
159+
exit 1
160+
fi
161+
162+
make -j ${CPU_CORE}
163+
164+
if [ $? -eq 0 ]; then
165+
echo -e "pika compile complete, output file ${C_GREEN} ${BUILD_DIR}/pika-migrate ${C_END}"
166+
else
167+
echo -e "${C_RED} pika compile fail ${C_END}"
168+
exit 1
169+
fi

0 commit comments

Comments
 (0)