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.ScheduledChore;
29  import org.apache.hadoop.hbase.Stoppable;
30  import org.apache.hadoop.hbase.classification.InterfaceAudience;
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 ScheduledChore {
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    /**
57     * Whether all storefiles should be refreshed, as opposed to just hbase:meta's
58     * Meta region doesn't have WAL replication for replicas enabled yet
59     */
60    public static final String REGIONSERVER_META_STOREFILE_REFRESH_PERIOD
61       = "hbase.regionserver.meta.storefile.refresh.period";
62    private HRegionServer regionServer;
63    private long hfileTtl;
64    private int period;
65    private boolean onlyMetaRefresh = true;
66  
67    //ts of last time regions store files are refreshed
68    private Map<String, Long> lastRefreshTimes; // encodedName -> long
69  
70    public StorefileRefresherChore(int period, boolean onlyMetaRefresh, HRegionServer regionServer,
71        Stoppable stoppable) {
72      super("StorefileRefresherChore", stoppable, period);
73      this.period = period;
74      this.regionServer = regionServer;
75      this.hfileTtl = this.regionServer.getConfiguration().getLong(
76        TimeToLiveHFileCleaner.TTL_CONF_KEY, TimeToLiveHFileCleaner.DEFAULT_TTL);
77      this.onlyMetaRefresh = onlyMetaRefresh;
78      if (period > hfileTtl / 2) {
79        throw new RuntimeException(REGIONSERVER_STOREFILE_REFRESH_PERIOD +
80          " should be set smaller than half of " + TimeToLiveHFileCleaner.TTL_CONF_KEY);
81      }
82      lastRefreshTimes = new HashMap<String, Long>();
83    }
84  
85    @Override
86    protected void chore() {
87      for (Region r : regionServer.getOnlineRegionsLocalContext()) {
88        if (!r.isReadOnly()) {
89          // skip checking for this region if it can accept writes
90          continue;
91        }
92        // don't refresh unless enabled for all files, or it the meta region
93        // meta region don't have WAL replication for replicas enabled yet
94        if (onlyMetaRefresh && !r.getRegionInfo().isMetaTable()) continue;
95        String encodedName = r.getRegionInfo().getEncodedName();
96        long time = EnvironmentEdgeManager.currentTime();
97        if (!lastRefreshTimes.containsKey(encodedName)) {
98          lastRefreshTimes.put(encodedName, time);
99        }
100       try {
101         for (Store store : r.getStores()) {
102           // TODO: some stores might see new data from flush, while others do not which
103           // MIGHT break atomic edits across column families. We can fix this with setting
104           // mvcc read numbers that we know every store has seen
105           store.refreshStoreFiles();
106         }
107       } catch (IOException ex) {
108         LOG.warn("Exception while trying to refresh store files for region:" + r.getRegionInfo()
109           + ", exception:" + StringUtils.stringifyException(ex));
110 
111         // Store files have a TTL in the archive directory. If we fail to refresh for that long, we stop serving reads
112         if (isRegionStale(encodedName, time)) {
113           ((HRegion)r).setReadsEnabled(false); // stop serving reads
114         }
115         continue;
116       }
117       lastRefreshTimes.put(encodedName, time);
118       ((HRegion)r).setReadsEnabled(true); // restart serving reads
119     }
120 
121     // remove closed regions
122     Iterator<String> lastRefreshTimesIter = lastRefreshTimes.keySet().iterator();
123     while (lastRefreshTimesIter.hasNext()) {
124       String encodedName = lastRefreshTimesIter.next();
125       if (regionServer.getFromOnlineRegions(encodedName) == null) {
126         lastRefreshTimesIter.remove();
127       }
128     }
129   }
130 
131   protected boolean isRegionStale(String encodedName, long time) {
132     long lastRefreshTime = lastRefreshTimes.get(encodedName);
133     return time - lastRefreshTime > hfileTtl - period;
134   }
135 }