Search in sources :

Example 76 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class JdbcSpringSelfTest method testSpringBean.

/**
     * Test that we have valid Spring context and also could create beans from it.
     *
     * @throws Exception If test failed.
     */
public void testSpringBean() throws Exception {
    String url = CFG_URL_PREFIX + configURL();
    // Create connection.
    try (Connection conn = DriverManager.getConnection(url)) {
        assertNotNull(conn);
        TestInjectTarget target = new TestInjectTarget();
        IgniteKernal kernal = (IgniteKernal) ((JdbcConnection) conn).ignite();
        // Inject Spring context to test object.
        kernal.context().resource().inject(target, GridResourceIoc.AnnotationSet.GENERIC);
        assertNotNull(target.appCtx);
        IgniteSpringHelper spring = IgniteComponentType.SPRING.create(false);
        // Load bean by name.
        DataSource ds = spring.loadBeanFromAppContext(target.appCtx, "dsTest");
        assertNotNull(ds);
    }
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) Connection(java.sql.Connection) IgniteSpringHelper(org.apache.ignite.internal.util.spring.IgniteSpringHelper) DataSource(javax.sql.DataSource)

Example 77 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class JdbcQueryTask method call.

/** {@inheritDoc} */
@Override
public JdbcQueryTask.QueryResult call() throws Exception {
    Cursor cursor = CURSORS.get(uuid);
    List<String> tbls = null;
    List<String> cols = null;
    List<String> types = null;
    boolean first;
    if (first = (cursor == null)) {
        IgniteCache<?, ?> cache = ignite.cache(cacheName);
        // Don't create caches on server nodes in order to avoid of data rebalancing.
        boolean start = ignite.configuration().isClientMode();
        if (cache == null && cacheName == null)
            cache = ((IgniteKernal) ignite).context().cache().getOrStartPublicCache(start, !loc && locQry);
        if (cache == null) {
            if (cacheName == null)
                throw new SQLException("Failed to execute query. No suitable caches found.");
            else
                throw new SQLException("Cache not found [cacheName=" + cacheName + ']');
        }
        SqlFieldsQuery qry = (isQry != null ? new JdbcSqlFieldsQuery(sql, isQry) : new SqlFieldsQuery(sql)).setArgs(args);
        qry.setPageSize(fetchSize);
        qry.setLocal(locQry);
        qry.setCollocated(collocatedQry);
        qry.setDistributedJoins(distributedJoins);
        qry.setSchema(schemaName);
        QueryCursorImpl<List<?>> qryCursor = (QueryCursorImpl<List<?>>) cache.withKeepBinary().query(qry);
        if (isQry == null)
            isQry = qryCursor.isQuery();
        Collection<GridQueryFieldMetadata> meta = qryCursor.fieldsMeta();
        tbls = new ArrayList<>(meta.size());
        cols = new ArrayList<>(meta.size());
        types = new ArrayList<>(meta.size());
        for (GridQueryFieldMetadata desc : meta) {
            tbls.add(desc.typeName());
            cols.add(desc.fieldName().toUpperCase());
            types.add(desc.fieldTypeName());
        }
        CURSORS.put(uuid, cursor = new Cursor(qryCursor, qryCursor.iterator()));
    }
    List<List<?>> rows = new ArrayList<>();
    for (List<?> row : cursor) {
        List<Object> row0 = new ArrayList<>(row.size());
        for (Object val : row) row0.add(val == null || JdbcUtils.isSqlType(val.getClass()) ? val : val.toString());
        rows.add(row0);
        if (// If fetchSize is 0 then unlimited
        rows.size() == fetchSize)
            break;
    }
    boolean finished = !cursor.hasNext();
    if (finished)
        remove(uuid, cursor);
    else if (first) {
        if (!loc)
            scheduleRemoval(uuid, RMV_DELAY);
    } else if (!loc && !CURSORS.replace(uuid, cursor, new Cursor(cursor.cursor, cursor.iter)))
        assert !CURSORS.containsKey(uuid) : "Concurrent cursor modification.";
    assert isQry != null : "Query flag must be set prior to returning result";
    return new QueryResult(uuid, finished, isQry, rows, cols, tbls, types);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) GridQueryFieldMetadata(org.apache.ignite.internal.processors.query.GridQueryFieldMetadata) QueryCursorImpl(org.apache.ignite.internal.processors.cache.QueryCursorImpl) QueryCursor(org.apache.ignite.cache.query.QueryCursor) SqlFieldsQuery(org.apache.ignite.cache.query.SqlFieldsQuery) ArrayList(java.util.ArrayList) List(java.util.List)

