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);
}
}
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);
}
}
Aggregations