概述

每个分区有n个副本,可以承受n-1个节点故障。

每个副本都有自己的leader,其余都是follower。

zk中存放分区的leader和 follower replica的信息。(get /brokers/topics/mytest2/partitions/0/state)

每个副本存储消息的部分数据在本地的log和offset中,周期性同步到disk,确保消息写入全部副本或不写入任何一个。

leader故障时,消息或者在写入本地log,或者在producer收到ack消息前,resent partition到new leader。

kafka支持的副本模型

a)同步复制:producer从zk中找leader,并发送消息,消息立即写入本地log,而且follower开始pull消息。每个follower将消息写入各自的log后,向leader发送确认回执,leader在本地副本的写入工作均完成并且收到所有follower的确认回执后,再向producer发送确认回执。

b)异步复制:leader的本地log写入完成后即向producer发送确认回执。

原文——摘自《[PACKT]Apache Kafka.pdf》

Replication in Kafka

Before we talk about replication in Kafka, let's talk about message partitioning.In Kafka, message partitioning strategy is used at the Kafka broker end. The decisionabout how the message is partitioned is taken by the producer, and the broker storesthe messages in the same order as they arrive. The number of partitions can beconfigured for each topic within the Kafka broker.

Kafka replication is one of the very important features introduced in Kafka 0.8.Though Kafka is highly scalable, for better durability of messages and high availability of Kafka clusters, replication guarantees that the message will be published and consumed even in case of broker failure, which may be caused by any reason. Here, both producers and consumers are replication aware in Kafka.The following diagram explains replication in Kafka:

Let's discuss the preceding diagram in detail.

In replication, each partition of a message has n replicas and can afford n-1 failures to guarantee message delivery. Out of the n replicas, one replica acts as the lead replica for the rest of the replicas. ZooKeeper keeps the information about the lead replica and the current in-sync follower replica (lead replica maintains the list of all in-sync follower replicas).

Each replica stores its part of the message in local logs and offsets, and is periodically synced to the disk. This process also ensures that either a message is written to all the replicas or to none of them.

If the lead replica fails, either while writing the message partition to its local log or before sending the acknowledgement to the message producer, a message partition is resent by the producer to the new lead broker.

The process of choosing the new lead replica is that all followers' In-sync Replicas (ISRs) register themselves with ZooKeeper. The very first registered replica becomes the new lead replica, and the rest of the registered replicas become the followers.

Kafka supports the following replication modes:

• Synchronous replication: In synchronous replication, a producer first identifies the lead replica from ZooKeeper and publishes the message.As soon as the message is published, it is written to the log of the lead replica and all the followers of the lead start pulling the message, and by using a single channel,the order of messages is ensured. Each follower replica sends an acknowledgement to the lead replica once the message is written to its respective logs. Once replications are complete and all expected acknowledgements are received, the lead replica sends an acknowledgement to the producer.On the consumer side, all the pulling of messages is done from the lead replica.

• Asynchronous replication: The only difference in this mode is that as soon as a lead replica writes the message to its local log, it sends the acknowledgement to the message client and does not wait for the acknowledgements from follower replicas. But as a down side, this mode does not ensure the message delivery in case of broker failure.

翻译

kafka的副本机制
在我们讨论Kafka的副本机制之前,让我们来谈谈消息分区。
在Kafka中,消息分区发生在broker端。
如何分区消息是由producer决定的。
broker存储消息的顺序与它们到达的顺序相同。
可以为broker中的每个主题配置分区数量。

Kafka副本策略是Kafka 0.8中引入的非常重要的功能之一。
虽然Kafka具有高度可扩展性,但副本是为了kafka集群有更好的消息持久性和高可用性。
副本保证了即使在某个broker故障时也可以发布和消费消息。
在这里,生产者和消费者都可以在Kafka中识别副本。
下图说明了Kafka中的副本策略:

我们将详细讨论上图。

在副本策略中,消息的每个分区都有n个副本,并且可以承受n-1个节点故障,保证消息分发。
在n个副本中,1个副本充当其余副本的lead。
ZooKeeper保存关于lead副本和当前同步的follower副本的信息(lead副本维护所有的同步follower副本的列表)

每个副本将消息的一部分存储在本地logs和offsets中,并定期同步到磁盘。此过程还确保可以将消息写入所有副本或不写入任何副本。
如果lead副本故障,则在将消息分区写入其本地log时或在向producer发送确认之前,producer将消息分区重新分发给新的lead broker。
推选新的leader过程就是followers在ZooKeeper中的注册过程,第一个注册的就是leader,其余注册成为followers。

