Request.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.zookeeper.server;
  19. import java.nio.ByteBuffer;
  20. import java.util.List;
  21. import org.apache.jute.Record;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import org.apache.zookeeper.KeeperException;
  25. import org.apache.zookeeper.ZooDefs.OpCode;
  26. import org.apache.zookeeper.data.Id;
  27. import org.apache.zookeeper.txn.TxnHeader;
  28. /**
  29. * This is the structure that represents a request moving through a chain of
  30. * RequestProcessors. There are various pieces of information that is tacked
  31. * onto the request as it is processed.
  32. */
  33. public class Request {
  34. private static final Logger LOG = LoggerFactory.getLogger(Request.class);
  35. public final static Request requestOfDeath = new Request(null, 0, 0, 0,
  36. null, null);
  37. /**
  38. * @param cnxn
  39. * @param sessionId
  40. * @param xid
  41. * @param type
  42. * @param bb
  43. */
  44. public Request(ServerCnxn cnxn, long sessionId, int xid, int type,
  45. ByteBuffer bb, List<Id> authInfo) {
  46. this.cnxn = cnxn;
  47. this.sessionId = sessionId;
  48. this.cxid = xid;
  49. this.type = type;
  50. this.request = bb;
  51. this.authInfo = authInfo;
  52. }
  53. public final long sessionId;
  54. public final int cxid;
  55. public final int type;
  56. public final ByteBuffer request;
  57. public final ServerCnxn cnxn;
  58. public TxnHeader hdr;
  59. public Record txn;
  60. public long zxid = -1;
  61. public final List<Id> authInfo;
  62. public final long createTime = System.currentTimeMillis();
  63. private Object owner;
  64. private KeeperException e;
  65. public Object getOwner() {
  66. return owner;
  67. }
  68. public void setOwner(Object owner) {
  69. this.owner = owner;
  70. }
  71. /**
  72. * is the packet type a valid packet in zookeeper
  73. *
  74. * @param type
  75. * the type of the packet
  76. * @return true if a valid packet, false if not
  77. */
  78. static boolean isValid(int type) {
  79. // make sure this is always synchronized with Zoodefs!!
  80. switch (type) {
  81. case OpCode.notification:
  82. return false;
  83. case OpCode.create:
  84. case OpCode.delete:
  85. case OpCode.createSession:
  86. case OpCode.exists:
  87. case OpCode.getData:
  88. case OpCode.check:
  89. case OpCode.multi:
  90. case OpCode.setData:
  91. case OpCode.sync:
  92. case OpCode.getACL:
  93. case OpCode.setACL:
  94. case OpCode.getChildren:
  95. case OpCode.getChildren2:
  96. case OpCode.ping:
  97. case OpCode.closeSession:
  98. case OpCode.setWatches:
  99. return true;
  100. default:
  101. return false;
  102. }
  103. }
  104. static boolean isQuorum(int type) {
  105. switch (type) {
  106. case OpCode.exists:
  107. case OpCode.getACL:
  108. case OpCode.getChildren:
  109. case OpCode.getChildren2:
  110. case OpCode.getData:
  111. return false;
  112. case OpCode.error:
  113. case OpCode.closeSession:
  114. case OpCode.create:
  115. case OpCode.createSession:
  116. case OpCode.delete:
  117. case OpCode.setACL:
  118. case OpCode.setData:
  119. case OpCode.check:
  120. case OpCode.multi:
  121. return true;
  122. default:
  123. return false;
  124. }
  125. }
  126. static String op2String(int op) {
  127. switch (op) {
  128. case OpCode.notification:
  129. return "notification";
  130. case OpCode.create:
  131. return "create";
  132. case OpCode.setWatches:
  133. return "setWatches";
  134. case OpCode.delete:
  135. return "delete";
  136. case OpCode.exists:
  137. return "exists";
  138. case OpCode.getData:
  139. return "getData";
  140. case OpCode.check:
  141. return "check";
  142. case OpCode.multi:
  143. return "multi";
  144. case OpCode.setData:
  145. return "setData";
  146. case OpCode.sync:
  147. return "sync:";
  148. case OpCode.getACL:
  149. return "getACL";
  150. case OpCode.setACL:
  151. return "setACL";
  152. case OpCode.getChildren:
  153. return "getChildren";
  154. case OpCode.getChildren2:
  155. return "getChildren2";
  156. case OpCode.ping:
  157. return "ping";
  158. case OpCode.createSession:
  159. return "createSession";
  160. case OpCode.closeSession:
  161. return "closeSession";
  162. case OpCode.error:
  163. return "error";
  164. default:
  165. return "unknown " + op;
  166. }
  167. }
  168. @Override
  169. public String toString() {
  170. StringBuilder sb = new StringBuilder();
  171. sb.append("sessionid:0x").append(Long.toHexString(sessionId))
  172. .append(" type:").append(op2String(type))
  173. .append(" cxid:0x").append(Long.toHexString(cxid))
  174. .append(" zxid:0x").append(Long.toHexString(hdr == null ?
  175. -2 : hdr.getZxid()))
  176. .append(" txntype:").append(hdr == null ?
  177. "unknown" : "" + hdr.getType());
  178. // best effort to print the path assoc with this request
  179. String path = "n/a";
  180. if (type != OpCode.createSession
  181. && type != OpCode.setWatches
  182. && type != OpCode.closeSession
  183. && request != null
  184. && request.remaining() >= 4)
  185. {
  186. try {
  187. // make sure we don't mess with request itself
  188. ByteBuffer rbuf = request.asReadOnlyBuffer();
  189. rbuf.clear();
  190. int pathLen = rbuf.getInt();
  191. // sanity check
  192. if (pathLen >= 0
  193. && pathLen < 4096
  194. && rbuf.remaining() >= pathLen)
  195. {
  196. byte b[] = new byte[pathLen];
  197. rbuf.get(b);
  198. path = new String(b);
  199. }
  200. } catch (Exception e) {
  201. // ignore - can't find the path, will output "n/a" instead
  202. }
  203. }
  204. sb.append(" reqpath:").append(path);
  205. return sb.toString();
  206. }
  207. public void setException(KeeperException e) {
  208. this.e = e;
  209. }
  210. public KeeperException getException() {
  211. return e;
  212. }
  213. }