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.regionserver.storefiletracker; 019 020import java.io.IOException; 021import java.util.Collection; 022import java.util.Collections; 023import java.util.List; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.fs.FileSystem; 026import org.apache.hadoop.hbase.TableName; 027import org.apache.hadoop.hbase.client.RegionInfo; 028import org.apache.hadoop.hbase.client.RegionInfoBuilder; 029import org.apache.hadoop.hbase.regionserver.HRegionFileSystem; 030import org.apache.hadoop.hbase.regionserver.StoreContext; 031import org.apache.hadoop.hbase.regionserver.StoreFileInfo; 032import org.mockito.Mockito; 033import org.slf4j.Logger; 034import org.slf4j.LoggerFactory; 035 036public class DummyStoreFileTrackerForReadOnlyMode extends StoreFileTrackerBase { 037 private static final Logger LOG = 038 LoggerFactory.getLogger(DummyStoreFileTrackerForReadOnlyMode.class); 039 040 private boolean readOnlyUsed = false; 041 private boolean compactionExecuted = false; 042 private boolean addExecuted = false; 043 private boolean setExecuted = false; 044 045 private static StoreContext buildStoreContext(Configuration conf, TableName tableName) { 046 RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build(); 047 HRegionFileSystem hfs = Mockito.mock(HRegionFileSystem.class); 048 try { 049 Mockito.when(hfs.getRegionInfo()).thenReturn(regionInfo); 050 Mockito.when(hfs.getFileSystem()).thenReturn(FileSystem.get(conf)); 051 } catch (IOException e) { 052 LOG.error("Failed to get FileSystem for StoreContext creation", e); 053 } 054 return StoreContext.getBuilder().withRegionFileSystem(hfs).build(); 055 } 056 057 public DummyStoreFileTrackerForReadOnlyMode(Configuration conf, boolean isPrimaryReplica, 058 TableName tableName) { 059 super(conf, isPrimaryReplica, buildStoreContext(conf, tableName)); 060 } 061 062 @Override 063 protected void doAddCompactionResults(Collection<StoreFileInfo> compactedFiles, 064 Collection<StoreFileInfo> newFiles) { 065 compactionExecuted = true; 066 } 067 068 @Override 069 protected void doSetStoreFiles(Collection<StoreFileInfo> files) throws IOException { 070 setExecuted = true; 071 } 072 073 @Override 074 protected List<StoreFileInfo> doLoadStoreFiles(boolean readOnly) { 075 readOnlyUsed = readOnly; 076 return Collections.emptyList(); 077 } 078 079 @Override 080 protected void doAddNewStoreFiles(Collection<StoreFileInfo> newFiles) throws IOException { 081 addExecuted = true; 082 } 083 084 boolean wasReadOnlyLoad() { 085 return readOnlyUsed; 086 } 087 088 boolean wasCompactionExecuted() { 089 return compactionExecuted; 090 } 091 092 boolean wasAddExecuted() { 093 return addExecuted; 094 } 095 096 boolean wasSetExecuted() { 097 return setExecuted; 098 } 099 100 @Override 101 public boolean requireWritingToTmpDirFirst() { 102 return false; 103 } 104}