/sentinel-extension/sentinel-datasource-zookeeper/src/test/java/com/alibaba/csp/sentinel/datasource/zookeeper/ZookeeperDataSourceTest.java

https://github.com/alibaba/Sentinel · Java · 224 lines · 176 code · 41 blank · 7 comment · 10 complexity · 74cbe278873c8fbe20f439a80732e475 MD5 · raw file

  1. package com.alibaba.csp.sentinel.datasource.zookeeper;
  2. import com.alibaba.csp.sentinel.datasource.Converter;
  3. import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
  4. import com.alibaba.csp.sentinel.slots.block.RuleConstant;
  5. import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
  6. import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
  7. import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
  8. import com.alibaba.fastjson.JSON;
  9. import com.alibaba.fastjson.TypeReference;
  10. import org.apache.curator.framework.AuthInfo;
  11. import org.apache.curator.framework.CuratorFramework;
  12. import org.apache.curator.framework.CuratorFrameworkFactory;
  13. import org.apache.curator.retry.ExponentialBackoffRetry;
  14. import org.apache.curator.test.TestingServer;
  15. import org.apache.zookeeper.CreateMode;
  16. import org.apache.zookeeper.ZooDefs;
  17. import org.apache.zookeeper.data.ACL;
  18. import org.apache.zookeeper.data.Id;
  19. import org.apache.zookeeper.data.Stat;
  20. import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;
  21. import org.junit.Assert;
  22. import org.junit.Test;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.concurrent.Callable;
  26. import java.util.concurrent.TimeUnit;
  27. import static org.awaitility.Awaitility.await;
  28. import static org.junit.Assert.assertEquals;
  29. import static org.junit.Assert.assertTrue;
  30. /**
  31. * @author Eric Zhao
  32. */
  33. public class ZookeeperDataSourceTest {
  34. @Test
  35. public void testZooKeeperDataSource() throws Exception {
  36. TestingServer server = new TestingServer(21812);
  37. server.start();
  38. final String remoteAddress = server.getConnectString();
  39. final String path = "/sentinel-zk-ds-demo/flow-HK";
  40. ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress, path,
  41. new Converter<String, List<FlowRule>>() {
  42. @Override
  43. public List<FlowRule> convert(String source) {
  44. return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
  45. });
  46. }
  47. });
  48. FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
  49. CuratorFramework zkClient = CuratorFrameworkFactory.newClient(remoteAddress,
  50. new ExponentialBackoffRetry(3, 1000));
  51. zkClient.start();
  52. Stat stat = zkClient.checkExists().forPath(path);
  53. if (stat == null) {
  54. zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null);
  55. }
  56. final String resourceName = "HK";
  57. publishThenTestFor(zkClient, path, resourceName, 10);
  58. publishThenTestFor(zkClient, path, resourceName, 15);
  59. zkClient.close();
  60. server.stop();
  61. }
  62. @Test
  63. public void testZooKeeperDataSourceAuthorization() throws Exception {
  64. TestingServer server = new TestingServer(21812);
  65. server.start();
  66. final String remoteAddress = server.getConnectString();
  67. final String groupId = "sentinel-zk-ds-demo";
  68. final String dataId = "flow-HK";
  69. final String path = "/" + groupId + "/" + dataId;
  70. final String scheme = "digest";
  71. final String auth = "root:123456";
  72. AuthInfo authInfo = new AuthInfo(scheme, auth.getBytes());
  73. List<AuthInfo> authInfoList = Collections.singletonList(authInfo);
  74. CuratorFramework zkClient = CuratorFrameworkFactory.builder().
  75. connectString(remoteAddress).
  76. retryPolicy(new ExponentialBackoffRetry(3, 100)).
  77. authorization(authInfoList).
  78. build();
  79. zkClient.start();
  80. Stat stat = zkClient.checkExists().forPath(path);
  81. if (stat == null) {
  82. ACL acl = new ACL(ZooDefs.Perms.ALL, new Id(scheme, DigestAuthenticationProvider.generateDigest(auth)));
  83. zkClient.create().creatingParentContainersIfNeeded().withACL(Collections.singletonList(acl)).forPath(path, null);
  84. }
  85. ReadableDataSource<String, List<FlowRule>> flowRuleDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress,
  86. authInfoList, groupId, dataId,
  87. new Converter<String, List<FlowRule>>() {
  88. @Override
  89. public List<FlowRule> convert(String source) {
  90. return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
  91. });
  92. }
  93. });
  94. FlowRuleManager.register2Property(flowRuleDataSource.getProperty());
  95. final String resourceName = "HK";
  96. publishThenTestFor(zkClient, path, resourceName, 10);
  97. publishThenTestFor(zkClient, path, resourceName, 15);
  98. zkClient.close();
  99. server.stop();
  100. }
  101. private void publishThenTestFor(CuratorFramework zkClient, String path, String resourceName, long count) throws Exception {
  102. FlowRule rule = new FlowRule().setResource(resourceName)
  103. .setLimitApp("default")
  104. .as(FlowRule.class)
  105. .setCount(count)
  106. .setGrade(RuleConstant.FLOW_GRADE_QPS);
  107. String ruleString = JSON.toJSONString(Collections.singletonList(rule));
  108. zkClient.setData().forPath(path, ruleString.getBytes());
  109. await().timeout(5, TimeUnit.SECONDS)
  110. .until(new Callable<Boolean>() {
  111. @Override
  112. public Boolean call() throws Exception {
  113. List<FlowRule> rules = FlowRuleManager.getRules();
  114. return rules != null && !rules.isEmpty();
  115. }
  116. });
  117. List<FlowRule> rules = FlowRuleManager.getRules();
  118. boolean exists = false;
  119. for (FlowRule r : rules) {
  120. if (resourceName.equals(r.getResource())) {
  121. exists = true;
  122. assertEquals(count, new Double(r.getCount()).longValue());
  123. }
  124. }
  125. assertTrue(exists);
  126. }
  127. /**
  128. * Test whether different dataSources can share the same zkClient when the connection parameters are the same.
  129. * @throws Exception
  130. */
  131. @Test
  132. public void testZooKeeperDataSourceSameZkClient() throws Exception {
  133. TestingServer server = new TestingServer(21813);
  134. server.start();
  135. final String remoteAddress = server.getConnectString();
  136. final String flowPath = "/sentinel-zk-ds-demo/flow-HK";
  137. final String degradePath = "/sentinel-zk-ds-demo/degrade-HK";
  138. ZookeeperDataSource<List<FlowRule>> flowRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, flowPath,
  139. new Converter<String, List<FlowRule>>() {
  140. @Override
  141. public List<FlowRule> convert(String source) {
  142. return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
  143. });
  144. }
  145. });
  146. ZookeeperDataSource<List<DegradeRule>> degradeRuleZkDataSource = new ZookeeperDataSource<>(remoteAddress, degradePath,
  147. new Converter<String, List<DegradeRule>>() {
  148. @Override
  149. public List<DegradeRule> convert(String source) {
  150. return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
  151. });
  152. }
  153. });
  154. Assert.assertTrue(flowRuleZkDataSource.getZkClient() == degradeRuleZkDataSource.getZkClient());
  155. final String groupId = "sentinel-zk-ds-demo";
  156. final String flowDataId = "flow-HK";
  157. final String degradeDataId = "degrade-HK";
  158. final String scheme = "digest";
  159. final String auth = "root:123456";
  160. AuthInfo authInfo = new AuthInfo(scheme, auth.getBytes());
  161. List<AuthInfo> authInfoList = Collections.singletonList(authInfo);
  162. ZookeeperDataSource<List<FlowRule>> flowRuleZkAutoDataSource = new ZookeeperDataSource<List<FlowRule>>(remoteAddress,
  163. authInfoList, groupId, flowDataId,
  164. new Converter<String, List<FlowRule>>() {
  165. @Override
  166. public List<FlowRule> convert(String source) {
  167. return JSON.parseObject(source, new TypeReference<List<FlowRule>>() {
  168. });
  169. }
  170. });
  171. ZookeeperDataSource<List<DegradeRule>> degradeRuleZkAutoDataSource = new ZookeeperDataSource<List<DegradeRule>>(remoteAddress,
  172. authInfoList, groupId, degradeDataId,
  173. new Converter<String, List<DegradeRule>>() {
  174. @Override
  175. public List<DegradeRule> convert(String source) {
  176. return JSON.parseObject(source, new TypeReference<List<DegradeRule>>() {
  177. });
  178. }
  179. });
  180. Assert.assertTrue(flowRuleZkAutoDataSource.getZkClient() == degradeRuleZkAutoDataSource.getZkClient());
  181. server.stop();
  182. }
  183. }