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; 024 025import org.apache.yetus.audience.InterfaceAudience; 026import org.apache.hadoop.hbase.util.CancelableProgressable; 027import org.apache.hadoop.hdfs.protocol.DatanodeInfo; 028 029/** 030 * Interface for asynchronous filesystem output stream. 031 * <p> 032 * The implementation is not required to be thread safe. 033 */ 034@InterfaceAudience.Private 035public interface AsyncFSOutput extends Closeable { 036 037 /** 038 * Just call write(b, 0, b.length). 039 * @see #write(byte[], int, int) 040 */ 041 void write(byte[] b); 042 043 /** 044 * Copy the data into the buffer. Note that you need to call {@link #flush(boolean)} to flush the 045 * buffer manually. 046 */ 047 void write(byte[] b, int off, int len); 048 049 /** 050 * Write an int to the buffer. 051 */ 052 void writeInt(int i); 053 054 /** 055 * Copy the data in the given {@code bb} into the buffer. 056 */ 057 void write(ByteBuffer bb); 058 059 /** 060 * Return the current size of buffered data. 061 */ 062 int buffered(); 063 064 /** 065 * Whether the stream is broken. 066 */ 067 boolean isBroken(); 068 069 /** 070 * Return current pipeline. Empty array if no pipeline. 071 */ 072 DatanodeInfo[] getPipeline(); 073 074 /** 075 * Flush the buffer out. 076 * @param sync persistent the data to device 077 * @return A CompletableFuture that hold the acked length after flushing. 078 */ 079 CompletableFuture<Long> flush(boolean sync); 080 081 /** 082 * The close method when error occurred. 083 */ 084 void recoverAndClose(CancelableProgressable reporter) throws IOException; 085 086 /** 087 * Close the file. You should call {@link #recoverAndClose(CancelableProgressable)} if this method 088 * throws an exception. 089 */ 090 @Override 091 void close() throws IOException; 092}