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