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