001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import java.io.IOException;
021import java.net.InetSocketAddress;
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.List;
025import java.util.Map;
026import java.util.concurrent.ConcurrentSkipListMap;
027import java.util.concurrent.atomic.AtomicBoolean;
028
029import org.apache.hadoop.conf.Configuration;
030import org.apache.hadoop.fs.FileSystem;
031import org.apache.hadoop.hbase.client.ClusterConnection;
032import org.apache.hadoop.hbase.client.Connection;
033import org.apache.hadoop.hbase.client.RegionInfo;
034import org.apache.hadoop.hbase.client.locking.EntityLock;
035import org.apache.hadoop.hbase.executor.ExecutorService;
036import org.apache.hadoop.hbase.fs.HFileSystem;
037import org.apache.hadoop.hbase.ipc.RpcServerInterface;
038import org.apache.hadoop.hbase.quotas.RegionServerRpcQuotaManager;
039import org.apache.hadoop.hbase.quotas.RegionServerSpaceQuotaManager;
040import org.apache.hadoop.hbase.regionserver.FlushRequester;
041import org.apache.hadoop.hbase.regionserver.HRegion;
042import org.apache.hadoop.hbase.regionserver.HeapMemoryManager;
043import org.apache.hadoop.hbase.regionserver.Leases;
044import org.apache.hadoop.hbase.regionserver.MetricsRegionServer;
045import org.apache.hadoop.hbase.regionserver.Region;
046import org.apache.hadoop.hbase.regionserver.RegionServerAccounting;
047import org.apache.hadoop.hbase.regionserver.RegionServerServices;
048import org.apache.hadoop.hbase.regionserver.SecureBulkLoadManager;
049import org.apache.hadoop.hbase.regionserver.ServerNonceManager;
050import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequester;
051import org.apache.hadoop.hbase.regionserver.throttle.ThroughputController;
052import org.apache.hadoop.hbase.util.Bytes;
053import org.apache.hadoop.hbase.wal.WAL;
054import org.apache.hadoop.hbase.zookeeper.MetaTableLocator;
055import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
056import org.apache.zookeeper.KeeperException;
057import org.slf4j.Logger;
058import org.slf4j.LoggerFactory;
059import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
060
061import com.google.protobuf.Service;
062
063/**
064 * Basic mock region server services.  Should only be instantiated by HBaseTestingUtility.b
065 */
066public class MockRegionServerServices implements RegionServerServices {
067  protected static final Logger LOG = LoggerFactory.getLogger(MockRegionServerServices.class);
068  private final Map<String, Region> regions = new HashMap<>();
069  private final ConcurrentSkipListMap<byte[], Boolean> rit =
070    new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR);
071  private HFileSystem hfs = null;
072  private final Configuration conf;
073  private ZKWatcher zkw = null;
074  private ServerName serverName = null;
075  private RpcServerInterface rpcServer = null;
076  private volatile boolean abortRequested;
077  private volatile boolean stopping = false;
078  private final AtomicBoolean running = new AtomicBoolean(true);
079
080  MockRegionServerServices(ZKWatcher zkw) {
081    this(zkw, null);
082  }
083
084  MockRegionServerServices(ZKWatcher zkw, ServerName serverName) {
085    this.zkw = zkw;
086    this.serverName = serverName;
087    this.conf = (zkw == null ? new Configuration() : zkw.getConfiguration());
088  }
089
090  MockRegionServerServices(){
091    this(null, null);
092  }
093
094  public MockRegionServerServices(Configuration conf) {
095    this.conf = conf;
096  }
097
098  @Override
099  public boolean removeRegion(HRegion r, ServerName destination) {
100    return this.regions.remove(r.getRegionInfo().getEncodedName()) != null;
101  }
102
103  @Override
104  public Region getRegion(String encodedRegionName) {
105    return this.regions.get(encodedRegionName);
106  }
107
108  @Override
109  public List<Region> getRegions(TableName tableName) throws IOException {
110    return null;
111  }
112
113  @Override
114  public List<Region> getRegions() {
115    return null;
116  }
117
118  @Override
119  public void addRegion(HRegion r) {
120    this.regions.put(r.getRegionInfo().getEncodedName(), r);
121  }
122
123  @Override
124  public void postOpenDeployTasks(PostOpenDeployContext context) throws KeeperException,
125      IOException {
126    addRegion(context.getRegion());
127  }
128
129  @Override
130  public boolean isStopping() {
131    return this.stopping;
132  }
133
134  @Override
135  public RpcServerInterface getRpcServer() {
136    return rpcServer;
137  }
138
139  public void setRpcServer(RpcServerInterface rpc) {
140    this.rpcServer = rpc;
141  }
142
143  @Override
144  public ConcurrentSkipListMap<byte[], Boolean> getRegionsInTransitionInRS() {
145    return rit;
146  }
147
148  @Override
149  public FlushRequester getFlushRequester() {
150    return null;
151  }
152
153  @Override
154  public CompactionRequester getCompactionRequestor() {
155    return null;
156  }
157
158  @Override
159  public ClusterConnection getConnection() {
160    return null;
161  }
162
163  @Override
164  public MetaTableLocator getMetaTableLocator() {
165    return null;
166  }
167
168  @Override
169  public ZKWatcher getZooKeeper() {
170    return zkw;
171  }
172
173  @Override
174  public CoordinatedStateManager getCoordinatedStateManager() {
175    return null;
176  }
177
178  @Override
179  public RegionServerAccounting getRegionServerAccounting() {
180    return null;
181  }
182
183  @Override
184  public RegionServerRpcQuotaManager getRegionServerRpcQuotaManager() {
185    return null;
186  }
187
188  @Override
189  public ServerName getServerName() {
190    return this.serverName;
191  }
192
193  @Override
194  public Configuration getConfiguration() {
195    return conf;
196  }
197
198  @Override
199  public void abort(String why, Throwable e) {
200    this.abortRequested = true;
201    stop(why);
202  }
203
204  @Override
205  public void stop(String why) {
206    this.stopping = true;
207    if (running.compareAndSet(true, false)) {
208      LOG.info("Shutting down due to request '" + why + "'");
209    }
210  }
211
212  @Override
213  public boolean isStopped() {
214    return !(running.get());
215  }
216
217  @Override
218  public boolean isAborted() {
219    return this.abortRequested;
220  }
221
222  @Override
223  public HFileSystem getFileSystem() {
224    return this.hfs;
225  }
226
227  public void setFileSystem(FileSystem hfs) {
228    this.hfs = (HFileSystem)hfs;
229  }
230
231  @Override
232  public Leases getLeases() {
233    return null;
234  }
235
236  @Override
237  public List<WAL> getWALs() throws IOException {
238    return Collections.emptyList();
239  }
240
241  @Override
242  public WAL getWAL(RegionInfo regionInfo) throws IOException {
243    return null;
244  }
245
246  @Override
247  public ExecutorService getExecutorService() {
248    return null;
249  }
250
251  @Override
252  public ChoreService getChoreService() {
253    return null;
254  }
255
256  @Override
257  public void updateRegionFavoredNodesMapping(String encodedRegionName,
258      List<HBaseProtos.ServerName> favoredNodes) {
259  }
260
261  @Override
262  public InetSocketAddress[] getFavoredNodesForRegion(String encodedRegionName) {
263    return null;
264  }
265
266  @Override
267  public ServerNonceManager getNonceManager() {
268    // TODO Auto-generated method stub
269    return null;
270  }
271
272  @Override
273  public boolean reportRegionStateTransition(RegionStateTransitionContext context) {
274    return false;
275  }
276
277  @Override
278  public boolean registerService(Service service) {
279    // TODO Auto-generated method stub
280    return false;
281  }
282
283  @Override
284  public HeapMemoryManager getHeapMemoryManager() {
285    return null;
286  }
287
288  @Override
289  public double getCompactionPressure() {
290    return 0;
291  }
292
293  @Override
294  public ClusterConnection getClusterConnection() {
295    return null;
296  }
297
298  @Override
299  public ThroughputController getFlushThroughputController() {
300    return null;
301  }
302
303  @Override
304  public double getFlushPressure() {
305    return 0;
306  }
307
308  @Override
309  public MetricsRegionServer getMetrics() {
310    return null;
311  }
312
313  @Override
314  public EntityLock regionLock(List<RegionInfo> regionInfos, String description, Abortable abort)
315      throws IOException {
316    return null;
317  }
318
319  @Override
320  public SecureBulkLoadManager getSecureBulkLoadManager() {
321    return null;
322  }
323
324  @Override
325  public void unassign(byte[] regionName) throws IOException {
326  }
327
328  @Override
329  public RegionServerSpaceQuotaManager getRegionServerSpaceQuotaManager() {
330    return null;
331  }
332
333  @Override
334  public Connection createConnection(Configuration conf) throws IOException {
335    return null;
336  }
337
338  @Override
339  public boolean isClusterUp() {
340    return true;
341  }
342}