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.backup.regionserver; 019 020import java.nio.charset.StandardCharsets; 021import java.util.HashMap; 022import java.util.List; 023import java.util.concurrent.Callable; 024import org.apache.hadoop.hbase.backup.impl.BackupSystemTable; 025import org.apache.hadoop.hbase.backup.master.LogRollMasterProcedureManager; 026import org.apache.hadoop.hbase.backup.util.BackupUtils; 027import org.apache.hadoop.hbase.client.Connection; 028import org.apache.hadoop.hbase.errorhandling.ForeignException; 029import org.apache.hadoop.hbase.errorhandling.ForeignExceptionDispatcher; 030import org.apache.hadoop.hbase.procedure.ProcedureMember; 031import org.apache.hadoop.hbase.procedure.Subprocedure; 032import org.apache.hadoop.hbase.regionserver.HRegionServer; 033import org.apache.hadoop.hbase.regionserver.RegionServerServices; 034import org.apache.hadoop.hbase.regionserver.wal.AbstractFSWAL; 035import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 036import org.apache.hadoop.hbase.wal.WAL; 037import org.apache.yetus.audience.InterfaceAudience; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041/** 042 * This backup sub-procedure implementation forces a WAL rolling on a RS. 043 */ 044@InterfaceAudience.Private 045public class LogRollBackupSubprocedure extends Subprocedure { 046 private static final Logger LOG = LoggerFactory.getLogger(LogRollBackupSubprocedure.class); 047 048 private final RegionServerServices rss; 049 private final LogRollBackupSubprocedurePool taskManager; 050 private String backupRoot; 051 052 public LogRollBackupSubprocedure(RegionServerServices rss, ProcedureMember member, 053 ForeignExceptionDispatcher errorListener, long wakeFrequency, long timeout, 054 LogRollBackupSubprocedurePool taskManager, byte[] data) { 055 super(member, LogRollMasterProcedureManager.ROLLLOG_PROCEDURE_NAME, errorListener, 056 wakeFrequency, timeout); 057 LOG.info("Constructing a LogRollBackupSubprocedure."); 058 this.rss = rss; 059 this.taskManager = taskManager; 060 if (data != null) { 061 backupRoot = new String(data, StandardCharsets.UTF_8); 062 } 063 } 064 065 /** 066 * Callable task. TODO. We don't need a thread pool to execute roll log. This can be simplified 067 * with no use of sub-procedure pool. 068 */ 069 class RSRollLogTask implements Callable<Void> { 070 RSRollLogTask() { 071 } 072 073 @Override 074 public Void call() throws Exception { 075 if (LOG.isDebugEnabled()) { 076 LOG.debug("DRPC started: " + rss.getServerName()); 077 } 078 079 AbstractFSWAL<?> fsWAL = (AbstractFSWAL<?>) rss.getWAL(null); 080 long filenum = fsWAL.getFilenum(); 081 List<WAL> wals = rss.getWALs(); 082 long highest = -1; 083 for (WAL wal : wals) { 084 if (wal == null) { 085 continue; 086 } 087 088 if (((AbstractFSWAL<?>) wal).getFilenum() > highest) { 089 highest = ((AbstractFSWAL<?>) wal).getFilenum(); 090 } 091 } 092 093 LOG.info("Trying to roll log in backup subprocedure, current log number: " + filenum 094 + " highest: " + highest + " on " + rss.getServerName()); 095 ((HRegionServer) rss).getWalRoller().requestRollAll(); 096 long start = EnvironmentEdgeManager.currentTime(); 097 while (!((HRegionServer) rss).getWalRoller().walRollFinished()) { 098 Thread.sleep(20); 099 } 100 LOG.debug("log roll took " + (EnvironmentEdgeManager.currentTime() - start)); 101 LOG.info("After roll log in backup subprocedure, current log number: " + fsWAL.getFilenum() 102 + " on " + rss.getServerName()); 103 104 Connection connection = rss.getConnection(); 105 try (final BackupSystemTable table = new BackupSystemTable(connection)) { 106 // sanity check, good for testing 107 HashMap<String, Long> serverTimestampMap = 108 table.readRegionServerLastLogRollResult(backupRoot); 109 String host = rss.getServerName().getHostname(); 110 int port = rss.getServerName().getPort(); 111 String server = BackupUtils.toHostKey(host + ":" + port); 112 Long sts = serverTimestampMap.get(server); 113 if (sts != null && sts > highest) { 114 LOG 115 .warn("Won't update server's last roll log result: current=" + sts + " new=" + highest); 116 return null; 117 } 118 // write the log number to backup system table. 119 table.writeRegionServerLastLogRollResult(server, highest, backupRoot); 120 return null; 121 } catch (Exception e) { 122 LOG.error(e.toString(), e); 123 throw e; 124 } 125 } 126 } 127 128 private void rolllog() throws ForeignException { 129 monitor.rethrowException(); 130 131 taskManager.submitTask(new RSRollLogTask()); 132 monitor.rethrowException(); 133 134 // wait for everything to complete. 135 taskManager.waitForOutstandingTasks(); 136 monitor.rethrowException(); 137 } 138 139 @Override 140 public void acquireBarrier() { 141 // do nothing, executing in inside barrier step. 142 } 143 144 /** 145 * do a log roll. 146 * @return some bytes 147 */ 148 @Override 149 public byte[] insideBarrier() throws ForeignException { 150 rolllog(); 151 return null; 152 } 153 154 /** 155 * Cancel threads if they haven't finished. 156 */ 157 @Override 158 public void cleanup(Exception e) { 159 taskManager.abort("Aborting log roll subprocedure tasks for backup due to error", e); 160 } 161 162 /** 163 * Hooray! 164 */ 165 public void releaseBarrier() { 166 // NO OP 167 } 168}