Kafka支持以下复制模式:
•同步复制:在同步复制中,producer首先从ZooKeeper中识别lead副本并发布消息。消息一发布,就会将其写入lead副本的日志中,并且所有follwers都会开始拉消息,并通过使用单信道,确保消息的顺序。一旦将消息写入其各自的日志,每个follwers副本就向该lead副本发送确认。一旦复制完成并且收到所有预期的确认,lead副本就向priducer发送确认。在consumer方面,所有消息的拉取都是从lead副本完成的。
•异步复制:此模式与同步复制的唯一区别是,只要lead副本将消息写入其本地日志,它就会将确认发送到消息客户端,而不会等待来自followers副本的确认。但缺点是,这种模式不能确保在broker故障的情况下传递消息。

练习

1.搭建单节点多broker的kafka后,启动zk和kafka。

[root@hadoop ~]# cd /usr/local/kafka
[root@hadoop kafka]# zookeeper-server-start.sh config/zookeeper.properties
...
[root@hadoop kafka]# kafka-server-start.sh config/server0.properties &
...
[root@hadoop kafka]# kafka-server-start.sh config/server1.properties &
...
[root@hadoop kafka]# kafka-server-start.sh config/server2.properties &
... [root@hadoop ~]# jps
QuorumPeerMain
Kafka
Kafka
Kafka
Jps

2.创建主题(3个分区2个副本)

[root@hadoop ~]# cd /usr/local/kafka
[root@hadoop kafka]# kafka-topics.sh --create --zookeeper localhost: --replication-factor --partitions --topic mytest2
Created topic "mytest2".

2.1 查看主题

[root@hadoop ~]# kafka-topics.sh --describe --zookeeper localhost: --topic mytest2
Topic:mytest2 PartitionCount: ReplicationFactor: Configs:
Topic: mytest2 Partition: Leader: Replicas: , Isr: , #副本0在broker1和broker2上,leader是broker1
Topic: mytest2 Partition: Leader: Replicas: , Isr: ,
Topic: mytest2 Partition: Leader: Replicas: , Isr: ,

2.2 查看zk上面topic信息(与 查看主题 信息对应)

[root@hadoop ~]# cd /usr/local/kafka
[root@hadoop kafka]# zkCli.sh -server hadoop: #启动zk客户端
...
[zk: localhost:(CONNECTED) ] ls /brokers/topics/mytest2/partitions
[, , ]
[zk: localhost:(CONNECTED) ] get /brokers/topics/mytest2/partitions//state
{"controller_epoch":,"leader":,"version":,"leader_epoch":,"isr":[,]}
...
[zk: localhost:(CONNECTED) ] get /brokers/topics/mytest2/partitions//state
{"controller_epoch":,"leader":,"version":,"leader_epoch":,"isr":[,]}
...
[zk: localhost:(CONNECTED) ] get /brokers/topics/mytest2/partitions//state
{"controller_epoch":,"leader":,"version":,"leader_epoch":,"isr":[,]}
...

2.3 查看3个broker的日志目录

[root@hadoop ~]# ls /tmp/kafka-logs0
mytest2- mytest2- ... [root@hadoop ~]# ls /tmp/kafka-logs1
mytest2- mytest2- ... [root@hadoop ~]# ls /tmp/kafka-logs2
mytest2- mytest2- ...

可见 主题是按照replication-factor 2 * partitions 3 = 6 然后在broker中均衡分配的

3.修改主题的副本位置

[root@hadoop kafka]# kafka-topics.sh --alter --zookeeper localhost: replica-assigment :,:,: --topic mytest2

再次查看主题,发现没变,按理说这个命令没错啊?未完待续。。。

