數(shù)據(jù)復(fù)制基于PacificA
對(duì)kafka為了吞吐返回客戶端的時(shí)候不保證數(shù)據(jù)已經(jīng)落盤了,所以后面不管怎么復(fù)制寫(xiě)多少個(gè)副本都不保證數(shù)據(jù)落盤持久(全部機(jī)器一起掛還是可能會(huì)丟數(shù)據(jù))
Then it will write the new messages to local disk. On Linux, the messages are written to the filesystem cache and there is no guarantee about when they will be written to disk. Kafka does not wait for the data to get persisted to disk—it relies on replication for message durability.
kafka以partition為單位復(fù)制數(shù)據(jù)钞速。
一些關(guān)鍵的配置:
- unclean.leader.election.enable
false表示leader只會(huì)從in-sync里選leader平斩。0.11開(kāi)始默認(rèn)false绵脯。非leader不保證有最新數(shù)據(jù)伞租,也就是acks=all的請(qǐng)求只保證發(fā)到了in-sync里的副本敦跌,如果后面在非in-sync里選leader, 這些沒(méi)復(fù)制到這個(gè)副本的數(shù)據(jù)都會(huì)丟贴谎。如果in-sync里的副本都掛了,這時(shí)只能不可用或者選擇在非in-sync里選leader但可能丟一些數(shù)據(jù)旺芽。 - replication.factor
復(fù)制個(gè)數(shù) - min.insync.replicas
表示最后要insync集合最少要有多少個(gè)副本沪猴。對(duì)于acks=all寫(xiě)成功就表示最少有min.insync.replicas個(gè)副本收到了msg
客戶端 producer 推數(shù)據(jù)的時(shí)候可以配置acks=all, all表示leader收到后要得到全部in-sync副本的ack才返回客戶端成功了。
kafka會(huì)維護(hù)一個(gè)in-sync副本集合采章,leader會(huì)把落后太多的副本從in-sync集合里剔除运嗜。
- kafka有沒(méi)可能存在兩個(gè)leader同時(shí)接受可寫(xiě)的情況(network partition),最后丟一部分?jǐn)?shù)據(jù)悯舟?
看PacificA論文是用lease來(lái)防止同時(shí)存在兩個(gè)leader的担租,也就是如果zero clock drift是可以防止存在這種情況的。配置都存zk, 修改配置提交zk,版本號(hào)遞增抵怎。leader有可能隨時(shí)被剔除奋救,其它in-sync副本修改配置成為leader, 但原來(lái)但leader還不知道仍然接受請(qǐng)求(如果ack=1他寫(xiě)成功了就返回成功了)。為了防止這種情況便贵,leader會(huì)給fellower請(qǐng)求得到一個(gè)lease, 比如5s內(nèi)你保證不去選舉成leader, 這樣leader只要得到這個(gè)lease在一定時(shí)間內(nèi)就可以放心當(dāng)不會(huì)自己已經(jīng)被剔除成為leader了菠镇。
https://cwiki.apache.org/confluence/display/KAFKA/KIP-101+-+Alter+Replication+Protocol+to+use+Leader+Epoch+rather+than+High+Watermark+for+Truncation
https://stackoverflow.com/questions/48825755/how-does-kafka-handle-network-partitions
https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Replication
https://cwiki.apache.org/confluence/display/KAFKA/KIP-101+-+Alter+Replication+Protocol+to+use+Leader+Epoch+rather+than+High+Watermark+for+Truncation
https://stackoverflow.com/questions/48825755/how-does-kafka-handle-network-partitions?rq=1