|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | +package demo; |
| 10 | + |
| 11 | +import org.apache.logging.log4j.LogManager; |
| 12 | +import org.apache.logging.log4j.Logger; |
| 13 | +import org.opensearch.common.SuppressForbidden; |
| 14 | +import org.opensearch.common.io.PathUtils; |
| 15 | +import org.opensearch.flowframework.template.ProcessNode; |
| 16 | +import org.opensearch.flowframework.template.TemplateParser; |
| 17 | +import org.opensearch.flowframework.workflow.WorkflowStep; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.nio.charset.StandardCharsets; |
| 21 | +import java.nio.file.Files; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Locale; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import java.util.concurrent.CompletableFuture; |
| 29 | +import java.util.stream.Collectors; |
| 30 | + |
| 31 | +/** |
| 32 | + * Demo class exercising {@link TemplateParser}. This will be moved to a unit test. |
| 33 | + */ |
| 34 | +public class Demo { |
| 35 | + |
| 36 | + private static final Logger logger = LogManager.getLogger(Demo.class); |
| 37 | + |
| 38 | + // This is temporary. We need a factory class to generate these workflow steps |
| 39 | + // based on a field in the JSON. |
| 40 | + private static Map<String, WorkflowStep> workflowMap = new HashMap<>(); |
| 41 | + static { |
| 42 | + workflowMap.put("fetch_model", new DemoWorkflowStep(3000)); |
| 43 | + workflowMap.put("create_ingest_pipeline", new DemoWorkflowStep(3000)); |
| 44 | + workflowMap.put("create_search_pipeline", new DemoWorkflowStep(5000)); |
| 45 | + workflowMap.put("create_neural_search_index", new DemoWorkflowStep(2000)); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Demonstrate parsing a JSON graph. |
| 50 | + * |
| 51 | + * @param args unused |
| 52 | + */ |
| 53 | + @SuppressForbidden(reason = "just a demo class that will be deleted") |
| 54 | + public static void main(String[] args) { |
| 55 | + String path = "src/test/resources/template/demo.json"; |
| 56 | + String json; |
| 57 | + try { |
| 58 | + json = new String(Files.readAllBytes(PathUtils.get(path)), StandardCharsets.UTF_8); |
| 59 | + } catch (IOException e) { |
| 60 | + logger.error("Failed to read JSON at path {}", path); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + logger.info("Parsing graph to sequence..."); |
| 65 | + List<ProcessNode> processSequence = TemplateParser.parseJsonGraphToSequence(json, workflowMap); |
| 66 | + List<CompletableFuture<?>> futureList = new ArrayList<>(); |
| 67 | + |
| 68 | + for (ProcessNode n : processSequence) { |
| 69 | + Set<ProcessNode> predecessors = n.getPredecessors(); |
| 70 | + logger.info( |
| 71 | + "Queueing process [{}].{}", |
| 72 | + n.id(), |
| 73 | + predecessors.isEmpty() |
| 74 | + ? " Can start immediately!" |
| 75 | + : String.format( |
| 76 | + Locale.getDefault(), |
| 77 | + " Must wait for [%s] to complete first.", |
| 78 | + predecessors.stream().map(p -> p.id()).collect(Collectors.joining(", ")) |
| 79 | + ) |
| 80 | + ); |
| 81 | + // TODO need to handle this better, passing an argument when we start them all at the beginning is silly |
| 82 | + futureList.add(n.execute()); |
| 83 | + } |
| 84 | + futureList.forEach(CompletableFuture::join); |
| 85 | + logger.info("All done!"); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments