PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/test/unit/org/apache/cassandra/db/SystemKeyspaceMigrator41Test.java

https://github.com/thelastpickle/cassandra
Java | 267 lines | 220 code | 27 blank | 20 comment | 8 complexity | b752c575f57cf73058ee8e729c512b01 MD5 | raw file
  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.cassandra.db;
  19. import java.net.InetAddress;
  20. import java.nio.ByteBuffer;
  21. import java.util.UUID;
  22. import com.google.common.collect.ImmutableMap;
  23. import com.google.common.collect.ImmutableSet;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.junit.Test;
  26. import org.apache.cassandra.config.DatabaseDescriptor;
  27. import org.apache.cassandra.cql3.CQLTester;
  28. import org.apache.cassandra.cql3.UntypedResultSet;
  29. import org.apache.cassandra.db.marshal.BytesType;
  30. import org.apache.cassandra.db.marshal.Int32Type;
  31. import org.apache.cassandra.db.marshal.TimeUUIDType;
  32. import org.apache.cassandra.db.marshal.UTF8Type;
  33. import org.apache.cassandra.dht.Range;
  34. import org.apache.cassandra.dht.Token;
  35. import org.apache.cassandra.io.sstable.SequenceBasedSSTableId;
  36. import org.apache.cassandra.schema.SchemaConstants;
  37. import org.apache.cassandra.utils.CassandraVersion;
  38. import org.apache.cassandra.utils.FBUtilities;
  39. import org.apache.cassandra.utils.TimeUUID;
  40. import static org.apache.cassandra.utils.TimeUUID.Generator.nextTimeUUID;
  41. import static org.junit.Assert.assertEquals;
  42. public class SystemKeyspaceMigrator41Test extends CQLTester
  43. {
  44. @Test
  45. public void testMigratePeers() throws Throwable
  46. {
  47. String legacyTab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_PEERS);
  48. String tab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.PEERS_V2);
  49. String insert = String.format("INSERT INTO %s ("
  50. + "peer, "
  51. + "data_center, "
  52. + "host_id, "
  53. + "preferred_ip, "
  54. + "rack, "
  55. + "release_version, "
  56. + "rpc_address, "
  57. + "schema_version, "
  58. + "tokens) "
  59. + " values ( ?, ?, ? , ? , ?, ?, ?, ?, ?)",
  60. legacyTab);
  61. UUID hostId = UUID.randomUUID();
  62. UUID schemaVersion = UUID.randomUUID();
  63. execute(insert,
  64. InetAddress.getByName("127.0.0.1"),
  65. "dcFoo",
  66. hostId,
  67. InetAddress.getByName("127.0.0.2"),
  68. "rackFoo", "4.0",
  69. InetAddress.getByName("127.0.0.3"),
  70. schemaVersion,
  71. ImmutableSet.of("foobar"));
  72. SystemKeyspaceMigrator41.migratePeers();
  73. int rowCount = 0;
  74. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  75. {
  76. rowCount++;
  77. assertEquals(InetAddress.getByName("127.0.0.1"), row.getInetAddress("peer"));
  78. assertEquals(DatabaseDescriptor.getStoragePort(), row.getInt("peer_port"));
  79. assertEquals("dcFoo", row.getString("data_center"));
  80. assertEquals(hostId, row.getUUID("host_id"));
  81. assertEquals(InetAddress.getByName("127.0.0.2"), row.getInetAddress("preferred_ip"));
  82. assertEquals(DatabaseDescriptor.getStoragePort(), row.getInt("preferred_port"));
  83. assertEquals("rackFoo", row.getString("rack"));
  84. assertEquals("4.0", row.getString("release_version"));
  85. assertEquals(InetAddress.getByName("127.0.0.3"), row.getInetAddress("native_address"));
  86. assertEquals(DatabaseDescriptor.getNativeTransportPort(), row.getInt("native_port"));
  87. assertEquals(schemaVersion, row.getUUID("schema_version"));
  88. assertEquals(ImmutableSet.of("foobar"), row.getSet("tokens", UTF8Type.instance));
  89. }
  90. assertEquals(1, rowCount);
  91. //Test nulls/missing don't prevent the row from propagating
  92. execute(String.format("TRUNCATE %s", legacyTab));
  93. execute(String.format("TRUNCATE %s", tab));
  94. execute(String.format("INSERT INTO %s (peer) VALUES (?)", legacyTab),
  95. InetAddress.getByName("127.0.0.1"));
  96. SystemKeyspaceMigrator41.migratePeers();
  97. rowCount = 0;
  98. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  99. {
  100. rowCount++;
  101. }
  102. assertEquals(1, rowCount);
  103. }
  104. @Test
  105. public void testMigratePeerEvents() throws Throwable
  106. {
  107. String legacyTab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_PEER_EVENTS);
  108. String tab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.PEER_EVENTS_V2);
  109. String insert = String.format("INSERT INTO %s ("
  110. + "peer, "
  111. + "hints_dropped) "
  112. + " values ( ?, ? )",
  113. legacyTab);
  114. TimeUUID uuid = nextTimeUUID();
  115. execute(insert,
  116. InetAddress.getByName("127.0.0.1"),
  117. ImmutableMap.of(uuid, 42));
  118. SystemKeyspaceMigrator41.migratePeerEvents();
  119. int rowCount = 0;
  120. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  121. {
  122. rowCount++;
  123. assertEquals(InetAddress.getByName("127.0.0.1"), row.getInetAddress("peer"));
  124. assertEquals(DatabaseDescriptor.getStoragePort(), row.getInt("peer_port"));
  125. assertEquals(ImmutableMap.of(uuid, 42), row.getMap("hints_dropped", TimeUUIDType.instance, Int32Type.instance));
  126. }
  127. assertEquals(1, rowCount);
  128. //Test nulls/missing don't prevent the row from propagating
  129. execute(String.format("TRUNCATE %s", legacyTab));
  130. execute(String.format("TRUNCATE %s", tab));
  131. execute(String.format("INSERT INTO %s (peer) VALUES (?)", legacyTab),
  132. InetAddress.getByName("127.0.0.1"));
  133. SystemKeyspaceMigrator41.migratePeerEvents();
  134. rowCount = 0;
  135. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  136. {
  137. rowCount++;
  138. }
  139. assertEquals(1, rowCount);
  140. }
  141. @Test
  142. public void testMigrateTransferredRanges() throws Throwable
  143. {
  144. String legacyTab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_TRANSFERRED_RANGES);
  145. String tab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.TRANSFERRED_RANGES_V2);
  146. String insert = String.format("INSERT INTO %s ("
  147. + "operation, "
  148. + "peer, "
  149. + "keyspace_name, "
  150. + "ranges) "
  151. + " values ( ?, ?, ?, ? )",
  152. legacyTab);
  153. execute(insert,
  154. "foo",
  155. InetAddress.getByName("127.0.0.1"),
  156. "bar",
  157. ImmutableSet.of(ByteBuffer.wrap(new byte[] { 42 })));
  158. SystemKeyspaceMigrator41.migrateTransferredRanges();
  159. int rowCount = 0;
  160. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  161. {
  162. rowCount++;
  163. assertEquals("foo", row.getString("operation"));
  164. assertEquals(InetAddress.getByName("127.0.0.1"), row.getInetAddress("peer"));
  165. assertEquals(DatabaseDescriptor.getStoragePort(), row.getInt("peer_port"));
  166. assertEquals("bar", row.getString("keyspace_name"));
  167. assertEquals(ImmutableSet.of(ByteBuffer.wrap(new byte[] { 42 })), row.getSet("ranges", BytesType.instance));
  168. }
  169. assertEquals(1, rowCount);
  170. //Test nulls/missing don't prevent the row from propagating
  171. execute(String.format("TRUNCATE %s", legacyTab));
  172. execute(String.format("TRUNCATE %s", tab));
  173. execute(String.format("INSERT INTO %s (operation, peer, keyspace_name) VALUES (?, ?, ?)", legacyTab),
  174. "foo",
  175. InetAddress.getByName("127.0.0.1"),
  176. "bar");
  177. SystemKeyspaceMigrator41.migrateTransferredRanges();
  178. rowCount = 0;
  179. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  180. {
  181. rowCount++;
  182. }
  183. assertEquals(1, rowCount);
  184. }
  185. @Test
  186. public void testMigrateAvailableRanges() throws Throwable
  187. {
  188. String legacyTab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_AVAILABLE_RANGES);
  189. String tab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.AVAILABLE_RANGES_V2);
  190. Range<Token> testRange = new Range<>(DatabaseDescriptor.getPartitioner().getRandomToken(), DatabaseDescriptor.getPartitioner().getRandomToken());
  191. String insert = String.format("INSERT INTO %s ("
  192. + "keyspace_name, "
  193. + "ranges) "
  194. + " values ( ?, ? )",
  195. legacyTab);
  196. execute(insert,
  197. "foo",
  198. ImmutableSet.of(SystemKeyspace.rangeToBytes(testRange)));
  199. SystemKeyspaceMigrator41.migrateAvailableRanges();
  200. int rowCount = 0;
  201. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  202. {
  203. rowCount++;
  204. assertEquals("foo", row.getString("keyspace_name"));
  205. assertEquals(ImmutableSet.of(testRange), SystemKeyspace.rawRangesToRangeSet(row.getSet("full_ranges", BytesType.instance), DatabaseDescriptor.getPartitioner()));
  206. }
  207. assertEquals(1, rowCount);
  208. }
  209. @Test
  210. public void testMigrateSSTableActivity() throws Throwable
  211. {
  212. FBUtilities.setPreviousReleaseVersionString(CassandraVersion.NULL_VERSION.toString());
  213. String legacyTab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.LEGACY_SSTABLE_ACTIVITY);
  214. String tab = String.format("%s.%s", SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.SSTABLE_ACTIVITY_V2);
  215. String insert = String.format("INSERT INTO %s (%s) VALUES (%s)",
  216. legacyTab,
  217. StringUtils.join(new String[] {"keyspace_name",
  218. "columnfamily_name",
  219. "generation",
  220. "rate_120m",
  221. "rate_15m"}, ", "),
  222. StringUtils.repeat("?", ", ", 5));
  223. execute(insert, "ks", "tab", 5, 123.234d, 345.456d);
  224. ColumnFamilyStore cf = getColumnFamilyStore(SchemaConstants.SYSTEM_KEYSPACE_NAME, SystemKeyspace.SSTABLE_ACTIVITY_V2);
  225. cf.truncateBlocking();
  226. cf.clearUnsafe();
  227. SystemKeyspaceMigrator41.migrateSSTableActivity();
  228. int rowCount = 0;
  229. for (UntypedResultSet.Row row : execute(String.format("SELECT * FROM %s", tab)))
  230. {
  231. rowCount++;
  232. assertEquals("ks", row.getString("keyspace_name"));
  233. assertEquals("tab", row.getString("table_name"));
  234. assertEquals(new SequenceBasedSSTableId(5).asBytes(), row.getBytes("id"));
  235. assertEquals(123.234d, row.getDouble("rate_120m"), 0.001d);
  236. assertEquals(345.456d, row.getDouble("rate_15m"), 0.001d);
  237. }
  238. assertEquals(1, rowCount);
  239. }
  240. }