HBASE-26412 Handle sink failure in RegionReplicationSink (#3815)
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / regionserver / CompactedHFilesDischarger.java
blob2be04c22729fcb25164f121da092638cf848e918
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.regionserver;
20 import java.util.List;
22 import org.apache.hadoop.hbase.ScheduledChore;
23 import org.apache.hadoop.hbase.Server;
24 import org.apache.hadoop.hbase.Stoppable;
25 import org.apache.hadoop.hbase.executor.EventType;
26 import org.apache.yetus.audience.InterfaceAudience;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 /**
31 * A chore service that periodically cleans up the compacted files when there are no active readers
32 * using those compacted files and also helps in clearing the block cache of these compacted
33 * file entries.
35 @InterfaceAudience.Private
36 public class CompactedHFilesDischarger extends ScheduledChore {
37 private static final Logger LOG = LoggerFactory.getLogger(CompactedHFilesDischarger.class);
38 private RegionServerServices regionServerServices;
39 // Default is to use executor
40 private boolean useExecutor = true;
42 /**
43 * @param period the period of time to sleep between each run
44 * @param stopper the stopper
45 * @param regionServerServices the region server that starts this chore
47 public CompactedHFilesDischarger(final int period, final Stoppable stopper,
48 final RegionServerServices regionServerServices) {
49 // Need to add the config classes
50 super("CompactedHFilesCleaner", stopper, period);
51 this.regionServerServices = regionServerServices;
54 /**
55 * @param period the period of time to sleep between each run
56 * @param stopper the stopper
57 * @param regionServerServices the region server that starts this chore
58 * @param useExecutor true if to use the region server's executor service, false otherwise
60 public CompactedHFilesDischarger(final int period, final Stoppable stopper,
61 final RegionServerServices regionServerServices, boolean useExecutor) {
62 // Need to add the config classes
63 this(period, stopper, regionServerServices);
64 this.useExecutor = useExecutor;
67 /**
68 * CompactedHFilesDischarger runs asynchronously by default using the hosting
69 * RegionServer's Executor. In tests it can be useful to force a synchronous
70 * cleanup. Use this method to set no-executor before you call run.
71 * @return The old setting for <code>useExecutor</code>
73 boolean setUseExecutor(final boolean useExecutor) {
74 boolean oldSetting = this.useExecutor;
75 this.useExecutor = useExecutor;
76 return oldSetting;
79 @Override
80 public void chore() {
81 // Noop if rss is null. This will never happen in a normal condition except for cases
82 // when the test case is not spinning up a cluster
83 if (regionServerServices == null) return;
84 List<HRegion> onlineRegions = (List<HRegion>) regionServerServices.getRegions();
85 if (onlineRegions == null) return;
86 for (HRegion region : onlineRegions) {
87 if (LOG.isTraceEnabled()) {
88 LOG.trace("Started compacted hfiles cleaner on " + region.getRegionInfo());
90 for (HStore store : region.getStores()) {
91 try {
92 if (useExecutor && regionServerServices != null) {
93 CompactedHFilesDischargeHandler handler = new CompactedHFilesDischargeHandler(
94 (Server) regionServerServices, EventType.RS_COMPACTED_FILES_DISCHARGER, store);
95 regionServerServices.getExecutorService().submit(handler);
96 } else {
97 // call synchronously if the RegionServerServices are not
98 // available
99 store.closeAndArchiveCompactedFiles();
101 if (LOG.isTraceEnabled()) {
102 LOG.trace("Completed archiving the compacted files for the region "
103 + region.getRegionInfo() + " under the store " + store.getColumnFamilyName());
105 } catch (Exception e) {
106 LOG.error("Exception while trying to close and archive the compacted store "
107 + "files of the store " + store.getColumnFamilyName() + " in the" + " region "
108 + region.getRegionInfo(), e);
111 if (LOG.isTraceEnabled()) {
112 LOG.trace(
113 "Completed the compacted hfiles cleaner for the region " + region.getRegionInfo());