/** * @param args Arguments. * @throws Exception If failed. */ public static void main(String[] args) throws Exception { try { G.start("examples/config/example-cache.xml"); JOptionPane.showMessageDialog(null, "Press OK to stop test node."); } finally { G.stopAll(false); } }
/** @throws Exception If failed. */ @SuppressWarnings("unchecked") public void testCancel() throws Exception { Grid grid = G.grid(getTestGridName()); grid.compute() .localDeployTask(GridCancelTestTask.class, U.detectClassLoader(GridCancelTestTask.class)); GridComputeTaskFuture<?> fut = grid.compute().execute(GridCancelTestTask.class.getName(), null); // Wait until jobs begin execution. boolean await = startSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS); assert await : "Jobs did not start."; info("Test task result: " + fut); assert fut != null; // Only first job should successfully complete. Object res = fut.get(); assert (Integer) res == 1; // Wait for all jobs to finish. await = stopSignal.await(WAIT_TIME, TimeUnit.MILLISECONDS); assert await : "Jobs did not stop."; // One is definitely processed. But there might be some more processed or cancelled or processed // and cancelled. // Thus total number should be at least SPLIT_COUNT and at most (SPLIT_COUNT - 1) *2 +1 assert (cancelCnt + processedCnt) >= SPLIT_COUNT && (cancelCnt + processedCnt) <= (SPLIT_COUNT - 1) * 2 + 1 : "Invalid cancel count value: " + cancelCnt; }
/** * Check if flags in correct state. * * @param msg Message. */ private void checkSyncFlags(GridIoMessage msg) { if (!commSpiEnabled) return; Object o = msg.message(); if (!(o instanceof GridDistributedLockRequest)) return; GridKernal g = (GridKernal) G.grid(nodeId); GridCacheTxManager<Object, Object> tm = g.internalCache(REPLICATED_ASYNC_CACHE_NAME).context().tm(); GridCacheVersion v = ((GridCacheVersionable) o).version(); GridCacheTxEx t = tm.tx(v); if (t.hasWriteKey("x1")) { assertFalse(t.syncCommit()); } else if (t.hasWriteKey("x2")) { assertTrue(t.syncCommit()); } else if (t.hasWriteKey("x3")) { assertFalse(t.syncCommit()); } else if (t.hasWriteKey("x4")) { assertTrue(t.syncCommit()); } }
/** @throws Exception If failed. */ public void testRemoteIfDataCacheNameEquals() throws Exception { GridConfiguration g2Cfg = getConfiguration("g2"); GridGgfsConfiguration g2GgfsCfg1 = new GridGgfsConfiguration(g1GgfsCfg1); GridGgfsConfiguration g2GgfsCfg2 = new GridGgfsConfiguration(g1GgfsCfg2); g2GgfsCfg1.setName("g2GgfsCfg1"); g2GgfsCfg2.setName("g2GgfsCfg2"); g2GgfsCfg1.setMetaCacheName("g2MetaCache1"); g2GgfsCfg2.setMetaCacheName("g2MetaCache2"); g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setCacheConfiguration( concat( dataCaches(1024), metaCaches("g2MetaCache1", "g2MetaCache2"), GridCacheConfiguration.class)); g2Cfg.setGgfsConfiguration(g2GgfsCfg1, g2GgfsCfg2); G.start(g1Cfg); checkGridStartFails( g2Cfg, "Data cache names should be different for different GGFS instances", false); }
/** @throws Exception If failed. */ public void testDisabledRest() throws Exception { restEnabled = false; final Grid g = startGrid("disabled-rest"); try { Thread.sleep(2 * TOP_REFRESH_FREQ); // As long as we have round robin load balancer this will cause every node to be queried. for (int i = 0; i < NODES_CNT + 1; i++) assertEquals(NODES_CNT + 1, client.compute().refreshTopology(false, false).size()); final GridClientData data = client.data(PARTITIONED_CACHE_NAME); // Check rest-disabled node is unavailable. try { String affKey; do { affKey = UUID.randomUUID().toString(); } while (!data.affinity(affKey).equals(g.localNode().id())); data.put(affKey, "asdf"); assertEquals("asdf", cache(0, PARTITIONED_CACHE_NAME).get(affKey)); } catch (GridServerUnreachableException e) { // Thrown for direct client-node connections. assertTrue( "Unexpected exception message: " + e.getMessage(), e.getMessage() .startsWith("No available endpoints to connect (is rest enabled for this node?)")); } catch (GridClientException e) { // Thrown for routed client-router-node connections. String msg = e.getMessage(); assertTrue( "Unexpected exception message: " + msg, protocol() == GridClientProtocol.TCP ? msg.contains("No available endpoints to connect (is rest enabled for this node?)") : // TCP router. msg.startsWith( "No available nodes on the router for destination node ID")); // HTTP router. } // Check rest-enabled nodes are available. String affKey; do { affKey = UUID.randomUUID().toString(); } while (data.affinity(affKey).equals(g.localNode().id())); data.put(affKey, "fdsa"); assertEquals("fdsa", cache(0, PARTITIONED_CACHE_NAME).get(affKey)); } finally { restEnabled = true; G.stop(g.name(), true); } }
/** @throws Exception If failed. */ public void testInvalidateFlag() throws Exception { GridEx g0 = grid(0); GridCache<String, String> cache = g0.cache(PARTITIONED_CACHE_NAME); String key = null; for (int i = 0; i < 10_000; i++) { if (!cache.affinity().isPrimaryOrBackup(g0.localNode(), String.valueOf(i))) { key = String.valueOf(i); break; } } assertNotNull(key); cache.put(key, key); // Create entry in near cache, it is invalidated if INVALIDATE flag is set. assertNotNull(cache.peek(key)); GridClientData d = client.data(PARTITIONED_CACHE_NAME); d.flagsOn(GridClientCacheFlag.INVALIDATE).put(key, "zzz"); for (Grid g : G.allGrids()) { cache = g.cache(PARTITIONED_CACHE_NAME); if (cache.affinity().isPrimaryOrBackup(g.localNode(), key)) assertEquals("zzz", cache.peek(key)); else assertNull(cache.peek(key)); } }
/** @throws Exception If failed. */ @SuppressWarnings("NullableProblems") public void testLocalNullGgfsNameIsSupported() throws Exception { g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g1GgfsCfg1.setName(null); assertFalse(G.start(g1Cfg).nodes().isEmpty()); }
/** @throws Exception If failed. */ public void testRemoteIfAffinityMapperGroupSizeDiffers() throws Exception { GridConfiguration g2Cfg = getConfiguration("g2"); g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setCacheConfiguration( concat(dataCaches(4021), metaCaches(), GridCacheConfiguration.class)); G.start(g1Cfg); checkGridStartFails( g2Cfg, "Affinity mapper group size should be the same on all nodes in grid for GGFS", false); }
/** * Starts the local node and checks for presence of log file. Also checks that this is really a * log of a started node. * * @param id Test-local node ID. * @throws Exception If error occurred. */ private void checkOneNode(int id) throws Exception { try (Grid grid = G.start(getConfiguration("grid" + id))) { String id8 = U.id8(grid.localNode().id()); String logPath = "work/log/gridgain-" + id8 + ".log"; File logFile = U.resolveGridGainPath(logPath); assertNotNull("Failed to resolve path: " + logPath, logFile); assertTrue("Log file does not exist: " + logFile, logFile.exists()); String logContent = U.readFileToString(logFile.getAbsolutePath(), "UTF-8"); assertTrue( "Log file does not contain it's node ID: " + logFile, logContent.contains(">>> Local node [ID=" + id8.toUpperCase())); } }
/** * Starts new grid node. * * @param gridName name of new node. * @param springCfg file with spring configuration to use for this node. * @return a grid instance local to new node {@link GridGain#start(GridConfiguration)}. * @throws Exception if node run failed. */ protected Grid startNode(String gridName, File springCfg) throws Exception { assert springCfg != null; ListableBeanFactory springCtx = new FileSystemXmlApplicationContext("file:///" + springCfg.getAbsolutePath()); Map cfgMap = springCtx.getBeansOfType(GridConfiguration.class); assert cfgMap != null; assert !cfgMap.isEmpty(); GridConfiguration cfg = (GridConfiguration) cfgMap.values().iterator().next(); cfg.setGridName(gridName + "-" + getNextNodeNum()); return G.start(cfg); }
/** @throws Exception If failed. */ public void testRemoteIfDataBlockSizeDiffers() throws Exception { GridConfiguration g2Cfg = getConfiguration("g2"); g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); GridGgfsConfiguration g2GgfsCfg1 = new GridGgfsConfiguration(g1GgfsCfg1); g2GgfsCfg1.setBlockSize(g2GgfsCfg1.getBlockSize() + 100); g2Cfg.setGgfsConfiguration(g2GgfsCfg1, g1GgfsCfg2); G.start(g1Cfg); checkGridStartFails( g2Cfg, "Data block size should be same on all nodes in grid for GGFS", false); }
/** * Checks that the given grid configuration will lead to {@link GridException} upon grid startup. * * @param cfg Grid configuration to check. * @param excMsgSnippet Root cause (assertion) exception message snippet. * @param testLoc {@code True} if checking is done for "testLocal" tests. */ private void checkGridStartFails( GridConfiguration cfg, CharSequence excMsgSnippet, boolean testLoc) { assertNotNull(cfg); assertNotNull(excMsgSnippet); try { G.start(cfg); fail("No exception has been thrown."); } catch (GridException e) { if (testLoc) { if ("Failed to start processor: GridProcessorAdapter []".equals(e.getMessage()) && e.getCause().getMessage().contains(excMsgSnippet)) return; // Expected exception. } else if (e.getMessage().contains(excMsgSnippet)) return; // Expected exception. error("Caught unexpected exception.", e); fail(); } }
/** @throws Exception If failed. */ public void testRemoteIfPathModeDiffers() throws Exception { GridConfiguration g2Cfg = getConfiguration("g2"); GridGgfsConfiguration g2GgfsCfg1 = new GridGgfsConfiguration(g1GgfsCfg1); GridGgfsConfiguration g2GgfsCfg2 = new GridGgfsConfiguration(g1GgfsCfg2); g2GgfsCfg1.setPathModes(ImmutableMap.of("/somePath", DUAL_SYNC)); g2GgfsCfg2.setPathModes(ImmutableMap.of("/somePath", DUAL_SYNC)); g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setGgfsConfiguration(g2GgfsCfg1, g2GgfsCfg2); G.start(g1Cfg); checkGridStartFails( g2Cfg, "Path modes should be the same on all nodes in grid for GGFS", false); }
/** @throws Exception If failed. */ public void testRemoteIfDefaultModeDiffers() throws Exception { GridConfiguration g2Cfg = getConfiguration("g2"); GridGgfsConfiguration g2GgfsCfg1 = new GridGgfsConfiguration(g1GgfsCfg1); GridGgfsConfiguration g2GgfsCfg2 = new GridGgfsConfiguration(g1GgfsCfg2); g1GgfsCfg1.setDefaultMode(DUAL_ASYNC); g1GgfsCfg2.setDefaultMode(DUAL_ASYNC); g2GgfsCfg1.setDefaultMode(DUAL_SYNC); g2GgfsCfg2.setDefaultMode(DUAL_SYNC); g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setGgfsConfiguration(g2GgfsCfg1, g2GgfsCfg2); G.start(g1Cfg); checkGridStartFails( g2Cfg, "Default mode should be the same on all nodes in grid for GGFS", false); }
/** @throws Exception If failed. */ public void testRemoteIfMetaCacheNameDiffers() throws Exception { GridConfiguration g2Cfg = getConfiguration("g2"); GridGgfsConfiguration g2GgfsCfg1 = new GridGgfsConfiguration(g1GgfsCfg1); GridGgfsConfiguration g2GgfsCfg2 = new GridGgfsConfiguration(g1GgfsCfg2); g2GgfsCfg1.setMetaCacheName("g2MetaCache1"); g2GgfsCfg2.setMetaCacheName("g2MetaCache2"); g1Cfg.setCacheConfiguration( concat(dataCaches(1024), metaCaches(), GridCacheConfiguration.class)); g2Cfg.setCacheConfiguration( concat( dataCaches(1024), metaCaches("g2MetaCache1", "g2MetaCache2"), GridCacheConfiguration.class)); g2Cfg.setGgfsConfiguration(g2GgfsCfg1, g2GgfsCfg2); G.start(g1Cfg); checkGridStartFails( g2Cfg, "Meta cache name should be the same on all nodes in grid for GGFS", false); }
/** {@inheritDoc} */ @Override protected void afterTestsStopped() throws Exception { G.stopAll(true); }