001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.io.asyncfs;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.nio.ByteBuffer;
023import java.util.concurrent.CompletableFuture;
024import org.apache.hadoop.hbase.util.CancelableProgressable;
025import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
026import org.apache.yetus.audience.InterfaceAudience;
027
028/**
029 * Interface for asynchronous filesystem output stream.
030 * <p>
031 * The implementation is not required to be thread safe.
032 */
033@InterfaceAudience.Private
034public interface AsyncFSOutput extends Closeable {
035
036  /**
037   * Just call write(b, 0, b.length).
038   * @see #write(byte[], int, int)
039   */
040  void write(byte[] b);
041
042  /**
043   * Copy the data into the buffer. Note that you need to call {@link #flush(boolean)} to flush the
044   * buffer manually.
045   */
046  void write(byte[] b, int off, int len);
047
048  /**
049   * Write an int to the buffer.
050   */
051  void writeInt(int i);
052
053  /**
054   * Copy the data in the given {@code bb} into the buffer.
055   */
056  void write(ByteBuffer bb);
057
058  /**
059   * Return the current size of buffered data.
060   */
061  int buffered();
062
063  /**
064   * Whether the stream is broken.
065   */
066  boolean isBroken();
067
068  /**
069   * Return current pipeline. Empty array if no pipeline.
070   */
071  DatanodeInfo[] getPipeline();
072
073  /**
074   * Flush the buffer out.
075   * @param sync persistent the data to device
076   * @return A CompletableFuture that hold the acked length after flushing.
077   */
078  CompletableFuture<Long> flush(boolean sync);
079
080  /**
081   * The close method when error occurred.
082   */
083  void recoverAndClose(CancelableProgressable reporter) throws IOException;
084
085  /**
086   * Close the file. You should call {@link #recoverAndClose(CancelableProgressable)} if this method
087   * throws an exception.
088   */
089  @Override
090  void close() throws IOException;
091
092  /** Returns byteSize success synced to underlying filesystem. */
093  long getSyncedLength();
094}