Package org.infinispan.distexec

Examples of org.infinispan.distexec.DistributedExecutorService


            TestKey key = new TestKey(random.nextInt(KEY_RANGE));
            try {

                log.info("Submitting a task " + key);
                EmbeddedCacheManager cacheManager = cacheManagers.get(random.nextInt(cacheManagers.size()));
                DistributedExecutorService ispnExecutor = new DefaultExecutorService(cacheManager.getCache("serviceGroup"));

                Future<String> z = ispnExecutor.submit(new TransactionTask(), key);
                log.info("Task result=" + z.get());
            } catch (Exception ex) {
                log.warn("error during executing task " + key, ex);
            }
        }


      try {
         if (isMaster) {
            int numServers = cache.getCacheManager().getMembers().size();
            int numberPerWorker = numPoints / numServers;

            DistributedExecutorService des = new DefaultExecutorService(cache);

            long start = System.currentTimeMillis();
            List<Future<Integer>> results = des.submitEverywhere(new CircleTest(numberPerWorker));

            int insideCircleCount = 0;

            for (Future<Integer> f : results) insideCircleCount += f.get();

            // If cache mode is LOCAL or REPL, dump local keyset.
            // Defensive copy to serialize and transmit across a network
            keys = new HashSet<Object>(cache.keySet());
         } else {
            // If cache mode is DIST, use a map/reduce task
            DistributedExecutorService des = new DefaultExecutorService(cache);
            List<Future<Set<Object>>> keysets = des.submitEverywhere(new GlobalKeysetTask(cache));
            Set<Object> combinedKeyset = new HashSet<Object>();

            for (Future<Set<Object>> keyset : keysets)
               combinedKeyset.addAll(keyset.get());

   @Override
   public List<Future<T>> executeDistributedTask(String distributedCallableFqn,
                                                 String executionPolicyName, String failoverPolicyFqn, String nodeAddress, Map<String, String> params) {

      Cache<K, V> cache = (Cache<K, V>) service.getCache(null);
      DistributedExecutorService des = new DefaultExecutorService(cache);
      Callable<T> callable = null;
      DistributedTaskBuilder<T> taskBuilder = null;
      List<Future<T>> result = null;
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

      if (distributedCallableFqn == null) {
         log.fatal("The distributedCallableFqn parameter must be specified.");
      } else {
         try {
            callable = Utils.instantiate(classLoader, distributedCallableFqn);
            taskBuilder = des.createDistributedTaskBuilder(callable);
            callable = (Callable<T>) Utils.invokeMethodWithString(callable, params);
         } catch (Exception e1) {
            throw (new IllegalArgumentException("Could not instantiate '" + distributedCallableFqn + "' as a Callable",
                  e1));
         }
      }

      if (callable != null) {
         if (executionPolicyName != null) {
            DistributedTaskExecutionPolicy executionPolicy = Enum.valueOf(DistributedTaskExecutionPolicy.class,
                  executionPolicyName);
            if (executionPolicy == null) {
               log.error("No DistributedTaskExecutionPolicy found with name: " + executionPolicyName);
            } else {
               taskBuilder = taskBuilder.executionPolicy(executionPolicy);
            }
         }

         if (failoverPolicyFqn != null) {
            try {
               DistributedTaskFailoverPolicy failoverPolicy = Utils.instantiate(classLoader, failoverPolicyFqn);
               taskBuilder = taskBuilder.failoverPolicy(failoverPolicy);
            } catch (Exception e) {
               log.error("Could not instantiate DistributedTaskFailoverPolicy class: " + failoverPolicyFqn, e);
            }
         }

         if (nodeAddress != null) {
            Address target = findHostPhysicalAddress(nodeAddress);
            if (target == null) {
               log.error("No host found with address: " + nodeAddress);
            } else {
               result = new ArrayList<Future<T>>();
               result.add(des.submit(target, taskBuilder.build()));
            }
         } else {
            result = des.submitEverywhere(taskBuilder.build());
         }
      }

      return result;
   }

      }

      @Override
      public long getTotalSize() {
         long totalSize = 0;
         DistributedExecutorService des = new DefaultExecutorService(cache);
         CacheSizer<?, ?, Integer> cacheSizer = new CacheSizer<Object, Object, Integer>();
         DistributedTaskBuilder<Integer> taskBuilder = des.createDistributedTaskBuilder(cacheSizer);
         List<Future<Integer>> futureList = des.submitEverywhere(taskBuilder.build());
        
         for (Future<Integer> future : futureList) {
            try {
               totalSize += future.get().intValue();
            } catch (InterruptedException e) {

            TestKey key = new TestKey(random.nextInt(KEY_RANGE));
            try {

                log.info("Submitting a task " + key);
                EmbeddedCacheManager cacheManager = cacheManagers.get(random.nextInt(cacheManagers.size()));
                DistributedExecutorService ispnExecutor = new DefaultExecutorService(cacheManager.getCache("serviceGroup"));

                Future<String> z = ispnExecutor.submit(new TransactionTask(), key);
                log.info("Task result=" + z.get());
            } catch (Exception ex) {
                log.warn("error during executing task " + key, ex);
            }
        }

            TestKey key = new TestKey(random.nextInt(KEY_RANGE));
            try {

                log.info("Submitting a task " + key);
                EmbeddedCacheManager cacheManager = cacheManagers.get(random.nextInt(cacheManagers.size()));
                DistributedExecutorService ispnExecutor = new DefaultExecutorService(cacheManager.getCache("serviceGroup"));

                Future<String> z = ispnExecutor.submit(new TransactionTask(), key);
                log.info("Task result=" + z.get());
            } catch (Exception ex) {
                log.warn("error during executing task " + key, ex);
            }
        }

                                       Location location,
                                       DistributedCallable<K, V, T> callable,
                                       Combiner<T> combiner ) throws InterruptedException, ExecutionException {
        if (location == null) location = Location.LOCALLY;

        DistributedExecutorService distributedExecutor = new DefaultExecutorService(cache);
        List<StoreConfiguration> stores = cache.getCacheConfiguration().persistence().stores();
        boolean shared = (stores != null) && !stores.isEmpty() && stores.get(0).shared();
        T result = null;
        if (!shared) {
            // store is not shared so every node must return key list of the store
            List<Future<T>> futures = null;
            switch (location) {
                case EVERYWHERE:
                    futures = distributedExecutor.submitEverywhere(callable);
                    break;
                case LOCALLY:
                    futures = Collections.singletonList(distributedExecutor.submit(callable));
                    break;
            }

            while (futures != null && !futures.isEmpty()) {
                // Get the next future that is ready ...
                Iterator<Future<T>> futureIter = futures.iterator();
                while (futureIter.hasNext()) {
                    Future<T> future = futureIter.next();
                    try {
                        // But done't wait too long for this future ...
                        T value = future.get(100, TimeUnit.MILLISECONDS);
                        // We got some keys, so this future is done and should be removed from our list ...
                        futureIter.remove();
                        result = combiner.combine(result, value);
                    } catch (TimeoutException e) {
                        // continue;
                    }
                }
                if (futures.isEmpty()) break;
            }
        } else {
            // store is shared, so we can short-circuit the logic and just run locally; otherwise, if distributed
            // each process will see the all of the keys ...
            result = distributedExecutor.submit(callable).get();
        }
        return result;
    }

            TestKey key = new TestKey(random.nextInt(KEY_RANGE));
            try {

                log.info("Submitting a task " + key);
                EmbeddedCacheManager cacheManager = cacheManagers.get(random.nextInt(cacheManagers.size()));
                DistributedExecutorService ispnExecutor = new DefaultExecutorService(cacheManager.getCache("serviceGroup"));

                Future<String> z = ispnExecutor.submit(new TransactionTask(), key);
                log.info("Task result=" + z.get());
            } catch (Exception ex) {
                log.warn("error during executing task " + key, ex);
            }
        }

      try {
         if (isMaster) {
            int numServers = cache.getCacheManager().getMembers().size();
            int numberPerWorker = numPoints / numServers;

            DistributedExecutorService des = new DefaultExecutorService(cache);

            long start = System.currentTimeMillis();
            List<Future<Integer>> results = des.submitEverywhere(new CircleTest(numberPerWorker));

            int insideCircleCount = 0;

            for (Future<Integer> f : results) insideCircleCount += f.get();

TOP

Related Classes of org.infinispan.distexec.DistributedExecutorService

Copyright © 2018 www.massapicom. 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.