[ 
https://issues.apache.org/jira/browse/BEAM-4071?focusedWorklogId=92268&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-92268
 ]

ASF GitHub Bot logged work on BEAM-4071:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 18/Apr/18 22:51
            Start Date: 18/Apr/18 22:51
    Worklog Time Spent: 10m 
      Work Description: jkff commented on a change in pull request #5150:  
[BEAM-4071] Add Portable Runner Job API shim
URL: https://github.com/apache/beam/pull/5150#discussion_r182591993
 
 

 ##########
 File path: 
runners/reference/java/src/main/java/org/apache/beam/runners/reference/CloseableResource.java
 ##########
 @@ -0,0 +1,119 @@
+/*
+ * 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.beam.runners.reference;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+
+import javax.annotation.Nullable;
+
+/**
+ * An {@link AutoCloseable} that wraps a resource that needs to be cleaned up 
but does not implement
+ * {@link AutoCloseable} itself.
+ *
+ * <p>Recipients of a {@link CloseableResource} are in general responsible for 
cleanup. Ownership
+ * can be transferred from one context to another via {@link #transfer()}. 
Transferring relinquishes
+ * ownership from the original resource. This allows resources to be safely 
constructed and
+ * transferred within a try-with-resources block. For example:
+ *
+ * <pre>try (CloseableResource&lt;Foo&gt; resource = 
CloseableResource.of(...)) {
+ *   // Do something with resource.
+ *   ...
+ *   // Then transfer ownership to some consumer.
+ *   resourceConsumer(resource.release());
+ * }</pre>
+ *
+ * <p>Not thread-safe.
+ */
+public class CloseableResource<T> implements AutoCloseable {
+
+  private final T resource;
+
+  /**
+   * {@link Closer } for the underlying resource. Closers are nullable to 
allow transfer of
+   * ownership. However, newly-constructed {@link CloseableResource 
CloseableResources} must always
+   * have non-null closers.
+   */
+  @Nullable private Closer<T> closer;
+
+  private boolean isClosed = false;
+
+  private CloseableResource(T resource, Closer<T> closer) {
+    this.resource = resource;
+    this.closer = closer;
+  }
+
+  /** Creates a {@link CloseableResource} with the given resource and closer. 
*/
+  public static <T> CloseableResource<T> of(T resource, Closer<T> closer) {
+    checkArgument(resource != null, "Resource must be non-null");
+    checkArgument(closer != null, "%s must be non-null", 
Closer.class.getName());
+    return new CloseableResource<>(resource, closer);
+  }
+
+  /** Gets the underlying resource. */
+  public T get() {
+    checkState(closer != null, "%s has transferred ownership", 
CloseableResource.class.getName());
+    checkState(!isClosed, "% is closed", CloseableResource.class.getName());
+    return resource;
+  }
+
+  /**
+   * Returns a new {@link CloseableResource} that owns the underlying resource 
and relinquishes
+   * ownership from this {@link CloseableResource}. {@link #close()} on the 
original instance
+   * becomes a no-op.
+   */
+  public CloseableResource<T> transfer() {
 
 Review comment:
   <!--thread_id:cc_182519308_t; 
commit:1196e2664330e9135b7d5fcc5b1fe75bf3f91bda; resolved:1-->
   <!--section:context-quote-->
   > **bsidhom** wrote:
   > It would be really nice to have the compiler enforce that existing 
`CloseableResource` references are never passed as arguments but that they are 
instead always `transfer()`'d in. I'm envisioning something like safe RAII move 
semantics in C++. Is there a FindBugs check that could enforce this?
   
   <!--section:body-->
   There's no findbugs check that can do that AFAIK. It should be possible to 
write a check using a compiler plugin, but I think practically speaking we 
won't :-/

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 92268)
    Time Spent: 9h 20m  (was: 9h 10m)

> Portable Runner Job API shim
> ----------------------------
>
>                 Key: BEAM-4071
>                 URL: https://issues.apache.org/jira/browse/BEAM-4071
>             Project: Beam
>          Issue Type: New Feature
>          Components: runner-core
>            Reporter: Ben Sidhom
>            Assignee: Ben Sidhom
>            Priority: Minor
>          Time Spent: 9h 20m
>  Remaining Estimate: 0h
>
> There needs to be a way to execute Java-SDK pipelines against a portable job 
> server. The job server itself is expected to be started up out-of-band. The 
> "PortableRunner" should take an option indicating the Job API endpoint and 
> defer other runner configurations to the backend itself.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to