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 java.util.concurrent.atomic.AtomicLong; 023import org.apache.hadoop.fs.FSDataOutputStream; 024import org.apache.hadoop.fs.FileSystem; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.fs.StreamCapabilities; 027import org.apache.hadoop.hbase.Cell; 028import org.apache.hadoop.hbase.io.asyncfs.monitor.StreamSlowMonitor; 029import org.apache.hadoop.hbase.util.AtomicUtils; 030import org.apache.hadoop.hbase.util.CommonFSUtils; 031import org.apache.hadoop.hbase.util.CommonFSUtils.StreamLacksCapabilityException; 032import org.apache.hadoop.hbase.wal.FSHLogProvider; 033import org.apache.hadoop.hbase.wal.WAL.Entry; 034import org.apache.yetus.audience.InterfaceAudience; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.WALHeader; 039import org.apache.hadoop.hbase.shaded.protobuf.generated.WALProtos.WALTrailer; 040 041/** 042 * Writer for protobuf-based WAL. 043 */ 044@InterfaceAudience.Private 045public class ProtobufLogWriter extends AbstractProtobufLogWriter implements FSHLogProvider.Writer { 046 047 private static final Logger LOG = LoggerFactory.getLogger(ProtobufLogWriter.class); 048 049 protected FSDataOutputStream output; 050 051 private final AtomicLong syncedLength = new AtomicLong(0); 052 053 @Override 054 public void append(Entry entry) throws IOException { 055 entry.getKey().getBuilder(compressor).setFollowingKvCount(entry.getEdit().size()).build() 056 .writeDelimitedTo(output); 057 for (Cell cell : entry.getEdit().getCells()) { 058 // cellEncoder must assume little about the stream, since we write PB and cells in turn. 059 cellEncoder.write(cell); 060 } 061 length.set(output.getPos()); 062 } 063 064 @Override 065 public void close() throws IOException { 066 if (this.output != null) { 067 if (!trailerWritten) { 068 writeWALTrailer(); 069 } 070 this.output.close(); 071 this.output = null; 072 } 073 } 074 075 @Override 076 public void sync(boolean forceSync) throws IOException { 077 FSDataOutputStream fsdos = this.output; 078 if (fsdos == null) { 079 return; // Presume closed 080 } 081 fsdos.flush(); 082 if (forceSync) { 083 fsdos.hsync(); 084 } else { 085 fsdos.hflush(); 086 } 087 AtomicUtils.updateMax(this.syncedLength, fsdos.getPos()); 088 } 089 090 @Override 091 public long getSyncedLength() { 092 return this.syncedLength.get(); 093 } 094 095 public FSDataOutputStream getStream() { 096 return this.output; 097 } 098 099 @Override 100 protected void initOutput(FileSystem fs, Path path, boolean overwritable, int bufferSize, 101 short replication, long blockSize, StreamSlowMonitor monitor, boolean noLocalWrite) 102 throws IOException, StreamLacksCapabilityException { 103 this.output = CommonFSUtils.createForWal(fs, path, overwritable, bufferSize, replication, 104 blockSize, noLocalWrite, false); 105 if (fs.getConf().getBoolean(CommonFSUtils.UNSAFE_STREAM_CAPABILITY_ENFORCE, true)) { 106 if (!output.hasCapability(StreamCapabilities.HFLUSH)) { 107 throw new StreamLacksCapabilityException(StreamCapabilities.HFLUSH); 108 } 109 if (!output.hasCapability(StreamCapabilities.HSYNC)) { 110 throw new StreamLacksCapabilityException(StreamCapabilities.HSYNC); 111 } 112 } 113 } 114 115 @Override 116 protected void closeOutputIfNecessary() { 117 if (this.output != null) { 118 try { 119 this.output.close(); 120 } catch (IOException e) { 121 LOG.warn("Close output failed", e); 122 } 123 } 124 } 125 126 @Override 127 protected long writeMagicAndWALHeader(byte[] magic, WALHeader header) throws IOException { 128 output.write(magic); 129 header.writeDelimitedTo(output); 130 return output.getPos(); 131 } 132 133 @Override 134 protected OutputStream getOutputStreamForCellEncoder() { 135 return this.output; 136 } 137 138 @Override 139 protected long writeWALTrailerAndMagic(WALTrailer trailer, byte[] magic) throws IOException { 140 trailer.writeTo(output); 141 output.writeInt(trailer.getSerializedSize()); 142 output.write(magic); 143 return output.getPos(); 144 } 145}