来源:蜘蛛抓取(WebSpider)
时间:2016-01-19 08:15
标签:
严以用权解决哪些问题
深入浅出Zookeeper之四Create请求和处理_小组_ThinkSAAS
深入浅出Zookeeper之四Create请求和处理
深入浅出Zookeeper之四Create请求和处理
客户端接口
public String create(final String path, byte data[], List&ACL& acl,
CreateMode createMode)
throws KeeperException, InterruptedException
final String clientPath =
PathUtils.validatePath(clientPath, createMode.isSequential());
final String serverPath = prependChroot(clientPath);
RequestHeader h = new RequestHeader();
h.setType(ZooDefs.OpCode.create);
CreateRequest request = new CreateRequest();
//CREATE请求需要server端响应
CreateResponse response = new CreateResponse();
request.setData(data);
//node类型
request.setFlags(createMode.toFlag());
request.setPath(serverPath);
if (acl != null && acl.size() == 0) {
throw new KeeperException.InvalidACLException();
request.setAcl(acl);
//同步提交
ReplyHeader r = cnxn.submitRequest(h, request, response, null);
//异常情况
if (r.getErr() != 0) {
throw KeeperException.create(KeeperException.Code.get(r.getErr()),
clientPath);
//真实路径,对于SEQUENTIAL NODE后面会加上序号
if (cnxn.chrootPath == null) {
return response.getPath();
return response.getPath().substring(cnxn.chrootPath.length());
请求提交过程和之前的exists一样,都是通过sendthread写出去,都会进入pengding队列等待server端返回。
server端处理也是一样,变化的只是RequestProcessor的业务逻辑。也就是说transport层是通用的,变化的是上层的业务层。
server端执行处理链,PrepRequestProcessor
switch (request.type) {
case OpCode.create:
//反序列化的对象
CreateRequest createRequest = new CreateRequest();
//zxid递增
pRequest2Txn(request.type, zks.getNextZxid(), request, createRequest, true);
request.zxid = zks.getZxid();
nextProcessor.processRequest(request);
protected void pRequest2Txn(int type, long zxid, Request request, Record record, boolean deserialize)
throws KeeperException, IOException, RequestProcessorException
//构造内部的事务头
request.hdr = new TxnHeader(request.sessionId, request.cxid, zxid,
zks.getTime(), type);
switch (type) {
case OpCode.create:
//检查session是否还有效
zks.sessionTracker.checkSession(request.sessionId, request.getOwner());
CreateRequest createRequest = (CreateRequest)
//反序列化
if(deserialize)
ByteBufferInputStream.byteBuffer2Record(request.request, createRequest);
String path = createRequest.getPath();
//路径规则检查
int lastSlash = path.lastIndexOf('/');
if (lastSlash == -1 || path.indexOf('