salt-netapi-client
salt-netapi-client copied to clipboard
Using the asynchronous run method, the test found that the actual task was less than the original task.
SaltClient.class
By using the following asynchronous run method, saltstack has fewer actual tasks than it does in concurrent situations, What should I do? Are there any configurations that are not configured correctly?
public <T> CompletionStage<Map<String, Object>> run(String username, String password, AuthModule eauth, String client, Target<T> target, String function, List<Object> args, Map<String, Object> kwargs) { Map<String, Object> props = new HashMap(); props.put("username", username); props.put("password", password); props.put("eauth", eauth.getValue()); props.put("client", client); props.putAll(target.getProps()); props.put("fun", function); props.put("arg", args); props.put("kwarg", kwargs); List<Map<String, Object>> list = Collections.singletonList(props); String payload = this.gson.toJson(list); CompletionStage<Map<String, Object>> result = this.asyncHttpClient.post(this.uri.resolve("run"), payload, JsonParser.RUN_RESULTS).thenApply((s) -> { return (Map)((List)s.getResult()).get(0); }); return result; }
Hey @zhuhaolin162, what exactly do you mean when you say "saltstack has fewer actual tasks than it does in concurrent situations"? How exactly do you use this method and what is the unexpected outcome vs. the expected?
Test purpose : I want to generate 100 files in /tmp/test , but in fact it just generate 75 files(the nums is not exactly every times)
Test result: [root@rhel72 test]# pwd /tmp/test [root@rhel72 test]# ls -l |grep '^-'|wc -l 75
Test code: public static String ayscExecute(String ip, final String command) { try { CloseableHttpAsyncClient httpClient = HttpClientUtils.defaultClient(); SaltClient client = new SaltClient(URI.create(ConstUtils.SALT_API_URL), new HttpAsyncClientImpl(httpClient)); IPCidr target = new IPCidr(ip); List<Object> list = new ArrayList(); list.add(command); Runnable cleanup = () -> { try { httpClient.close(); } catch (Exception e) { logger.error("close error",e); } }; CompletionStage<Map<String, Object>> result = client.run(ConstUtils.SALT_API_USER, ConstUtils.SALT_API_PASSWORD, AuthModule.PAM,"local", target,"cmd.run",list,new LinkedHashMap<>()); result .thenAccept(t-> System.out.println(t)) .thenRun(cleanup); }catch (Exception e){ logger.error("aysc exec failed",e); return "fail"; } return "ok"; }
public static void main(String[] args) { ConstUtils.SALT_API_USER = "user"; ConstUtils.SALT_API_PASSWORD = "password"; ConstUtils.SALT_API_URL = "http://192.168.6.138:8000"; String ip = "192.168.6.138"; ExecutorService executorService = Executors.newFixedThreadPool(1); AtomicInteger count = new AtomicInteger(); executorService.submit(()->{ for(int i=0;i<100;i++) { int num = count.getAndIncrement(); String command = String.format("touch /tmp/test/%s;echo %s",num,num); System.out.println(command); String result = SaltUtil.ayscExecute(ip,command); System.out.println(result); } }); }
Thank you for you help! @renner