public static void main(String[] args) { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IExecutorService executor = hz.getExecutorService("executor"); ExecutionCallback<Long> executionCallback = new ExecutionCallback<Long>() { public void onFailure(Throwable t) { t.printStackTrace(); } public void onResponse(Long response) { System.out.println("Result: " + response); } }; executor.submit(new FibonacciCallable(10), executionCallback); System.out.println("Fibonacci task submitted"); }
/** * Submits a task to multiple members of the Hazelcast task cluster. * * @param task * The task * @param where * A {@link MemberSelector}, an {@link Iterable} of {@link Member}, a * string (comma-separated member tags), or null for all members * @return A map of members to futures for the task */ @SuppressWarnings("unchecked") private <T> Map<Member, Future<T>> multiTask( SerializableApplicationTask<T> task, Object where ) { IExecutorService executor = getHazelcastExecutorService(); if( where instanceof String ) return executor.submitToMembers( task, new TaggedMembers( HAZELCAST_MEMBER_TAGS_ATTRIBUTE, (String) where ) ); else if( where instanceof MemberSelector ) return executor.submitToMembers( task, (MemberSelector) where ); else if( where instanceof Collection ) return executor.submitToMembers( task, (Collection<Member>) where ); else if( where instanceof Iterable ) { ArrayList<Member> members = new ArrayList<Member>(); for( Member member : (Iterable<Member>) where ) members.add( member ); return executor.submitToMembers( task, members ); } else return executor.submitToAllMembers( task ); }
private static void notifyAdmins(HazelcastInstance sysInstance, Member local, String schemaName, boolean initialized) { int cnt = 0; IExecutorService execService = sysInstance.getExecutorService(PN_XDM_SYSTEM_POOL); Set<Member> admins = getAdmins(sysInstance); // notify admin about new schema node (local) // hzInstance -> system instance, SchemaManagement is in its context // submit task to init member in admin.. SchemaAdministrator adminTask = new SchemaAdministrator(schemaName, !initialized, local.getUuid()); Map<Member, Future<Boolean>> result = execService.submitToMembers(adminTask, admins); for (Map.Entry<Member, Future<Boolean>> e: result.entrySet()) { try { if (e.getValue().get()) { cnt++; } else { logger.info("notifyAdmins; failed admin notification on member {}", e.getKey()); } } catch (InterruptedException | ExecutionException ex) { logger.error("notifyAdmins.error; ", ex); } } logger.debug("notifyAdmins; notified {} admin nodes out of {} admins", cnt, admins.size()); }
static <T> ServiceCommandResult<T> submit(HazelcastInstance instance, ServiceCommand<T> command, ClusterMember member) { IExecutorService exService = instance.getExecutorService(HAZELCAST_EXECUTOR_NAME) ; HazelcastMemberSelector selector = new HazelcastMemberSelector(instance) ; Member hzmember = selector.selectMember(member) ; ServiceCommandWrapper<T> wrapper = new ServiceCommandWrapper<T>(command) ; Future<T> future = exService.submitToMember(wrapper, hzmember) ; ServiceCommandResult<T> result = new ServiceCommandResult<T>() ; result.setFromMember(member); try { T ret = future.get(command.getTimeout(), TimeUnit.MILLISECONDS) ; result.setResult(ret); return result ; } catch (InterruptedException | ExecutionException | TimeoutException error) { result.setError(error) ; return result ; } }
static <T> ServiceCommandResult<T>[] submit(HazelcastInstance instance, ServiceCommand<T> command, ClusterMember[] member) { IExecutorService exService = instance.getExecutorService(HAZELCAST_EXECUTOR_NAME) ; HazelcastMemberSelector selector = new HazelcastMemberSelector(instance) ; List<Member> hzmembers = selector.selectMemberAsList(member) ; ServiceCommandWrapper<T> wrapper = new ServiceCommandWrapper<T>(command) ; Map<Member, Future<T>> futures = exService.submitToMembers(wrapper, hzmembers) ; ServiceCommandResult<T>[] results = new ServiceCommandResult[member.length] ; long startTime = System.currentTimeMillis() ; long waitTime = command.getTimeout() ; for(int i = 0; i < member.length; i++) { Member hzmember = hzmembers.get(i) ; Future<T> future = futures.get(hzmember) ; results[i] = new ServiceCommandResult<T>() ; results[i].setFromMember(member[i]); try { T ret = future.get(waitTime, TimeUnit.MILLISECONDS) ; results[i].setResult(ret); } catch (InterruptedException | ExecutionException | TimeoutException error) { results[i].setError(error) ; } waitTime = command.getTimeout() - (System.currentTimeMillis() - startTime) ; } return results ; }
static <T> ServiceCommandResult<T>[] submit(HazelcastInstance instance, ServiceCommand<T> command) { IExecutorService exService = instance.getExecutorService(HAZELCAST_EXECUTOR_NAME) ; ServiceCommandWrapper<T> wrapper = new ServiceCommandWrapper<T>(command) ; Map<Member, Future<T>> futures = exService.submitToAllMembers(wrapper) ; ServiceCommandResult<T>[] results = new ServiceCommandResult[futures.size()] ; long ctime = System.currentTimeMillis() ; long waitTime = command.getTimeout() ; Iterator<Map.Entry<Member, Future<T>>> i = futures.entrySet().iterator() ; int idx = 0 ; while(i.hasNext()) { Map.Entry<Member, Future<T>> entry = i.next() ; Member hzmember = entry.getKey() ; Future<T> future = entry.getValue() ; results[idx] = new ServiceCommandResult<T>() ; results[idx].setFromMember(new ClusterMemberImpl(hzmember)) ; try { T ret = future.get(waitTime, TimeUnit.MILLISECONDS) ; results[idx].setResult(ret); } catch (InterruptedException | ExecutionException | TimeoutException error) { results[idx].setError(error) ; } waitTime = command.getTimeout() - (System.currentTimeMillis() - ctime) ; idx++ ; } return results ; }
static <T> ServerCommandResult<T> submit(HazelcastInstance instance, ServerCommand<T> command, ClusterMember member) { IExecutorService exService = instance.getExecutorService(HAZELCAST_EXECUTOR_NAME) ; HazelcastMemberSelector selector = new HazelcastMemberSelector(instance) ; Member hzmember = selector.selectMember(member) ; ServerCommandWrapper<T> wrapper = new ServerCommandWrapper<T>(command) ; Future<T> future = exService.submitToMember(wrapper, hzmember) ; ServerCommandResult<T> result = new ServerCommandResult<T>() ; try { T ret = future.get(command.getTimeout(), TimeUnit.MILLISECONDS) ; result.setResult(ret); result.setFromMember(member); return result ; } catch (InterruptedException | ExecutionException | TimeoutException error) { result.setError(error) ; return result ; } }
static <T> ServerCommandResult<T>[] submit(HazelcastInstance instance, ServerCommand<T> command, ClusterMember[] member) { IExecutorService exService = instance.getExecutorService(HAZELCAST_EXECUTOR_NAME) ; HazelcastMemberSelector selector = new HazelcastMemberSelector(instance) ; List<Member> hzmembers = selector.selectMemberAsList(member) ; ServerCommandWrapper<T> wrapper = new ServerCommandWrapper<T>(command) ; Map<Member, Future<T>> futures = exService.submitToMembers(wrapper, hzmembers) ; ServerCommandResult<T>[] results = new ServerCommandResult[member.length] ; long startTime = System.currentTimeMillis() ; long waitTime = command.getTimeout() ; for(int i = 0; i < member.length; i++) { Member hzmember = hzmembers.get(i) ; Future<T> future = futures.get(hzmember) ; results[i] = new ServerCommandResult<T>() ; results[i].setFromMember(member[i]); try { T ret = future.get(waitTime, TimeUnit.MILLISECONDS) ; results[i].setResult(ret); } catch (InterruptedException | ExecutionException | TimeoutException error) { results[i].setError(error) ; } waitTime = command.getTimeout() - (System.currentTimeMillis() - startTime) ; } return results ; }
static <T> ServerCommandResult<T>[] submit(HazelcastInstance instance, ServerCommand<T> command) { IExecutorService exService = instance.getExecutorService(HAZELCAST_EXECUTOR_NAME) ; ServerCommandWrapper<T> wrapper = new ServerCommandWrapper<T>(command) ; Map<Member, Future<T>> futures = exService.submitToAllMembers(wrapper) ; ServerCommandResult<T>[] results = new ServerCommandResult[futures.size()] ; long ctime = System.currentTimeMillis() ; long waitTime = command.getTimeout() ; Iterator<Map.Entry<Member, Future<T>>> i = futures.entrySet().iterator() ; int idx = 0 ; while(i.hasNext()) { Map.Entry<Member, Future<T>> entry = i.next() ; Future<T> future = entry.getValue() ; results[idx] = new ServerCommandResult<T>() ; results[idx].setFromMember(new ClusterMemberImpl(entry.getKey())); try { T ret = future.get(waitTime, TimeUnit.MILLISECONDS) ; results[idx].setResult(ret); } catch (InterruptedException | ExecutionException | TimeoutException error) { results[idx].setError(error) ; } waitTime = command.getTimeout() - (System.currentTimeMillis() - ctime) ; idx++ ; } return results ; }
public static void main(String[] args) throws Exception { HazelcastInstance hz = Hazelcast.newHazelcastInstance(); Map<String, Integer> map = hz.getMap("map"); for (int k = 0; k < 42; k++) map.put(UUID.randomUUID().toString(), 1); IExecutorService executor = hz.getExecutorService("executor"); Map<Member, Future<Integer>> result = executor.submitToAllMembers(new SumTask()); int sum = 0; for (Future<Integer> future : result.values()) sum += future.get(); System.out.println("Result: " + sum); }
@Override public IExecutorService getIExecutorService(String name) { name = Objects.requireNonNull(name); final IExecutorService valu = getBeanSafely(name, IExecutorService.class); if (null != valu) { return valu; } return hz().getExecutorService(name); }
private static boolean initSchema(HazelcastInstance hzInstance, Member member, Schema schema) { logger.trace("initSchema.enter; schema: {}", schema); SchemaInitiator init = new SchemaInitiator(schema); IExecutorService execService = hzInstance.getExecutorService(PN_XDM_SYSTEM_POOL); Future<Boolean> result = execService.submitToMember(init, member); Boolean ok = false; try { ok = result.get(); } catch (InterruptedException | ExecutionException ex) { logger.error("initSchema.error; ", ex); } logger.info("initSchema.exit; schema {} {}initialized", schema.getName(), ok ? "" : "NOT "); return ok; }
protected int updateNodesInCluster(Node node, String comment) { logger.trace("updateNodesInCluster.enter; node: {}", node); // do this on Named nodes only, not on ALL nodes! Set<Member> all = hzInstance.getCluster().getMembers(); List<Member> named = new ArrayList<Member>(all.size()); String name = node.getName(); for (Member member: all) { if (name.equals(member.getStringAttribute(pn_cluster_node_name))) { named.add(member); } } int cnt = 0; if (named.size() > 0) { logger.info("updateNodesInCluster; going to update {} Members", named.size()); NodeOptionSetter setter = new NodeOptionSetter(getAdmin(), comment, node.getOptions()); IExecutorService execService = hzInstance.getExecutorService(PN_XDM_SYSTEM_POOL); Map<Member, Future<Boolean>> result = execService.submitToMembers(setter, named); for (Map.Entry<Member, Future<Boolean>> entry: result.entrySet()) { try { Boolean ok = entry.getValue().get(); if (ok) cnt++; logger.debug("updateNodesInCluster; Member {} {}updated", entry.getKey(), ok ? "" : "NOT "); } catch (InterruptedException | ExecutionException ex) { logger.error("updateNodesInCluster.error; ", ex); } } } logger.info("updateNodesInCluster.exit; {} Members updated", cnt); return cnt; }
@Override public Boolean call() throws Exception { logger.trace("call.enter; schema: {}; init: {}", schemaName, init); //logger.trace("call.enter; HZ: {}; SM: {}", hzInstance, schemaService); boolean result = false; for (Member member: hzInstance.getCluster().getMembers()) { if (uuid.equals(member.getUuid())) { if (init) { Schema schema = schemaService.getSchema(schemaName); SchemaInitiator initTask = new SchemaInitiator(schema); IExecutorService execService = hzInstance.getExecutorService(PN_XDM_SYSTEM_POOL); Future<Boolean> initiated = execService.submitToMember(initTask, member); Boolean ok = false; try { ok = initiated.get(); } catch (InterruptedException | ExecutionException ex) { logger.error("initSchema.error; ", ex); } } schemaService.initMember(member); result = true; break; } } logger.trace("call.exit; returning: {} for member: {}", result, uuid); return result; }
@Test public void testTask() throws Exception { // Submit the hello task on instance 1 IExecutorService exService = instance1.getExecutorService("default"); instance2.getCluster().getLocalMember().setStringAttribute("instance", "instance2"); Map<Member, Future<Object>> futures = exService.submitToAllMembers(new HelloTask()); Iterator<Future<Object>> itr = futures.values().iterator() ; while(itr.hasNext()) { Object result = itr.next().get(25, TimeUnit.SECONDS); System.out.println(result); } }
public IndexSettingsResponse updateIndex(String indexName, org.lumongo.cluster.message.LumongoIndex.IndexSettings request) throws InvalidIndexConfig, MongoException, IOException { globalLock.readLock().lock(); try { log.info("Updating index settings for <" + indexName + ">:\n" + JsonFormat.printer().print(request)); LumongoIndex i = indexMap.get(indexName); if (i == null) { throw new IndexDoesNotExist(indexName); } i.updateIndexSettings(request); Set<Member> currentMembers = hazelcastManager.getMembers(); IExecutorService executorService = hazelcastManager.getExecutorService(); Member self = hazelcastManager.getSelf(); for (Member m : currentMembers) { try { ReloadIndexSettingsTask rist = new ReloadIndexSettingsTask(m.getSocketAddress().getPort(), indexName); if (!self.equals(m)) { Future<Void> dt = executorService.submitToMember(rist, m); dt.get(); } else { rist.call(); } } catch (Exception e) { log.error(e.getClass().getSimpleName() + ": ", e); } } return IndexSettingsResponse.newBuilder().build(); } finally { globalLock.readLock().unlock(); } }
@Setup public void setup() { executors = new IExecutorService[executorCount]; for (int i = 0; i < executors.length; i++) { executors[i] = targetInstance.getExecutorService(name + '-' + i); } executedCounter = targetInstance.getAtomicLong(name + ":ExecutedCounter"); expectedExecutedCounter = targetInstance.getAtomicLong(name + ":ExpectedExecutedCounter"); }
@Teardown(global = true) public void teardown() throws Exception { executedCounter.destroy(); expectedExecutedCounter.destroy(); for (IExecutorService executor : executors) { executor.shutdownNow(); if (!executor.awaitTermination(120, TimeUnit.SECONDS)) { logger.fatal("Time out while waiting for shutdown of executor: " + executor.getName()); } executor.destroy(); } }
@Override public IExecutorService getExecutorService(String s) { return null; }
private HazelcastCommandExecutor(IExecutorService executorService) { this.executorService = executorService; }
public static void main(String[] args) { if (args.length != 2) { logger.error("expected two arguments: profile name and node instance"); return; } String address = args[0]; String schemas = args[1]; logger.info("Stopping XDM nodes [{}] on hosts [{}]", schemas, address); ClientConfig config = new ClientConfig(); config.getGroupConfig().setName("system").setPassword("syspwd"); config.getNetworkConfig().addAddress(address.split(",")); config.getNetworkConfig().setSmartRouting(false); HazelcastInstance client = HazelcastClient.newHazelcastClient(config); IExecutorService es = client.getExecutorService(PN_XDM_SYSTEM_POOL); Runnable task; boolean closeAll = "ALL".equals(schemas); if (closeAll) { task = new NodeKiller(); } else { task = new NodeKiller(schemas); } int cnt = 0; List<Member> members = new ArrayList<>(client.getCluster().getMembers()); Collections.sort(members, new MemberComparator()); for (Member member: members) { try { if (shutdownMember(member, address)) { es.executeOnMember(task, member); cnt++; } } catch (RejectedExecutionException ex) { logger.info(ex.getMessage()); } } logger.info("Stopper invoked on {} nodes", cnt); client.shutdown(); }
public void setExecService(IExecutorService execService) { this.execService = execService; }
public TopologyManager(HazelcastInstance hzInstance, IExecutorService execService, Member member) { this.hzInstance = hzInstance; this.execService = execService; this.member = member; }
@Autowired public void setExecService(IExecutorService execService) { this.execService = execService; //logger.trace("setSchemaManager; got manager: {}", schemaManager); }
@Override public IExecutorService getExecutorService(final String name) { return getHazelcastInstance().getExecutorService(name); }
public IndexDeleteResponse deleteIndex(IndexDeleteRequest request) throws Exception { globalLock.writeLock().lock(); try { String indexName = request.getIndexName(); LumongoIndex i = indexMap.get(indexName); if (i == null) { if (getIndexNames().contains(indexName)) { //TODO delete index from database return IndexDeleteResponse.newBuilder().build(); } else { throw new IndexDoesNotExist(indexName); } } Set<Member> currentMembers = hazelcastManager.getMembers(); IExecutorService executorService = hazelcastManager.getExecutorService(); Member self = hazelcastManager.getSelf(); log.info("Unload index <" + indexName + "> for delete"); for (Member m : currentMembers) { try { UnloadIndexTask uit = new UnloadIndexTask(m.getSocketAddress().getPort(), indexName, true); if (!self.equals(m)) { Future<Void> dt = executorService.submitToMember(uit, m); dt.get(); } else { uit.call(); } } catch (Exception e) { log.error(e.getClass().getSimpleName() + ": ", e); } } log.info("Deleting index <" + indexName + ">"); i.deleteIndex(); indexMap.remove(indexName); return IndexDeleteResponse.newBuilder().build(); } finally { globalLock.writeLock().unlock(); } }
public IExecutorService getExecutorService() { return hazelcastInstance.getExecutorService("default"); }
public String runOnASingleNode(SingleResourceReader reader) throws Exception{ HazelcastInstance hz = Hazelcast.newHazelcastInstance(); IExecutorService executorService = hz.getExecutorService("default"); Future<String> future = executorService.submit(reader); return future.get(); }
/** * The Hazelcast executor service. * <p> * The name can be configured via the * "com.threecrickets.prudence.hazelcast.executorName" application context * attribute, and defaults to "default". * * @return The Hazelcast executor service * @throws RuntimeException * If the Hazelcast executor service has not been found * @see #getHazelcastTaskInstance() */ public IExecutorService getHazelcastExecutorService() { String name = getExecutorServiceName(); IExecutorService executor = getHazelcastTaskInstance().getExecutorService( name ); if( executor == null ) throw new RuntimeException( "Cannot find a Hazelcast executor service named \"" + name + "\"" ); return executor; }
/** * Create a new {@link HazelcastCommandExecutor} using the supplied {@link IExecutorService}. * @param executorService The {@link IExecutorService} to use to execute {@link Command}s. * @return The constructed {@link CommandExecutor}. */ public static CommandExecutor using(IExecutorService executorService) { return new HazelcastCommandExecutor(executorService); }
public IExecutorService getIExecutorService(String name);