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