Search in sources :

Example 71 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class GridSessionSetFutureAttributeSelfTest method checkTask.

/**
 * @param num Number.
 * @throws InterruptedException if failed.
 * @throws IgniteCheckedException if failed.
 */
private void checkTask(int num) throws InterruptedException, IgniteCheckedException {
    Ignite ignite = G.ignite(getTestIgniteInstanceName());
    ComputeTaskFuture<?> fut = executeAsync(ignite.compute(), GridTaskSessionTestTask.class, num);
    assert fut != null;
    try {
        // Wait until jobs begin execution.
        boolean await = startSignal[num].await(WAIT_TIME, TimeUnit.MILLISECONDS);
        assert await : "Jobs did not start.";
        info("Set attribute 'testName'.");
        fut.getTaskSession().setAttribute("testName", "testVal");
        Object res = fut.get();
        assert (Integer) res == SPLIT_COUNT : "Invalid result [num=" + num + ", fut=" + fut + ']';
    } finally {
        // We must wait for the jobs to be sure that they have completed
        // their execution since they use static variable (shared for the tests).
        fut.get();
    }
}
Also used : Ignite(org.apache.ignite.Ignite)

Example 72 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class AgentClusterDemo method tryStart.

/**
 * Start ignite node with cacheEmployee and populate it with data.
 */
public static CountDownLatch tryStart() {
    if (initGuard.compareAndSet(false, true)) {
        log.info("DEMO: Starting embedded nodes for demo...");
        System.setProperty(IGNITE_NO_ASCII, "true");
        System.setProperty(IGNITE_QUIET, "false");
        System.setProperty(IGNITE_UPDATE_NOTIFIER, "false");
        System.setProperty(IGNITE_ATOMIC_CACHE_DELETE_HISTORY_SIZE, "20");
        System.setProperty(IGNITE_PERFORMANCE_SUGGESTIONS_DISABLED, "true");
        final AtomicInteger basePort = new AtomicInteger(60700);
        final AtomicInteger cnt = new AtomicInteger(-1);
        final ScheduledExecutorService execSrv = newScheduledThreadPool(1, "demo-nodes-start");
        execSrv.scheduleAtFixedRate(new Runnable() {

            @Override
            public void run() {
                int idx = cnt.incrementAndGet();
                int port = basePort.get();
                boolean first = idx == 0;
                try {
                    IgniteConfiguration cfg = igniteConfiguration(port, idx, false);
                    if (first) {
                        U.delete(Paths.get(cfg.getWorkDirectory()));
                        U.resolveWorkDirectory(cfg.getWorkDirectory(), cfg.getDataStorageConfiguration().getStoragePath(), true);
                    }
                    Ignite ignite = Ignition.start(cfg);
                    if (first) {
                        ClusterNode node = ignite.cluster().localNode();
                        Collection<String> jettyAddrs = node.attribute(ATTR_REST_JETTY_ADDRS);
                        if (jettyAddrs == null) {
                            Ignition.stopAll(true);
                            throw new IgniteException("DEMO: Failed to start Jetty REST server on embedded node");
                        }
                        String jettyHost = jettyAddrs.iterator().next();
                        Integer jettyPort = node.attribute(ATTR_REST_JETTY_PORT);
                        if (F.isEmpty(jettyHost) || jettyPort == null)
                            throw new IgniteException("DEMO: Failed to start Jetty REST handler on embedded node");
                        log.info("DEMO: Started embedded node for demo purpose [TCP binary port={}, Jetty REST port={}]", port, jettyPort);
                        demoUrl = String.format("http://%s:%d", jettyHost, jettyPort);
                        initLatch.countDown();
                    }
                } catch (Throwable e) {
                    if (first) {
                        basePort.getAndAdd(50);
                        log.warn("DEMO: Failed to start embedded node.", e);
                    } else
                        log.error("DEMO: Failed to start embedded node.", e);
                } finally {
                    if (idx == NODE_CNT) {
                        Ignite ignite = Ignition.ignite(SRV_NODE_NAME + 0);
                        if (ignite != null) {
                            ignite.cluster().active(true);
                            deployServices(ignite.services(ignite.cluster().forServers()));
                        }
                        log.info("DEMO: All embedded nodes for demo successfully started");
                        execSrv.shutdown();
                    }
                }
            }
        }, 1, 5, TimeUnit.SECONDS);
    }
    return initLatch;
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite)

Example 73 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class TestJtaTxServlet method doGet.

