You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by afs <gi...@git.apache.org> on 2016/07/09 21:42:26 UTC

[GitHub] jena pull request #153: JENA-1090: Txn - a java8-centric transaction API

GitHub user afs opened a pull request:

    https://github.com/apache/jena/pull/153

    JENA-1090: Txn - a java8-centric transaction API

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/afs/jena txn

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/jena/pull/153.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #153
    
----
commit 952aebd0ac8f2d5d590811f0ac97d3c17c9bea5a
Author: Andy Seaborne <an...@apache.org>
Date:   2016-07-09T21:40:03Z

    JENA-1090 : Txn - a java8-centric transaction API

commit 52ff200c2756c9844b4346ffa1bff2a09081b649
Author: Andy Seaborne <an...@apache.org>
Date:   2016-07-09T21:41:01Z

    Merge branch 'master' of https://github.com/apache/jena into txn

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by ajs6f <gi...@git.apache.org>.
Github user ajs6f commented on the issue:

    https://github.com/apache/jena/pull/153
  
    Okay, so the appropriateness of `finally` depends on where you are (app vs. system code). Makes sense.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/153
  
    There is markdown documentation to go with this.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/153
  
    If one of the transaction operations throws anything then the system is broken. It should not happen and would indicate an unrepairable error condition (e.g. NPE, broken journal). 
    
    After that, all bets are off!



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/153
  
    Semaphore hand out permits so 0 means no permit.
    
    There is a `semaStart.release()` in `run()` and the corresponding `acquire` in the lambda in `create` which has comments. Renaming the local `semaStart` would help.
    
    The `AtomicReference` can be combined in `AtomicReference<Throwable>`.
    
    `ThreadTxn` came about for testing purposes - `Txn` is the main contribution.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request #153: JENA-1090: Txn - a java8-centric transaction API

Posted by ajs6f <gi...@git.apache.org>.
Github user ajs6f commented on a diff in the pull request:

    https://github.com/apache/jena/pull/153#discussion_r70181662
  
    --- Diff: jena-arq/src/main/java/org/apache/jena/system/ThreadTxn.java ---
    @@ -0,0 +1,148 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.jena.system;
    +
    +import java.util.Objects ;
    +import java.util.concurrent.Executor ;
    +import java.util.concurrent.Executors ;
    +import java.util.concurrent.Semaphore ;
    +import java.util.concurrent.atomic.AtomicReference ;
    +
    +import org.apache.jena.query.ReadWrite ;
    +import org.apache.jena.sparql.core.Transactional ;
    +
    +/**
    + * An action that will happen on a different thread later when {@link #run} is
    + * called. A thread is created and the transaction started during a call to the
    + * creation operations {@link #threadTxnRead} or {@link #threadTxnWrite}.
    + * The associated Runnable is called and the transaction completed when
    + * {@link #run} is called. Being on a thread, the state of the world the
    + * forked transaction sees is outside the creating thread which may itself be in a
    + * transaction. Warning: creating a write transaction inside a write transaction
    + * will cause deadlock.
    + */ 
    +public class ThreadTxn {
    +    // ---- Thread
    +
    +    /** Create a thread-backed delayed READ transaction action. 
    +     * Call {@link ThreadTxn#run} to perform the read transaction.
    +     */
    +    public static ThreadTxn threadTxnRead(Transactional trans, Runnable action) {
    +        return ThreadTxn.create(trans, ReadWrite.READ, action, false) ;
    +    }
    +
    +    /** Create a thread-backed delayed WRITE  action.
    +     * Call {@link ThreadTxn#run} to perform the write transaction.
    +     * (If called from inside a write transaction on the {@code trans},
    +     * this will deadlock.)
    +     */
    +    public static ThreadTxn threadTxnWrite(Transactional trans, Runnable action) {
    +        return ThreadTxn.create(trans, ReadWrite.WRITE, action, true) ;
    +    }
    +   
    +    /** Create a thread-backed delayed WRITE-abort action (mainly for testing). */
    +    public static ThreadTxn threadTxnWriteAbort(Transactional trans, Runnable action) {
    +        return ThreadTxn.create(trans, ReadWrite.WRITE, action, false) ;
    +    }
    +    
    +    private final Semaphore semaStart ;
    +    private final Semaphore semaFinish ;
    +    private final AtomicReference<RuntimeException> thrownRuntimeException = new AtomicReference<>(null) ; 
    +    private final AtomicReference<Error> thrownError = new AtomicReference<>(null) ;
    +    private final Runnable action ;
    +    
    +    private ThreadTxn(Runnable action) {
    +        this.action = action ;
    +        this.semaStart = new Semaphore(0, true) ;
    --- End diff --
    
    I'm a little confused about the way these semaphores work. E.g. how do they function with 0 permits? Below, in `::run()` you release the start semaphore, but I can't see where you acquire it\u2026�on the theory (possibly a bad one) that other newbies will also find this opaque, can you comment this up a bit to explain the concurrency control at work here?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by ajs6f <gi...@git.apache.org>.
Github user ajs6f commented on the issue:

    https://github.com/apache/jena/pull/153
  
    Thanks, @afs , that definitely helps. IIUC, `ThreadTxn` looks like kind of a "single-class" version of the mechanics in `Txn`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by afs <gi...@git.apache.org>.
Github user afs commented on the issue:

    https://github.com/apache/jena/pull/153
  
    Merged but this code always benefits from ongoing code review!


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena issue #153: JENA-1090: Txn - a java8-centric transaction API

Posted by ajs6f <gi...@git.apache.org>.
Github user ajs6f commented on the issue:

    https://github.com/apache/jena/pull/153
  
    We usually use `try-finally` for the transaction cycle, i.e.
    ```
    try {
        transactionalThing.begin(readWrite);
        //do some stuff
        transactionalThing.commit(); // if a write transaction
    }
    finally {
        transactionalThing.end();
    }
    ```
    Looking at `onThrowable` it looks like if the `Transactional txn` throws anything other than a `JenaTransactionException` out of `txn.abort()`, then `txn.end()` will never get called. Does that open a whole for `txn` to never actually finish aborting/ending?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

[GitHub] jena pull request #153: JENA-1090: Txn - a java8-centric transaction API

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/jena/pull/153


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---