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 void postOpenDeployTasks(PostOpenDeployContext context) throws KeeperException,
123       IOException {
124     addToOnlineRegions(context.getRegion());
125   }
126 
127   @Override
128   public boolean isStopping() {
129     return this.stopping;
130   }
131 
132   @Override
133   public RpcServerInterface getRpcServer() {
134     return rpcServer;
135   }
136 
137   public void setRpcServer(RpcServerInterface rpc) {
138     this.rpcServer = rpc;
139   }
140 
141   @Override
142   public ConcurrentSkipListMap<byte[], Boolean> getRegionsInTransitionInRS() {
143     return rit;
144   }
145 
146   @Override
147   public FlushRequester getFlushRequester() {
148     return null;
149   }
150 
151   @Override
152   public CompactionRequestor getCompactionRequester() {
153     return null;
154   }
155 
156   @Override
157   public ClusterConnection getConnection() {
158     return null;
159   }
160 
161   @Override
162   public MetaTableLocator getMetaTableLocator() {
163     return null;
164   }
165 
166   @Override
167   public ZooKeeperWatcher getZooKeeper() {
168     return zkw;
169   }
170 
171   @Override
172   public CoordinatedStateManager getCoordinatedStateManager() {
173     return null;
174   }
175 
176   public RegionServerAccounting getRegionServerAccounting() {
177     return null;
178   }
179 
180   @Override
181   public TableLockManager getTableLockManager() {
182     return new NullTableLockManager();
183   }
184   
185   @Override
186   public RegionServerQuotaManager getRegionServerQuotaManager() {
187     return null;
188   }
189 
190   @Override
191   public ServerName getServerName() {
192     return this.serverName;
193   }
194 
195   @Override
196   public Configuration getConfiguration() {
197     return conf;
198   }
199 
200   @Override
201   public void abort(String why, Throwable e) {
202     this.abortRequested = true;
203     stop(why);
204   }
205 
206   @Override
207   public void stop(String why) {
208     this.stopping = true;
209     if (running.compareAndSet(true, false)) {
210       LOG.info("Shutting down due to request '" + why + "'");
211     }
212   }
213 
214   @Override
215   public boolean isStopped() {
216     return !(running.get());
217   }
218 
219   @Override
220   public boolean isAborted() {
221     return this.abortRequested;
222   }
223 
224   @Override
225   public HFileSystem getFileSystem() {
226     return this.hfs;
227   }
228 
229   public void setFileSystem(FileSystem hfs) {
230     this.hfs = (HFileSystem)hfs;
231   }
232 
233   @Override
234   public Leases getLeases() {
235     return null;
236   }
237 
238   @Override
239   public WAL getWAL(HRegionInfo regionInfo) throws IOException {
240     return null;
241   }
242 
243   @Override
244   public ExecutorService getExecutorService() {
245     return null;
246   }
247 
248   @Override
249   public ChoreService getChoreService() {
250     return null;
251   }
252 
253   @Override
254   public void updateRegionFavoredNodesMapping(String encodedRegionName,
255       List<org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName> favoredNodes) {
256   }
257 
258   @Override
259   public InetSocketAddress[] getFavoredNodesForRegion(String encodedRegionName) {
260     return null;
261   }
262 
263   @Override
264   public Map<String, Region> getRecoveringRegions() {
265     // TODO Auto-generated method stub
266     return null;
267   }
268 
269   @Override
270   public ServerNonceManager getNonceManager() {
271     // TODO Auto-generated method stub
272     return null;
273   }
274 
275   @Override
276   public boolean reportRegionStateTransition(TransitionCode code, long openSeqNum,
277       HRegionInfo... hris) {
278     return false;
279   }
280 
281   @Override
282   public boolean reportRegionStateTransition(TransitionCode code,
283       HRegionInfo... hris) {
284     return false;
285   }
286 
287   @Override
288   public boolean reportRegionStateTransition(RegionStateTransitionContext context) {
289     return false;
290   }
291 
292   @Override
293   public boolean registerService(Service service) {
294     // TODO Auto-generated method stub
295     return false;
296   }
297 
298   @Override
299   public HeapMemoryManager getHeapMemoryManager() {
300     return null;
301   }
302 
303   @Override
304   public double getCompactionPressure() {
305     return 0;
306   }
307 }