ajfabbri commented on a change in pull request #575: HADOOP-13327 Output Stream Specification URL: https://github.com/apache/hadoop/pull/575#discussion_r265691746
########## File path: hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/impl/StreamStateModel.java ########## @@ -0,0 +1,205 @@ +/* + * 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.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +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. + * + * 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. + * This is volatile: it can be queried without encountering any locks. + * However, to guarantee the state is constant through the life of an + * operation, updates must be through the synchronized methods. + */ + private volatile State state = State.Open; + + /** Any exception to raise on the next checkOpen call. */ + private IOException exception; + + public StreamStateModel(final Path path) { + this.path = path.toString(); + } + + public StreamStateModel(final String path) { + this.path = path; + } + + /** + * Get the current state. + * Not synchronized; lock if you want consistency across calls. + * @return the current state. + */ + public State getState() { + return state; + } + + /** + * Change state to closed. No-op if the state was in closed or error + * @return true if the state changed. + */ + public synchronized boolean enterClosedState() { + if (state == State.Open) { + state = State.Closed; + return true; + } else { + return false; + } + } + + /** + * Change state to error and stores first error so it can be re-thrown. + * If already in error: return previous exception. + * @param ex the exception to record + * @return the exception set when the error state was entered. Review comment: Hi Sean! Current code, yes, but would we ever actually need that information? (Maybe in a unit test). It is going to be in error when the call returns either way. ---------------------------------------------------------------- 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]
