View Javadoc

1   /**
2    * Copyright 2010 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.master;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import java.io.IOException;
26  import java.util.concurrent.Delayed;
27  import java.util.concurrent.TimeUnit;
28  
29  abstract class RegionServerOperation implements Delayed {
30    protected static final Log LOG =
31      LogFactory.getLog(RegionServerOperation.class.getName());
32  
33    private long expire;
34    protected final HMaster master;
35    /* How long we stay on queue.
36     */
37    private int delay;
38  
39    protected RegionServerOperation(HMaster master) {
40      this.master = master;
41      this.delay = this.master.getConfiguration().
42        getInt("hbase.server.thread.wakefrequency", 10 * 1000);
43      // Set the future time at which we expect to be released from the
44      // DelayQueue we're inserted in on lease expiration.
45      resetExpiration();
46    }
47  
48    /**
49     * Call before putting this back on the delay queue.
50     * @return When we will expire next.
51     */
52    long resetExpiration() {
53      // Set the future time at which we expect to be released from the
54      // DelayQueue we're inserted in on lease expiration.
55      this.expire = System.currentTimeMillis() + this.delay;
56      return this.expire;
57    }
58  
59    public long getDelay(TimeUnit unit) {
60      return unit.convert(this.expire - System.currentTimeMillis(),
61        TimeUnit.MILLISECONDS);
62    }
63  
64    void setDelay(final int d) {
65      this.delay = d;
66    }
67  
68    public int compareTo(Delayed o) {
69      return Long.valueOf(getDelay(TimeUnit.MILLISECONDS)
70          - o.getDelay(TimeUnit.MILLISECONDS)).intValue();
71    }
72  
73    protected void requeue() {
74      this.master.getRegionServerOperationQueue().putOnDelayQueue(this);
75    }
76  
77    private long whenToExpire() {
78      return System.currentTimeMillis() + this.delay;
79    }
80  
81    protected boolean rootAvailable() {
82      boolean available = true;
83      if (this.master.getRegionManager().getRootRegionLocation() == null) {
84        available = false;
85        requeue();
86      }
87      return available;
88    }
89  
90    protected boolean metaTableAvailable() {
91      boolean available = true;
92      if ((master.getRegionManager().numMetaRegions() !=
93        master.getRegionManager().numOnlineMetaRegions()) ||
94        master.getRegionManager().metaRegionsInTransition()) {
95        // We can't proceed because not all of the meta regions are online.
96        // We can't block either because that would prevent the meta region
97        // online message from being processed. In order to prevent spinning
98        // in the run queue, put this request on the delay queue to give
99        // other threads the opportunity to get the meta regions on-line.
100       if (LOG.isDebugEnabled()) {
101         LOG.debug("numberOfMetaRegions: " +
102             master.getRegionManager().numMetaRegions() +
103             ", onlineMetaRegions.size(): " +
104             master.getRegionManager().numOnlineMetaRegions());
105         LOG.debug("Requeuing because not all meta regions are online");
106       }
107       available = false;
108       requeue();
109     }
110     return available;
111   }
112 
113   public int compareTo(RegionServerOperation other) {
114     return getPriority() - other.getPriority();
115   }
116 
117   // the Priority of this operation, 0 is lowest priority
118   protected int getPriority() {
119     return Integer.MAX_VALUE;
120   }
121 
122   protected abstract boolean process() throws IOException;
123 }