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.regionserver.wal; 019 020import java.io.IOException; 021import java.io.OutputStream; 022import org.apache.hadoop.fs.FSDataOutputStream; 023import org.apache.hadoop.fs.FileSystem; 024import org.apache.hadoop.fs.Path; 025import org.apache.hadoop.hbase.Cell; 026import org.apache.hadoop.hbase.util.CommonFSUtils; 027import org.apache.hadoop.hbase.util.CommonFSUtils.StreamLacksCapabilityException; 028import org.apache.hadoop.hbase.wal.FSHLogProvider; 029import org.apache.hadoop.hbase.wal.WAL.Entry; 030import org.apache.yetus.audience.InterfaceAudience; 031import org.slf4j.Logger; 032import org.slf4j.LoggerFactory; 033 034import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.WALHeader; 035import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.WALTrailer; 036 037/** 038 * Writer for protobuf-based WAL. 039 */ 040@InterfaceAudience.Private 041public class ProtobufLogWriter extends AbstractProtobufLogWriter 042 implements FSHLogProvider.Writer { 043 044 private static final Logger LOG = LoggerFactory.getLogger(ProtobufLogWriter.class); 045 046 protected FSDataOutputStream output; 047 048 @Override 049 public void append(Entry entry) throws IOException { 050 entry.setCompressionContext(compressionContext); 051 entry.getKey().getBuilder(compressor). 052 setFollowingKvCount(entry.getEdit().size()).build().writeDelimitedTo(output); 053 for (Cell cell : entry.getEdit().getCells()) { 054 // cellEncoder must assume little about the stream, since we write PB and cells in turn. 055 cellEncoder.write(cell); 056 } 057 length.set(output.getPos()); 058 } 059 060 @Override 061 public void close() throws IOException { 062 if (this.output != null) { 063 try { 064 if (!trailerWritten) writeWALTrailer(); 065 this.output.close(); 066 } catch (NullPointerException npe) { 067 // Can get a NPE coming up from down in DFSClient$DFSOutputStream#close 068 LOG.warn(npe.toString(), npe); 069 } 070 this.output = null; 071 } 072 } 073 074 @Override 075 public void sync() throws IOException { 076 FSDataOutputStream fsdos = this.output; 077 if (fsdos == null) return; // Presume closed 078 fsdos.flush(); 079 fsdos.hflush(); 080 } 081 082 public FSDataOutputStream getStream() { 083 return this.output; 084 } 085 086 @SuppressWarnings("deprecation") 087 @Override 088 protected void initOutput(FileSystem fs, Path path, boolean overwritable, int bufferSize, 089 short replication, long blockSize) throws IOException, StreamLacksCapabilityException { 090 this.output = fs.createNonRecursive(path, overwritable, bufferSize, replication, blockSize, 091 null); 092 // TODO Be sure to add a check for hsync if this branch includes HBASE-19024 093 if (fs.getConf().getBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE, true) && 094 !(CommonFSUtils.hasCapability(output, "hflush"))) { 095 throw new StreamLacksCapabilityException("hflush"); 096 } 097 } 098 099 @Override 100 protected long writeMagicAndWALHeader(byte[] magic, WALHeader header) throws IOException { 101 output.write(magic); 102 header.writeDelimitedTo(output); 103 return output.getPos(); 104 } 105 106 @Override 107 protected OutputStream getOutputStreamForCellEncoder() { 108 return this.output; 109 } 110 111 @Override 112 protected long writeWALTrailerAndMagic(WALTrailer trailer, byte[] magic) throws IOException { 113 trailer.writeTo(output); 114 output.writeInt(trailer.getSerializedSize()); 115 output.write(magic); 116 return output.getPos(); 117 } 118}