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.master; 019 020import java.io.IOException; 021import java.io.UncheckedIOException; 022import java.util.HashSet; 023import java.util.Set; 024import org.apache.hadoop.hbase.Abortable; 025import org.apache.hadoop.hbase.HConstants; 026import org.apache.hadoop.hbase.ServerName; 027import org.apache.hadoop.hbase.client.Delete; 028import org.apache.hadoop.hbase.client.Put; 029import org.apache.hadoop.hbase.client.Result; 030import org.apache.hadoop.hbase.client.ResultScanner; 031import org.apache.hadoop.hbase.client.Scan; 032import org.apache.hadoop.hbase.log.HBaseMarkers; 033import org.apache.hadoop.hbase.master.assignment.ServerState; 034import org.apache.hadoop.hbase.master.region.MasterRegion; 035import org.apache.hadoop.hbase.master.region.MasterRegionFactory; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.yetus.audience.InterfaceAudience; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041/** 042 * {@link MasterRegion} based {@link RegionServerList}. 043 * <p/> 044 * This is useful when we want to restart a cluster with only the data on file system, as when 045 * restarting, we need to get the previous live region servers for scheduling SCP. Before we have 046 * this class, we need to scan the WAL directory on WAL file system to find out the previous live 047 * region servers, which means we can not restart a cluster without the previous WAL file system, 048 * even if we have flushed all the data. 049 * <p/> 050 * Please see HBASE-26245 for more details. 051 */ 052@InterfaceAudience.Private 053public class MasterRegionServerList implements RegionServerList { 054 055 private static final Logger LOG = LoggerFactory.getLogger(MasterRegionServerList.class); 056 057 private final MasterRegion region; 058 059 private final Abortable abortable; 060 061 public MasterRegionServerList(MasterRegion region, Abortable abortable) { 062 this.region = region; 063 this.abortable = abortable; 064 } 065 066 @Override 067 public void started(ServerName sn) { 068 Put put = 069 new Put(Bytes.toBytes(sn.getServerName())).addColumn(MasterRegionFactory.REGION_SERVER_FAMILY, 070 HConstants.STATE_QUALIFIER, Bytes.toBytes(ServerState.ONLINE.name())); 071 try { 072 region.update(r -> r.put(put)); 073 } catch (IOException e) { 074 LOG.error(HBaseMarkers.FATAL, "Failed to record region server {} as started, aborting...", sn, 075 e); 076 abortable.abort("Failed to record region server as started"); 077 throw new UncheckedIOException(e); 078 } 079 } 080 081 @Override 082 public void expired(ServerName sn) { 083 Delete delete = new Delete(Bytes.toBytes(sn.getServerName())) 084 .addFamily(MasterRegionFactory.REGION_SERVER_FAMILY); 085 try { 086 region.update(r -> r.delete(delete)); 087 } catch (IOException e) { 088 LOG.error(HBaseMarkers.FATAL, "Failed to record region server {} as expired, aborting...", sn, 089 e); 090 abortable.abort("Failed to record region server as expired"); 091 throw new UncheckedIOException(e); 092 } 093 } 094 095 @Override 096 public Set<ServerName> getAll() throws IOException { 097 Set<ServerName> rsList = new HashSet<>(); 098 try (ResultScanner scanner = 099 region.getScanner(new Scan().addFamily(MasterRegionFactory.REGION_SERVER_FAMILY))) { 100 for (;;) { 101 Result result = scanner.next(); 102 if (result == null) { 103 break; 104 } 105 rsList.add(ServerName.valueOf(Bytes.toString(result.getRow()))); 106 } 107 } 108 return rsList; 109 } 110 111}