Search in sources :

Example 1 with IgniteThreadFactory

use of org.apache.ignite.thread.IgniteThreadFactory in project ignite by apache.

the class CassandraCacheStore method loadCache.

/** {@inheritDoc} */
@Override
public void loadCache(IgniteBiInClosure<K, V> clo, Object... args) throws CacheLoaderException {
    if (clo == null)
        return;
    if (args == null || args.length == 0)
        args = new String[] { "select * from " + controller.getPersistenceSettings().getKeyspace() + "." + cassandraTable() + ";" };
    ExecutorService pool = null;
    Collection<Future<?>> futs = new ArrayList<>(args.length);
    try {
        pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
        CassandraSession ses = getCassandraSession();
        for (Object obj : args) {
            LoadCacheCustomQueryWorker<K, V> task = null;
            if (obj instanceof Statement)
                task = new LoadCacheCustomQueryWorker<>(ses, (Statement) obj, controller, log, clo);
            else if (obj instanceof String) {
                String qry = ((String) obj).trim();
                if (qry.toLowerCase().startsWith("select"))
                    task = new LoadCacheCustomQueryWorker<>(ses, (String) obj, controller, log, clo);
            }
            if (task != null)
                futs.add(pool.submit(task));
        }
        for (Future<?> fut : futs) U.get(fut);
        if (log != null && log.isDebugEnabled() && storeSes != null)
            log.debug("Cache loaded from db: " + storeSes.cacheName());
    } catch (IgniteCheckedException e) {
        if (storeSes != null)
            throw new CacheLoaderException("Failed to load Ignite cache: " + storeSes.cacheName(), e.getCause());
        else
            throw new CacheLoaderException("Failed to load cache", e.getCause());
    } finally {
        U.shutdownNow(getClass(), pool, log);
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) ArrayList(java.util.ArrayList) CassandraSession(org.apache.ignite.cache.store.cassandra.session.CassandraSession) IgniteThreadFactory(org.apache.ignite.thread.IgniteThreadFactory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LoadCacheCustomQueryWorker(org.apache.ignite.cache.store.cassandra.session.LoadCacheCustomQueryWorker) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 2 with IgniteThreadFactory

use of org.apache.ignite.thread.IgniteThreadFactory in project ignite by apache.

the class CacheAbstractJdbcStore method loadCache.

/** {@inheritDoc} */
@Override
public void loadCache(final IgniteBiInClosure<K, V> clo, @Nullable Object... args) throws CacheLoaderException {
    ExecutorService pool = null;
    String cacheName = session().cacheName();
    try {
        pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
        Collection<Future<?>> futs = new ArrayList<>();
        Map<Object, EntryMapping> mappings = getOrCreateCacheMappings(cacheName);
        if (args != null && args.length > 0) {
            if (args.length % 2 != 0)
                throw new CacheLoaderException("Expected even number of arguments, but found: " + args.length);
            if (log.isDebugEnabled())
                log.debug("Start loading entries from db using user queries from arguments...");
            for (int i = 0; i < args.length; i += 2) {
                final String keyType = args[i].toString();
                if (!F.exist(mappings.values(), new IgnitePredicate<EntryMapping>() {

                    @Override
                    public boolean apply(EntryMapping em) {
                        return em.keyType().equals(keyType);
                    }
                }))
                    throw new CacheLoaderException("Provided key type is not found in store or cache configuration " + "[cache=" + U.maskName(cacheName) + ", key=" + keyType + ']');
                EntryMapping em = entryMapping(cacheName, typeIdForTypeName(kindForName(keyType), keyType));
                Object arg = args[i + 1];
                LoadCacheCustomQueryWorker<K, V> task;
                if (arg instanceof PreparedStatement) {
                    PreparedStatement stmt = (PreparedStatement) arg;
                    if (log.isInfoEnabled())
                        log.info("Started load cache using custom statement [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", stmt=" + stmt + ']');
                    task = new LoadCacheCustomQueryWorker<>(em, stmt, clo);
                } else {
                    String qry = arg.toString();
                    if (log.isInfoEnabled())
                        log.info("Started load cache using custom query [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ", query=" + qry + ']');
                    task = new LoadCacheCustomQueryWorker<>(em, qry, clo);
                }
                futs.add(pool.submit(task));
            }
        } else {
            Collection<String> processedKeyTypes = new HashSet<>();
            for (EntryMapping em : mappings.values()) {
                String keyType = em.keyType();
                if (processedKeyTypes.contains(keyType))
                    continue;
                processedKeyTypes.add(keyType);
                if (log.isInfoEnabled())
                    log.info("Started load cache [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
                if (parallelLoadCacheMinThreshold > 0) {
                    Connection conn = null;
                    try {
                        conn = connection();
                        PreparedStatement stmt = conn.prepareStatement(em.loadCacheSelRangeQry);
                        stmt.setInt(1, parallelLoadCacheMinThreshold);
                        ResultSet rs = stmt.executeQuery();
                        if (rs.next()) {
                            if (log.isDebugEnabled())
                                log.debug("Multithread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
                            int keyCnt = em.keyCols.size();
                            Object[] upperBound = new Object[keyCnt];
                            for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
                            futs.add(pool.submit(loadCacheRange(em, clo, null, upperBound, 0)));
                            while (rs.next()) {
                                Object[] lowerBound = upperBound;
                                upperBound = new Object[keyCnt];
                                for (int i = 0; i < keyCnt; i++) upperBound[i] = rs.getObject(i + 1);
                                futs.add(pool.submit(loadCacheRange(em, clo, lowerBound, upperBound, 0)));
                            }
                            futs.add(pool.submit(loadCacheRange(em, clo, upperBound, null, 0)));
                            continue;
                        }
                    } catch (SQLException e) {
                        log.warning("Failed to load entries from db in multithreaded mode, will try in single thread " + "[cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']', e);
                    } finally {
                        U.closeQuiet(conn);
                    }
                }
                if (log.isDebugEnabled())
                    log.debug("Single thread loading entries from db [cache=" + U.maskName(cacheName) + ", keyType=" + keyType + ']');
                futs.add(pool.submit(loadCacheFull(em, clo)));
            }
        }
        for (Future<?> fut : futs) U.get(fut);
        if (log.isInfoEnabled())
            log.info("Finished load cache: " + U.maskName(cacheName));
    } catch (IgniteCheckedException e) {
        throw new CacheLoaderException("Failed to load cache: " + U.maskName(cacheName), e.getCause());
    } finally {
        U.shutdownNow(getClass(), pool, log);
    }
}
Also used : SQLException(java.sql.SQLException) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ArrayList(java.util.ArrayList) IgniteThreadFactory(org.apache.ignite.thread.IgniteThreadFactory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) CacheLoaderException(javax.cache.integration.CacheLoaderException) ResultSet(java.sql.ResultSet) HashSet(java.util.HashSet) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Aggregations

ArrayList (java.util.ArrayList)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 CacheLoaderException (javax.cache.integration.CacheLoaderException)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteThreadFactory (org.apache.ignite.thread.IgniteThreadFactory)2 BoundStatement (com.datastax.driver.core.BoundStatement)1 PreparedStatement (com.datastax.driver.core.PreparedStatement)1 Statement (com.datastax.driver.core.Statement)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashSet (java.util.HashSet)1 CassandraSession (org.apache.ignite.cache.store.cassandra.session.CassandraSession)1 LoadCacheCustomQueryWorker (org.apache.ignite.cache.store.cassandra.session.LoadCacheCustomQueryWorker)1 IgnitePredicate (org.apache.ignite.lang.IgnitePredicate)1