Skip to content

Commit

Permalink
Almost found the core bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Rojods committed Dec 8, 2023
1 parent 34db1b7 commit 211ca22
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,10 @@ default IdentificationResult identification(Set<DesignModel> designModels, Set<D

default Optional<Javalin> standaloneModule(String[] args) {
var cachedDecisionModels = new ConcurrentHashMap<ByteBuffer, Optional<DecisionModel>>();
var cachedDecisionModelHashes = new ConcurrentSkipListSet<ByteBuffer>();
var cachedSolvedDecisionModels = new ConcurrentHashMap<ByteBuffer, DecisionModel>();
var cachedDesignModels = new ConcurrentHashMap<ByteBuffer, Optional<DesignModel>>();
var cachedReversedDesignModels = new ConcurrentHashMap<ByteBuffer, DesignModel>();
var sessionDesignModels = new ConcurrentHashMap<String, ConcurrentSkipListSet<DesignModel>>();
var sessionDecisionModels = new ConcurrentHashMap<String, ConcurrentSkipListSet<DecisionModel>>();
var sessionIdentifiedDecisionModels = new ConcurrentHashMap<String, Deque<DecisionModel>>();
var sessionExplorationStream = new ConcurrentHashMap<String, Stream<? extends ExplorationSolution>>();
var sessionExploredModels = new ConcurrentHashMap<String, ConcurrentSkipListSet<DecisionModel>>();
var sessionReversedDesignModels = new ConcurrentHashMap<String, Deque<DesignModel>>();
try (var server = Javalin.create()) {
server
Expand All @@ -103,7 +98,7 @@ default Optional<Javalin> standaloneModule(String[] args) {
.get("/decision/cache/exists",
ctx -> {
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
if (cachedDecisionModelHashes.contains(bb)) {
if (cachedDecisionModels.contains(bb)) {
// System.out.println("YES decision cache exists of "
// + Arrays.toString(ctx.bodyAsBytes()));
ctx.result("true");
Expand Down Expand Up @@ -171,6 +166,7 @@ default Optional<Javalin> standaloneModule(String[] args) {
ctx -> {
// System.out.println("Adding to decision cache: " + ctx.body());
OpaqueDecisionModel.fromJsonString(ctx.body()).ifPresent(opaque -> {
System.out.println("Adding to decision cache: " + opaque.globalMD5Hash().map(Arrays::toString).orElse("NO HASH"));
opaque.globalMD5Hash().ifPresent(hash -> cachedDecisionModels
.put(ByteBuffer.wrap(hash), fromOpaqueDecision(opaque)));
});
Expand Down Expand Up @@ -352,6 +348,7 @@ default Optional<Javalin> standaloneModule(String[] args) {
ctx.result(objectMapper.writeValueAsString(
explorers().stream().map(Explorer::uniqueIdentifier).collect(Collectors.toSet())));
}).get("/{explorerName}/bid", ctx -> {
System.out.println("Bidding with %s".formatted(ctx.pathParam("explorerName")));
explorers().stream()
.filter(e -> e.uniqueIdentifier().equalsIgnoreCase(ctx.pathParam("explorerName")))
.findAny()
Expand All @@ -376,20 +373,26 @@ default Optional<Javalin> standaloneModule(String[] args) {
}
});
} else {
System.out.println("Not multipart");
var bb = ByteBuffer.wrap(ctx.bodyAsBytes());
System.out.println("Bidding with %s and %s".formatted(Arrays.toString(ctx.bodyAsBytes()),
explorer.uniqueIdentifier()));
if (cachedDecisionModels.containsKey(bb)) {
cachedDecisionModels.get(bb)
.map(decisionModel -> explorer.bid(explorers(), decisionModel))
.map(decisionModel -> {
System.out.println("Bidding with %s and %s".formatted(decisionModel.category(), explorer.uniqueIdentifier()));
return explorer.bid(explorers(), decisionModel);
})
.ifPresentOrElse(bid -> {
try {
ctx.result(objectMapper.writeValueAsString(bid));
objectMapper.writeValueAsString(bid);
} catch (JsonProcessingException e1) {
e1.printStackTrace();
ctx.status(500);
}
}, () -> ctx.status(404));
} else {
System.out.println("Decision model not found in cache");
ctx.status(404);
}
}
Expand Down
10 changes: 8 additions & 2 deletions rust-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ pub trait DesignModel: Send + DowncastSync {
let mut hasher = md5::Context::new();
hasher.consume(self.format().as_bytes());
hasher.consume(self.category().as_bytes());
for e in self.elements() {
let elements = self.elements();
let mut sorted = elements.iter().collect::<Vec<&String>>();
sorted.sort();
for e in sorted {
hasher.consume(e.as_bytes());
}
hasher.compute().to_vec()
Expand Down Expand Up @@ -192,7 +195,10 @@ pub trait DecisionModel: Send + DowncastSync {
fn global_md5_hash(&self) -> Vec<u8> {
let mut hasher = md5::Context::new();
hasher.consume(self.category().as_bytes());
for e in self.part() {
let part = self.part();
let mut sorted: Vec<&String> = part.iter().collect();
sorted.sort();
for e in sorted {
hasher.consume(e.as_bytes());
}
hasher.compute().to_vec()
Expand Down
40 changes: 30 additions & 10 deletions rust-orchestration/src/exploration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,37 @@ impl Explorer for ExternalExplorer {
_explorers: &Vec<Arc<dyn Explorer>>,
m: Arc<dyn DecisionModel>,
) -> ExplorationBid {
let mut form = reqwest::blocking::multipart::Form::new();
form = form.part(
format!("decisionModel"),
reqwest::blocking::multipart::Part::text(
OpaqueDecisionModel::from(m)
.to_json()
.expect("Failed to make Json out of opaque decision model. Should never fail."),
),
);
// let mut form = reqwest::blocking::multipart::Form::new();
// form = form.part(
// format!("decisionModel"),
// reqwest::blocking::multipart::Part::text(
// OpaqueDecisionModel::from(m)
// .to_json()
// .expect("Failed to make Json out of opaque decision model. Should never fail."),
// ),
// );
let model_hash = m.global_md5_hash();
let exists = self
.url
.join("/decision/cache/exists")
.ok()
.and_then(|u| self.client.get(u).body(model_hash.clone()).send().ok())
.and_then(|r| r.text().ok())
.map(|t| t.eq_ignore_ascii_case("true"))
.unwrap_or(false);
if !exists {
debug!("{} is not in cache for {}. Adding it.", m.category(), self.unique_identifier());
if let Ok(json_str) = OpaqueDecisionModel::from(m).to_json() {
if let Ok(r) = self.client
.put(self.url.join("/decision/cache/add").unwrap())
.body(json_str)
.send() {
debug!("Added decision model to cache: {}", r.text().unwrap_or("Failed to get response text".to_owned()));
};
}
}
if let Ok(bid_url) = self.url.join(format!("/{}/bid", self.name).as_str()) {
match self.client.get(bid_url).multipart(form).send() {
match self.client.get(bid_url).body(model_hash).send() {
Ok(result) => match result.text() {
Ok(text) => {
if !text.is_empty() {
Expand Down

0 comments on commit 211ca22

Please sign in to comment.