4 Star 11 Fork 4

唯品会 / redis-migrate-tool

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

redis-migrate-tool

redis-migrate-tool is a convenient and useful tool for migrating data between redis.

中文介绍

Features

  • Fast.
  • Multi-Threads.
  • Based on redis replication.
  • Live migration.
  • In the process of migrating data, the source redis can also provide services for users.
  • Heterogeneous migration.
  • Twemproxy and redis cluster support.
  • When the target is twemproxy, keys are direct imported into redis behind the twemproxy.
  • Migration Status view.
  • Data Verification Mechanism.

Dependence

Please install automake, libtool, autoconf and bzip2 at first.

Build

To build redis-migrate-tool:

$ cd redis-migrate-tool
$ autoreconf -fvi
$ ./configure
$ make
$ src/redis-migrate-tool -h

RUN

src/redis-migrate-tool -c rmt.conf -o log -d

WARNING

Before run this tool, make sure your source redis machines have enough memory allowed at least one redis generate rdb file.

If your source machines have large enough memory allowed all the redis generate rdb files at one time, you can set 'source_safe: false' in the rmt.conf.

Not supported redis command

The following commands are not supported to be propagated to the target redis group, because the keys in those commands maybe cross different target redis nodes:

RENAME,RENAMENX,RPOPLPUSH,BRPOPLPUSH,FLUSHALL,FLUSHDB,BITOP,MOVE,GEORADIUS,GEORADIUSBYMEMBER,EVAL,EVALSHA,SCRIPT,PFMERGE

Configuration

Config file has three parts: source, target and common.

source OR target:

  • type: The group redis type. Possible values are:
  • single
  • twemproxy
  • redis cluster
  • rdb file
  • aof file
  • servers:: The list of redis address in the group. If type is twemproxy, this is same as the twemproxy config file. If type is rdb file, this is the file name.
  • redis_auth: Authenticate to the Redis server on connect.
  • timeout: Timeout in milliseconds for read/write with Redis server. Now just for source redis group. Defaults to 120000.
  • hash: The name of the hash function. Just for type is twemproxy. Possible values are:
  • one_at_a_time
  • md5
  • crc16
  • crc32 (crc32 implementation compatible with libmemcached)
  • crc32a (correct crc32 implementation as per the spec)
  • fnv1_64
  • fnv1a_64
  • fnv1_32
  • fnv1a_32
  • hsieh
  • murmur
  • jenkins
  • hash_tag: A two character string that specifies the part of the key used for hashing. Eg "{}" or "$$". Hash tag enable mapping different keys to the same server as long as the part of the key within the tag is the same. Just for type is twemproxy.
  • distribution: The key distribution mode. Just for type is twemproxy. Possible values are:
  • ketama
  • modula
  • random

common:

  • listen: The listening address and port (name:port or ip:port). Defaults to 127.0.0.1:8888.
  • max_clients: The max clients count for the listen port. Defaults to 100.
  • threads: The max threads count can be used by redis-migrate-tool. Defaults to the cpu core count.
  • step: The step for parse request. The higher the number, the more quickly to migrate, but the more memory used. Defaults to 1.
  • mbuf_size: Mbuf size for request. Defaults to 512.
  • noreply: A boolean value that decide whether to check the target group replies. Defaults to false.
  • source_safe: A boolean value that protect the source group machines memory safe. If it is true, the tool can guarantee only one redis to generate rdb file at one time on the same machine for source group. In addition, 'source_safe: true' may use less threads then you set. Defaults to true.
  • dir: Work directory, used to store files(such as rdb file). Defaults to the current directory.
  • filter: Filter keys if they do not match the pattern. The pattern is Glob-style. Defaults is NULL.

filter supported glob-style patterns:

  • h?llo matches hello, hallo and hxllo

  • h*llo matches hllo and heeeello

  • h[ae]llo matches hello and hallo, but not hillo

  • h[^e]llo matches hallo, hbllo, ... but not hello

  • h[a-b]llo matches hallo and hbllo

Use \ to escape special characters if you want to match them verbatim.

For example, the configuration file shown below is to migrate data from single to twemproxy.

[source]
type: single
servers:
 - 127.0.0.1:6379
 - 127.0.0.1:6380
 - 127.0.0.1:6381
 - 127.0.0.1:6382

