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.Collection;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027import java.util.Map.Entry;
028import java.util.Optional;
029import java.util.concurrent.ConcurrentSkipListMap;
030import java.util.concurrent.atomic.AtomicBoolean;
031import org.apache.hadoop.conf.Configuration;
032import org.apache.hadoop.fs.FileSystem;
033import org.apache.hadoop.hbase.client.AsyncClusterConnection;
034import org.apache.hadoop.hbase.client.Connection;
035import org.apache.hadoop.hbase.client.RegionInfo;
036import org.apache.hadoop.hbase.client.locking.EntityLock;
037import org.apache.hadoop.hbase.executor.ExecutorService;
038import org.apache.hadoop.hbase.fs.HFileSystem;
039import org.apache.hadoop.hbase.io.hfile.BlockCache;
040import org.apache.hadoop.hbase.ipc.RpcServerInterface;
041import org.apache.hadoop.hbase.mob.MobFileCache;
042import org.apache.hadoop.hbase.quotas.RegionServerRpcQuotaManager;
043import org.apache.hadoop.hbase.quotas.RegionServerSpaceQuotaManager;
044import org.apache.hadoop.hbase.quotas.RegionSizeStore;
045import org.apache.hadoop.hbase.regionserver.FlushRequester;
046import org.apache.hadoop.hbase.regionserver.HRegion;
047import org.apache.hadoop.hbase.regionserver.HeapMemoryManager;
048import org.apache.hadoop.hbase.regionserver.LeaseManager;
049import org.apache.hadoop.hbase.regionserver.MetricsRegionServer;
050import org.apache.hadoop.hbase.regionserver.RegionServerAccounting;
051import org.apache.hadoop.hbase.regionserver.RegionServerServices;
052import org.apache.hadoop.hbase.regionserver.ReplicationSourceService;
053import org.apache.hadoop.hbase.regionserver.SecureBulkLoadManager;
054import org.apache.hadoop.hbase.regionserver.ServerNonceManager;
055import org.apache.hadoop.hbase.regionserver.compactions.CompactionRequester;
056import org.apache.hadoop.hbase.regionserver.regionreplication.RegionReplicationBufferManager;
057import org.apache.hadoop.hbase.regionserver.throttle.ThroughputController;
058import org.apache.hadoop.hbase.security.access.AccessChecker;
059import org.apache.hadoop.hbase.security.access.ZKPermissionWatcher;
060import org.apache.hadoop.hbase.util.Bytes;
061import org.apache.hadoop.hbase.wal.WAL;
062import org.apache.hadoop.hbase.zookeeper.ZKWatcher;
063import org.apache.yetus.audience.InterfaceAudience;
064import org.slf4j.Logger;
065import org.slf4j.LoggerFactory;
066
067import org.apache.hbase.thirdparty.com.google.protobuf.Service;
068
069import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
070
071/**
072 * Basic mock region server services. Should only be instantiated by HBaseTestingUtility.b
073 */
074@InterfaceAudience.Private
075public class MockRegionServerServices implements RegionServerServices {
076  protected static final Logger LOG = LoggerFactory.getLogger(MockRegionServerServices.class);
077  private final Map<String, HRegion> regions = new HashMap<>();
078  private final ConcurrentSkipListMap<byte[], Boolean> rit =
079    new ConcurrentSkipListMap<>(Bytes.BYTES_COMPARATOR);
080  private HFileSystem hfs = null;
081  private final Configuration conf;
082  private ZKWatcher zkw = null;
083  private ServerName serverName = null;
084  private RpcServerInterface rpcServer = null;
085  private volatile boolean abortRequested;
086  private volatile boolean stopping = false;
087  private final AtomicBoolean running = new AtomicBoolean(true);
088
089  public MockRegionServerServices(ZKWatcher zkw) {
090    this(zkw, null);
091  }
092
093  public MockRegionServerServices(ZKWatcher zkw, ServerName serverName) {
094    this.zkw = zkw;
095    this.serverName = serverName;
096    this.conf = (zkw == null ? new Configuration() : zkw.getConfiguration());
097  }
098
099  public MockRegionServerServices() {
100    this(null, null);
101  }
102
103  public MockRegionServerServices(Configuration conf) {
104    this.conf = conf;
105  }
106
107  @Override
108  public boolean removeRegion(HRegion r, ServerName destination) {
109    return this.regions.remove(r.getRegionInfo().getEncodedName()) != null;
110  }
111
112  @Override
113  public HRegion getRegion(String encodedRegionName) {
114    return this.regions.get(encodedRegionName);
115  }
116
117  @Override
118  public List<HRegion> getRegions(TableName tableName) throws IOException {
119    return null;
120  }
121
122  @Override
123  public List<HRegion> getRegions() {
124    return null;
125  }
126
127  @Override
128  public void addRegion(HRegion r) {
129    this.regions.put(r.getRegionInfo().getEncodedName(), r);
130  }
131
132  @Override
133  public void postOpenDeployTasks(PostOpenDeployContext context) throws IOException {
134    addRegion(context.getRegion());
135  }
136
137  @Override
138  public boolean isStopping() {
139    return this.stopping;
140  }
141
142  @Override
143  public RpcServerInterface getRpcServer() {
144    return rpcServer;
145  }
146
147  public void setRpcServer(RpcServerInterface rpc) {
148    this.rpcServer = rpc;
149  }
150
151  @Override
152  public ConcurrentSkipListMap<byte[], Boolean> getRegionsInTransitionInRS() {
153    return rit;
154  }
155
156  @Override
157  public FlushRequester getFlushRequester() {
158    return null;
159  }
160
161  @Override
162  public CompactionRequester getCompactionRequestor() {
163    return null;
164  }
165
166  @Override
167  public Connection getConnection() {
168    return null;
169  }
170
171  @Override
172  public ZKWatcher getZooKeeper() {
173    return zkw;
174  }
175
176  @Override
177  public CoordinatedStateManager getCoordinatedStateManager() {
178    return null;
179  }
180
181  @Override
182  public RegionServerAccounting getRegionServerAccounting() {
183    return null;
184  }
185
186  @Override
187  public RegionServerRpcQuotaManager getRegionServerRpcQuotaManager() {
188    return null;
189  }
190
191  @Override
192  public ServerName getServerName() {
193    return this.serverName;
194  }
195
196  @Override
197  public Configuration getConfiguration() {
198    return conf;
199  }
200
201  @Override
202  public void abort(String why, Throwable e) {
203    this.abortRequested = true;
204    stop(why);
205  }
206
207  @Override
208  public void stop(String why) {
209    this.stopping = true;
210    if (running.compareAndSet(true, false)) {
211      LOG.info("Shutting down due to request '" + why + "'");
212    }
213  }
214
215  @Override
216  public boolean isStopped() {
217    return !(running.get());
218  }
219
220  @Override
221  public boolean isAborted() {
222    return this.abortRequested;
223  }
224
225  @Override
226  public HFileSystem getFileSystem() {
227    return this.hfs;
228  }
229
230  public void setFileSystem(FileSystem hfs) {
231    this.hfs = (HFileSystem) hfs;
232  }
233
234  @Override
235  public LeaseManager getLeaseManager() {
236    return null;
237  }
238
239  @Override
240  public List<WAL> getWALs() throws IOException {
241    return Collections.emptyList();
242  }
243
244  @Override
245  public WAL getWAL(RegionInfo regionInfo) throws IOException {
246    return null;
247  }
248
249  @Override
250  public ExecutorService getExecutorService() {
251    return null;
252  }
253
254  @Override
255  public ChoreService getChoreService() {
256    return null;
257  }
258
259  @Override
260  public void updateRegionFavoredNodesMapping(String encodedRegionName,
261    List<HBaseProtos.ServerName> favoredNodes) {
262  }
263
264  @Override
265  public InetSocketAddress[] getFavoredNodesForRegion(String encodedRegionName) {
266    return null;
267  }
268
269  @Override
270  public ServerNonceManager getNonceManager() {
271    return null;
272  }
273
274  @Override
275  public boolean reportRegionStateTransition(RegionStateTransitionContext context) {
276    return false;
277  }
278
279  @Override
280  public boolean registerService(Service service) {
281    return false;
282  }
283
284  @Override
285  public HeapMemoryManager getHeapMemoryManager() {
286    return null;
287  }
288
289  @Override
290  public double getCompactionPressure() {
291    return 0;
292  }
293
294  @Override
295  public ThroughputController getFlushThroughputController() {
296    return null;
297  }
298
299  @Override
300  public double getFlushPressure() {
301    return 0;
302  }
303
304  @Override
305  public MetricsRegionServer getMetrics() {
306    return null;
307  }
308
309  @Override
310  public EntityLock regionLock(List<RegionInfo> regionInfos, String description, Abortable abort)
311    throws IOException {
312    return null;
313  }
314
315  @Override
316  public SecureBulkLoadManager getSecureBulkLoadManager() {
317    return null;
318  }
319
320  @Override
321  public void unassign(byte[] regionName) throws IOException {
322  }
323
324  @Override
325  public RegionServerSpaceQuotaManager getRegionServerSpaceQuotaManager() {
326    return null;
327  }
328
329  @Override
330  public Connection createConnection(Configuration conf) throws IOException {
331    return null;
332  }
333
334  @Override
335  public boolean reportRegionSizesForQuotas(RegionSizeStore sizeStore) {
336    return true;
337  }
338
339  @Override
340  public boolean reportFileArchivalForQuotas(TableName tableName,
341    Collection<Entry<String, Long>> archivedFiles) {
342    return true;
343  }
344
345  @Override
346  public boolean isClusterUp() {
347    return true;
348  }
349
350  @Override
351  public ReplicationSourceService getReplicationSourceService() {
352    return null;
353  }
354
355  @Override
356  public TableDescriptors getTableDescriptors() {
357    return null;
358  }
359
360  @Override
361  public Optional<BlockCache> getBlockCache() {
362    return Optional.empty();
363  }
364
365  @Override
366  public Optional<MobFileCache> getMobFileCache() {
367    return Optional.empty();
368  }
369
370  @Override
371  public AccessChecker getAccessChecker() {
372    return null;
373  }
374
375  @Override
376  public ZKPermissionWatcher getZKPermissionWatcher() {
377    return null;
378  }
379
380  @Override
381  public AsyncClusterConnection getAsyncClusterConnection() {
382    return null;
383  }
384
385  @Override
386  public RegionReplicationBufferManager getRegionReplicationBufferManager() {
387    return null;
388  }
389}