hadoop-yetus commented on a change in pull request #575: HADOOP-13327 Output 
Stream Specification
URL: https://github.com/apache/hadoop/pull/575#discussion_r341726640
 
 

 ##########
 File path: 
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/impl/StreamStateModel.java
 ##########
 @@ -0,0 +1,267 @@
+/*
+ * 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.hadoop.fs.impl;
+
+import java.io.IOException;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
+
+import javax.annotation.Nullable;
+
+import com.google.common.base.Preconditions;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.hadoop.classification.InterfaceAudience;
+import org.apache.hadoop.classification.InterfaceStability;
+import org.apache.hadoop.fs.FSExceptionMessages;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.PathIOException;
+
+import static org.apache.hadoop.fs.FSExceptionMessages.STREAM_IS_CLOSED;
+
+/**
+ * Models a stream's state and can be used for checking this state before
+ * any operation, and for using a lock to manage exclusive access to operations
+ * within the stream.
+ *
+ * The model designed to ensure that a stream knows when it is closed,
+ * and, once it has entered a failed state, that the failure
+ * is not forgotten.
+ *
+ * This state model is based on the design of
+ * {@code org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream} and
+ * {@code org.apache.hadoop.fs.azure.PageBlobOutputStream} which both
+ * store and rethrow any previously raised error.
+ *
+ *
+ * The model has three states: Open, Error, and Closed:
+ *
+ * <pre>
+ *   {@link State#Open}: caller can interact with the stream.
+ *   {@link State#Error}: all operations will raise the previously recorded 
exception.
+ *   {@link State#Closed}: operations will be rejected.
+ * </pre>
+
+ * When an instance of the model is created, it is in {@link State#Open};
+ * a call to {@link #enterClosedState()} will move it from Open to Closed;
+ *
+ *
+ * <p>
+ * The lock/unlock operation relays to {@code java.util.concurrent.locks.Lock}
+ * and has a similar usage pattern. A key difference is that a check for the
+ * stream being open can be integrated with the lock acquisition.
+ * If {@link #acquireLock(boolean)} is called with checkOpen == true, then
+ * the caller will know that after the lock is granted then the stream
+ * is open.
+ * <pre>
+ *   model.acquireLock(true);
+ *   try {
+ *     (do some work)
+ *   } catch (IOException e) {
+ *     model.enterErrorState(e);
+ *   } finally {
+ *     model.releaseLock();
+ *   }
+ * </pre>
+ *
+ */
[email protected]("Filesystems")
[email protected]
+public class StreamStateModel {
+
+  /**
+   * States of the stream.
+   */
+  public enum State {
+
+    /**
+     * Stream is open.
+     */
+    Open,
+
+    /**
+     * Stream is in an error state.
+     * It is not expected to recover from this.
+     */
+    Error,
+
+    /**
+     * Stream is now closed. Operations will fail.
+     */
+    Closed
+  }
+
+  /**
+   * Path; if not empty then a {@link PathIOException} will be raised
+   * containing this path.
+   */
+  private final String path;
+
+  /** Lock. Not considering an InstrumentedWriteLock, but it is an option. */
+  private final Lock lock = new ReentrantLock();
+
+  /**
+   * Initial state: open.
+   */
+  private final AtomicReference<State> state =
+      new AtomicReference<>(State.Open);
+
+  /** Any exception to raise on the next checkOpen call. */
+  private IOException exception;
+
+  /**
+   * Create for a path. 
 
 Review comment:
   whitespace:end of line
   

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


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to