kafka4 副本机制的更多相关文章

  1. HDFS副本机制&负载均衡&机架感知&访问方式&健壮性&删除恢复机制&HDFS缺点

    副本机制 1.副本摆放策略 第一副本:放置在上传文件的DataNode上:如果是集群外提交,则随机挑选一台磁盘不太慢.CPU不太忙的节点上:第二副本:放置在于第一个副本不同的机架的节点上:第三副本:与 ...

  2. kafka副本机制之数据可靠性

    一.概述 为了提升集群的HA,Kafka从0.8版本开始引入了副本(Replica)机制,增加副本机制后,每个副本可以有多个副本,针对每个分区,都会从副本集(Assigned Replica,AR)中 ...

  3. hdfs深入:03、hdfs的架构以及副本机制和block块存储

    HDFS分布式文件系统设计目标 1.            硬件错误  由于集群很多时候由数量众多的廉价机组成,使得硬件错误成为常态 2.            数据流访问  所有应用以流的方式访问数 ...

  4. 深入理解 Kafka 副本机制

    一.Kafka集群 二.副本机制         2.1 分区和副本         2.2 ISR机制         2.3 不完全的首领选举         2.4 最少同步副本         ...

  5. Kafka 学习之路(五)—— 深入理解Kafka副本机制

    一.Kafka集群 Kafka使用Zookeeper来维护集群成员(brokers)的信息.每个broker都有一个唯一标识broker.id,用于标识自己在集群中的身份,可以在配置文件server. ...

  6. Kafka 系列(五)—— 深入理解 Kafka 副本机制

    一.Kafka集群 Kafka 使用 Zookeeper 来维护集群成员 (brokers) 的信息.每个 broker 都有一个唯一标识 broker.id,用于标识自己在集群中的身份,可以在配置文 ...

  7. 大数据:Hadoop(HDFS 的设计思路、设计目标、架构、副本机制、副本存放策略)

    一.HDFS 的设计思路 1)思路 切分数据,并进行多副本存储: 2)如果文件只以多副本进行存储,而不进行切分,会有什么问题 缺点 不管文件多大,都存储在一个节点上,在进行数据处理的时候很难进行并行处 ...

  8. C和C++中的副本机制

    函数的形参.return 都有副本机制.数组没有副本机制 (为了节约内存) 函数形参和局部变量的生命周期.函数调用结束后就会被回收.

  9. C语言副本机制

    1.除了数组外,其他都有副本机制(包括结构体数组) 2.结构体作为参数具有副本机制,结构体返回值也有副本机制 . 3.函数的参数和返回值都有他的副本机制. #include<stdio.h> ...

随机推荐

  1. js替换元素与设置时间间隔

    var lastReportTime = 0; //设置时间间隔 window.onload = function(){ setInterval(handleRefresh, 3000); } fun ...

  2. file类型input框设置上传相同文件,并都可以触发change事件。

    在使用file类型input框是,删除了第一次上传到文件,再次上传相同文件,无法触发change事件,所以在删除的js上添加如下js代码: document.getElementById('fileU ...

  3. mui---获取上一级窗口

    我们在用MUI做APP的时候,会用到要获取上一级的窗口.具体方法:获取当前webview窗口的创建者. 代码: plus.webview.currentWebview().opener();

  4. 6.26 py GIL

    在python中,多进程效率远大于多线程效率 python中存在GIL这个"线程锁", 关键地方可以使用c语言解决  GIL问题  然后可以提高cpu占用效率 异步的实现!!! 同 ...

  5. MySQL数据库一个字段对应多个值得模糊查询和多个字段对应一个模糊查询

    当一个字段想模糊查询出多个字段的时候,正常情况下一般会这么作 1 select * from a where name like 'a%' or name like 'b%' ....or ...; ...

  6. 引用:WebAPI中的定时处理-使用Quartz.Net

    引用: https://blog.csdn.net/lordwish/article/details/78926252 主要是给自己做个记录,不用到处找,这篇文章写的很全,推荐 文中:在项目的Glob ...

  7. 初级ai思维导图,基础人工智能设计图

    2017年2月8日09:35:46 仅供代码和逻辑设计图纸,只提供一个参考设计,后面可能会更新具体实施说明

  8. EF将IEnumerable<T>类型转换为Dictionary<T,T>类型

    x 无标题 #region 博客Code {DBEntities}生成EFModel的时候自己命名的 using ({DBEntities} db = new { DBEntities }()) { ...

  9. MSSQL优化之————探索MSSQL执行计划

    最近总想整理下对MSSQL的一些理解与感悟,却一直没有心思和时间写,晚上无事便写了一篇探索MSSQL执行计划,本文讲执行计划但不仅限于讲执行计划. 网上的SQL优化的文章实在是很多,说实在的,我也曾经 ...

  10. ES6:export default 和 export 区别

    export default 和 export 区别: 1.export与export default均可用于导出常量.函数.文件.模块等 2.你可以在其它文件或模块中通过import+(常量 | 函 ...