PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/org/apache/cassandra/streaming/StreamingTransferTest.java

https://github.com/yuvrajm/cassandra
Java | 321 lines | 243 code | 37 blank | 41 comment | 16 complexity | dce8d6d61afc11c969862b7d79f826af MD5 | raw file
  1. package org.apache.cassandra.streaming;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import static junit.framework.Assert.assertEquals;
  21. import org.apache.cassandra.Util;
  22. import static org.apache.cassandra.Util.column;
  23. import static org.apache.cassandra.Util.addMutation;
  24. import java.net.InetAddress;
  25. import java.util.*;
  26. import org.apache.cassandra.CleanupHelper;
  27. import org.apache.cassandra.Util;
  28. import org.apache.cassandra.db.*;
  29. import org.apache.cassandra.db.columniterator.IdentityQueryFilter;
  30. import org.apache.cassandra.db.context.CounterContext;
  31. import org.apache.cassandra.db.filter.IFilter;
  32. import org.apache.cassandra.db.filter.QueryFilter;
  33. import org.apache.cassandra.db.filter.QueryPath;
  34. import org.apache.cassandra.dht.IPartitioner;
  35. import org.apache.cassandra.dht.Range;
  36. import org.apache.cassandra.io.sstable.SSTableUtils;
  37. import org.apache.cassandra.io.sstable.SSTableReader;
  38. import org.apache.cassandra.service.StorageService;
  39. import org.apache.cassandra.thrift.IndexClause;
  40. import org.apache.cassandra.thrift.IndexExpression;
  41. import org.apache.cassandra.thrift.IndexOperator;
  42. import org.apache.cassandra.utils.FBUtilities;
  43. import org.apache.cassandra.utils.NodeId;
  44. import org.junit.BeforeClass;
  45. import org.junit.Test;
  46. import org.apache.cassandra.utils.ByteBufferUtil;
  47. public class StreamingTransferTest extends CleanupHelper
  48. {
  49. public static final InetAddress LOCAL = FBUtilities.getBroadcastAddress();
  50. @BeforeClass
  51. public static void setup() throws Exception
  52. {
  53. StorageService.instance.initServer();
  54. }
  55. /**
  56. * Create and transfer a single sstable, and return the keys that should have been transferred.
  57. * The Mutator must create the given column, but it may also create any other columns it pleases.
  58. */
  59. private List<String> createAndTransfer(Table table, ColumnFamilyStore cfs, Mutator mutator) throws Exception
  60. {
  61. // write a temporary SSTable, and unregister it
  62. long timestamp = 1234;
  63. for (int i = 1; i <= 3; i++)
  64. mutator.mutate("key" + i, "col" + i, timestamp);
  65. cfs.forceBlockingFlush();
  66. Util.compactAll(cfs).get();
  67. assertEquals(1, cfs.getSSTables().size());
  68. SSTableReader sstable = cfs.getSSTables().iterator().next();
  69. // We acquire a reference now, because removeAllSSTables will mark the sstable compacted, and we have work to do with it
  70. sstable.acquireReference();
  71. cfs.removeAllSSTables();
  72. // transfer the first and last key
  73. int[] offs = new int[]{1, 3};
  74. IPartitioner p = StorageService.getPartitioner();
  75. List<Range> ranges = new ArrayList<Range>();
  76. ranges.add(new Range(p.getMinimumToken(), p.getToken(ByteBufferUtil.bytes("key1"))));
  77. ranges.add(new Range(p.getToken(ByteBufferUtil.bytes("key2")), p.getMinimumToken()));
  78. StreamOutSession session = StreamOutSession.create(table.name, LOCAL, null);
  79. StreamOut.transferSSTables(session, Arrays.asList(sstable), ranges, OperationType.BOOTSTRAP);
  80. session.await();
  81. // confirm that a single SSTable was transferred and registered
  82. assertEquals(1, cfs.getSSTables().size());
  83. // and that the index and filter were properly recovered
  84. List<Row> rows = Util.getRangeSlice(cfs);
  85. assertEquals(offs.length, rows.size());
  86. for (int i = 0; i < offs.length; i++)
  87. {
  88. String key = "key" + offs[i];
  89. String col = "col" + offs[i];
  90. assert null != cfs.getColumnFamily(QueryFilter.getIdentityFilter(Util.dk(key),
  91. new QueryPath(cfs.columnFamily)));
  92. assert rows.get(i).key.key.equals(ByteBufferUtil.bytes(key));
  93. assert rows.get(i).cf.getColumn(ByteBufferUtil.bytes(col)) != null;
  94. }
  95. // and that the max timestamp for the file was rediscovered
  96. assertEquals(timestamp, cfs.getSSTables().iterator().next().getMaxTimestamp());
  97. List<String> keys = new ArrayList<String>();
  98. for (int off : offs)
  99. keys.add("key" + off);
  100. return keys;
  101. }
  102. @Test
  103. public void testTransferTable() throws Exception
  104. {
  105. final Table table = Table.open("Keyspace1");
  106. final ColumnFamilyStore cfs = table.getColumnFamilyStore("Indexed1");
  107. List<String> keys = createAndTransfer(table, cfs, new Mutator()
  108. {
  109. public void mutate(String key, String col, long timestamp) throws Exception
  110. {
  111. long val = key.hashCode();
  112. RowMutation rm = new RowMutation("Keyspace1", ByteBufferUtil.bytes(key));
  113. ColumnFamily cf = ColumnFamily.create(table.name, cfs.columnFamily);
  114. cf.addColumn(column(col, "v", timestamp));
  115. cf.addColumn(new Column(ByteBufferUtil.bytes("birthdate"), ByteBufferUtil.bytes(val), timestamp));
  116. rm.add(cf);
  117. rm.apply();
  118. }
  119. });
  120. // confirm that the secondary index was recovered
  121. for (String key : keys)
  122. {
  123. long val = key.hashCode();
  124. IPartitioner p = StorageService.getPartitioner();
  125. IndexExpression expr = new IndexExpression(ByteBufferUtil.bytes("birthdate"),
  126. IndexOperator.EQ,
  127. ByteBufferUtil.bytes(val));
  128. IndexClause clause = new IndexClause(Arrays.asList(expr), ByteBufferUtil.EMPTY_BYTE_BUFFER, 100);
  129. IFilter filter = new IdentityQueryFilter();
  130. Range range = new Range(p.getMinimumToken(), p.getMinimumToken());
  131. List<Row> rows = cfs.search(clause, range, filter);
  132. assertEquals(1, rows.size());
  133. assert rows.get(0).key.key.equals(ByteBufferUtil.bytes(key));
  134. }
  135. }
  136. @Test
  137. public void testTransferTableSuper() throws Exception
  138. {
  139. final Table table = Table.open("Keyspace1");
  140. final ColumnFamilyStore cfs = table.getColumnFamilyStore("Super1");
  141. createAndTransfer(table, cfs, new Mutator()
  142. {
  143. public void mutate(String key, String col, long timestamp) throws Exception
  144. {
  145. RowMutation rm = new RowMutation(table.name, ByteBufferUtil.bytes(key));
  146. addMutation(rm, cfs.columnFamily, col, 1, "val1", timestamp);
  147. rm.apply();
  148. }
  149. });
  150. }
  151. @Test
  152. public void testTransferTableCounter() throws Exception
  153. {
  154. final Table table = Table.open("Keyspace1");
  155. final ColumnFamilyStore cfs = table.getColumnFamilyStore("Counter1");
  156. final CounterContext cc = new CounterContext();
  157. final Map<String, ColumnFamily> cleanedEntries = new HashMap<String, ColumnFamily>();
  158. List<String> keys = createAndTransfer(table, cfs, new Mutator()
  159. {
  160. /** Creates a new SSTable per key: all will be merged before streaming. */
  161. public void mutate(String key, String col, long timestamp) throws Exception
  162. {
  163. Map<String, ColumnFamily> entries = new HashMap<String, ColumnFamily>();
  164. ColumnFamily cf = ColumnFamily.create(cfs.metadata);
  165. ColumnFamily cfCleaned = ColumnFamily.create(cfs.metadata);
  166. CounterContext.ContextState state = CounterContext.ContextState.allocate(4, 1);
  167. state.writeElement(NodeId.fromInt(2), 9L, 3L, true);
  168. state.writeElement(NodeId.fromInt(4), 4L, 2L);
  169. state.writeElement(NodeId.fromInt(6), 3L, 3L);
  170. state.writeElement(NodeId.fromInt(8), 2L, 4L);
  171. cf.addColumn(new CounterColumn(ByteBufferUtil.bytes(col),
  172. state.context,
  173. timestamp));
  174. cfCleaned.addColumn(new CounterColumn(ByteBufferUtil.bytes(col),
  175. cc.clearAllDelta(state.context),
  176. timestamp));
  177. entries.put(key, cf);
  178. cleanedEntries.put(key, cfCleaned);
  179. cfs.addSSTable(SSTableUtils.prepare()
  180. .ks(table.name)
  181. .cf(cfs.columnFamily)
  182. .generation(0)
  183. .write(entries));
  184. }
  185. });
  186. // filter pre-cleaned entries locally, and ensure that the end result is equal
  187. cleanedEntries.keySet().retainAll(keys);
  188. SSTableReader cleaned = SSTableUtils.prepare()
  189. .ks(table.name)
  190. .cf(cfs.columnFamily)
  191. .generation(0)
  192. .write(cleanedEntries);
  193. SSTableReader streamed = cfs.getSSTables().iterator().next();
  194. SSTableUtils.assertContentEquals(cleaned, streamed);
  195. }
  196. @Test
  197. public void testTransferTableMultiple() throws Exception
  198. {
  199. // write temporary SSTables, but don't register them
  200. Set<String> content = new HashSet<String>();
  201. content.add("test");
  202. content.add("test2");
  203. content.add("test3");
  204. SSTableReader sstable = SSTableUtils.prepare().write(content);
  205. String tablename = sstable.getTableName();
  206. String cfname = sstable.getColumnFamilyName();
  207. content = new HashSet<String>();
  208. content.add("transfer1");
  209. content.add("transfer2");
  210. content.add("transfer3");
  211. SSTableReader sstable2 = SSTableUtils.prepare().write(content);
  212. // transfer the first and last key
  213. IPartitioner p = StorageService.getPartitioner();
  214. List<Range> ranges = new ArrayList<Range>();
  215. ranges.add(new Range(p.getMinimumToken(), p.getToken(ByteBufferUtil.bytes("test"))));
  216. ranges.add(new Range(p.getToken(ByteBufferUtil.bytes("transfer2")), p.getMinimumToken()));
  217. // Acquiring references, transferSSTables needs it
  218. sstable.acquireReference();
  219. sstable2.acquireReference();
  220. StreamOutSession session = StreamOutSession.create(tablename, LOCAL, null);
  221. StreamOut.transferSSTables(session, Arrays.asList(sstable, sstable2), ranges, OperationType.BOOTSTRAP);
  222. session.await();
  223. // confirm that the sstables were transferred and registered and that 2 keys arrived
  224. ColumnFamilyStore cfstore = Table.open(tablename).getColumnFamilyStore(cfname);
  225. List<Row> rows = Util.getRangeSlice(cfstore);
  226. assertEquals(2, rows.size());
  227. assert rows.get(0).key.key.equals(ByteBufferUtil.bytes("test"));
  228. assert rows.get(1).key.key.equals(ByteBufferUtil.bytes("transfer3"));
  229. assert rows.get(0).cf.getColumnCount() == 1;
  230. assert rows.get(1).cf.getColumnCount() == 1;
  231. // these keys fall outside of the ranges and should not be transferred
  232. assert null == cfstore.getColumnFamily(QueryFilter.getIdentityFilter(Util.dk("transfer1"), new QueryPath("Standard1")));
  233. assert null == cfstore.getColumnFamily(QueryFilter.getIdentityFilter(Util.dk("transfer2"), new QueryPath("Standard1")));
  234. assert null == cfstore.getColumnFamily(QueryFilter.getIdentityFilter(Util.dk("test2"), new QueryPath("Standard1")));
  235. assert null == cfstore.getColumnFamily(QueryFilter.getIdentityFilter(Util.dk("test3"), new QueryPath("Standard1")));
  236. }
  237. @Test
  238. public void testTransferOfMultipleColumnFamilies() throws Exception
  239. {
  240. String keyspace = "KeyCacheSpace";
  241. IPartitioner p = StorageService.getPartitioner();
  242. String[] columnFamilies = new String[] { "Standard1", "Standard2", "Standard3" };
  243. List<SSTableReader> ssTableReaders = new ArrayList<SSTableReader>();
  244. NavigableMap<DecoratedKey,String> keys = new TreeMap<DecoratedKey,String>();
  245. for (String cf : columnFamilies)
  246. {
  247. Set<String> content = new HashSet<String>();
  248. content.add("data-" + cf + "-1");
  249. content.add("data-" + cf + "-2");
  250. content.add("data-" + cf + "-3");
  251. SSTableUtils.Context context = SSTableUtils.prepare().ks(keyspace).cf(cf);
  252. ssTableReaders.add(context.write(content));
  253. // collect dks for each string key
  254. for (String str : content)
  255. keys.put(Util.dk(str), cf);
  256. }
  257. // transfer the first and last keys
  258. Map.Entry<DecoratedKey,String> first = keys.firstEntry();
  259. Map.Entry<DecoratedKey,String> last = keys.lastEntry();
  260. Map.Entry<DecoratedKey,String> secondtolast = keys.lowerEntry(last.getKey());
  261. List<Range> ranges = new ArrayList<Range>();
  262. ranges.add(new Range(p.getMinimumToken(), first.getKey().token));
  263. // the left hand side of the range is exclusive, so we transfer from the second-to-last token
  264. ranges.add(new Range(secondtolast.getKey().token, p.getMinimumToken()));
  265. // Acquiring references, transferSSTables needs it
  266. if (!SSTableReader.acquireReferences(ssTableReaders))
  267. throw new AssertionError();
  268. StreamOutSession session = StreamOutSession.create(keyspace, LOCAL, null);
  269. StreamOut.transferSSTables(session, ssTableReaders, ranges, OperationType.BOOTSTRAP);
  270. session.await();
  271. // check that only two keys were transferred
  272. for (Map.Entry<DecoratedKey,String> entry : Arrays.asList(first, last))
  273. {
  274. ColumnFamilyStore store = Table.open(keyspace).getColumnFamilyStore(entry.getValue());
  275. List<Row> rows = Util.getRangeSlice(store);
  276. assertEquals(rows.toString(), 1, rows.size());
  277. assertEquals(entry.getKey(), rows.get(0).key);
  278. }
  279. }
  280. public interface Mutator
  281. {
  282. public void mutate(String key, String col, long timestamp) throws Exception;
  283. }
  284. }