View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.regionserver;
20  
21  import java.io.IOException;
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.classification.InterfaceAudience;
29  import org.apache.hadoop.hbase.Chore;
30  import org.apache.hadoop.hbase.Stoppable;
31  import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
32  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
33  import org.apache.hadoop.util.StringUtils;
34  
35  /**
36   * A chore for refreshing the store files for secondary regions hosted in the region server.
37   *
38   * This chore should run periodically with a shorter interval than HFile TTL
39   * ("hbase.master.hfilecleaner.ttl", default 5 minutes).
40   * It ensures that if we cannot refresh files longer than that amount, the region
41   * will stop serving read requests because the referenced files might have been deleted (by the
42   * primary region).
43   */
44  @InterfaceAudience.Private
45  public class StorefileRefresherChore extends Chore {
46  
47    private static final Log LOG = LogFactory.getLog(StorefileRefresherChore.class);
48  
49    /**
50     * The period (in milliseconds) for refreshing the store files for the secondary regions.
51     */
52    public static final String REGIONSERVER_STOREFILE_REFRESH_PERIOD
53      = "hbase.regionserver.storefile.refresh.period";
54    static final int DEFAULT_REGIONSERVER_STOREFILE_REFRESH_PERIOD = 0; //disabled by default
55  
56    private HRegionServer regionServer;
57    private long hfileTtl;
58    private int period;
59  
60    //ts of last time regions store files are refreshed
61    private Map<String, Long> lastRefreshTimes; // encodedName -> long
62  
63    public StorefileRefresherChore(int period, HRegionServer regionServer, Stoppable stoppable) {
64      super("StorefileRefresherChore", period, stoppable);
65      this.period = period;
66      this.regionServer = regionServer;
67      this.hfileTtl = this.regionServer.getConfiguration().getLong(
68        TimeToLiveHFileCleaner.TTL_CONF_KEY, TimeToLiveHFileCleaner.DEFAULT_TTL);
69      if (period > hfileTtl / 2) {
70        throw new RuntimeException(REGIONSERVER_STOREFILE_REFRESH_PERIOD +
71          " should be set smaller than half of " + TimeToLiveHFileCleaner.TTL_CONF_KEY);
72      }
73      lastRefreshTimes = new HashMap<String, Long>();
74    }
75  
76    @Override
77    protected void chore() {
78      for (HRegion r : regionServer.getOnlineRegionsLocalContext()) {
79        if (!r.writestate.isReadOnly()) {
80          // skip checking for this region if it can accept writes
81          continue;
82        }
83        String encodedName = r.getRegionInfo().getEncodedName();
84        long time = EnvironmentEdgeManager.currentTime();
85        if (!lastRefreshTimes.containsKey(encodedName)) {
86          lastRefreshTimes.put(encodedName, time);
87        }
88        try {
89          for (Store store : r.getStores().values()) {
90            // TODO: some stores might see new data from flush, while others do not which
91            // MIGHT break atomic edits across column families. We can fix this with setting
92            // mvcc read numbers that we know every store has seen
93            store.refreshStoreFiles();
94          }
95        } catch (IOException ex) {
96          LOG.warn("Exception while trying to refresh store files for region:" + r.getRegionInfo()
97            + ", exception:" + StringUtils.stringifyException(ex));
98  
99          // Store files have a TTL in the archive directory. If we fail to refresh for that long, we stop serving reads
100         if (isRegionStale(encodedName, time)) {
101           r.setReadsEnabled(false); // stop serving reads
102         }
103         continue;
104       }
105       lastRefreshTimes.put(encodedName, time);
106       r.setReadsEnabled(true); // restart serving reads
107     }
108 
109     // remove closed regions
110     Iterator<String> lastRefreshTimesIter = lastRefreshTimes.keySet().iterator();
111     while (lastRefreshTimesIter.hasNext()) {
112       String encodedName = lastRefreshTimesIter.next();
113       if (regionServer.getFromOnlineRegions(encodedName) == null) {
114         lastRefreshTimesIter.remove();
115       }
116     }
117   }
118 
119   protected boolean isRegionStale(String encodedName, long time) {
120     long lastRefreshTime = lastRefreshTimes.get(encodedName);
121     return time - lastRefreshTime > hfileTtl - period;
122   }
123 }