Way to do prepare commit before final commit using Java API?

51 views
Skip to first unread message

Saxo

unread,
Dec 9, 2012, 7:50:55 AM12/9/12
to scala-stm-e...@googlegroups.com
Hello,

I played a bit with the Java API as I'm no Scala whiz. I was looking for a way to do a prepare commit inside the atomic block, but couln't find a way to do this. Reason is that I want to send notifications out that some data changed. Therafter I do the final commit. If I send the notifications out after the commit I'm out of the protected area spanned by the atomic block and might run into trouble caused by subsequent context switches.

Is there some way to achieve what I'm looking for?

Thank you, Oliver

Saxo

unread,
Dec 11, 2012, 4:02:55 AM12/11/12
to scala-stm-e...@googlegroups.com
I tried to get this to compile:


final TMap.View<String, String> map = STM.newTMap();
        STM.atomic(new Runnable() {
            public void run() {
                final InTxn txn = Txn.findCurrent(TxnUnknown$.MODULE$).get();
                map.tmap().put("1", "1", txn);
            }
        });

This TxnUnknown$.MODULE$ thing does not compile. I also looked at this ssample code: https://gist.github.com/2516410 But I dont understand what you mean by TxnUnknown$.MODULE$.

Any hints how to get my code snippet to work appreciate. I also get some compiler error TMap<String, String> refers to missing class Option. Does this mean I need to have the Scala jars as well.

Thank you, Oliver

Nathan Bronson

unread,
Dec 11, 2012, 10:46:36 AM12/11/12
to scala-stm-e...@googlegroups.com
Oliver,

Perhaps you are missing an import? Yes, you will need to add scala-library.jar to your path even though you are writing in Java.

In this particular example you don't need an explicit handle to the current transaction, the TMap.View will look it up dynamically for you while implementing the generic java.util.Map interface. You can just call map.put("1", "1") from inside STM.atomic and it will participate in the transaction. (TMap.View.put always thread-safe even outside a transaction, btw.)

Can you tell me a bit more about your original question's use case? The STM equivalent to the post-preparation phase is a TxnDecider, but that is most likely not the right thing unless you are integrating with another transactional resource (like a database). Are the notifications you are sending synchronous or asynchronous? Will they do I/O? Do you want them to read values that are updated in the atomic block?

Thanks,
  Nathan 



--
 
 
 



--
Nathan Grasso Bronson
ngbr...@gmail.com

Saxo

unread,
Dec 11, 2012, 5:23:50 PM12/11/12
to scala-stm-e...@googlegroups.com
Thanks Nathan,

I got it to compile after I supplied scala-library.jar and it works now:

final Map<String, String> map = STM.newMap();

        STM.atomic(new Runnable() {           
            public void run() {
                map.put("1", "2");
            }
        });


Here is some sample code to show what the use case was in my initial post:

        final Map<String, String> map1 = STM.newMap();
        final Map<String, String> map2 = STM.newMap();
        // add some data to map1 and map2
 
        STM.atomic(Txn txn, new Runnable() {           
            public void run() {
                map1.put("1", "2");
                map2.remove("3");
                txn.prepare();
                // prepare commit was successful if we get here
                // now send out notifications that value added to map1 with key "1" and value removed from map2 with key "3"
                tx.commit();
            }
        });

The idea is here that I can do a prepare commit. If it is successful, it is guaranteed that the final commit will succeed. So after the prepare commit I can send my notifications out about what data changed in what day. This way the notifications are send out in the order respective atomic blocks commit. For a real scenario some change log would be walked through to send all the notifications out. When that has been completed the final commit is done. Synchronous notification would be fine enough for me.

Problem is that when I do something like this:


STM.atomic(new Runnable() {          
            public void run() {
                map1.put("1", "2");
                // make entry to change log
                map2.remove("3");
             // make entry to change log

            }
        });
       
        STM.atomic(new Runnable() {
            public void run() {
                changelog do: send every change notification out               
            }           
        });

I'm not sure that the notifications will be sent out in the order individual atomic blocks commit as there might be context switches between the former atomic block and the latter where the change log is processed.

I'm not looking for something specific that das a prepare commit and then a final commit. My question is only whether there is a way to do what I'm looking for.

Kind regards, Oliver
Reply all
Reply to author
Forward
0 new messages