Request.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yahoo.zookeeper.server;
  17. import java.nio.ByteBuffer;
  18. import java.util.ArrayList;
  19. import java.util.List;
  20. import com.yahoo.jute.Record;
  21. import com.yahoo.zookeeper.ZooDefs.OpCode;
  22. import com.yahoo.zookeeper.data.Id;
  23. import com.yahoo.zookeeper.txn.TxnHeader;
  24. /**
  25. * This is the structure that represents a request moving through a chain of
  26. * RequestProcessors. There are various pieces of information that is tacked
  27. * onto the request as it is processed.
  28. */
  29. public class Request {
  30. public final static Request requestOfDeath = new Request(null, 0, 0, 0,
  31. null, null);
  32. /**
  33. * @param cnxn
  34. * @param sessionId
  35. * @param xid
  36. * @param type
  37. * @param bb
  38. */
  39. public Request(ServerCnxn cnxn, long sessionId, int xid, int type,
  40. ByteBuffer bb, List<Id> authInfo) {
  41. this.cnxn = cnxn;
  42. this.sessionId = sessionId;
  43. this.cxid = xid;
  44. this.type = type;
  45. this.request = bb;
  46. this.authInfo = authInfo;
  47. }
  48. public long sessionId;
  49. public int cxid;
  50. public int type;
  51. public ByteBuffer request;
  52. public ServerCnxn cnxn;
  53. public TxnHeader hdr;
  54. public Record txn;
  55. public long zxid = -1;
  56. public List<Id> authInfo;
  57. public long createTime = System.currentTimeMillis();
  58. /**
  59. * is the packet type a valid packet in zookeeper
  60. *
  61. * @param type
  62. * the type of the packet
  63. * @return true if a valid packet, false if not
  64. */
  65. static boolean isValid(int type) {
  66. // make sure this is always synchronized with Zoodefs!!
  67. switch (type) {
  68. case OpCode.notification:
  69. return false;
  70. case OpCode.create:
  71. case OpCode.delete:
  72. case OpCode.createSession:
  73. case OpCode.exists:
  74. case OpCode.getData:
  75. case OpCode.setData:
  76. case OpCode.sync:
  77. case OpCode.getACL:
  78. case OpCode.setACL:
  79. case OpCode.getChildren:
  80. case OpCode.ping:
  81. case OpCode.closeSession:
  82. return true;
  83. default:
  84. return false;
  85. }
  86. }
  87. static boolean isQuorum(int type) {
  88. switch (type) {
  89. case OpCode.exists:
  90. case OpCode.getACL:
  91. case OpCode.getChildren:
  92. case OpCode.getData:
  93. return false;
  94. case OpCode.error:
  95. case OpCode.closeSession:
  96. case OpCode.create:
  97. case OpCode.createSession:
  98. case OpCode.delete:
  99. case OpCode.setACL:
  100. case OpCode.setData:
  101. return true;
  102. default:
  103. return false;
  104. }
  105. }
  106. static String op2String(int op) {
  107. switch (op) {
  108. case OpCode.notification:
  109. return "notification";
  110. case OpCode.create:
  111. return "create";
  112. case OpCode.delete:
  113. return "delete";
  114. case OpCode.exists:
  115. return "exists";
  116. case OpCode.getData:
  117. return "getDate";
  118. case OpCode.setData:
  119. return "setData";
  120. case OpCode.sync:
  121. return "sync:";
  122. case OpCode.getACL:
  123. return "getACL";
  124. case OpCode.setACL:
  125. return "setACL";
  126. case OpCode.getChildren:
  127. return "getChildren";
  128. case OpCode.ping:
  129. return "ping";
  130. case OpCode.createSession:
  131. return "createSession";
  132. case OpCode.closeSession:
  133. return "closeSession";
  134. case OpCode.error:
  135. return "error";
  136. default:
  137. return "unknown " + op;
  138. }
  139. }
  140. public String toString() {
  141. StringBuffer sb = new StringBuffer();
  142. sb.append(Long.toHexString(sessionId)).append(" ");
  143. sb.append(Long.toHexString(cxid)).append(" ");
  144. sb.append(Long.toHexString((hdr == null ? -2 : hdr.getZxid()))).append(
  145. " ");
  146. sb
  147. .append(
  148. " txn type = "
  149. + (hdr == null ? "unknown" : "" + hdr.getType()))
  150. .append(" ");
  151. sb.append(op2String(type)).append(" ");
  152. String path = "n/a";
  153. if (type != OpCode.createSession) {
  154. try {
  155. request.clear();
  156. int pathLen = request.getInt();
  157. byte b[] = new byte[pathLen];
  158. request.get(b);
  159. path = new String(b);
  160. request.clear();
  161. } catch (Exception e) {
  162. }
  163. }
  164. sb.append(path).append(" ");
  165. return sb.toString();
  166. }
  167. }