123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- /*
- * Copyright 2008, Yahoo! Inc.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.yahoo.zookeeper.server;
- import java.nio.ByteBuffer;
- import java.util.ArrayList;
- import java.util.List;
- import com.yahoo.jute.Record;
- import com.yahoo.zookeeper.ZooDefs.OpCode;
- import com.yahoo.zookeeper.data.Id;
- import com.yahoo.zookeeper.txn.TxnHeader;
- /**
- * This is the structure that represents a request moving through a chain of
- * RequestProcessors. There are various pieces of information that is tacked
- * onto the request as it is processed.
- */
- public class Request {
- public final static Request requestOfDeath = new Request(null, 0, 0, 0,
- null, null);
- /**
- * @param cnxn
- * @param sessionId
- * @param xid
- * @param type
- * @param bb
- */
- public Request(ServerCnxn cnxn, long sessionId, int xid, int type,
- ByteBuffer bb, List<Id> authInfo) {
- this.cnxn = cnxn;
- this.sessionId = sessionId;
- this.cxid = xid;
- this.type = type;
- this.request = bb;
- this.authInfo = authInfo;
- }
- public long sessionId;
- public int cxid;
- public int type;
- public ByteBuffer request;
- public ServerCnxn cnxn;
- public TxnHeader hdr;
- public Record txn;
- public long zxid = -1;
- public List<Id> authInfo;
- public long createTime = System.currentTimeMillis();
- /**
- * is the packet type a valid packet in zookeeper
- *
- * @param type
- * the type of the packet
- * @return true if a valid packet, false if not
- */
- static boolean isValid(int type) {
- // make sure this is always synchronized with Zoodefs!!
- switch (type) {
- case OpCode.notification:
- return false;
- case OpCode.create:
- case OpCode.delete:
- case OpCode.createSession:
- case OpCode.exists:
- case OpCode.getData:
- case OpCode.setData:
- case OpCode.sync:
- case OpCode.getACL:
- case OpCode.setACL:
- case OpCode.getChildren:
- case OpCode.ping:
- case OpCode.closeSession:
- return true;
- default:
- return false;
- }
- }
- static boolean isQuorum(int type) {
- switch (type) {
- case OpCode.exists:
- case OpCode.getACL:
- case OpCode.getChildren:
- case OpCode.getData:
- return false;
- case OpCode.error:
- case OpCode.closeSession:
- case OpCode.create:
- case OpCode.createSession:
- case OpCode.delete:
- case OpCode.setACL:
- case OpCode.setData:
- return true;
- default:
- return false;
- }
- }
-
- static String op2String(int op) {
- switch (op) {
- case OpCode.notification:
- return "notification";
- case OpCode.create:
- return "create";
- case OpCode.delete:
- return "delete";
- case OpCode.exists:
- return "exists";
- case OpCode.getData:
- return "getDate";
- case OpCode.setData:
- return "setData";
- case OpCode.sync:
- return "sync:";
- case OpCode.getACL:
- return "getACL";
- case OpCode.setACL:
- return "setACL";
- case OpCode.getChildren:
- return "getChildren";
- case OpCode.ping:
- return "ping";
- case OpCode.createSession:
- return "createSession";
- case OpCode.closeSession:
- return "closeSession";
- case OpCode.error:
- return "error";
- default:
- return "unknown " + op;
- }
- }
- public String toString() {
- StringBuffer sb = new StringBuffer();
- sb.append(Long.toHexString(sessionId)).append(" ");
- sb.append(Long.toHexString(cxid)).append(" ");
- sb.append(Long.toHexString((hdr == null ? -2 : hdr.getZxid()))).append(
- " ");
- sb
- .append(
- " txn type = "
- + (hdr == null ? "unknown" : "" + hdr.getType()))
- .append(" ");
- sb.append(op2String(type)).append(" ");
- String path = "n/a";
- if (type != OpCode.createSession) {
- try {
- request.clear();
- int pathLen = request.getInt();
- byte b[] = new byte[pathLen];
- request.get(b);
- path = new String(b);
- request.clear();
- } catch (Exception e) {
- }
- }
- sb.append(path).append(" ");
- return sb.toString();
- }
- }
|