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; 019 020import java.io.IOException; 021import org.apache.hadoop.hbase.executor.EventType; 022import org.apache.hadoop.hbase.procedure2.BaseRSProcedureCallable; 023import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL; 024import org.apache.hadoop.hbase.util.Pair; 025import org.apache.hadoop.hbase.wal.AbstractWALRoller; 026import org.apache.yetus.audience.InterfaceAudience; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 031import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.LogRollRemoteProcedureResult; 032 033@InterfaceAudience.Private 034public class LogRollCallable extends BaseRSProcedureCallable { 035 036 private static final Logger LOG = LoggerFactory.getLogger(LogRollCallable.class); 037 038 private int maxRollRetry; 039 040 @Override 041 protected byte[] doCall() throws Exception { 042 for (int nAttempt = 0; nAttempt < maxRollRetry; nAttempt++) { 043 try { 044 Pair<Long, Long> filenumPairBefore = getFilenumPair(); 045 046 rs.getWalRoller().requestRollAll(); 047 rs.getWalRoller().waitUntilWalRollFinished(); 048 049 Pair<Long, Long> filenumPairAfter = getFilenumPair(); 050 LOG.info( 051 "Before rolling log, highest filenum = {} default WAL filenum = {}, After " 052 + "rolling log, highest filenum = {} default WAL filenum = {}", 053 filenumPairBefore.getFirst(), filenumPairBefore.getSecond(), filenumPairAfter.getFirst(), 054 filenumPairAfter.getSecond()); 055 return LogRollRemoteProcedureResult.newBuilder() 056 .setServerName(ProtobufUtil.toServerName(rs.getServerName())) 057 .setLastHighestWalFilenum(filenumPairBefore.getFirst()).build().toByteArray(); 058 } catch (Exception e) { 059 LOG.warn("Failed rolling log on attempt={}", nAttempt, e); 060 if (nAttempt == maxRollRetry - 1) { 061 throw e; 062 } 063 } 064 } 065 return null; 066 } 067 068 private Pair<Long, Long> getFilenumPair() throws IOException { 069 long highestFilenum = rs.getWALs().stream() 070 .mapToLong(wal -> ((AbstractFSWAL<?>) wal).getFilenum()).max().orElse(-1L); 071 long defaultWALFilenum = ((AbstractFSWAL<?>) rs.getWAL(null)).getFilenum(); 072 return Pair.newPair(highestFilenum, defaultWALFilenum); 073 } 074 075 @Override 076 protected void initParameter(byte[] parameter) throws Exception { 077 this.maxRollRetry = rs.getConfiguration().getInt(AbstractWALRoller.WAL_ROLL_RETRIES, 1); 078 } 079 080 @Override 081 public EventType getEventType() { 082 return EventType.RS_LOG_ROLL; 083 } 084}