Skip to content

Commit

Permalink
Changes made to storage after the last sync are stored (closes #130)
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Jan 4, 2021
1 parent a9236f3 commit 9b475a9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ a link to this page somewhere in the documentation/system about section.
* :bug_up: Removed extraneous dependencies from `pom.xml` (([#133](https://github.com/bThink-BGU/BPjs/issues/133)).
* :arrow_up: BThread data documentation updated (([#134](https://github.com/bThink-BGU/BPjs/issues/134)).
* :arrow_up: BProgram storage can be updated by the b-program before b-threads run (([#129](https://github.com/bThink-BGU/BPjs/issues/129)).
* :arrow_up: Changes made to the b-program store after the last sync of a b-thread are now applied. (([#130](https://github.com/bThink-BGU/BPjs/issues/130)).


[Earlier Changes](changelog-2020.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public static void clearCurrentBThread(){
CURRENT_BTHREAD.remove();
}

public static MapProxy getCurrentChanges() {
BThreadData bThreadData = CURRENT_BTHREAD.get();
return (bThreadData != null) ? bThreadData.storeModifications : null;
}

// /thread-local
///////////////////////////////

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import il.ac.bgu.cs.bp.bpjs.exceptions.BPjsRuntimeException;
import il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy;
import il.ac.bgu.cs.bp.bpjs.execution.jsproxy.BProgramJsProxy.CapturedBThreadState;
import il.ac.bgu.cs.bp.bpjs.execution.jsproxy.MapProxy;
import il.ac.bgu.cs.bp.bpjs.model.BProgram;
import il.ac.bgu.cs.bp.bpjs.model.BProgramSyncSnapshot;
import il.ac.bgu.cs.bp.bpjs.model.SyncStatement;
Expand Down Expand Up @@ -56,6 +57,13 @@ public BThreadSyncSnapshot call() {
try {
BProgramJsProxy.setCurrentBThread(bpss, btss);
callImpl( jsContext );
// capture proxy changes
MapProxy<String,Object> changes = BProgramJsProxy.getCurrentChanges();
if ( changes != null ) {
if ( ! changes.getModifications().isEmpty() ) {
return new BThreadSyncSnapshot(btss.getName(), null, null, null, null, null, changes);
}
}
return null;

} catch (ContinuationPending cbs) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/il/ac/bgu/cs/bp/bpjs/model/BEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import il.ac.bgu.cs.bp.bpjs.internal.ScriptableUtils;
import il.ac.bgu.cs.bp.bpjs.model.eventsets.EventSet;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -127,7 +128,7 @@ public boolean equals(Object obj) {

@Override
public int hashCode() {
return 19*name.hashCode() ^ getDataField().map(ScriptableUtils::jsHash).orElse(0);
return 19*Objects.hashCode(name) ^ getDataField().map(ScriptableUtils::jsHash).orElse(0);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ private BProgramSyncSnapshot createNextSnapshot(Set<BThreadSyncSnapshot> nextRou
mpcRes = sms.resolve((Conflict) mpcRes, this, nextRound);
}

// remove b-threads that are done, but but submitted some storage modifications.
nextRound = nextRound.stream().filter(BThreadSyncSnapshot::canContinue).collect(toSet());

if ( mpcRes instanceof Success ) {
Success success = (Success)mpcRes;
success = sms.incomingModifications(success, this, nextRound);
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/model/BThreadSyncSnapshot.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,17 @@ public void clearStorageModifications() {
bprogramStoreModifications.reset();
}

/**
* When {@code true}, this b-thread can continue. In cases where the return
* value is {@code false}, this object holds data about the results of the
* b-thread execution, e.g. storage modifications.
*
* @return {@code true} iff this b-thread can run another round.
*/
public boolean canContinue(){
return (getContinuation()!=null) || (getEntryPoint()!=null);
}

public ContinuationProgramState getContinuationProgramState() {
if ( programState == null ) {
programState = new ContinuationProgramState(continuation);
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/il/ac/bgu/cs/bp/bpjs/model/BProgramStoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,18 @@ public void preRunStorageTest() {
assertEquals( List.of("Josh", "Agenta Falskog"), evtListener.eventNames() );
}

@Test
public void lastLeg_run() {
BProgram sut = new ResourceBProgram("bp_store/bpStore_lastLegChanges.js");
BProgramRunner rnr = new BProgramRunner();
rnr.addListener(new PrintBProgramRunnerListener());
var evtListener = rnr.addListener(new InMemoryEventLoggingListener());

rnr.setBProgram(sut);
rnr.run();

assertEquals( List.of("A","B","C","D"), evtListener.eventNames() );
}


}
42 changes: 42 additions & 0 deletions src/test/resources/bp_store/bpStore_lastLegChanges.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* The MIT License
*
* Copyright 2021 michael.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

/* global bp */

bp.registerBThread("t0", function(){
bp.store.put("C", "D");
});

bp.registerBThread("t1", function(){
bp.sync({request:bp.Event("A")});
bp.store.put("A", "C");
});

bp.registerBThread("t2", function(){
bp.sync({waitFor:bp.Event("A")});
bp.sync({request:bp.Event("B")});
bp.sync({request:bp.Event(bp.store.get("A"))});
bp.sync({request:bp.Event(bp.store.get("C"))});
});

0 comments on commit 9b475a9

Please sign in to comment.