/src/java/org/apache/cassandra/streaming/StreamRequestMessage.java

https://github.com/yuvrajm/cassandra · Java · 206 lines · 158 code · 19 blank · 29 comment · 15 complexity · a6568d8c9e098e0b6e2e6ae718a3c83e MD5 · raw file

  1. package org.apache.cassandra.streaming;
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. import java.io.*;
  23. import java.net.InetAddress;
  24. import java.util.*;
  25. import com.google.common.collect.Iterables;
  26. import org.apache.cassandra.config.DatabaseDescriptor;
  27. import org.apache.cassandra.db.ColumnFamilyStore;
  28. import org.apache.cassandra.db.Table;
  29. import org.apache.cassandra.dht.AbstractBounds;
  30. import org.apache.cassandra.dht.Range;
  31. import org.apache.cassandra.io.ICompactSerializer;
  32. import org.apache.cassandra.io.util.FastByteArrayOutputStream;
  33. import org.apache.cassandra.net.CompactEndpointSerializationHelper;
  34. import org.apache.cassandra.net.Message;
  35. import org.apache.cassandra.net.MessageProducer;
  36. import org.apache.cassandra.net.MessagingService;
  37. import org.apache.cassandra.service.StorageService;
  38. import org.apache.cassandra.utils.FBUtilities;
  39. /**
  40. * This class encapsulates the message that needs to be sent to nodes
  41. * that handoff data. The message contains information about ranges
  42. * that need to be transferred and the target node.
  43. *
  44. * If a file is specified, ranges and table will not. vice-versa should hold as well.
  45. */
  46. class StreamRequestMessage implements MessageProducer
  47. {
  48. private static ICompactSerializer<StreamRequestMessage> serializer_;
  49. static
  50. {
  51. serializer_ = new StreamRequestMessageSerializer();
  52. }
  53. protected static ICompactSerializer<StreamRequestMessage> serializer()
  54. {
  55. return serializer_;
  56. }
  57. protected final long sessionId;
  58. protected final InetAddress target;
  59. // if this is specified, ranges and table should not be.
  60. protected final PendingFile file;
  61. // if these are specified, file shoud not be.
  62. protected final Collection<Range> ranges;
  63. protected final String table;
  64. protected final Iterable<ColumnFamilyStore> columnFamilies;
  65. protected final OperationType type;
  66. StreamRequestMessage(InetAddress target, Collection<Range> ranges, String table, Iterable<ColumnFamilyStore> columnFamilies, long sessionId, OperationType type)
  67. {
  68. this.target = target;
  69. this.ranges = ranges;
  70. this.table = table;
  71. this.columnFamilies = columnFamilies;
  72. this.sessionId = sessionId;
  73. this.type = type;
  74. file = null;
  75. }
  76. StreamRequestMessage(InetAddress target, PendingFile file, long sessionId)
  77. {
  78. this.target = target;
  79. this.file = file;
  80. this.sessionId = sessionId;
  81. this.type = file.type;
  82. ranges = null;
  83. table = null;
  84. columnFamilies = null;
  85. }
  86. public Message getMessage(Integer version)
  87. {
  88. FastByteArrayOutputStream bos = new FastByteArrayOutputStream();
  89. DataOutputStream dos = new DataOutputStream(bos);
  90. try
  91. {
  92. StreamRequestMessage.serializer().serialize(this, dos, version);
  93. }
  94. catch (IOException e)
  95. {
  96. throw new IOError(e);
  97. }
  98. return new Message(FBUtilities.getBroadcastAddress(), StorageService.Verb.STREAM_REQUEST, bos.toByteArray(), version);
  99. }
  100. public String toString()
  101. {
  102. StringBuilder sb = new StringBuilder("");
  103. if (file == null)
  104. {
  105. sb.append(table);
  106. sb.append("@");
  107. sb.append(columnFamilies.toString());
  108. sb.append("@");
  109. sb.append(target);
  110. sb.append("------->");
  111. for ( Range range : ranges )
  112. {
  113. sb.append(range);
  114. sb.append(" ");
  115. }
  116. sb.append(type);
  117. }
  118. else
  119. {
  120. sb.append(file.toString());
  121. }
  122. return sb.toString();
  123. }
  124. private static class StreamRequestMessageSerializer implements ICompactSerializer<StreamRequestMessage>
  125. {
  126. public void serialize(StreamRequestMessage srm, DataOutputStream dos, int version) throws IOException
  127. {
  128. dos.writeLong(srm.sessionId);
  129. CompactEndpointSerializationHelper.serialize(srm.target, dos);
  130. if (srm.file != null)
  131. {
  132. dos.writeBoolean(true);
  133. PendingFile.serializer().serialize(srm.file, dos, version);
  134. }
  135. else
  136. {
  137. dos.writeBoolean(false);
  138. dos.writeUTF(srm.table);
  139. dos.writeInt(srm.ranges.size());
  140. for (Range range : srm.ranges)
  141. {
  142. AbstractBounds.serializer().serialize(range, dos);
  143. }
  144. if (version > MessagingService.VERSION_07)
  145. dos.writeUTF(srm.type.name());
  146. if (version > MessagingService.VERSION_080)
  147. {
  148. dos.writeInt(Iterables.size(srm.columnFamilies));
  149. for (ColumnFamilyStore cfs : srm.columnFamilies)
  150. dos.writeInt(cfs.metadata.cfId);
  151. }
  152. }
  153. }
  154. public StreamRequestMessage deserialize(DataInputStream dis, int version) throws IOException
  155. {
  156. long sessionId = dis.readLong();
  157. InetAddress target = CompactEndpointSerializationHelper.deserialize(dis);
  158. boolean singleFile = dis.readBoolean();
  159. if (singleFile)
  160. {
  161. PendingFile file = PendingFile.serializer().deserialize(dis, version);
  162. return new StreamRequestMessage(target, file, sessionId);
  163. }
  164. else
  165. {
  166. String table = dis.readUTF();
  167. int size = dis.readInt();
  168. List<Range> ranges = (size == 0) ? null : new ArrayList<Range>();
  169. for( int i = 0; i < size; ++i )
  170. {
  171. ranges.add((Range) AbstractBounds.serializer().deserialize(dis));
  172. }
  173. OperationType type = OperationType.RESTORE_REPLICA_COUNT;
  174. if (version > MessagingService.VERSION_07)
  175. type = OperationType.valueOf(dis.readUTF());
  176. List<ColumnFamilyStore> stores = new ArrayList<ColumnFamilyStore>();
  177. if (version > MessagingService.VERSION_080)
  178. {
  179. int cfsSize = dis.readInt();
  180. for (int i = 0; i < cfsSize; ++i)
  181. stores.add(Table.open(table).getColumnFamilyStore(dis.readInt()));
  182. }
  183. return new StreamRequestMessage(target, ranges, table, stores, sessionId, type);
  184. }
  185. }
  186. }
  187. }