Example 78 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class CacheDefaultBinaryAffinityKeyMapper method affinityKey.

/** {@inheritDoc} */
@Override
public Object affinityKey(Object key) {
    IgniteKernal kernal = (IgniteKernal) ignite;
    CacheObjectBinaryProcessorImpl proc = (CacheObjectBinaryProcessorImpl) kernal.context().cacheObjects();
    try {
        key = proc.toBinary(key);
    } catch (IgniteException e) {
        U.error(log, "Failed to marshal key to binary: " + key, e);
    }
    if (key instanceof BinaryObject)
        return proc.affinityKey((BinaryObject) key);
    else
        return super.affinityKey(key);
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) BinaryObject(org.apache.ignite.binary.BinaryObject) IgniteException(org.apache.ignite.IgniteException)

Example 79 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class GridContinuousOperationsLoadTest method main.

/**
     * Main method.
     *
     * @param args Command line arguments.
     * @throws Exception If error occurs.
     */
public static void main(String[] args) throws Exception {
    final String cfgPath = args.length > 0 ? args[0] : "examples/config/example-cache.xml";
    final String cacheName = getStringProperty(CACHE_NAME, "partitioned");
    final Integer valSize = getIntProperty(VALUE_SIZE, 1024);
    final Integer threadsCnt = getIntProperty(THREADS_CNT, 8);
    final Integer testDurSec = getIntProperty(TEST_DUR_SEC, 180);
    final Integer filterSkipProb = getIntProperty("FILTER_SKIP_PROBABILITY", 10, new C1<Integer, String>() {

        @Nullable
        @Override
        public String apply(Integer val) {
            if (val < 0 || val > 100)
                return "The value should be between 1 and 100.";
            return null;
        }
    });
    final boolean useQry = getBooleanProperty("IGNITE_USE_QUERIES", true);
    final int bufSize = getIntProperty("IGNITE_BUFFER_SIZE", 1);
    final long timeInterval = getLongProperty("IGNITE_TIME_INTERVAL", 0);
    final int parallelCnt = getIntProperty("IGNITE_PARALLEL_COUNT", 8);
    final int keyRange = getIntProperty("IGNITE_KEY_RANGE", 100000);
    final long updSleepMs = getLongProperty("IGNITE_UPDATE_SLEEP_MS", 0);
    final long filterSleepMs = getLongProperty("IGNITE_FILTER_SLEEP_MS", 0);
    final long cbSleepMs = getLongProperty("IGNITE_CALLBACK_SLEEP_MS", 0);
    X.println("The test will start with the following parameters:");
    dumpProperties(System.out);
    try (Ignite ignite = Ignition.start(cfgPath)) {
        final IgniteCache<Object, Object> cache = ignite.cache(cacheName);
        if (cache == null)
            throw new IgniteCheckedException("Cache is not configured: " + cacheName);
        // Continuous query manager, used to monitor queue size.
        final CacheContinuousQueryManager contQryMgr = ((IgniteKernal) ignite).context().cache().cache(cacheName).context().continuousQueries();
        if (contQryMgr == null)
            throw new IgniteCheckedException("Could not access CacheContinuousQueryManager");
        // Stop flag.
        final AtomicBoolean stop = new AtomicBoolean();
        // Callback counter.
        final AtomicLong cbCntr = new AtomicLong();
        // Update counter.
        final AtomicLong updCntr = new AtomicLong();
        for (int i = 0; i < parallelCnt; i++) {
            if (useQry) {
                ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
                qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

                    @Override
                    public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                        if (cbSleepMs > 0) {
                            try {
                                U.sleep(cbSleepMs);
                            } catch (IgniteInterruptedCheckedException e) {
                                throw new IgniteException(e);
                            }
                        }
                        for (CacheEntryEvent<?, ?> ignored : evts) cbCntr.incrementAndGet();
                    }
                });
                qry.setRemoteFilter(new CacheEntryEventSerializableFilter<Object, Object>() {

                    @Override
                    public boolean evaluate(CacheEntryEvent<?, ?> evt) {
                        if (filterSleepMs > 0) {
                            try {
                                U.sleep(filterSleepMs);
                            } catch (IgniteInterruptedCheckedException e) {
                                throw new IgniteException(e);
                            }
                        }
                        return Math.random() * 100 >= filterSkipProb;
                    }
                });
                qry.setPageSize(bufSize);
                qry.setTimeInterval(timeInterval);
                cache.query(qry);
            } else {
                ignite.events().remoteListen(bufSize, timeInterval, true, new PX2<UUID, Event>() {

                    @Override
                    public boolean applyx(UUID uuid, Event evt) throws IgniteInterruptedCheckedException {
                        if (cbSleepMs > 0)
                            U.sleep(cbSleepMs);
                        cbCntr.incrementAndGet();
                        // Continue listening.
                        return true;
                    }
                }, new PX1<Event>() {

                    @Override
                    public boolean applyx(Event evt) throws IgniteInterruptedCheckedException {
                        if (filterSleepMs > 0)
                            U.sleep(filterSleepMs);
                        return Math.random() * 100 >= filterSkipProb;
                    }
                }, EVT_CACHE_OBJECT_PUT);
            }
        }
        // Start collector thread.
        startDaemon(new Runnable() {

            @Override
            public void run() {
                try {
                    while (!stop.get() && !Thread.currentThread().isInterrupted()) {
                        long cbCntr0 = cbCntr.get();
                        long updCntr0 = updCntr.get();
                        U.sleep(1000);
                        long cbDelta = cbCntr.get() - cbCntr0;
                        long updDelta = updCntr.get() - updCntr0;
                        X.println("Stats [entriesPerSec=" + cbDelta + ", updatesPerSec=" + updDelta + ']');
                    }
                } catch (IgniteInterruptedCheckedException ignored) {
                // No-op.
                }
            }
        });
        X.println("Starting " + threadsCnt + " generator thread(s).");
        // Start generator threads.
        IgniteInternalFuture<Long> genFut = runMultiThreadedAsync(new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                byte[] val = new byte[valSize];
                ThreadLocalRandom8 rnd = ThreadLocalRandom8.current();
                while (!stop.get() && !Thread.currentThread().isInterrupted()) {
                    Integer key = rnd.nextInt(keyRange);
                    cache.put(key, val);
                    updCntr.incrementAndGet();
                    if (updSleepMs > 0)
                        U.sleep(updSleepMs);
                }
                return true;
            }
        }, threadsCnt, "load-test-generator");
        U.sleep(testDurSec * 1000);
        stop.set(true);
        genFut.get();
    }
}
Also used : CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheContinuousQueryManager(org.apache.ignite.internal.processors.cache.query.continuous.CacheContinuousQueryManager) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteException(org.apache.ignite.IgniteException) Ignite(org.apache.ignite.Ignite) UUID(java.util.UUID) IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) ThreadLocalRandom8(org.jsr166.ThreadLocalRandom8) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Event(org.apache.ignite.events.Event) CacheEntryEvent(javax.cache.event.CacheEntryEvent) Nullable(org.jetbrains.annotations.Nullable)

