Skip to content

Commit

Permalink
feat(agama): allow the config-api to perform syntax check of flows (#…
Browse files Browse the repository at this point in the history
…1621)

* chore: optimize and refactor transpiler #1611

* feat: allow antlr4 source files be generated on the fly #1611

* chore: minor updates #1611
  • Loading branch information
jgomer2001 authored Jun 28, 2022
1 parent 3b64853 commit 2e99d3a
Show file tree
Hide file tree
Showing 18 changed files with 100 additions and 6,508 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package io.jans.agama.engine.continuation;

import java.util.Map;

import org.mozilla.javascript.NativeContinuation;

public class PendingRenderException extends PendingException {

private String templatePath;
private Map<String, Object> dataModel;
private Object dataModel;

public PendingRenderException(NativeContinuation continuation) {
super(continuation);
Expand All @@ -21,11 +19,11 @@ public void setTemplatePath(String templatePath) {
this.templatePath = templatePath;
}

public Map<String, Object> getDataModel() {
public Object getDataModel() {
return dataModel;
}

public void setDataModel(Map<String, Object> dataModel) {
public void setDataModel(Object dataModel) {
this.dataModel = dataModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Pair<Boolean, String> pauseForRender(String page, boolean allowCal
PendingRenderException pending = new PendingRenderException(
(NativeContinuation) cx.captureContinuation().getContinuation());
pending.setTemplatePath(page);
pending.setDataModel((Map<String, Object>) data);
pending.setDataModel(data);
pending.setAllowCallbackResume(allowCallbackResume);
LOG.debug("Pausing flow");
throw pending;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,13 +240,12 @@ private void verifyCode(Flow fl) throws IOException {
throw new IOException(msg);
}

if (Optional.ofNullable(engineConfig.getDisableTCHV()).orElse(false)) {
if (!Optional.ofNullable(engineConfig.getDisableTCHV()).orElse(false)) {

String hash = fl.getTransHash();
//null hash means the code is being regenerated in this moment

if (hash != null && !flowUtils.hash(code).equals(hash))
throw new IOException("Flow code seems to have been altered. " +
throw new IOException("Transpiled code seems to have been altered. " +
"Restore the code by increasing this flow's jansRevision attribute");
}

Expand Down
45 changes: 21 additions & 24 deletions agama/engine/src/main/java/io/jans/agama/timer/Transpilation.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import jakarta.inject.Inject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
Expand All @@ -38,7 +39,7 @@
public class Transpilation {

private static final int DELAY = 10 + (int) (10 * Math.random()); //seconds
private static final int INTERVAL = 45; // seconds
private static final int INTERVAL = 30; // seconds
private static final double PR = 0.25;

@Inject
Expand Down Expand Up @@ -89,11 +90,6 @@ public void run(@Observes @Scheduled TranspilationEvent event) {

}

private Map<String, Integer> makeSimpleMap(Map<String, ProtoFlow> map) {
return map.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey, e -> e.getValue().getRevision()));
}

/**
* This method assumes that when a flow is created (eg. via an administrative tool),
* attribute revision is set to a negative value
Expand All @@ -108,32 +104,33 @@ public void process() throws IOException {
Collectors.toMap(ProtoFlow::getQName, Function.identity()));

if (traces == null) {
traces = makeSimpleMap(map);
traces = map.entrySet().stream().collect(Collectors.toMap(
Map.Entry::getKey, e -> e.getValue().getRevision()));
//make it modifiable
traces = new HashMap<>(traces);
} else {
//remove flows that were disabled/removed wrt the previous timer run
traces.keySet().retainAll(map.keySet());
}

List<String> candidates = new ArrayList<>();
for (String name : map.keySet()) {

int rev;
Integer revision = traces.get(name);

if (revision == null) {
//A newcomer. This script was enabled recently
candidates.add(name);
} else {
ProtoFlow pfl = map.get(name);

if (pfl.getTransHash() == null && Math.random() < PR) {
ProtoFlow pfl = map.get(name);
Integer rev = pfl.getRevision();

if (rev != null) {
if (!traces.containsKey(name)) {
//A newcomer. This script was enabled recently
candidates.add(name);
traces.put(name, rev);
} else if (
//there might be a compilation of this script running already.
//If the node in charge of this crashed before completion, the random
//condition helps to get the job done by another node in the near future
(pfl.getTransHash() == null && Math.random() < PR) ||
(rev < 0 || rev > traces.get(name))) {
candidates.add(name);
} else {

rev = pfl.getRevision();
if (rev < 0 || rev > revision) {
candidates.add(name);
}
}
}
}
Expand All @@ -153,6 +150,7 @@ public void process() throws IOException {

logger.debug("Marking the script is under compilation");
entryManager.merge(pfl);
traces.put(qname, pfl.getRevision());

//This time retrieve all attributes for the flow of interest
Flow fl = entryManager.findEntries(AgamaPersistenceService.AGAMA_FLOWS_BASE,
Expand Down Expand Up @@ -205,7 +203,6 @@ public void process() throws IOException {
logger.warn("Check database for errors");
}
}
traces = makeSimpleMap(map);

}

Expand Down
165 changes: 0 additions & 165 deletions agama/transpiler/grammar/AuthnFlow.interp

This file was deleted.

96 changes: 0 additions & 96 deletions agama/transpiler/grammar/AuthnFlow.tokens

This file was deleted.

Loading

0 comments on commit 2e99d3a

Please sign in to comment.