1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  package org.apache.hadoop.hbase.regionserver.handler;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.classification.InterfaceAudience;
26  import org.apache.hadoop.hbase.HRegionInfo;
27  import org.apache.hadoop.hbase.Server;
28  import org.apache.hadoop.hbase.ServerName;
29  import org.apache.hadoop.hbase.coordination.CloseRegionCoordination;
30  import org.apache.hadoop.hbase.executor.EventHandler;
31  import org.apache.hadoop.hbase.executor.EventType;
32  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
33  import org.apache.hadoop.hbase.regionserver.HRegion;
34  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
35  import org.apache.hadoop.hbase.util.ConfigUtil;
36  
37  
38  
39  
40  @InterfaceAudience.Private
41  public class CloseRegionHandler extends EventHandler {
42    
43    
44    
45    
46    
47    private static final Log LOG = LogFactory.getLog(CloseRegionHandler.class);
48  
49    private final RegionServerServices rsServices;
50    private final HRegionInfo regionInfo;
51  
52    
53    
54    private final boolean abort;
55    private ServerName destination;
56    private CloseRegionCoordination closeRegionCoordination;
57    private CloseRegionCoordination.CloseRegionDetails closeRegionDetails;
58    private final boolean useZKForAssignment;
59  
60    
61  
62  
63  
64  
65  
66  
67  
68  
69    public CloseRegionHandler(final Server server,
70        final RegionServerServices rsServices,
71        final HRegionInfo regionInfo, final boolean abort,
72        CloseRegionCoordination closeRegionCoordination,
73        CloseRegionCoordination.CloseRegionDetails crd) {
74      this(server, rsServices,  regionInfo, abort, closeRegionCoordination, crd,
75        EventType.M_RS_CLOSE_REGION, null);
76    }
77  
78    public CloseRegionHandler(final Server server,
79        final RegionServerServices rsServices,
80        final HRegionInfo regionInfo, final boolean abort,
81        CloseRegionCoordination closeRegionCoordination,
82        CloseRegionCoordination.CloseRegionDetails crd,
83        ServerName destination) {
84      this(server, rsServices, regionInfo, abort, closeRegionCoordination, crd,
85        EventType.M_RS_CLOSE_REGION, destination);
86    }
87  
88    public CloseRegionHandler(final Server server,
89        final RegionServerServices rsServices, HRegionInfo regionInfo,
90        boolean abort, CloseRegionCoordination closeRegionCoordination,
91        CloseRegionCoordination.CloseRegionDetails crd, EventType eventType) {
92      this(server, rsServices, regionInfo, abort, closeRegionCoordination, crd, eventType, null);
93    }
94  
95      protected CloseRegionHandler(final Server server,
96        final RegionServerServices rsServices, HRegionInfo regionInfo,
97        boolean abort, CloseRegionCoordination closeRegionCoordination,
98        CloseRegionCoordination.CloseRegionDetails crd,
99        EventType eventType, ServerName destination) {
100     super(server, eventType);
101     this.server = server;
102     this.rsServices = rsServices;
103     this.regionInfo = regionInfo;
104     this.abort = abort;
105     this.destination = destination;
106     this.closeRegionCoordination = closeRegionCoordination;
107     this.closeRegionDetails = crd;
108     useZKForAssignment = ConfigUtil.useZKForAssignment(server.getConfiguration());
109   }
110 
111   public HRegionInfo getRegionInfo() {
112     return regionInfo;
113   }
114 
115   @Override
116   public void process() {
117     try {
118       String name = regionInfo.getRegionNameAsString();
119       LOG.debug("Processing close of " + name);
120       String encodedRegionName = regionInfo.getEncodedName();
121       
122       HRegion region = (HRegion)rsServices.getFromOnlineRegions(encodedRegionName);
123       if (region == null) {
124         LOG.warn("Received CLOSE for region " + name + " but currently not serving - ignoring");
125         
126         return;
127       }
128 
129       
130       try {
131         if (useZKForAssignment && closeRegionCoordination.checkClosingState(
132             regionInfo, closeRegionDetails)) {
133           return;
134         }
135 
136         
137         
138         if (region.close(abort) == null) {
139           
140           
141           
142           LOG.warn("Can't close region: was already closed during close(): " +
143             regionInfo.getRegionNameAsString());
144           return;
145         }
146       } catch (IOException ioe) {
147         
148         
149         
150         server.abort("Unrecoverable exception while closing region " +
151           regionInfo.getRegionNameAsString() + ", still finishing close", ioe);
152         throw new RuntimeException(ioe);
153       }
154 
155       this.rsServices.removeFromOnlineRegions(region, destination);
156       if (!useZKForAssignment) {
157         rsServices.reportRegionStateTransition(TransitionCode.CLOSED, regionInfo);
158       } else {
159         closeRegionCoordination.setClosedState(region, this.server.getServerName(),
160           closeRegionDetails);
161       }
162 
163       
164       LOG.debug("Closed " + region.getRegionInfo().getRegionNameAsString());
165     } finally {
166       this.rsServices.getRegionsInTransitionInRS().
167           remove(this.regionInfo.getEncodedNameAsBytes());
168     }
169   }
170 }