教训
zk的API 和 封装后的API,语义和原生的执行行为,要自己进去瞅瞅
zk 里面bytes的编解码要保持一致
create和set 以及reset要区分来。第一次create,之后是set以及reset
需求背景
需要重建路径,然后写入状态信息。要求,路径不存在,就创建后写入;如果存在,直接覆盖老的内容。
遇到问题
覆盖和创建API 不能混用,特别是自己封装后的API,语义可能与zookeeper原生的不一致。覆盖写,默认path 已经存在;创建写,默认path 不存在。如果path存在,然后create 其实无效。可能存在写丢失。
setData 里面的byte,获取后的转化。通常是string .getBytes. 如果遇到int2bytes 或者 object2bytes 啥的,那么解码byte的时候需要保持读写一致,如果没有文档说明,就之后测试了。
解决方法
exist path,然后决定是set or create
create的时候,如果failed,执行写第二次。
部分代码
privatevoidfeedbackForGroup(booleanisSuc, Stringmsg, StringusrPoint){TerminatorZkClientzkClient=ZkClientHolder.zkClient;StringpathApp=fullDumpStatsZkPath+context.getServiceName();Stringid=context.getCoreName().split("-")[1];StringpathGroup=pathApp+"/"+id;if(isSuc){StringsucMsg=(SuccessTag+usrPoint+"\"}").toString();try{if(zkClient.exists(pathGroup)){zkClient.setData(pathGroup, sucMsg.getBytes());}else{booleancreateStatus=zkClient.createPathIfAbsent(pathGroup, sucMsg.getBytes(), true);if(!createStatus){booleanrecreateStatus=zkClient.createPathIfAbsent(pathGroup, sucMsg.getBytes(), true);if(!recreateStatus)logger.error("---->Rewrite group failed -- FeedBack for group fulldump sucess_status to zk. Path="+pathGroup+",sucMsg="+sucMsg);elselogger.warn("Rewriter group ok-- FeedBack for group fulldump sucess_status to zk. Path="+pathGroup+",sucMsg="+sucMsg);}elselogger.warn("Writer group ok-- FeedBack for group fulldump sucess_status to zk ok. Path="+pathGroup+",sucMsg="+sucMsg);}}catch(TerminatorZKExceptione){logger.error("feedback for group -- sucess back zk excption", e);}}else{//failedgroupthenfailedappStringfailMsg=(FailedTag+msg+"\"}").toString();try{if(zkClient.exists(pathGroup)){zkClient.setData(pathGroup, failMsg.getBytes());}else{booleancreateStatus=zkClient.createPathIfAbsent(pathGroup, failMsg.getBytes(), true);if(!createStatus){booleanrecreateStatus=zkClient.createPathIfAbsent(pathGroup, failMsg.getBytes(), true);if(!recreateStatus)logger.error("---->Rewriter group failed-- FeedBack for group fulldump failed_status to zk. Path="+pathGroup+",failMsg="+failMsg);elselogger.warn("Rewriter group ok-- FeedBack for group fulldump failed_status to zk. Path="+pathGroup+",failMsg="+failMsg);}elselogger.warn("Writer group ok-- FeedBack for group fulldump failed_status to zk. Path="+pathGroup+",failMsg="+failMsg);}}catch(TerminatorZKExceptione){logger.error("feedback for group -- failed back zk exception", e);}}}