/test/unit/org/apache/cassandra/service/StorageProxyTest.java

https://github.com/thepaul/cassandra · Java · 243 lines · 161 code · 26 blank · 56 comment · 2 complexity · f0ed1cecbe8eb834ae41ce8cb36ade37 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,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.cassandra.service;
  20. import java.net.InetAddress;
  21. import java.util.List;
  22. import org.junit.BeforeClass;
  23. import org.junit.Test;
  24. import static org.junit.Assert.assertEquals;
  25. import org.apache.cassandra.SchemaLoader;
  26. import static org.apache.cassandra.Util.token;
  27. import static org.apache.cassandra.Util.rp;
  28. import org.apache.cassandra.db.RowPosition;
  29. import org.apache.cassandra.dht.AbstractBounds;
  30. import org.apache.cassandra.dht.Bounds;
  31. import org.apache.cassandra.dht.Range;
  32. import org.apache.cassandra.dht.Token;
  33. import org.apache.cassandra.dht.ExcludingBounds;
  34. import org.apache.cassandra.dht.IncludingExcludingBounds;
  35. import org.apache.cassandra.locator.TokenMetadata;
  36. import org.apache.cassandra.utils.ByteBufferUtil;
  37. public class StorageProxyTest extends SchemaLoader
  38. {
  39. private static Range<RowPosition> range(RowPosition left, RowPosition right)
  40. {
  41. return new Range<RowPosition>(left, right);
  42. }
  43. private static Bounds<RowPosition> bounds(RowPosition left, RowPosition right)
  44. {
  45. return new Bounds<RowPosition>(left, right);
  46. }
  47. private static ExcludingBounds<RowPosition> exBounds(RowPosition left, RowPosition right)
  48. {
  49. return new ExcludingBounds<RowPosition>(left, right);
  50. }
  51. private static IncludingExcludingBounds<RowPosition> incExBounds(RowPosition left, RowPosition right)
  52. {
  53. return new IncludingExcludingBounds<RowPosition>(left, right);
  54. }
  55. private static RowPosition startOf(String key)
  56. {
  57. return StorageService.getPartitioner().getToken(ByteBufferUtil.bytes(key)).minKeyBound();
  58. }
  59. private static RowPosition endOf(String key)
  60. {
  61. return StorageService.getPartitioner().getToken(ByteBufferUtil.bytes(key)).maxKeyBound();
  62. }
  63. private static Range<Token> tokenRange(String left, String right)
  64. {
  65. return new Range<Token>(token(left), token(right));
  66. }
  67. private static Bounds<Token> tokenBounds(String left, String right)
  68. {
  69. return new Bounds<Token>(token(left), token(right));
  70. }
  71. @BeforeClass
  72. public static void beforeClass() throws Throwable
  73. {
  74. TokenMetadata tmd = StorageService.instance.getTokenMetadata();
  75. tmd.updateNormalToken(token("1"), InetAddress.getByName("127.0.0.1"));
  76. tmd.updateNormalToken(token("6"), InetAddress.getByName("127.0.0.6"));
  77. }
  78. // test getRestrictedRanges for token
  79. private void testGRR(AbstractBounds<Token> queryRange, AbstractBounds<Token>... expected)
  80. {
  81. // Testing for tokens
  82. List<AbstractBounds<Token>> restricted = StorageProxy.getRestrictedRanges(queryRange);
  83. assertEquals(restricted.toString(), expected.length, restricted.size());
  84. for (int i = 0; i < expected.length; i++)
  85. assertEquals("Mismatch for index " + i + ": " + restricted, expected[i], restricted.get(i));
  86. }
  87. // test getRestrictedRanges for keys
  88. private void testGRRKeys(AbstractBounds<RowPosition> queryRange, AbstractBounds<RowPosition>... expected)
  89. {
  90. // Testing for keys
  91. List<AbstractBounds<RowPosition>> restrictedKeys = StorageProxy.getRestrictedRanges(queryRange);
  92. assertEquals(restrictedKeys.toString(), expected.length, restrictedKeys.size());
  93. for (int i = 0; i < expected.length; i++)
  94. assertEquals("Mismatch for index " + i + ": " + restrictedKeys, expected[i], restrictedKeys.get(i));
  95. }
  96. @Test
  97. public void testGRR() throws Throwable
  98. {
  99. // no splits
  100. testGRR(tokenRange("2", "5"), tokenRange("2", "5"));
  101. testGRR(tokenBounds("2", "5"), tokenBounds("2", "5"));
  102. // single split
  103. testGRR(tokenRange("2", "7"), tokenRange("2", "6"), tokenRange("6", "7"));
  104. testGRR(tokenBounds("2", "7"), tokenBounds("2", "6"), tokenRange("6", "7"));
  105. // single split starting from min
  106. testGRR(tokenRange("", "2"), tokenRange("", "1"), tokenRange("1", "2"));
  107. testGRR(tokenBounds("", "2"), tokenBounds("", "1"), tokenRange("1", "2"));
  108. // single split ending with max
  109. testGRR(tokenRange("5", ""), tokenRange("5", "6"), tokenRange("6", ""));
  110. testGRR(tokenBounds("5", ""), tokenBounds("5", "6"), tokenRange("6", ""));
  111. // two splits
  112. testGRR(tokenRange("0", "7"), tokenRange("0", "1"), tokenRange("1", "6"), tokenRange("6", "7"));
  113. testGRR(tokenBounds("0", "7"), tokenBounds("0", "1"), tokenRange("1", "6"), tokenRange("6", "7"));
  114. // Keys
  115. // no splits
  116. testGRRKeys(range(rp("2"), rp("5")), range(rp("2"), rp("5")));
  117. testGRRKeys(bounds(rp("2"), rp("5")), bounds(rp("2"), rp("5")));
  118. testGRRKeys(exBounds(rp("2"), rp("5")), exBounds(rp("2"), rp("5")));
  119. // single split testGRRKeys(range("2", "7"), range(rp("2"), endOf("6")), range(endOf("6"), rp("7")));
  120. testGRRKeys(bounds(rp("2"), rp("7")), bounds(rp("2"), endOf("6")), range(endOf("6"), rp("7")));
  121. testGRRKeys(exBounds(rp("2"), rp("7")), range(rp("2"), endOf("6")), exBounds(endOf("6"), rp("7")));
  122. testGRRKeys(incExBounds(rp("2"), rp("7")), bounds(rp("2"), endOf("6")), exBounds(endOf("6"), rp("7")));
  123. // single split starting from min
  124. testGRRKeys(range(rp(""), rp("2")), range(rp(""), endOf("1")), range(endOf("1"), rp("2")));
  125. testGRRKeys(bounds(rp(""), rp("2")), bounds(rp(""), endOf("1")), range(endOf("1"), rp("2")));
  126. testGRRKeys(exBounds(rp(""), rp("2")), range(rp(""), endOf("1")), exBounds(endOf("1"), rp("2")));
  127. testGRRKeys(incExBounds(rp(""), rp("2")), bounds(rp(""), endOf("1")), exBounds(endOf("1"), rp("2")));
  128. // single split ending with max
  129. testGRRKeys(range(rp("5"), rp("")), range(rp("5"), endOf("6")), range(endOf("6"), rp("")));
  130. testGRRKeys(bounds(rp("5"), rp("")), bounds(rp("5"), endOf("6")), range(endOf("6"), rp("")));
  131. testGRRKeys(exBounds(rp("5"), rp("")), range(rp("5"), endOf("6")), exBounds(endOf("6"), rp("")));
  132. testGRRKeys(incExBounds(rp("5"), rp("")), bounds(rp("5"), endOf("6")), exBounds(endOf("6"), rp("")));
  133. // two splits
  134. testGRRKeys(range(rp("0"), rp("7")), range(rp("0"), endOf("1")), range(endOf("1"), endOf("6")), range(endOf("6"), rp("7")));
  135. testGRRKeys(bounds(rp("0"), rp("7")), bounds(rp("0"), endOf("1")), range(endOf("1"), endOf("6")), range(endOf("6"), rp("7")));
  136. testGRRKeys(exBounds(rp("0"), rp("7")), range(rp("0"), endOf("1")), range(endOf("1"), endOf("6")), exBounds(endOf("6"), rp("7")));
  137. testGRRKeys(incExBounds(rp("0"), rp("7")), bounds(rp("0"), endOf("1")), range(endOf("1"), endOf("6")), exBounds(endOf("6"), rp("7")));
  138. }
  139. @Test
  140. public void testGRRExact() throws Throwable
  141. {
  142. // min
  143. testGRR(tokenRange("1", "5"), tokenRange("1", "5"));
  144. testGRR(tokenBounds("1", "5"), tokenBounds("1", "1"), tokenRange("1", "5"));
  145. // max
  146. testGRR(tokenRange("2", "6"), tokenRange("2", "6"));
  147. testGRR(tokenBounds("2", "6"), tokenBounds("2", "6"));
  148. // both
  149. testGRR(tokenRange("1", "6"), tokenRange("1", "6"));
  150. testGRR(tokenBounds("1", "6"), tokenBounds("1", "1"), tokenRange("1", "6"));
  151. // Keys
  152. // min
  153. testGRRKeys(range(endOf("1"), endOf("5")), range(endOf("1"), endOf("5")));
  154. testGRRKeys(range(rp("1"), endOf("5")), range(rp("1"), endOf("1")), range(endOf("1"), endOf("5")));
  155. testGRRKeys(bounds(startOf("1"), endOf("5")), bounds(startOf("1"), endOf("1")), range(endOf("1"), endOf("5")));
  156. testGRRKeys(exBounds(endOf("1"), rp("5")), exBounds(endOf("1"), rp("5")));
  157. testGRRKeys(exBounds(rp("1"), rp("5")), range(rp("1"), endOf("1")), exBounds(endOf("1"), rp("5")));
  158. testGRRKeys(exBounds(startOf("1"), endOf("5")), range(startOf("1"), endOf("1")), exBounds(endOf("1"), endOf("5")));
  159. testGRRKeys(incExBounds(rp("1"), rp("5")), bounds(rp("1"), endOf("1")), exBounds(endOf("1"), rp("5")));
  160. // max
  161. testGRRKeys(range(endOf("2"), endOf("6")), range(endOf("2"), endOf("6")));
  162. testGRRKeys(bounds(startOf("2"), endOf("6")), bounds(startOf("2"), endOf("6")));
  163. testGRRKeys(exBounds(rp("2"), rp("6")), exBounds(rp("2"), rp("6")));
  164. testGRRKeys(incExBounds(rp("2"), rp("6")), incExBounds(rp("2"), rp("6")));
  165. // bothKeys
  166. testGRRKeys(range(rp("1"), rp("6")), range(rp("1"), endOf("1")), range(endOf("1"), rp("6")));
  167. testGRRKeys(bounds(rp("1"), rp("6")), bounds(rp("1"), endOf("1")), range(endOf("1"), rp("6")));
  168. testGRRKeys(exBounds(rp("1"), rp("6")), range(rp("1"), endOf("1")), exBounds(endOf("1"), rp("6")));
  169. testGRRKeys(incExBounds(rp("1"), rp("6")), bounds(rp("1"), endOf("1")), exBounds(endOf("1"), rp("6")));
  170. }
  171. @Test
  172. public void testGRRWrapped() throws Throwable
  173. {
  174. // one token in wrapped range
  175. testGRR(tokenRange("7", "0"), tokenRange("7", ""), tokenRange("", "0"));
  176. // two tokens in wrapped range
  177. testGRR(tokenRange("5", "0"), tokenRange("5", "6"), tokenRange("6", ""), tokenRange("", "0"));
  178. testGRR(tokenRange("7", "2"), tokenRange("7", ""), tokenRange("", "1"), tokenRange("1", "2"));
  179. // full wraps
  180. testGRR(tokenRange("0", "0"), tokenRange("0", "1"), tokenRange("1", "6"), tokenRange("6", ""), tokenRange("", "0"));
  181. testGRR(tokenRange("", ""), tokenRange("", "1"), tokenRange("1", "6"), tokenRange("6", ""));
  182. // wrap on member tokens
  183. testGRR(tokenRange("6", "6"), tokenRange("6", ""), tokenRange("", "1"), tokenRange("1", "6"));
  184. testGRR(tokenRange("6", "1"), tokenRange("6", ""), tokenRange("", "1"));
  185. // end wrapped
  186. testGRR(tokenRange("5", ""), tokenRange("5", "6"), tokenRange("6", ""));
  187. // Keys
  188. // one token in wrapped range
  189. testGRRKeys(range(rp("7"), rp("0")), range(rp("7"), rp("")), range(rp(""), rp("0")));
  190. // two tokens in wrapped range
  191. testGRRKeys(range(rp("5"), rp("0")), range(rp("5"), endOf("6")), range(endOf("6"), rp("")), range(rp(""), rp("0")));
  192. testGRRKeys(range(rp("7"), rp("2")), range(rp("7"), rp("")), range(rp(""), endOf("1")), range(endOf("1"), rp("2")));
  193. // full wraps
  194. testGRRKeys(range(rp("0"), rp("0")), range(rp("0"), endOf("1")), range(endOf("1"), endOf("6")), range(endOf("6"), rp("")), range(rp(""), rp("0")));
  195. testGRRKeys(range(rp(""), rp("")), range(rp(""), endOf("1")), range(endOf("1"), endOf("6")), range(endOf("6"), rp("")));
  196. // wrap on member tokens
  197. testGRRKeys(range(rp("6"), rp("6")), range(rp("6"), endOf("6")), range(endOf("6"), rp("")), range(rp(""), endOf("1")), range(endOf("1"), rp("6")));
  198. testGRRKeys(range(rp("6"), rp("1")), range(rp("6"), endOf("6")), range(endOf("6"), rp("")), range(rp(""), rp("1")));
  199. // end wrapped
  200. testGRRKeys(range(rp("5"), rp("")), range(rp("5"), endOf("6")), range(endOf("6"), rp("")));
  201. }
  202. @Test
  203. public void testGRRExactBounds() throws Throwable
  204. {
  205. // equal tokens are special cased as non-wrapping for bounds
  206. testGRR(tokenBounds("0", "0"), tokenBounds("0", "0"));
  207. // completely empty bounds match everything
  208. testGRR(tokenBounds("", ""), tokenBounds("", "1"), tokenRange("1", "6"), tokenRange("6", ""));
  209. // Keys
  210. // equal tokens are special cased as non-wrapping for bounds
  211. testGRRKeys(bounds(rp("0"), rp("0")), bounds(rp("0"), rp("0")));
  212. // completely empty bounds match everything
  213. testGRRKeys(bounds(rp(""), rp("")), bounds(rp(""), endOf("1")), range(endOf("1"), endOf("6")), range(endOf("6"), rp("")));
  214. testGRRKeys(exBounds(rp(""), rp("")), range(rp(""), endOf("1")), range(endOf("1"), endOf("6")), exBounds(endOf("6"), rp("")));
  215. testGRRKeys(incExBounds(rp(""), rp("")), bounds(rp(""), endOf("1")), range(endOf("1"), endOf("6")), exBounds(endOf("6"), rp("")));
  216. }
  217. }