/**
 * {@inheritDoc}
 */
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse res) throws ServletException, IOException {
    final int key1 = 1;
    final int key2 = 2;
    final String correctVal1 = "correct_val1";
    final String correctVal2 = "correct_val1";
    final String incorrectVal1 = "incorrect_val2";
    final String incorrectVal2 = "incorrect_val2";
    final PrintWriter writer = res.getWriter();
    try {
        final Ignite ignite = Ignition.ignite();
        final IgniteCache<Integer, String> cache = ignite.cache("tx");
        TransactionManager tmMgr = TransactionManagerFactory.getTransactionManager();
        tmMgr.begin();
        cache.put(key1, correctVal1);
        cache.put(key2, correctVal2);
        writer.println("Transaction #1. Put values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
        writer.println();
        tmMgr.commit();
        try {
            tmMgr.begin();
            writer.println("Transaction #2. Current values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
            cache.put(key1, incorrectVal1);
            cache.put(key2, incorrectVal2);
            writer.println("Transaction #2. Put values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
            tmMgr.setRollbackOnly();
            tmMgr.commit();
        } catch (final RollbackException ignored) {
            writer.println("Transaction #2. setRollbackOnly [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
        }
        writer.println();
        tmMgr.begin();
        writer.println("Transaction #2. Current values [key1=" + key1 + ", val1=" + cache.get(key1) + ", key2=" + key2 + ", val2=" + cache.get(key2) + "]");
        tmMgr.commit();
    } catch (final Throwable e) {
        e.printStackTrace(writer);
    }
}
Also used : TransactionManager(javax.transaction.TransactionManager) Ignite(org.apache.ignite.Ignite) RollbackException(javax.transaction.RollbackException) PrintWriter(java.io.PrintWriter)

Example 74 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class ServletContextListenerStartup method contextInitialized.

/**
 * {@inheritDoc}
 */
@Override
public void contextInitialized(ServletContextEvent evt) {
    ServletContext ctx = evt.getServletContext();
    String cfgFile = ctx.getInitParameter(IGNITE_CFG_FILE_PATH_PARAM);
    Collection<IgniteConfiguration> cfgs;
    GridSpringResourceContext rsrcCtx = null;
    if (cfgFile != null) {
        URL cfgUrl = null;
        try {
            cfgUrl = evt.getServletContext().getResource("/META-INF/" + cfgFile);
        } catch (MalformedURLException ignored) {
        // Ignore, we still need to try with IGNITE_HOME.
        }
        if (cfgUrl == null)
            // Try with IGNITE_HOME and with context class loader.
            cfgUrl = U.resolveIgniteUrl(cfgFile);
        if (cfgUrl == null)
            throw new IgniteException("Failed to find Spring configuration file (path provided should be " + "either absolute, relative to IGNITE_HOME, or relative to META-INF folder): " + cfgFile);
        IgniteBiTuple<Collection<IgniteConfiguration>, ? extends GridSpringResourceContext> t;
        try {
            t = IgnitionEx.loadConfigurations(cfgUrl);
        } catch (IgniteCheckedException e) {
            throw new IgniteException("Failed to load Ignite configuration.", e);
        }
        cfgs = t.get1();
        rsrcCtx = t.get2();
        if (cfgs.isEmpty())
            throw new IgniteException("Can't find grid factory configuration in: " + cfgUrl);
    } else
        cfgs = Collections.<IgniteConfiguration>singleton(new IgniteConfiguration());
    try {
        assert !cfgs.isEmpty();
        for (IgniteConfiguration cfg : cfgs) {
            assert cfg != null;
            Ignite ignite;
            synchronized (ServletContextListenerStartup.class) {
                try {
                    ignite = G.ignite(cfg.getIgniteInstanceName());
                } catch (IgniteIllegalStateException ignored) {
                    ignite = IgnitionEx.start(new IgniteConfiguration(cfg), rsrcCtx);
                }
            }
            // Check if grid is not null - started properly.
            if (ignite != null)
                igniteInstanceNames.add(ignite.name());
        }
    } catch (IgniteCheckedException e) {
        // Stop started grids only.
        for (String name : igniteInstanceNames) G.stop(name, true);
        throw new IgniteException("Failed to start Ignite.", e);
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) IgniteIllegalStateException(org.apache.ignite.IgniteIllegalStateException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) IgniteException(org.apache.ignite.IgniteException) GridSpringResourceContext(org.apache.ignite.internal.processors.resource.GridSpringResourceContext) ServletContext(javax.servlet.ServletContext) Collection(java.util.Collection) Ignite(org.apache.ignite.Ignite)

Example 75 with Ignite

use of org.apache.ignite.Ignite in project ignite by apache.

the class GridSessionTaskWaitJobAttributeSelfTest method checkTask.

/**
 * @param num Number.
 */
private void checkTask(int num) {
    Ignite ignite = G.ignite(getTestIgniteInstanceName());
    ComputeTaskFuture<?> fut = ignite.compute().executeAsync(GridTaskSessionTestTask.class.getName(), num);
    Object res = fut.get();
    assert (Integer) res == SPLIT_COUNT : "Invalid result [num=" + num + ", fut=" + fut + ']';
}
Also used : Ignite(org.apache.ignite.Ignite)

Aggregations

Ignite (org.apache.ignite.Ignite)2007 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)358 CountDownLatch (java.util.concurrent.CountDownLatch)238 IgniteCache (org.apache.ignite.IgniteCache)234 IgniteException (org.apache.ignite.IgniteException)216 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)215 Transaction (org.apache.ignite.transactions.Transaction)194 ArrayList (java.util.ArrayList)177 ClusterNode (org.apache.ignite.cluster.ClusterNode)152 UUID (java.util.UUID)137 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)135 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)128 CacheException (javax.cache.CacheException)112 Event (org.apache.ignite.events.Event)112 HashMap (java.util.HashMap)105 List (java.util.List)89 IgniteEx (org.apache.ignite.internal.IgniteEx)85 Map (java.util.Map)84 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)81 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)78