Examples of ExecutorAllCompletionService


Examples of org.infinispan.executors.ExecutorAllCompletionService

      }
   }

   @Override
   public void purge(Executor threadPool, final PurgeListener listener) {
      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(threadPool);
      EntityManager em = emf.createEntityManager();
      try {
         CriteriaBuilder cb = em.getCriteriaBuilder();
         CriteriaQuery<MetadataEntity> cq = cb.createQuery(MetadataEntity.class);

         Root root = cq.from(MetadataEntity.class);
         long currentTime = timeService.wallClockTime();
         cq.where(cb.le(root.get(MetadataEntity.EXPIRATION), currentTime));

         for (MetadataEntity metadata : em.createQuery(cq).getResultList()) {
            EntityTransaction txn = em.getTransaction();
            final Object key;
            try {
               key = marshaller.objectFromByteBuffer(metadata.name);
            } catch (Exception e) {
               throw new JpaStoreException("Cannot unmarshall key", e);
            }
            long txnBegin = timeService.time();
            txn.begin();
            try {
               long metadataFindBegin = timeService.time();
               metadata = em.find(MetadataEntity.class, metadata.name);
               stats.addMetadataFind(timeService.time() - metadataFindBegin);
               // check for transaction - I hope write skew check is done here
               if (metadata.expiration > currentTime) {
                  txn.rollback();
                  continue;
               }
               long entityFindBegin = timeService.time();
               Object entity = em.find(configuration.entityClass(), key);
               stats.addEntityFind(timeService.time() - entityFindBegin);
               if (entity != null) { // the entry may have been removed
                  long entityRemoveBegin = timeService.time();
                  em.remove(entity);
                  stats.addEntityRemove(timeService.time() - entityRemoveBegin);
               }
               long metadataRemoveBegin = timeService.time();
               em.remove(metadata);
               stats.addMetadataRemove(timeService.time() - metadataRemoveBegin);
               txn.commit();
               stats.addRemoveTxCommitted(timeService.time() - txnBegin);

               if (trace) log.trace("Expired " + key + " -> " + entity + "(" + toString(metadata) + ")");
               if (listener != null) {
                  eacs.submit(new Runnable() {
                     @Override
                     public void run() {
                        listener.entryPurged(key);
                     }
                  }, null);
               }
            } catch (RuntimeException e) {
               stats.addRemoveTxFailed(timeService.time() - txnBegin);
               throw e;
            } finally {
               if (txn != null && txn.isActive()) {
                  txn.rollback();
               }
            }
         }
      } finally {
         em.close();
      }
      eacs.waitUntilAllCompleted();
      if (eacs.isExceptionThrown()) {
         throw new JpaStoreException(eacs.getFirstException());
      }
   }
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

            if (filter.accept(k))
               keysToLoad.add(k);
         }
      }

      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);

      final TaskContextImpl taskContext = new TaskContextImpl();
      for (final Object key : keysToLoad) {
         if (taskContext.isStopped())
            break;

         eacs.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
               try {
                  final MarshalledEntry marshalledEntry = _load(key, fetchValue, fetchMetadata);
                  if (marshalledEntry != null) {
                     task.processEntry(marshalledEntry, taskContext);
                  }
                  return null;
               } catch (Exception e) {
                  log.errorExecutingParallelStoreTask(e);
                  throw e;
               }
            }
         });
      }
      eacs.waitUntilAllCompleted();
      if (eacs.isExceptionThrown()) {
         throw new PersistenceException("Execution exception!", eacs.getFirstException());
      }
   }
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

         throws InterruptedException {
      List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(entrySet());
      int numCores = Runtime.getRuntime().availableProcessors();
      int splittingSize = Math.max(list.size() / (numCores << 2), 128);
      List<List<Map.Entry<K, V>>> partition = splitIntoLists(list, splittingSize);
      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);
      for (final List<java.util.Map.Entry<K, V>> inputPartition : partition) {
         eacs.submit(new Runnable() {

            @Override
            public void run() {
               int interruptCounter = 0;
               for (Map.Entry<K, V> e : inputPartition) {
                  if (checkInterrupt(interruptCounter++) && Thread.currentThread().isInterrupted()) {
                     break;
                  }
                  action.apply(e.getKey(), e.getValue());
               }
            }
         }, null);
      }
      eacs.waitUntilAllCompleted();
   }
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

         conn = connectionFactory.getConnection();
         ps = conn.prepareStatement(sql);
         ps.setLong(1, ctx.getTimeService().wallClockTime());
         rs = ps.executeQuery();
         rs.setFetchSize(tableManipulation.getFetchSize());
         ExecutorAllCompletionService ecs = new ExecutorAllCompletionService(executor);
         final TaskContextImpl taskContext = new TaskContextImpl();
         //we can do better here: ATM we load the entries in the caller's thread and process them in parallel
         // we can do the loading (expensive operation) in parallel as well.
         while (rs.next()) {
            InputStream binaryStream = rs.getBinaryStream(1);
            final Bucket bucket = unmarshallBucket(binaryStream);
            ecs.submit(new Callable<Void>() {
               @Override
               public Void call() throws Exception {
                  try {
                     for (MarshalledEntry me : bucket.getStoredEntries(filter, ctx.getTimeService()).values()) {
                        if (!taskContext.isStopped()) {
                           task.processEntry(me, taskContext);
                        }
                     }
                     return null;
                  } catch (Exception e) {
                     log.errorExecutingParallelStoreTask(e);
                     throw e;
                  }
               }
            });
         }
         ecs.waitUntilAllCompleted();
         if (ecs.isExceptionThrown()) {
            throw new PersistenceException("Execution exception!", ecs.getFirstException());
         }
      } catch (SQLException e) {
         log.sqlFailureFetchingAllStoredEntries(e);
         throw new PersistenceException("SQL error while fetching all StoredEntries", e);
      } finally {
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

      Connection conn = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      Set<Bucket> expiredBuckets = new HashSet<Bucket>();
      final int batchSize = 100;
      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(threadPool);
      Set<Bucket> emptyBuckets = new HashSet<Bucket>(batchSize);
      int taskCount = 0;
      try {
         try {
            String sql = tableManipulation.getSelectExpiredRowsSql();
            conn = connectionFactory.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, ctx.getTimeService().wallClockTime());
            rs = ps.executeQuery();
            while (rs.next()) {
               Integer bucketId = rs.getInt(2);
               if (immediateLockForWriting(bucketId)) {
                  if (log.isTraceEnabled()) {
                     log.tracef("Adding bucket keyed %s for purging.", bucketId);
                  }
                  Bucket bucket;
                  InputStream binaryStream = rs.getBinaryStream(1);
                  bucket = unmarshallBucket(binaryStream);
                  bucket.setBucketId(bucketId);
                  expiredBuckets.add(bucket);
                  if (expiredBuckets.size() == batchSize) {
                     eacs.submit(new BucketPurger(expiredBuckets, task, ctx.getMarshaller(), conn, emptyBuckets));
                     taskCount++;
                     expiredBuckets = new HashSet<Bucket>(batchSize);
                  }
               } else {
                  if (log.isTraceEnabled()) {
                     log.tracef("Could not acquire write lock for %s, this won't be purged even though it has expired elements", bucketId);
                  }
               }
            }

            if (!expiredBuckets.isEmpty())
               eacs.submit(new BucketPurger(expiredBuckets, task, ctx.getMarshaller(), conn, emptyBuckets));

         } catch (Exception ex) {
            // if something happens make sure buckets locks are being release
            releaseLocks(expiredBuckets);
            log.failedClearingJdbcCacheStore(ex);
            throw new PersistenceException("Failed clearing JdbcBinaryStore", ex);
         } finally {
            JdbcUtil.safeClose(ps);
            JdbcUtil.safeClose(rs);
         }


         eacs.waitUntilAllCompleted();
         if (eacs.isExceptionThrown()) {
            releaseLocks(emptyBuckets);
         }

         if (emptyBuckets.isEmpty())
            return;
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

   @SuppressWarnings("unchecked")
   @Override
   public void process(KeyFilter keyFilter, CacheLoaderTask cacheLoaderTask, Executor executor, boolean loadValues, boolean loadMetadata) {

      int batchSize = 100;
      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);
      final TaskContext taskContext = new TaskContextImpl();

      List<Map.Entry<byte[], byte[]>> entries = new ArrayList<Map.Entry<byte[], byte[]>>(batchSize);
      DBIterator it = db.iterator(new ReadOptions().fillCache(false));
      try {
         for (it.seekToFirst(); it.hasNext();) {
            Map.Entry<byte[], byte[]> entry = it.next();
            entries.add(entry);
            if (entries.size() == batchSize) {
               final List<Map.Entry<byte[], byte[]>> batch = entries;
               entries = new ArrayList<Map.Entry<byte[], byte[]>>(batchSize);
               submitProcessTask(cacheLoaderTask, keyFilter,eacs, taskContext, batch);
            }
         }
         if (!entries.isEmpty()) {
            submitProcessTask(cacheLoaderTask, keyFilter,eacs, taskContext, entries);
         }

         eacs.waitUntilAllCompleted();
         if (eacs.isExceptionThrown()) {
            throw new PersistenceException("Execution exception!", eacs.getFirstException());
         }
      } catch (Exception e) {
         throw new PersistenceException(e);
      } finally {
         try {
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

         conn = connectionFactory.getConnection();
         ps = conn.prepareStatement(sql);
         ps.setLong(1, ctx.getTimeService().wallClockTime());
         rs = ps.executeQuery();
         rs.setFetchSize(tableManipulation.getFetchSize());
         ExecutorAllCompletionService ecs = new ExecutorAllCompletionService(executor);
         final TaskContextImpl taskContext = new TaskContextImpl();
         //we can do better here: ATM we load the entries in the caller's thread and process them in parallel
         // we can do the loading (expensive operation) in parallel as well.
         while (rs.next()) {
            InputStream binaryStream = rs.getBinaryStream(1);
            final Bucket bucket = unmarshallBucket(binaryStream);
            ecs.submit(new Callable<Void>() {
               @Override
               public Void call() throws Exception {
                  try {
                     for (MarshalledEntry me : bucket.getStoredEntries(filter, ctx.getTimeService()).values()) {
                        if (!taskContext.isStopped()) {
                           task.processEntry(me, taskContext);
                        }
                     }
                     return null;
                  } catch (Exception e) {
                     log.errorExecutingParallelStoreTask(e);
                     throw e;
                  }
               }
            });
         }
         ecs.waitUntilAllCompleted();
         if (ecs.isExceptionThrown()) {
            throw new PersistenceException("Execution exception!", ecs.getFirstException());
         }
      } catch (SQLException e) {
         log.sqlFailureFetchingAllStoredEntries(e);
         throw new PersistenceException("SQL error while fetching all StoredEntries", e);
      } finally {
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

      Connection conn = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      Set<Bucket> expiredBuckets = new HashSet<Bucket>();
      final int batchSize = 100;
      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(threadPool);
      Set<Bucket> emptyBuckets = new HashSet<Bucket>(batchSize);
      int taskCount = 0;
      try {
         try {
            String sql = tableManipulation.getSelectExpiredRowsSql();
            conn = connectionFactory.getConnection();
            ps = conn.prepareStatement(sql);
            ps.setLong(1, ctx.getTimeService().wallClockTime());
            rs = ps.executeQuery();
            while (rs.next()) {
               Integer bucketId = rs.getInt(2);
               if (immediateLockForWriting(bucketId)) {
                  if (log.isTraceEnabled()) {
                     log.tracef("Adding bucket keyed %s for purging.", bucketId);
                  }
                  Bucket bucket;
                  InputStream binaryStream = rs.getBinaryStream(1);
                  bucket = unmarshallBucket(binaryStream);
                  bucket.setBucketId(bucketId);
                  expiredBuckets.add(bucket);
                  if (expiredBuckets.size() == batchSize) {
                     eacs.submit(new BucketPurger(expiredBuckets, task, ctx.getMarshaller(), conn, emptyBuckets));
                     taskCount++;
                     expiredBuckets = new HashSet<Bucket>(batchSize);
                  }
               } else {
                  if (log.isTraceEnabled()) {
                     log.tracef("Could not acquire write lock for %s, this won't be purged even though it has expired elements", bucketId);
                  }
               }
            }

            if (!expiredBuckets.isEmpty())
               eacs.submit(new BucketPurger(expiredBuckets, task, ctx.getMarshaller(), conn, emptyBuckets));

         } catch (Exception ex) {
            // if something happens make sure buckets locks are being release
            releaseLocks(expiredBuckets);
            log.failedClearingJdbcCacheStore(ex);
            throw new PersistenceException("Failed clearing JdbcBinaryStore", ex);
         } finally {
            JdbcUtil.safeClose(ps);
            JdbcUtil.safeClose(rs);
         }


         eacs.waitUntilAllCompleted();
         if (eacs.isExceptionThrown()) {
            releaseLocks(emptyBuckets);
         }

         if (emptyBuckets.isEmpty())
            return;
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

      get.addHeader(HttpHeaders.ACCEPT, "text/plain");
      try {
         HttpResponse response = httpClient.execute(httpHost, get);
         HttpEntity entity = response.getEntity();
         int batchSize = 1000;
         ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);
         final TaskContext taskContext = new TaskContextImpl();
         BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
         Set<Object> entries = new HashSet<Object>(batchSize);
         for (String stringKey = reader.readLine(); stringKey != null; stringKey = reader.readLine()) {
            Object key = key2StringMapper.getKeyMapping(stringKey);
            if (keyFilter == null || keyFilter.accept(key))
               entries.add(key);
            if (entries.size() == batchSize) {
               final Set<Object> batch = entries;
               entries = new HashSet<Object>(batchSize);
               submitProcessTask(cacheLoaderTask, eacs, taskContext, batch, loadValue, loadMetadata);
            }
         }
         if (!entries.isEmpty()) {
            submitProcessTask(cacheLoaderTask, eacs, taskContext, entries, loadValue, loadMetadata);
         }
         eacs.waitUntilAllCompleted();
         if (eacs.isExceptionThrown()) {
            throw new PersistenceException("Execution exception!", eacs.getFirstException());
         }
      } catch (Exception e) {
         throw log.errorLoadingRemoteEntries(e);
      } finally {
         get.releaseConnection();
View Full Code Here

Examples of org.infinispan.executors.ExecutorAllCompletionService

   }

   @Override
   public void process(final KeyFilter filter, final CacheLoaderTask task, Executor executor, boolean fetchValue, boolean fetchMetadata) {
      scanForUnknownDirectories();
      ExecutorAllCompletionService eacs = new ExecutorAllCompletionService(executor);

      final TaskContextImpl taskContext = new TaskContextImpl();
      for (final DirectoryLoaderAdaptor dir : openDirectories.values()) {
         eacs.submit(new Callable<Void>() {
            @Override
            public Void call() throws Exception {
               try {
                  final HashSet<MarshalledEntry> allInternalEntries = new HashSet<MarshalledEntry>();
                  dir.loadAllEntries(allInternalEntries, Integer.MAX_VALUE, ctx.getMarshaller());
                  for (MarshalledEntry me : allInternalEntries) {
                     if (taskContext.isStopped())
                        break;
                     if (filter == null || filter.shouldLoadKey(me.getKey())) {
                        task.processEntry(me, taskContext);
                     }
                  }
                  return null;
               }
               catch (Exception e) {
                  log.errorExecutingParallelStoreTask(e);
                  throw e;
               }
            }
         });
      }
      eacs.waitUntilAllCompleted();
      if (eacs.isExceptionThrown()) {
         throw new PersistenceException("Execution exception!", eacs.getFirstException());
      }
   }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.