@Test public void testCloseable() throws IOException { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = null; try { jc = new JedisCluster(jedisClusterNode); jc.set("51", "foo"); } finally { if (jc != null) { jc.close(); } } Iterator<JedisPool> poolIterator = jc.getClusterNodes().values().iterator(); while (poolIterator.hasNext()) { JedisPool pool = poolIterator.next(); try { pool.getResource(); fail("JedisCluster's internal pools should be already destroyed"); } catch (JedisConnectionException e) { // ok to go... } } }
@Test public void testJedisClusterRunsWithMultithreaded() throws InterruptedException, ExecutionException, IOException { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); final JedisCluster jc = new JedisCluster(jedisClusterNode); jc.set("foo", "bar"); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); List<Future<String>> futures = new ArrayList<Future<String>>(); for (int i = 0; i < 50; i++) { executor.submit( new Callable<String>() { @Override public String call() throws Exception { // FIXME : invalidate slot cache from JedisCluster to test // random connection also does work return jc.get("foo"); } }); } for (Future<String> future : futures) { String value = future.get(); assertEquals("bar", value); } jc.close(); }
@Test public void testDiscoverNodesAutomatically() { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); assertEquals(3, jc.getClusterNodes().size()); JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 7379)); assertEquals(3, jc2.getClusterNodes().size()); }
@Test(expected = JedisConnectionException.class) public void testIfPoolConfigAppliesToClusterPools() { GenericObjectPoolConfig config = new GenericObjectPoolConfig(); config.setMaxTotal(0); config.setMaxWaitMillis(2000); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode, config); jc.set("52", "poolTestValue"); }
@Test(expected = JedisClusterMaxRedirectionsException.class) public void testRedisClusterMaxRedirections() { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); int slot51 = JedisClusterCRC16.getSlot("51"); // This will cause an infinite redirection loop node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); jc.set("51", "foo"); }
@Test public void testAskResponse() throws InterruptedException { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); int slot51 = JedisClusterCRC16.getSlot("51"); node3.clusterSetSlotImporting(slot51, JedisClusterTestUtil.getNodeId(node2.clusterNodes())); node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); jc.set("51", "foo"); assertEquals("foo", jc.get("51")); }
@Test(expected = JedisClusterMaxRedirectionsException.class, timeout = 2000) public void testReturnConnectionOnRedirection() { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(1); JedisCluster jc = new JedisCluster(jedisClusterNode, 0, 2, config); // This will cause an infinite redirection between node 2 and 3 node3.clusterSetSlotMigrating(15363, JedisClusterTestUtil.getNodeId(node2.clusterNodes())); jc.get("e"); }
@Test public void testMigrateToNewNode() throws InterruptedException { log.info("test migrate slot to new node"); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(nodeInfo1); JedisCluster jc = new JedisCluster(jedisClusterNode); node4.clusterMeet(localHost, nodeInfo1.getPort()); String node3Id = JedisClusterTestUtil.getNodeId(node3.clusterNodes()); String node4Id = JedisClusterTestUtil.getNodeId(node4.clusterNodes()); JedisClusterTestUtil.waitForClusterReady(node4); node3.clusterSetSlotMigrating(15363, node4Id); node4.clusterSetSlotImporting(15363, node3Id); try { node4.set("e", "e"); } catch (JedisMovedDataException jme) { assertEquals(15363, jme.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo3.getPort()), jme.getTargetNode()); } try { node3.set("e", "e"); } catch (JedisAskDataException jae) { assertEquals(15363, jae.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo4.getPort()), jae.getTargetNode()); } jc.set("e", "e"); try { node4.get("e"); } catch (JedisMovedDataException jme) { assertEquals(15363, jme.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo3.getPort()), jme.getTargetNode()); } try { node3.get("e"); } catch (JedisAskDataException jae) { assertEquals(15363, jae.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo4.getPort()), jae.getTargetNode()); } assertEquals("e", jc.get("e")); node4.clusterSetSlotNode(15363, node4Id); node3.clusterSetSlotNode(15363, node4Id); // assertEquals("e", jc.get("e")); assertEquals("e", node4.get("e")); // assertEquals("e", node3.get("e")); }
@Test public void testClusterCountKeysInSlot() { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = new JedisCluster(jedisClusterNode); for (int index = 0; index < 5; index++) { jc.set("foo{bar}" + index, "hello"); } int slot = JedisClusterCRC16.getSlot("foo{bar}"); assertEquals(5, node1.clusterCountKeysInSlot(slot).intValue()); }
@Test public void testJedisClusterTimeout() { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = new JedisCluster(jedisClusterNode, 4000); for (JedisPool pool : jc.getClusterNodes().values()) { Jedis jedis = pool.getResource(); assertEquals(jedis.getClient().getConnectionTimeout(), 4000); assertEquals(jedis.getClient().getSoTimeout(), 4000); jedis.close(); } }
@Test public void testRecalculateSlotsWhenMoved() throws InterruptedException { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); int slot51 = JedisClusterCRC16.getSlot("51"); node2.clusterDelSlots(slot51); node3.clusterDelSlots(slot51); node3.clusterAddSlots(slot51); JedisClusterTestUtil.waitForClusterReady(node1, node2, node3); jc.set("51", "foo"); assertEquals("foo", jc.get("51")); }
/** slot->nodes 15363 node3 e */ @Test public void testMigrate() { log.info("test migrate slot"); Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(nodeInfo1); JedisCluster jc = new JedisCluster(jedisClusterNode); String node3Id = JedisClusterTestUtil.getNodeId(node3.clusterNodes()); String node2Id = JedisClusterTestUtil.getNodeId(node2.clusterNodes()); node3.clusterSetSlotMigrating(15363, node2Id); node2.clusterSetSlotImporting(15363, node3Id); try { node2.set("e", "e"); } catch (JedisMovedDataException jme) { assertEquals(15363, jme.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo3.getPort()), jme.getTargetNode()); } try { node3.set("e", "e"); } catch (JedisAskDataException jae) { assertEquals(15363, jae.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo2.getPort()), jae.getTargetNode()); } jc.set("e", "e"); try { node2.get("e"); } catch (JedisMovedDataException jme) { assertEquals(15363, jme.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo3.getPort()), jme.getTargetNode()); } try { node3.get("e"); } catch (JedisAskDataException jae) { assertEquals(15363, jae.getSlot()); assertEquals(new HostAndPort(localHost, nodeInfo2.getPort()), jae.getTargetNode()); } assertEquals("e", jc.get("e")); node2.clusterSetSlotNode(15363, node2Id); node3.clusterSetSlotNode(15363, node2Id); // assertEquals("e", jc.get("e")); assertEquals("e", node2.get("e")); // assertEquals("e", node3.get("e")); }
@Test(timeout = 2000) public void testReturnConnectionOnJedisConnectionException() throws InterruptedException { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(1); JedisCluster jc = new JedisCluster(jedisClusterNode, config); Jedis j = jc.getClusterNodes().get("127.0.0.1:7380").getResource(); ClientKillerUtil.tagClient(j, "DEAD"); ClientKillerUtil.killClient(j, "DEAD"); j.close(); jc.get("test"); }
public void handleAction(String jsonStr, MTMeanMetric rMetric) throws ParseException { try { JSONObject jsonObj = new JSONObject(jsonStr); if (!"getRecommendByDeal".equals(jsonObj.getString("action"))) { return; } JSONObject result = new JSONObject(); String gid = "app_dataapp_gid_" + jsonObj.getString("gid"); for (String colname : colnames) { result.put(colname, jsonObj.get(colname)); } // 更新监控时间 String createTime = jsonObj.getString("createTime"); String currentTime = Utils.getCurrentTime("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date beginDate = sd.parse(createTime); Date endDate = sd.parse(currentTime); long time = (endDate.getTime() - beginDate.getTime()) / 1000; rMetric.update(time); result.put("currentTime", currentTime); jedisCluster.setex(gid, 3600, result.toString()); // logger.info(result.toString()); } catch (JSONException e) { return; } }
/** 获得景点Lists并且以cf_jd_XCGnGt_lists为key存入redis 获得景点与景点编号的映射 并且以xc_jd_num为key存入redis */ public static void getGnGtJdAndLists(String cf) { Document contentDoc = Jsoup.parse(visitPageAndGet(XC.XCTours)); Elements elements = contentDoc.select("a"); String url; String jd; String jdNum; String[] strings; Matcher matcher; String html; String pageNum; for (Element ele : elements) { // http://vacations.ctrip.com + /tours/d-jakarta-435 + /grouptravel // http://vacations.ctrip.com/tours/d-jakarta-435/grouptravel if ((url = ele.attr("href")).contains("/you/")) { // 找到URL url = "http://vacations.ctrip.com" + url + "/grouptravel"; url = url.replace("/you/", "/tours/"); strings = url.split("-"); jd = strings[1]; jdNum = strings[2]; // 插入数据 jedisCluster.hset(XC.GnJdKey, jd, jdNum); html = visitPageAndGet(url); // 匹配分页数 matcher = XC.pageNumPattern.matcher(html); pageNum = "1"; int num; while (matcher.find()) { pageNum = matcher.group(2); } num = Integer.parseInt(pageNum); System.out.println("pageNum " + pageNum); System.out.println(cf + "_" + jd + "_" + XC.XCGnGt + "_" + "lists"); for (int i = 1; i <= num; i++) { jedisCluster.rpush(cf + "_" + jd + "_" + XC.XCGnGt + "lists", url + "/p" + i); System.out.println(url + "/p" + i); } System.out.println(); } } }
public static void getGnGtDetails() { Map<String, String> cfMap = new HashMap<String, String>(); Map<String, String> jdMap = jedisCluster.hgetAll(XC.GnJdKey); String tempKey; String tempURL; for (String cfKey : cfMap.keySet()) { for (String jdKey : jdMap.keySet()) { tempKey = cfKey + "_" + jdKey + "_" + XC.XCGnGt + "_lists"; System.out.println("tempKey " + tempKey); while ((tempURL = jedisCluster.lpop(tempKey)) != null) { matchDetails(tempURL, jdKey); } } } }
@Test public void testStableSlotWhenMigratingNodeOrImportingNodeIsNotSpecified() throws InterruptedException { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = new JedisCluster(jedisClusterNode); int slot51 = JedisClusterCRC16.getSlot("51"); jc.set("51", "foo"); // node2 is responsible of taking care of slot51 (7186) node3.clusterSetSlotImporting(slot51, JedisClusterTestUtil.getNodeId(node2.clusterNodes())); assertEquals("foo", jc.get("51")); node3.clusterSetSlotStable(slot51); assertEquals("foo", jc.get("51")); node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); // assertEquals("foo", jc.get("51")); // it leads Max Redirections node2.clusterSetSlotStable(slot51); assertEquals("foo", jc.get("51")); }
@Test public void testCalculateConnectionPerSlot() { Set<HostAndPort> jedisClusterNode = new HashSet<HostAndPort>(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode); jc.set("foo", "bar"); jc.set("test", "test"); assertEquals("bar", node3.get("foo")); assertEquals("test", node2.get("test")); JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 7379)); jc2.set("foo", "bar"); jc2.set("test", "test"); assertEquals("bar", node3.get("foo")); assertEquals("test", node2.get("test")); }
public static void matchDetails(String URL, String JdName) { String title; String subtitle; String contenturl; String cfNum = ""; String cfName = ""; String detailPage = visitPageAndGet(URL); Document page = Jsoup.parse(detailPage); Elements contents = page.select("div.aggregation_content"); int i = 1; for (Element ele : contents) { // 大标题 title = ele.select("h2.aggregation_title").first().text(); Elements subcontents = ele.select("li.flag_product"); for (Element subcontent : subcontents) { Element firsta = subcontent.select("a").first(); if (firsta != null) { subtitle = firsta.text(); title = title + "·" + subtitle; // http://vacations.ctrip.com/grouptravel/p3699856s158.html contenturl = "http://vacations.ctrip.com/" + subcontent.attr("data-href"); Matcher matcher = XC.numPattern.matcher(contenturl); if (matcher.find()) { cfNum = matcher.group(1); } cfName = jedisCluster.hget(XC.GnCfKey, cfNum); } else { // 没有小标题 } } } }
public static void getCf() { jedisCluster.hset(XC.GnCfKey, "158", "长春"); }