[target]
type: twemproxy
hash: fnv1a_64
hash_tag: "{}"
distribution: ketama
servers:
 - 127.0.0.1:6380:1 server1
 - 127.0.0.1:6381:1 server2
 - 127.0.0.1:6382:1 server3
 - 127.0.0.1:6383:1 server4

[common]
listen: 0.0.0.0:8888
threads: 2
step: 1
mbuf_size: 1024
source_safe: true

Migrate data from twemproxy to redis cluster.

[source]
type: twemproxy
hash: fnv1a_64
hash_tag: "{}"
distribution: ketama
servers:
 - 127.0.0.1:6379
 - 127.0.0.1:6380
 - 127.0.0.1:6381
 - 127.0.0.1:6382

[target]
type: redis cluster
servers:
 - 127.0.0.1:7379

[common]
listen: 0.0.0.0:8888
step: 1
mbuf_size: 512

Migrate data from a redis cluster to another redis cluster with key filter(key's prefix is "abc").

[source]
type: redis cluster
servers:
 - 127.0.0.1:8379

[target]
type: redis cluster
servers:
 - 127.0.0.1:7379

[common]
listen: 0.0.0.0:8888
filter: abc*

Load data from rdb file to redis cluster.

[source]
type: rdb file
servers:
 - /data/redis/dump1.rdb
 - /data/redis/dump2.rdb

[target]
type: redis cluster
servers:
 - 127.0.0.1:7379

[common]
listen: 0.0.0.0:8888
step: 2
mbuf_size: 512
source_safe: false

Just save rdb file from redis cluster.

[source]
type: redis cluster
servers:
 - 127.0.0.1:7379

[target]
type: rdb file

[common]
listen: 0.0.0.0:8888
source_safe: true

Load data from aof file to redis cluster.

[source]
type: aof file
servers:
 - /data/redis/appendonly1.aof
 - /data/redis/appendonly2.aof

[target]
type: redis cluster
servers:
 - 127.0.0.1:7379

[common]
listen: 0.0.0.0:8888
step: 2

STATUS

You can use redis-cli to connect with redis-migrate-tool. The listening address and port can be setted at common config.

info command

For example, you try the info command:

$redis-cli -h 127.0.0.1 -p 8888
127.0.0.1:8888> info
# Server
version:0.1.0
os:Linux 2.6.32-573.12.1.el6.x86_64 x86_64
multiplexing_api:epoll
gcc_version:4.4.7
process_id:9199
tcp_port:8888
uptime_in_seconds:1662
uptime_in_days:0
config_file:/ect/rmt.conf

# Clients
connected_clients:1
max_clients_limit:100
total_connections_received:3

# Memory
mem_allocator:jemalloc-4.0.4

# Group
source_nodes_count:32
target_nodes_count:48

# Stats
all_rdb_received:1
all_rdb_parsed:1
all_aof_loaded:0
rdb_received_count:32
rdb_parsed_count:32
aof_loaded_count:0
total_msgs_recv:7753587
total_msgs_sent:7753587
total_net_input_bytes:234636318
total_net_output_bytes:255384129
total_net_input_bytes_human:223.77M
total_net_output_bytes_human:243.55M
total_mbufs_inqueue:0
total_msgs_outqueue:0
127.0.0.1:8888>

info command response instruction:

Server:

  • version: The redis-migrate-tool version number.
  • os: The os uname.
  • multiplexing_api: Multiplexing API.
  • gcc_version: Gcc version.
  • process_id: The process id of the redis-migrate-tool.
  • tcp_port: The tcp port redis-migrate-tool listening.
  • uptime_in_seconds: Seconds the redis-migrate-tool running.
  • uptime_in_days: Days the redis-migrate-tool running.
  • config_file: The config file name for the redis-migrate-tool.

Clients:

  • connected_clients: The count of clients that connected at present.
  • max_clients_limit: The max number of clients that allows to accept at the same time.
  • total_connections_received: The total count of connections that received so far.

Group:

  • source_nodes_count: The nodes count of source redis group.
  • target_nodes_count: The nodes count of target redis group.

Stats:

  • all_rdb_received: Whether all the rdb of the nodes in source group received.
  • all_rdb_parsed: Whether all the rdb of the nodes in source group parsed finished.
  • all_aof_loaded: Whether all the aof file of the nodes in source group loaded finished.
  • rdb_received_count: The received rdb count for the nodes in source group.
  • rdb_parsed_count: The parsed finished rdb count for the nodes in source group.
  • aof_loaded_count: The loaded finished aof file count for the nodes in source group.
  • total_msgs_recv: The total count of messages that had received from the source group.
  • total_msgs_sent: The total count of messages that had sent to the target group and received response from target group.
  • total_net_input_bytes: The total count of input bytes that had received from the source group.
  • total_net_output_bytes: The total count of output bytes that had sent to the target group.
  • total_net_input_bytes_human: Same as the total_net_input_bytes, but convert into human readable.
  • total_net_output_bytes_human: Same as the total_net_output_bytes, but convert into human readable.
  • total_mbufs_inqueue: Cached commands data(not include rdb data) by mbufs input from source group.
  • total_msgs_outqueue: Msgs will be sent to target group and msgs had been sent to target but waiting for the response.

OTHER COMMANDS

shutdown [seconds|asap]

The command behavior is the following:

  • Stop the replication from the source redis.
  • Try to send the cached data in redis-migrate-tool to the target redis.
  • Redis-migrate-tool stop and exit.

Parameter:

  • seconds: Most of seconds that redis-migrate-tool can used to send cached data to target redis before exit. Defaults to 10 seconds.
  • asap: Don't care about the cached data, just exit right now.

For example, you try the shutdown command:

$redis-cli -h 127.0.0.1 -p 8888
127.0.0.1:8888> shutdown
OK

CHECK THE DATA

After migrate the data, you can use redis_check command to check data in the source group and target group.

Try the redis_check command:

$src/redis-migrate-tool -c rmt.conf -o log -C redis_check
Check job is running...

Checked keys: 1000
Inconsistent value keys: 0
Inconsistent expire keys : 0
Other check error keys: 0
Checked OK keys: 1000

All keys checked OK!
Check job finished, used 1.041s

If you want check more keys, try the follow:

$src/redis-migrate-tool -c rmt.conf -o log -C "redis_check 200000"
Check job is running...

Checked keys: 200000
Inconsistent value keys: 0
Inconsistent expire keys : 0
Other check error keys: 0
Checked OK keys: 200000

All keys checked OK!
Check job finished, used 11.962s

INSERT SOME KEYS JUST FOR TEST

Try the redis_testinsert command:

$src/redis-migrate-tool -c rmt.conf -o log -C "redis_testinsert"
Test insert job is running...

Insert string keys: 200
Insert list keys  : 200
Insert set keys   : 200
Insert zset keys  : 200
Insert hash keys  : 200
Insert total keys : 1000

Correct inserted keys: 1000
Test insert job finished, used 0.525s

If you want insert more keys, try the follow:

$src/redis-migrate-tool -c rmt.conf -o log -C "redis_testinsert 30000"
Test insert job is running...

Insert string keys: 6000
Insert list keys  : 6000
Insert set keys   : 6000
Insert zset keys  : 6000
Insert hash keys  : 6000
Insert total keys : 30000

Correct inserted keys: 30000
Test insert job finished, used 15.486s

If you want insert only string type keys, try the follow:

$src/redis-migrate-tool -c rmt.conf -o log -C "redis_testinsert string"
Test insert job is running...

Insert string keys: 1000
Insert list keys  : 0
Insert set keys   : 0
Insert zset keys  : 0
Insert hash keys  : 0
Insert total keys : 1000

Correct inserted keys: 1000
Test insert job finished, used 0.024s

If you want insert some special type keys, try the follow:

$src/redis-migrate-tool -c rmt.conf -o log -C "redis_testinsert string|set|list 10000"
Test insert job is running...

Insert string keys: 3336
Insert list keys  : 3336
Insert set keys   : 3328
Insert zset keys  : 0
Insert hash keys  : 0
Insert total keys : 10000

Correct inserted keys: 10000
Test insert job finished, used 5.539s

License

Copyright © 2016 VIPSHOP Inc.

Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS

简介

Redis 集群迁移工具,基于redis复制,快速,稳定 展开 收起
C 等 3 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
C
1
https://gitee.com/vipshop/redis-migrate-tool.git
git@gitee.com:vipshop/redis-migrate-tool.git
vipshop
redis-migrate-tool
redis-migrate-tool
master

搜索帮助