/** * Broadcasts a give phrase to all nodes. * * @param phrase Phrase to broadcast. * @throws GridException If failed. */ private static void broadcastWordsClosure(final String phrase) throws GridException { X.println(">>> Starting broadcastWordsClosure() example..."); G.grid() .run( BROADCAST, new GridAbsClosure() { @Override public void apply() { X.println(">>> Printing phrase: " + phrase); } }); // NOTE: // // Alternatively, you can use existing closure 'F.println()' to // print any text like so: // // G.grid().run(BROADCAST, F.println(">>> Printing phrase: " + phrase)); // X.println(">>>"); X.println(">>> Finished broadcasting a phrase to all grid nodes based on GridGain 3.0 API."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
/** * Prints every word a phrase on different nodes. * * @param phrase Phrase from which to print words on different nodes. * @throws GridException If failed. */ private static void spreadWordsClosure(String phrase) throws GridException { X.println(">>> Starting spreadWordsClosure() example..."); // Splits the passed in phrase into words and prints every word // on a individual grid node. If there are more words than nodes - // some nodes will print more than one word. G.grid() .run( SPREAD, F.yield( phrase.split(" "), new GridInClosure<String>() { @Override public void apply(String word) { X.println(word); } })); // NOTE: // // Alternatively, you can use existing closure 'F.println()' to // print any yield result in 'F.yield()' like so: // // G.grid().run(SPREAD, F.yield(phrase.split(" "), F.println())); // X.println(">>>"); X.println( ">>> Finished printing individual words on different nodes based on GridGain 3.0 API."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
public void testStructs() throws Exception { File tmp = new File("tmp"); PersistentMap<X> pm = new PersistentMap<X>(new File(tmp, "simple"), X.class); try { X x = new X(); x.abc = "def"; x.def = 5; x.list.add("abc"); assertNull(pm.put("abc", x)); pm.close(); PersistentMap<X> pm2 = new PersistentMap<X>(new File(tmp, "simple"), X.class); X x2 = pm2.get("abc"); assertEquals("def", x2.abc); assertEquals(5, x2.def); pm2.remove("abc"); assertEquals(0, pm2.size()); pm2.close(); } finally { pm.close(); IO.delete(tmp); } }
/** {@inheritDoc} */ @Override public void printMemoryStats() { super.printMemoryStats(); X.println(">>> threadsSize: " + threads.size()); X.println(">>> futsSize: " + futs.size()); }
/** * Queries all persons and shows their names. * * @param conn JDBC connection. * @throws SQLException In case of SQL error. */ private static void queryAllPersons(Connection conn) throws SQLException { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select name from Person"); X.println(">>> All persons:"); while (rs.next()) X.println(">>> " + rs.getString(1)); }
public void removeElement(X element) { int cont = 0; for (X e : neighbors) { if (e.equals(element) == true) { this.neighbors.remove(cont); return; } else cont++; } }
/** {@inheritDoc} */ @Override public Object onReceive(@Nullable Object obj) { if (obj instanceof byte[]) { X.println(">>> Byte array received over REST: " + Arrays.toString((byte[]) obj)); BigInteger val = new BigInteger((byte[]) obj); X.println(">>> Unpacked a BigInteger from byte array received over REST: " + val); return val; } else return obj; }
/** * Queries persons older than provided age. * * @param conn JDBC connection. * @param minAge Minimum age. * @throws SQLException In case of SQL error. */ private static void queryPersons(Connection conn, int minAge) throws SQLException { PreparedStatement stmt = conn.prepareStatement("select name, age from Person where age >= ?"); stmt.setInt(1, minAge); ResultSet rs = stmt.executeQuery(); X.println(">>> Persons older than " + minAge + ":"); while (rs.next()) X.println(">>> " + rs.getString("NAME") + " (" + rs.getInt("AGE") + " years old)"); }
/** {@inheritDoc} */ @Override public Object onSend(Object obj) { if (obj instanceof BigInteger) { X.println(">>> Creating byte array from BigInteger to send over REST: " + obj); byte[] bytes = ((BigInteger) obj).toByteArray(); X.println( ">>> Created byte array from BigInteger to send over REST: " + Arrays.toString(bytes)); return bytes; } else return obj; }
/** * Executes example. * * @param args Command line arguments, none required. */ public static void main(String[] args) { // Typedefs: // --------- // G -> GridFactory // CI1 -> GridInClosure // CO -> GridOutClosure // CA -> GridAbsClosure // F -> GridFunc // Data initialisation. Random rand = new Random(); final int size = 20; Collection<Integer> nums = new ArrayList<Integer>(size); // Generate list of random integers. for (int i = 0; i < size; i++) { nums.add(rand.nextInt(size)); } // Print generated list. X.println("Generated list:"); F.forEach(nums, F.<Integer>print("", " ")); // Retain all elements which value low than half generated list size. Collection<Integer> res = F.retain( nums, true, new P1<Integer>() { @Override public boolean apply(Integer i) { return i < size / 2; } }); // Print result. X.println("\nResult list:"); F.forEach(res, F.<Integer>print("", " ")); // Retain first half of result list. F.retain(res, false, res.size() / 2); // Print result. X.println("\nResult list:"); F.forEach(res, F.<Integer>print("", " ")); }
/** * Queries persons working in provided organization. * * @param conn JDBC * @param orgName Organization name. * @throws SQLException In case of SQL error. */ private static void queryPersonsInOrganization(Connection conn, String orgName) throws SQLException { PreparedStatement stmt = conn.prepareStatement( "select p.name from Person p, Organization o where p.orgId = o.id and o.name = ?"); stmt.setString(1, orgName); ResultSet rs = stmt.executeQuery(); X.println(">>> Persons working in " + orgName + ":"); while (rs.next()) X.println(">>> " + rs.getString(1)); }
/** * Executes example. * * @param args Command line arguments, none required. */ public static void main(String[] args) { // Typedefs: // --------- // G -> GridFactory // CI1 -> GridInClosure // CO -> GridOutClosure // CA -> GridAbsClosure // F -> GridFunc // Data initialisation. Random rand = new Random(); final int size = 20; Collection<Integer> nums = new ArrayList<Integer>(size); // Generate list of random integers. for (int i = 0; i < size; i++) { nums.add(rand.nextInt(size)); } // Print generated list. X.println("Generated list:"); F.forEach(nums, F.<Integer>print("", " ")); // Get new unmodifiable collection with elements which value low than half generated list size. Collection<Integer> view = F.view( nums, new P1<Integer>() { @Override public boolean apply(Integer i) { return i < size / 2; } }); // Print result. X.println("\nResult list:"); F.forEach(view, F.<Integer>print("", " ")); // Check for read only. try { view.add(12); } catch (Exception ignored) { X.println("\nView is read only."); } }
public X next() { int min = -1; for (int i = 0; i < pending.size(); i++) { X val = pending.get(i); if (min == -1 || (val != null && val.compareTo(pending.get(min)) < 0)) { min = i; } } X minVal = min != -1 ? pending.get(min) : null; if (min != -1) { pending.set(min, itrs.get(min).hasNext() ? itrs.get(min).next() : null); } return minVal; }
/** {@inheritDoc} */ @Override public void printMemoryStats(int threshold) { X.println( ">>> Cache partition topology stats [grid=" + cctx.gridName() + ", cache=" + cctx.name() + ']'); for (GridDhtLocalPartition part : locParts.values()) { int size = part.size(); if (size >= threshold) X.println(">>> Local partition [part=" + part.id() + ", size=" + size + ']'); } }
/** * Prints a phrase on the grid nodes running anonymous callable objects and calculating total * number of letters. * * @param phrase Phrase to print on of the grid nodes. * @throws GridException If failed. */ private static void countLettersCallable(String phrase) throws GridException { X.println(">>> Starting countLettersCallable() example..."); Collection<Callable<Integer>> calls = new HashSet<Callable<Integer>>(); for (final String word : phrase.split(" ")) calls.add( new GridCallable<Integer>() { // Create executable logic. @Override public Integer call() throws Exception { // 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(); } }); // Explicitly execute the collection of callable objects and receive a result. Collection<Integer> results = G.grid().call(SPREAD, calls); // Add up all results using convenience 'sum()' method on GridFunc class. int letterCnt = F.sum(results); X.println(">>>"); X.println( ">>> Finished execution of counting letters with callables 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(">>>"); }
/** * 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(">>>"); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair<?, ?> pair = (Pair<?, ?>) o; if (x != null ? !x.equals(pair.x) : pair.x != null) return false; return !(y != null ? !y.equals(pair.y) : pair.y != null); }
/** * @param log Logger. * @param time Time. * @param msg Message. */ private static void log0(@Nullable IgniteLogger log, long time, String msg) { if (log != null) { if (log.isDebugEnabled()) log.debug(msg); else log.warning(msg); } else X.println( String.format( "[%s][%s]%s", DEBUG_DATE_FMT.get().format(time), Thread.currentThread().getName(), msg)); }
/** * Closes the pipe. No more data will be allowed to be written to this Pipe's WriteHead. Any * blocked {@link DataPipe.Source#readAll()} will return whatever data they can, and other * blocked {@link DataPipe.Source#read()} will immediately return null following this call even * if they cannot get data (these two processes still happen in fair order). All subsequent * reads (blocking or nonblocking) will either return data immediately as long as any is still * buffered in the pipe, and then forevermore immediately return null once all buffered data is * depleted. */ public void close() { lockWrite(); try { $gate.close(); } finally { unlockWrite(); } X.notifyAll($gate); // trigger the return of any final readAll calls // give our listener a chance to notice our closure. invokeListener($el); }
/** * Prints a phrase on one of the grid nodes running anonymous runnable. * * @param phrase Phrase to print on one of the grid nodes. * @throws GridException If failed. */ private static void unicastWordsRunnable(final String phrase) throws GridException { X.println(">>> Starting unicastWordsRunnable() example..."); G.grid() .run( UNICAST, new GridRunnable() { @Override public void run() { X.println(">>> Printing phrase: " + phrase); } }); // NOTE: // // Alternatively, you can use existing closure 'F.println()' to // print any text like so: // // G.grid().run(UNICAST, F.println(">>> Printing phrase: " + phrase)); // X.println(">>>"); X.println(">>> Finished execution of runnable object based on GridGain 3.0 API."); X.println(">>> You should see the phrase '" + phrase + "' printed out on one of the nodes."); X.println(">>> Check all nodes for output (this node is also part of the grid)."); X.println(">>>"); }
/** * Runs JDBC example. * * @param args Command line arguments. * @throws Exception In case of error. */ public static void main(String[] args) throws Exception { Grid grid = G.start("examples/config/spring-cache.xml"); Connection conn = null; try { // Populate cache with data. populate(grid.cache(CACHE_NAME)); // Register JDBC driver. Class.forName("org.gridgain.jdbc.GridJdbcDriver"); // Open JDBC connection. conn = DriverManager.getConnection("jdbc:gridgain://localhost/" + CACHE_NAME, configuration()); X.println(">>>"); // Query all persons. queryAllPersons(conn); X.println(">>>"); // Query person older than 30 years. queryPersons(conn, 30); X.println(">>>"); // Query persons working in GridGain. queryPersonsInOrganization(conn, "GridGain"); X.println(">>>"); } finally { // Close JDBC connection. if (conn != null) conn.close(); G.stop(true); } }
/** * 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(">>>"); }
public Y call(X value) { Class cls = value.getClass(); try { Field f = cls.getField(fieldName); return (Y) f.get(value); } catch (SecurityException e) { e.printStackTrace(); throw new IllegalStateException(); } catch (NoSuchFieldException e) { e.printStackTrace(); throw new IllegalStateException(); } catch (IllegalAccessException e) { e.printStackTrace(); throw new IllegalStateException(); } }
public Y call(X value) { Class cls = value.getClass(); try { Method m = cls.getMethod(fieldName); return (Y) m.invoke(value); } catch (SecurityException e) { e.printStackTrace(); throw new IllegalStateException(); } catch (IllegalAccessException e) { e.printStackTrace(); throw new IllegalStateException(); } catch (InvocationTargetException e) { e.printStackTrace(); throw new IllegalStateException(); } catch (NoSuchMethodException e) { e.printStackTrace(); throw new IllegalStateException(); } }
/** @return {@code True} if succeeded. */ private boolean spreadPartitions() { try { sendAllPartitions(rmtNodes, exchId); return true; } catch (IgniteCheckedException e) { scheduleRecheck(); if (!X.hasCause(e, InterruptedException.class)) U.error( log, "Failed to send full partition map to nodes (will retry after timeout) [nodes=" + F.nodeId8s(rmtNodes) + ", exchangeId=" + exchId + ']', e); return false; } }
/** * Starts up grid and checks all provided values for prime. * * @param args Command line arguments, none required but if provided first one should point to the * Spring XML configuration file. See {@code "examples/config/"} for configuration file * examples. * @throws GridException If example execution failed. */ public static void main(String[] args) throws GridException { // Starts grid. Grid grid = args.length == 0 ? G.start() : G.start(args[0]); // Values we want to check for prime. long[] checkVals = {32452841, 32452843, 32452847, 32452849, 236887699, 217645199}; X.println(">>>"); X.println( ">>> Starting to check the following numbers for primes: " + Arrays.toString(checkVals)); try { long start = System.currentTimeMillis(); for (long checkVal : checkVals) { // This method will be executed on the Grid as it is // annotated with @Gridify annotation. Long divisor = GridPrimeChecker.checkPrime(checkVal, 2, checkVal); // If divisor is null, then the number is prime. if (divisor == null) { X.println("Value '" + checkVal + "'is a prime number."); } else { X.println("Value '" + checkVal + "' is divisible by '" + divisor + '\''); } } long totalTime = System.currentTimeMillis() - start; X.println(">>> Total time to calculate all primes (milliseconds): " + totalTime); X.println(">>>"); } finally { // Stops grid. G.stop(grid.name(), true); } }
/** @throws Exception If failed. */ public void testCompact() throws Exception { File file = new File(UUID.randomUUID().toString()); X.println("file: " + file.getPath()); FileSwapSpaceSpi.SwapFile f = new FileSwapSpaceSpi.SwapFile(file, 8); Random rnd = new Random(); ArrayList<FileSwapSpaceSpi.SwapValue> arr = new ArrayList<>(); int size = 0; for (int a = 0; a < 100; a++) { FileSwapSpaceSpi.SwapValue[] vals = new FileSwapSpaceSpi.SwapValue[1 + rnd.nextInt(10)]; int size0 = 0; for (int i = 0; i < vals.length; i++) { byte[] bytes = new byte[1 + rnd.nextInt(49)]; rnd.nextBytes(bytes); size0 += bytes.length; vals[i] = new FileSwapSpaceSpi.SwapValue(bytes); arr.add(vals[i]); } f.write(new FileSwapSpaceSpi.SwapValues(vals, size0), 1); size += size0; assertEquals(f.length(), size); assertEquals(file.length(), size); } int i = 0; for (FileSwapSpaceSpi.SwapValue val : arr) assertEquals(val.idx(), ++i); i = 0; for (int cnt = arr.size() / 2; i < cnt; i++) { FileSwapSpaceSpi.SwapValue v = arr.remove(rnd.nextInt(arr.size())); assertTrue(f.tryRemove(v.idx(), v)); } int hash0 = 0; for (FileSwapSpaceSpi.SwapValue val : arr) hash0 += Arrays.hashCode(val.readValue(f.readCh)); ArrayList<T2<ByteBuffer, ArrayDeque<FileSwapSpaceSpi.SwapValue>>> bufs = new ArrayList(); for (; ; ) { ArrayDeque<FileSwapSpaceSpi.SwapValue> que = new ArrayDeque<>(); ByteBuffer buf = f.compact(que, 1024); if (buf == null) break; bufs.add(new T2(buf, que)); } f.delete(); int hash1 = 0; for (FileSwapSpaceSpi.SwapValue val : arr) hash1 += Arrays.hashCode(val.value(null)); assertEquals(hash0, hash1); File file0 = new File(UUID.randomUUID().toString()); FileSwapSpaceSpi.SwapFile f0 = new FileSwapSpaceSpi.SwapFile(file0, 8); for (T2<ByteBuffer, ArrayDeque<FileSwapSpaceSpi.SwapValue>> t : bufs) f0.write(t.get2(), t.get1(), 1); int hash2 = 0; for (FileSwapSpaceSpi.SwapValue val : arr) hash2 += Arrays.hashCode(val.readValue(f0.readCh)); assertEquals(hash2, hash1); }
/** {@inheritDoc} */ @Override public ClusterStartNodeResult call() { JSch ssh = new JSch(); Session ses = null; try { if (spec.key() != null) ssh.addIdentity(spec.key().getAbsolutePath()); ses = ssh.getSession(spec.username(), spec.host(), spec.port()); if (spec.password() != null) ses.setPassword(spec.password()); ses.setConfig("StrictHostKeyChecking", "no"); ses.connect(timeout); boolean win = isWindows(ses); char separator = win ? '\\' : '/'; spec.fixPaths(separator); String igniteHome = spec.igniteHome(); if (igniteHome == null) igniteHome = win ? DFLT_IGNITE_HOME_WIN : DFLT_IGNITE_HOME_LINUX; String script = spec.script(); if (script == null) script = DFLT_SCRIPT_LINUX; String cfg = spec.configuration(); if (cfg == null) cfg = ""; String startNodeCmd; String scriptOutputFileName = FILE_NAME_DATE_FORMAT.format(new Date()) + '-' + UUID.randomUUID().toString().substring(0, 8) + ".log"; if (win) throw new UnsupportedOperationException( "Apache Ignite cannot be auto-started on Windows from IgniteCluster.startNodes(…) API."); else { // Assume Unix. int spaceIdx = script.indexOf(' '); String scriptPath = spaceIdx > -1 ? script.substring(0, spaceIdx) : script; String scriptArgs = spaceIdx > -1 ? script.substring(spaceIdx + 1) : ""; String rmtLogArgs = buildRemoteLogArguments(spec.username(), spec.host()); String tmpDir = env(ses, "$TMPDIR", "/tmp/"); String scriptOutputDir = tmpDir + "ignite-startNodes"; shell(ses, "mkdir " + scriptOutputDir); // Mac os don't support ~ in double quotes. Trying get home path from remote system. if (igniteHome.startsWith("~")) { String homeDir = env(ses, "$HOME", "~"); igniteHome = igniteHome.replaceFirst("~", homeDir); } startNodeCmd = new SB() . // Console output is consumed, started nodes must use Ignite file appenders for log. a("nohup ") .a("\"") .a(igniteHome) .a('/') .a(scriptPath) .a("\"") .a(" ") .a(scriptArgs) .a(!cfg.isEmpty() ? " \"" : "") .a(cfg) .a(!cfg.isEmpty() ? "\"" : "") .a(rmtLogArgs) .a(" > ") .a(scriptOutputDir) .a("/") .a(scriptOutputFileName) .a(" 2>& 1 &") .toString(); } info("Starting remote node with SSH command: " + startNodeCmd, spec.logger(), log); shell(ses, startNodeCmd); return new ClusterStartNodeResultImpl(spec.host(), true, null); } catch (IgniteInterruptedCheckedException e) { return new ClusterStartNodeResultImpl(spec.host(), false, e.getMessage()); } catch (Exception e) { return new ClusterStartNodeResultImpl(spec.host(), false, X.getFullStackTrace(e)); } finally { if (ses != null && ses.isConnected()) ses.disconnect(); } }
/** {@inheritDoc} */ @Override public void printMemoryStats() { X.println(">>>"); X.println(">>> Task session processor memory stats [grid=" + ctx.gridName() + ']'); X.println(">>> sesMapSize: " + sesMap.size()); }
public boolean contains(int index) { for (X e : neighbors) { if (e.intValue() == index) return true; } return false; }