Use parallel stream to process YANG text files 51/86851/1
authorRobert Varga <robert.varga@pantheon.tech>
Tue, 7 Jan 2020 23:21:49 +0000 (00:21 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 11 Jan 2020 22:52:31 +0000 (23:52 +0100)
When we are examining local YANG files, we are also performing
first step of parsing to determine what the module/revision is
without relying on the file name. This process can take quite
some time if the models are large and/or there are many of them.

Since each file is independent at this stage, use a parallel
stream (and hence common FJ pool) to perform this task in multiple
threads before moving on to processing.

JIRA: YANGTOOLS-1062
Change-Id: I31cee2aa7f313052565ba723cf4c52566821a625
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit fcb529766049a3ab75cd9a0de4603c0a815c5a0b)

yang/yang-maven-plugin/src/main/java/org/opendaylight/yangtools/yang2sources/plugin/YangToSourcesProcessor.java

index 2879f03fd94cf8cc51556d39668264e88639d7ca..4490cccb03e9bcabac9d12e70eb252836f9959d9 100644 (file)
@@ -267,10 +267,22 @@ class YangToSourcesProcessor {
 
             final YangParser parser = parserFactory.createParser(parserMode);
             final List<YangTextSchemaSource> sourcesInProject = new ArrayList<>(yangFilesInProject.size());
-            for (final File f : yangFilesInProject) {
-                final YangTextSchemaSource textSource = YangTextSchemaSource.forFile(f);
-                final ASTSchemaSource astSource = TextToASTTransformer.transformText(textSource);
 
+            final List<Entry<YangTextSchemaSource, ASTSchemaSource>> parsed = yangFilesInProject.parallelStream()
+                    .map(file -> {
+                        final YangTextSchemaSource textSource = YangTextSchemaSource.forFile(file);
+                        try {
+                            return new SimpleImmutableEntry<>(textSource,
+                                    TextToASTTransformer.transformText(textSource));
+                        } catch (YangSyntaxErrorException | SchemaSourceException | IOException e) {
+                            throw new IllegalArgumentException("Failed to parse " + file, e);
+                        }
+                    })
+                    .collect(Collectors.toList());
+
+            for (final Entry<YangTextSchemaSource, ASTSchemaSource> entry : parsed) {
+                final YangTextSchemaSource textSource = entry.getKey();
+                final ASTSchemaSource astSource = entry.getValue();
                 parser.addSource(astSource);
 
                 if (!astSource.getIdentifier().equals(textSource.getIdentifier())) {
@@ -288,7 +300,7 @@ class YangToSourcesProcessor {
             final ProcessorModuleReactor reactor = new ProcessorModuleReactor(parser, sourcesInProject, dependencies);
             LOG.debug("Initialized reactor {} with {}", reactor, yangFilesInProject);
             return Optional.of(reactor);
-        } catch (IOException | SchemaSourceException | YangSyntaxErrorException | RuntimeException e) {
+        } catch (IOException | YangSyntaxErrorException | RuntimeException e) {
             // MojoExecutionException is thrown since execution cannot continue
             LOG.error("{} Unable to parse YANG files from {}", LOG_PREFIX, yangFilesRootDir, e);
             Throwable rootCause = Throwables.getRootCause(e);