PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/gemfire-core/src/main/java/com/gemstone/gemfire/internal/cache/tier/sockets/command/RegisterInterestList66.java

https://gitlab.com/kidaa/incubator-geode
Java | 258 lines | 171 code | 21 blank | 66 comment | 26 complexity | f78710f7de1179fa8850a2b8790d7682 MD5 | raw file
  1. /*=========================================================================
  2. * Copyright (c) 2010-2014 Pivotal Software, Inc. All Rights Reserved.
  3. * This product is protected by U.S. and international copyright
  4. * and intellectual property laws. Pivotal products are covered by
  5. * one or more patents listed at http://www.pivotal.io/patents.
  6. *=========================================================================
  7. */
  8. /**
  9. *
  10. */
  11. package com.gemstone.gemfire.internal.cache.tier.sockets.command;
  12. import com.gemstone.gemfire.internal.Version;
  13. import com.gemstone.gemfire.internal.cache.LocalRegion;
  14. import com.gemstone.gemfire.internal.cache.tier.CachedRegionHelper;
  15. import com.gemstone.gemfire.internal.cache.tier.Command;
  16. import com.gemstone.gemfire.internal.cache.tier.InterestType;
  17. import com.gemstone.gemfire.internal.cache.tier.MessageType;
  18. import com.gemstone.gemfire.internal.cache.tier.sockets.*;
  19. import com.gemstone.gemfire.internal.i18n.LocalizedStrings;
  20. import com.gemstone.gemfire.internal.logging.log4j.LocalizedMessage;
  21. import com.gemstone.gemfire.internal.security.AuthorizeRequest;
  22. import com.gemstone.gemfire.cache.DynamicRegionFactory;
  23. import com.gemstone.gemfire.cache.InterestResultPolicy;
  24. import com.gemstone.gemfire.cache.operations.RegisterInterestOperationContext;
  25. import com.gemstone.org.jgroups.util.StringId;
  26. import java.io.IOException;
  27. import java.util.List;
  28. /**
  29. *
  30. * All keys of the register interest list are being sent as a single part since
  31. * 6.6. There is no need to send no keys as a separate part.In earlier versions
  32. * {@link RegisterInterestList61} number of keys & each individual key was sent
  33. * as a separate part.
  34. *
  35. * @author sbhokare
  36. *
  37. * @since 6.6
  38. */
  39. public class RegisterInterestList66 extends BaseCommand {
  40. private final static RegisterInterestList66 singleton = new RegisterInterestList66();
  41. public static Command getCommand() {
  42. return singleton;
  43. }
  44. private RegisterInterestList66() {
  45. }
  46. @Override
  47. public void cmdExecute(Message msg, ServerConnection servConn, long start)
  48. throws IOException, InterruptedException {
  49. Part regionNamePart = null, keyPart = null;// numberOfKeysPart = null;
  50. String regionName = null;
  51. Object key = null;
  52. InterestResultPolicy policy;
  53. List keys = null;
  54. CachedRegionHelper crHelper = servConn.getCachedRegionHelper();
  55. int numberOfKeys = 0, partNumber = 0;
  56. servConn.setAsTrue(REQUIRES_RESPONSE);
  57. servConn.setAsTrue(REQUIRES_CHUNKED_RESPONSE);
  58. ChunkedMessage chunkedResponseMsg = servConn.getRegisterInterestResponseMessage();
  59. // bserverStats.incLong(readDestroyRequestTimeId,
  60. // DistributionStats.getStatTime() - start);
  61. // bserverStats.incInt(destroyRequestsId, 1);
  62. // start = DistributionStats.getStatTime();
  63. // Retrieve the data from the message parts
  64. regionNamePart = msg.getPart(0);
  65. regionName = regionNamePart.getString();
  66. // Retrieve the InterestResultPolicy
  67. try {
  68. policy = (InterestResultPolicy)msg.getPart(1).getObject();
  69. }
  70. catch (Exception e) {
  71. writeChunkedException(msg, e, false, servConn);
  72. servConn.setAsTrue(RESPONDED);
  73. return;
  74. }
  75. boolean isDurable = false ;
  76. try {
  77. Part durablePart = msg.getPart(2);
  78. byte[] durablePartBytes = (byte[])durablePart.getObject();
  79. isDurable = durablePartBytes[0] == 0x01;
  80. }
  81. catch (Exception e) {
  82. writeChunkedException(msg, e, false, servConn);
  83. servConn.setAsTrue(RESPONDED);
  84. return;
  85. }
  86. // region data policy
  87. byte[] regionDataPolicyPartBytes;
  88. boolean serializeValues = false;
  89. try {
  90. Part regionDataPolicyPart = msg.getPart(msg.getNumberOfParts()-1);
  91. regionDataPolicyPartBytes = (byte[])regionDataPolicyPart.getObject();
  92. if (servConn.getClientVersion().compareTo(Version.GFE_80) >= 0) {
  93. // The second byte here is serializeValues
  94. serializeValues = regionDataPolicyPartBytes[1] == (byte)0x01;
  95. }
  96. }
  97. catch (Exception e) {
  98. writeChunkedException(msg, e, false, servConn);
  99. servConn.setAsTrue(RESPONDED);
  100. return;
  101. }
  102. partNumber = 3;
  103. Part list = msg.getPart(partNumber);
  104. try {
  105. keys = (List)list.getObject();
  106. numberOfKeys = keys.size();
  107. }
  108. catch (Exception e) {
  109. writeChunkedException(msg, e, false, servConn);
  110. servConn.setAsTrue(RESPONDED);
  111. return;
  112. }
  113. boolean sendUpdatesAsInvalidates = false;
  114. try {
  115. Part notifyPart = msg.getPart(partNumber + 1);
  116. byte[] notifyPartBytes = (byte[])notifyPart.getObject();
  117. sendUpdatesAsInvalidates = notifyPartBytes[0] == 0x01;
  118. }
  119. catch (Exception e) {
  120. writeChunkedException(msg, e, false, servConn);
  121. servConn.setAsTrue(RESPONDED);
  122. return;
  123. }
  124. if (logger.isDebugEnabled()) {
  125. logger.debug("{}: Received register interest 66 request ({} bytes) from {} for the following {} keys in region {}: {}", servConn.getName(), msg.getPayloadLength(), servConn.getSocketString(), numberOfKeys, regionName, keys);
  126. }
  127. /*
  128. AcceptorImpl acceptor = servConn.getAcceptor();
  129. // Check if the Server is running in NotifyBySubscription=true mode.
  130. if (!acceptor.getCacheClientNotifier().getNotifyBySubscription()) {
  131. // This should have been taken care at the client.
  132. String err = LocalizedStrings.RegisterInterest_INTEREST_REGISTRATION_IS_SUPPORTED_ONLY_FOR_SERVERS_WITH_NOTIFYBYSUBSCRIPTION_SET_TO_TRUE.toLocalizedString();
  133. writeChunkedErrorResponse(msg, MessageType.REGISTER_INTEREST_DATA_ERROR,
  134. err, servConn);
  135. servConn.setAsTrue(RESPONDED); return;
  136. }
  137. */
  138. // Process the register interest request
  139. if (keys.isEmpty() || regionName == null) {
  140. StringId errMessage = null;
  141. if (keys.isEmpty() && regionName == null) {
  142. errMessage = LocalizedStrings.RegisterInterestList_THE_INPUT_LIST_OF_KEYS_IS_EMPTY_AND_THE_INPUT_REGION_NAME_IS_NULL_FOR_THE_REGISTER_INTEREST_REQUEST;
  143. } else if (keys.isEmpty()) {
  144. errMessage = LocalizedStrings.RegisterInterestList_THE_INPUT_LIST_OF_KEYS_FOR_THE_REGISTER_INTEREST_REQUEST_IS_EMPTY;
  145. } else if (regionName == null) {
  146. errMessage = LocalizedStrings.RegisterInterest_THE_INPUT_REGION_NAME_FOR_THE_REGISTER_INTEREST_REQUEST_IS_NULL;
  147. }
  148. String s = errMessage.toLocalizedString();
  149. logger.warn("{}: {}", servConn.getName(), s);
  150. writeChunkedErrorResponse(msg, MessageType.REGISTER_INTEREST_DATA_ERROR,
  151. s, servConn);
  152. servConn.setAsTrue(RESPONDED);
  153. }
  154. else { // key not null
  155. LocalRegion region = (LocalRegion)crHelper.getRegion(regionName);
  156. if (region == null) {
  157. logger.info(LocalizedMessage.create(LocalizedStrings.RegisterInterestList_0_REGION_NAMED_1_WAS_NOT_FOUND_DURING_REGISTER_INTEREST_LIST_REQUEST, new Object[]{servConn.getName(), regionName}));
  158. // writeChunkedErrorResponse(msg,
  159. // MessageType.REGISTER_INTEREST_DATA_ERROR, message);
  160. // responded = true;
  161. } // else { // region not null
  162. try {
  163. AuthorizeRequest authzRequest = servConn.getAuthzRequest();
  164. if (authzRequest != null) {
  165. // TODO SW: This is a workaround for DynamicRegionFactory
  166. // registerInterest calls. Remove this when the semantics of
  167. // DynamicRegionFactory are cleaned up.
  168. if (!DynamicRegionFactory.regionIsDynamicRegionList(regionName)) {
  169. RegisterInterestOperationContext registerContext = authzRequest
  170. .registerInterestListAuthorize(regionName, keys, policy);
  171. keys = (List)registerContext.getKey();
  172. }
  173. }
  174. // Register interest
  175. servConn.getAcceptor().getCacheClientNotifier()
  176. .registerClientInterest(regionName, keys, servConn.getProxyID(),
  177. isDurable, sendUpdatesAsInvalidates,
  178. true, regionDataPolicyPartBytes[0],true);
  179. }
  180. catch (Exception ex) {
  181. // If an interrupted exception is thrown , rethrow it
  182. checkForInterrupt(servConn, ex);
  183. // Otherwise, write an exception message and continue
  184. writeChunkedException(msg, ex, false, servConn);
  185. servConn.setAsTrue(RESPONDED);
  186. return;
  187. }
  188. // Update the statistics and write the reply
  189. // bserverStats.incLong(processDestroyTimeId,
  190. // DistributionStats.getStatTime() - start);
  191. // start = DistributionStats.getStatTime();
  192. boolean isPrimary = servConn.getAcceptor().getCacheClientNotifier()
  193. .getClientProxy(servConn.getProxyID()).isPrimary();
  194. if (!isPrimary) {
  195. chunkedResponseMsg.setMessageType(MessageType.RESPONSE_FROM_SECONDARY);
  196. chunkedResponseMsg.setTransactionId(msg.getTransactionId());
  197. chunkedResponseMsg.sendHeader();
  198. chunkedResponseMsg.setLastChunk(true);
  199. if (logger.isDebugEnabled()) {
  200. logger.debug("{}: Sending register interest response chunk from secondary for region: {} for key: {} chunk=<{}>", servConn.getName(), regionName, key, chunkedResponseMsg);
  201. }
  202. chunkedResponseMsg.sendChunk(servConn);
  203. }
  204. else { // isPrimary
  205. // Send header which describes how many chunks will follow
  206. chunkedResponseMsg.setMessageType(MessageType.RESPONSE_FROM_PRIMARY);
  207. chunkedResponseMsg.setTransactionId(msg.getTransactionId());
  208. chunkedResponseMsg.sendHeader();
  209. // Send chunk response
  210. try {
  211. fillAndSendRegisterInterestResponseChunks(region, keys,
  212. InterestType.KEY, serializeValues, policy, servConn);
  213. servConn.setAsTrue(RESPONDED);
  214. }
  215. catch (Exception e) {
  216. // If an interrupted exception is thrown , rethrow it
  217. checkForInterrupt(servConn, e);
  218. // otherwise send the exception back to client
  219. writeChunkedException(msg, e, false, servConn);
  220. servConn.setAsTrue(RESPONDED);
  221. return;
  222. }
  223. if (logger.isDebugEnabled()) {
  224. // logger.debug(getName() + ": Sent chunk (1 of 1) of register interest
  225. // response (" + chunkedResponseMsg.getBufferLength() + " bytes) for
  226. // region " + regionName + " key " + key);
  227. logger.debug("{}: Sent register interest response for the following {} keys in region {}: {}", servConn.getName(), numberOfKeys, regionName, keys);
  228. }
  229. // bserverStats.incLong(writeDestroyResponseTimeId,
  230. // DistributionStats.getStatTime() - start);
  231. // bserverStats.incInt(destroyResponsesId, 1);
  232. } // isPrimary
  233. // } // region not null
  234. } // key not null
  235. }
  236. }