/** @return Client configuration for the test. */ protected GridClientConfiguration clientConfiguration() throws GridClientException { GridClientConfiguration cfg = new GridClientConfiguration(); cfg.setBalancer(getBalancer()); cfg.setTopologyRefreshFrequency(TOP_REFRESH_FREQ); cfg.setProtocol(protocol()); cfg.setServers(Arrays.asList(serverAddress())); cfg.setSslContextFactory(sslContextFactory()); GridClientDataConfiguration loc = new GridClientDataConfiguration(); GridClientDataConfiguration partitioned = new GridClientDataConfiguration(); partitioned.setName(PARTITIONED_CACHE_NAME); partitioned.setAffinity(new GridClientPartitionAffinity()); GridClientDataConfiguration replicated = new GridClientDataConfiguration(); replicated.setName(REPLICATED_CACHE_NAME); GridClientDataConfiguration replicatedAsync = new GridClientDataConfiguration(); replicatedAsync.setName(REPLICATED_ASYNC_CACHE_NAME); cfg.setDataConfigurations(Arrays.asList(loc, partitioned, replicated, replicatedAsync)); return cfg; }
/** * Prints a phrase on the grid nodes running anonymous closure objects and calculating total * number of letters. * * @param phrase Phrase to print on of the grid nodes. * @throws GridException If failed. */ private static void countLettersClosure(String phrase) throws GridException { X.println(">>> Starting countLettersClosure() example..."); // Explicitly execute the collection of callable objects and receive a result. Collection<Integer> results = G.grid() .call( SPREAD, new GridClosure<String, Integer>() { // Create executable logic. @Override public Integer apply(String word) { // Print out a given word, just so we can // see which node is doing what. X.println(">>> Executing word: " + word); // Return the length of a given word, i.e. number of letters. return word.length(); } }, Arrays.asList(phrase.split(" "))); // Collection of arguments for closures. // Add up all results using convenience 'sum()' method. int letterCnt = F.sum(results); X.println(">>>"); X.println(">>> Finished execution of counting letters with closure based on GridGain 3.0 API."); X.println(">>> You should see the phrase '" + phrase + "' printed out on the nodes."); X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
/** @throws Exception If failed. */ public void testClientAffinity() throws Exception { GridClientData partitioned = client.data(PARTITIONED_CACHE_NAME); Collection<Object> keys = new ArrayList<>(); keys.addAll(Arrays.asList(Boolean.TRUE, Boolean.FALSE, 1, Integer.MAX_VALUE)); Random rnd = new Random(); StringBuilder sb = new StringBuilder(); // Generate some random strings. for (int i = 0; i < 100; i++) { sb.setLength(0); for (int j = 0; j < 255; j++) // Only printable ASCII symbols for test. sb.append((char) (rnd.nextInt(0x7f - 0x20) + 0x20)); keys.add(sb.toString()); } // Generate some more keys to achieve better coverage. for (int i = 0; i < 100; i++) keys.add(UUID.randomUUID()); for (Object key : keys) { UUID nodeId = grid(0).mapKeyToNode(PARTITIONED_CACHE_NAME, key).id(); UUID clientNodeId = partitioned.affinity(key); assertEquals( "Invalid affinity mapping for REST response for key: " + key, nodeId, clientNodeId); } }
/** * @param g Grid. * @return Non-system caches. */ private Collection<GridCacheConfiguration> caches(Grid g) { return F.view( Arrays.asList(g.configuration().getCacheConfiguration()), new GridPredicate<GridCacheConfiguration>() { @Override public boolean apply(GridCacheConfiguration c) { return c.getName() == null || !c.getName().equals(CU.UTILITY_CACHE_NAME); } }); }
/** * Calculates length of a given phrase on the grid. * * @param phrase Phrase to count the number of letters in. * @throws GridException If failed. */ private static void countLettersReducer(String phrase) throws GridException { X.println(">>> Starting countLettersReducer() example..."); Grid grid = G.grid(); // Logger to use in your closure. Note that even though we assign it // to a local variable, GridGain still allows to use it from remotely // executed code. final GridLogger log = grid.log(); // Execute Hello World task. int letterCnt = grid.reduce( BALANCE, new GridClosure<String, Integer>() { // Create executable logic. @Override public Integer apply(String word) { // Print out a given word, just so we can // see which node is doing what. log.info(">>> Calculating for word: " + word); // Return the length of a given word, i.e. number of letters. return word.length(); } }, Arrays.asList(phrase.split(" ")), // Collection of words. // Create custom reducer. // NOTE: Alternatively, you can use existing reducer: F.sumIntReducer() new GridReducer<Integer, Integer>() { private int sum; @Override public boolean collect(Integer res) { sum += res; return true; // True means continue collecting until last result. } @Override public Integer apply() { return sum; } }); X.println(">>>"); X.println(">>> Finished execution of counting letters with reducer based on GridGain 3.0 API."); X.println(">>> Total number of letters in the phrase is '" + letterCnt + "'."); X.println(">>> You should see individual words printed out on different nodes."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
/** * Executes example. * * @param args Command line arguments, none required. * @throws GridException If example execution failed. */ public static void main(String[] args) throws GridException { try (Grid g = GridGain.start("examples/config/example-compute.xml")) { System.out.println(); System.out.println("Compute reducer example started."); Integer sum = g.compute() .apply( new GridClosure<String, Integer>() { @Override public Integer apply(String word) { System.out.println(); System.out.println(">>> Printing '" + word + "' on this node from grid job."); // Return number of letters in the word. return word.length(); } }, // Job parameters. GridGain will create as many jobs as there are parameters. Arrays.asList("Count characters using reducer".split(" ")), // Reducer to process results as they come. new GridReducer<Integer, Integer>() { private AtomicInteger sum = new AtomicInteger(); // Callback for every job result. @Override public boolean collect(Integer len) { sum.addAndGet(len); // Return true to continue waiting until all results are received. return true; } // Reduce all results into one. @Override public Integer reduce() { return sum.get(); } }) .get(); System.out.println(); System.out.println(">>> Total number of characters in the phrase is '" + sum + "'."); System.out.println(">>> Check all nodes for output (this node is also part of the grid)."); } }
/** * Constructs data loader peer-deploy aware. * * @param objs Collection of objects to detect deploy class and class loader. */ private DataLoaderPda(Object... objs) { this.objs = Arrays.asList(objs); }