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  package org.apache.hadoop.hbase;
19  
20  import java.io.IOException;
21  import java.net.InetSocketAddress;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentSkipListMap;
27  import java.util.concurrent.atomic.AtomicBoolean;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.FileSystem;
33  import org.apache.hadoop.hbase.client.ClusterConnection;
34  import org.apache.hadoop.hbase.executor.ExecutorService;
35  import org.apache.hadoop.hbase.fs.HFileSystem;
36  import org.apache.hadoop.hbase.ipc.RpcServerInterface;
37  import org.apache.hadoop.hbase.master.TableLockManager;
38  import org.apache.hadoop.hbase.master.TableLockManager.NullTableLockManager;
39  import org.apache.hadoop.hbase.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
40  import org.apache.hadoop.hbase.quotas.RegionServerQuotaManager;
41  import org.apache.hadoop.hbase.regionserver.CompactionRequestor;
42  import org.apache.hadoop.hbase.regionserver.FlushRequester;
43  import org.apache.hadoop.hbase.regionserver.HeapMemoryManager;
44  import org.apache.hadoop.hbase.regionserver.Leases;
45  import org.apache.hadoop.hbase.regionserver.Region;
46  import org.apache.hadoop.hbase.regionserver.RegionServerAccounting;
47  import org.apache.hadoop.hbase.regionserver.RegionServerServices;
48  import org.apache.hadoop.hbase.regionserver.ServerNonceManager;
49  import org.apache.hadoop.hbase.util.Bytes;
50  import org.apache.hadoop.hbase.wal.WAL;
51  import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
52  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
53  import org.apache.zookeeper.KeeperException;
54  
55  import com.google.protobuf.Service;
56  
57  /**
58   * Basic mock region server services.  Should only be instantiated by HBaseTestingUtility.b
59   */
60  public class MockRegionServerServices implements RegionServerServices {
61    protected static final Log LOG = LogFactory.getLog(MockRegionServerServices.class);
62    private final Map<String, Region> regions = new HashMap<String, Region>();
63    private final ConcurrentSkipListMap<byte[], Boolean> rit =
64      new ConcurrentSkipListMap<byte[], Boolean>(Bytes.BYTES_COMPARATOR);
65    private HFileSystem hfs = null;
66    private final Configuration conf;
67    private ZooKeeperWatcher zkw = null;
68    private ServerName serverName = null;
69    private RpcServerInterface rpcServer = null;
70    private volatile boolean abortRequested;
71    private volatile boolean stopping = false;
72    private final AtomicBoolean running = new AtomicBoolean(true);
73  
74    MockRegionServerServices(ZooKeeperWatcher zkw) {
75      this(zkw, null);
76    }
77  
78    MockRegionServerServices(ZooKeeperWatcher zkw, ServerName serverName) {
79      this.zkw = zkw;
80      this.serverName = serverName;
81      this.conf = (zkw == null ? new Configuration() : zkw.getConfiguration());
82    }
83  
84    MockRegionServerServices(){
85      this(null, null);
86    }
87  
88    public MockRegionServerServices(Configuration conf) {
89      this.conf = conf;
90    }
91  
92    @Override
93    public boolean removeFromOnlineRegions(Region r, ServerName destination) {
94      return this.regions.remove(r.getRegionInfo().getEncodedName()) != null;
95    }
96  
97    @Override
98    public Region getFromOnlineRegions(String encodedRegionName) {
99      return this.regions.get(encodedRegionName);
100   }
101 
102   public List<Region> getOnlineRegions(TableName tableName) throws IOException {
103     return null;
104   }
105   
106   @Override
107   public Set<TableName> getOnlineTables() {
108     return null;
109   }
110 
111   @Override
112   public void addToOnlineRegions(Region r) {
113     this.regions.put(r.getRegionInfo().getEncodedName(), r);
114   }
115 
116   @Override
117   public void postOpenDeployTasks(Region r) throws KeeperException, IOException {
118     addToOnlineRegions(r);
119   }
120 
121   @Override
122   public boolean isStopping() {
123     return this.stopping;
124   }
125 
126   @Override
127   public RpcServerInterface getRpcServer() {
128     return rpcServer;
129   }
130 
131   public void setRpcServer(RpcServerInterface rpc) {
132     this.rpcServer = rpc;
133   }
134 
135   @Override
136   public ConcurrentSkipListMap<byte[], Boolean> getRegionsInTransitionInRS() {
137     return rit;
138   }
139 
140   @Override
141   public FlushRequester getFlushRequester() {
142     return null;
143   }
144 
145   @Override
146   public CompactionRequestor getCompactionRequester() {
147     return null;
148   }
149 
150   @Override
151   public ClusterConnection getConnection() {
152     return null;
153   }
154 
155   @Override
156   public MetaTableLocator getMetaTableLocator() {
157     return null;
158   }
159 
160   @Override
161   public ZooKeeperWatcher getZooKeeper() {
162     return zkw;
163   }
164 
165   @Override
166   public CoordinatedStateManager getCoordinatedStateManager() {
167     return null;
168   }
169 
170   public RegionServerAccounting getRegionServerAccounting() {
171     return null;
172   }
173 
174   @Override
175   public TableLockManager getTableLockManager() {
176     return new NullTableLockManager();
177   }
178   
179   @Override
180   public RegionServerQuotaManager getRegionServerQuotaManager() {
181     return null;
182   }
183 
184   @Override
185   public ServerName getServerName() {
186     return this.serverName;
187   }
188 
189   @Override
190   public Configuration getConfiguration() {
191     return conf;
192   }
193 
194   @Override
195   public void abort(String why, Throwable e) {
196     this.abortRequested = true;
197     stop(why);
198   }
199 
200   @Override
201   public void stop(String why) {
202     this.stopping = true;
203     if (running.compareAndSet(true, false)) {
204       LOG.info("Shutting down due to request '" + why + "'");
205     }
206   }
207 
208   @Override
209   public boolean isStopped() {
210     return !(running.get());
211   }
212 
213   @Override
214   public boolean isAborted() {
215     return this.abortRequested;
216   }
217 
218   @Override
219   public HFileSystem getFileSystem() {
220     return this.hfs;
221   }
222 
223   public void setFileSystem(FileSystem hfs) {
224     this.hfs = (HFileSystem)hfs;
225   }
226 
227   @Override
228   public Leases getLeases() {
229     return null;
230   }
231 
232   @Override
233   public WAL getWAL(HRegionInfo regionInfo) throws IOException {
234     return null;
235   }
236 
237   @Override
238   public ExecutorService getExecutorService() {
239     return null;
240   }
241 
242   @Override
243   public ChoreService getChoreService() {
244     return null;
245   }
246 
247   @Override
248   public void updateRegionFavoredNodesMapping(String encodedRegionName,
249       List<org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName> favoredNodes) {
250   }
251 
252   @Override
253   public InetSocketAddress[] getFavoredNodesForRegion(String encodedRegionName) {
254     return null;
255   }
256 
257   @Override
258   public Map<String, Region> getRecoveringRegions() {
259     // TODO Auto-generated method stub
260     return null;
261   }
262 
263   @Override
264   public ServerNonceManager getNonceManager() {
265     // TODO Auto-generated method stub
266     return null;
267   }
268 
269   @Override
270   public boolean reportRegionStateTransition(TransitionCode code, long openSeqNum,
271       HRegionInfo... hris) {
272     return false;
273   }
274 
275   @Override
276   public boolean reportRegionStateTransition(TransitionCode code,
277       HRegionInfo... hris) {
278     return false;
279   }
280 
281   @Override
282   public boolean registerService(Service service) {
283     // TODO Auto-generated method stub
284     return false;
285   }
286 
287   @Override
288   public HeapMemoryManager getHeapMemoryManager() {
289     return null;
290   }
291 
292   @Override
293   public double getCompactionPressure() {
294     return 0;
295   }
296 }