Example 80 with IgniteKernal

use of org.apache.ignite.internal.IgniteKernal in project ignite by apache.

the class GridIoManagerBenchmark0 method testThroughput.

/**
     * @throws Exception If failed.
     */
@SuppressWarnings("deprecation")
public void testThroughput() throws Exception {
    final IgniteKernal sndKernal = (IgniteKernal) grid(0);
    final IgniteKernal rcvKernal = (IgniteKernal) grid(1);
    final ClusterNode sndNode = sndKernal.localNode();
    final ClusterNode rcvNode = rcvKernal.localNode();
    final GridIoManager snd = sndKernal.context().io();
    final GridIoManager rcv = rcvKernal.context().io();
    info("Senders: " + THREADS);
    info("Messages: " + CONCUR_MSGS);
    final Semaphore sem = new Semaphore(CONCUR_MSGS);
    final LongAdder8 msgCntr = new LongAdder8();
    final String topic = "test-topic";
    rcv.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            try {
                rcv.sendToCustomTopic(sndNode, topic, (Message) msg, PUBLIC_POOL);
            } catch (IgniteCheckedException e) {
                error("Failed to send message.", e);
            }
        }
    });
    snd.addMessageListener(topic, new GridMessageListener() {

        @Override
        public void onMessage(UUID nodeId, Object msg) {
            msgCntr.increment();
            sem.release();
        }
    });
    Timer t = new Timer("results-reporter");
    t.schedule(new TimerTask() {

        private long ts = System.currentTimeMillis();

        @Override
        public void run() {
            long newTs = System.currentTimeMillis();
            long qrys = msgCntr.sumThenReset();
            long time = newTs - ts;
            X.println("Communication benchmark [qps=" + qrys * 1000 / time + ", executed=" + qrys + ", time=" + time + ']');
            ts = newTs;
        }
    }, 10000, 10000);
    final AtomicBoolean finish = new AtomicBoolean();
    IgniteInternalFuture<?> f = GridTestUtils.runMultiThreadedAsync(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            try {
                IgniteUuid msgId = IgniteUuid.randomUuid();
                while (!finish.get()) {
                    sem.acquire();
                    snd.sendToCustomTopic(rcvNode, topic, new GridTestMessage(msgId, (String) null), PUBLIC_POOL);
                }
            } catch (IgniteCheckedException e) {
                X.println("Message send failed", e);
            } catch (InterruptedException ignored) {
            // No-op.
            }
            return null;
        }
    }, THREADS, "send-thread");
    Thread.sleep(TEST_TIMEOUT);
    finish.set(true);
    sem.release(CONCUR_MSGS * 2);
    t.cancel();
    f.get();
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteKernal(org.apache.ignite.internal.IgniteKernal) Message(org.apache.ignite.plugin.extensions.communication.Message) GridMessageListener(org.apache.ignite.internal.managers.communication.GridMessageListener) Semaphore(java.util.concurrent.Semaphore) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) GridIoManager(org.apache.ignite.internal.managers.communication.GridIoManager) IgniteUuid(org.apache.ignite.lang.IgniteUuid) UUID(java.util.UUID) LongAdder8(org.jsr166.LongAdder8)

Aggregations

IgniteKernal (org.apache.ignite.internal.IgniteKernal)164 Ignite (org.apache.ignite.Ignite)72 ClusterNode (org.apache.ignite.cluster.ClusterNode)30 Transaction (org.apache.ignite.transactions.Transaction)29 ArrayList (java.util.ArrayList)27 Map (java.util.Map)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)24 IgniteCache (org.apache.ignite.IgniteCache)24 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)22 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)22 UUID (java.util.UUID)21 IgniteException (org.apache.ignite.IgniteException)21 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)18 IgniteEx (org.apache.ignite.internal.IgniteEx)14 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)14 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)14 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)13 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)13 CacheException (javax.cache.CacheException)12 Collection (java.util.Collection)10