You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-issues@hadoop.apache.org by GitBox <gi...@apache.org> on 2019/06/07 15:41:54 UTC

[GitHub] [hadoop] hadoop-yetus commented on a change in pull request #575: HADOOP-13327 Output Stream Specification

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

 ##########
 File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/impl/StreamStateModel.java
 ##########
 @@ -0,0 +1,203 @@
+/*
+ * 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.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.
+ * 
+ * It's 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.
+ *
+ * The model has three states: Open, Error, and Closed,
+ *
+ * <pre>
+ *   Open: caller can interact with the stream.
+ *   Error: all operations will raise the previously recorded exception.
+ *   Closed: operations will be rejected.
+ * </pre>
+ */
+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. 
+   * @param path optional path for exception messages.
+   */
+  public StreamStateModel(@Nullable Path path) {
+    this.path = path != null ? path.toString() : "";
+  }
+
+  /**
+   * 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:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: common-issues-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-issues-help@hadoop.apache.org