|
| 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 |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 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. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hbase.master.migrate; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.stream.Collectors; |
| 28 | +import org.apache.commons.lang3.StringUtils; |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.apache.hadoop.hbase.ScheduledChore; |
| 31 | +import org.apache.hadoop.hbase.Stoppable; |
| 32 | +import org.apache.hadoop.hbase.TableDescriptors; |
| 33 | +import org.apache.hadoop.hbase.client.TableDescriptor; |
| 34 | +import org.apache.hadoop.hbase.master.MasterServices; |
| 35 | +import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; |
| 36 | +import org.apache.hadoop.hbase.procedure2.ProcedureExecutor; |
| 37 | +import org.apache.hadoop.hbase.regionserver.storefiletracker.MigrateStoreFileTrackerProcedure; |
| 38 | +import org.apache.hadoop.hbase.regionserver.storefiletracker.StoreFileTrackerFactory; |
| 39 | +import org.apache.yetus.audience.InterfaceAudience; |
| 40 | +import org.slf4j.Logger; |
| 41 | +import org.slf4j.LoggerFactory; |
| 42 | + |
| 43 | +/** |
| 44 | + * To avoid too many migrating/upgrade threads to be submitted at the time during master |
| 45 | + * initialization, RollingUpgradeChore handles all rolling-upgrade tasks. |
| 46 | + * */ |
| 47 | +@InterfaceAudience.Private |
| 48 | +public class RollingUpgradeChore extends ScheduledChore { |
| 49 | + |
| 50 | + static final String ROLLING_UPGRADE_CHORE_PERIOD_SECONDS_KEY = |
| 51 | + "hbase.master.rolling.upgrade.chore.period.secs"; |
| 52 | + static final int DFAULT_ROLLING_UPGRADE_CHORE_PERIOD_SECONDS = 10; // 10 seconds by default |
| 53 | + |
| 54 | + static final String ROLLING_UPGRADE_CHORE_DELAY_SECONDS_KEY = |
| 55 | + "hbase.master.rolling.upgrade.chore.delay.secs"; |
| 56 | + static final long DEFAULT_ROLLING_UPGRADE_CHORE_DELAY_SECONDS = 30; // 30 seconds |
| 57 | + |
| 58 | + static final int CONCURRENT_PROCEDURES_COUNT = 5; |
| 59 | + |
| 60 | + private final static Logger LOG = LoggerFactory.getLogger(RollingUpgradeChore.class); |
| 61 | + ProcedureExecutor<MasterProcedureEnv> procedureExecutor; |
| 62 | + private TableDescriptors tableDescriptors; |
| 63 | + private List<MigrateStoreFileTrackerProcedure> processingProcs = new ArrayList<>(); |
| 64 | + |
| 65 | + public RollingUpgradeChore(MasterServices masterServices) { |
| 66 | + this(masterServices.getConfiguration(), masterServices.getMasterProcedureExecutor(), |
| 67 | + masterServices.getTableDescriptors(), masterServices); |
| 68 | + } |
| 69 | + |
| 70 | + private RollingUpgradeChore(Configuration conf, |
| 71 | + ProcedureExecutor<MasterProcedureEnv> procedureExecutor, TableDescriptors tableDescriptors, |
| 72 | + Stoppable stopper) { |
| 73 | + super(RollingUpgradeChore.class.getSimpleName(), stopper, conf |
| 74 | + .getInt(ROLLING_UPGRADE_CHORE_PERIOD_SECONDS_KEY, |
| 75 | + DFAULT_ROLLING_UPGRADE_CHORE_PERIOD_SECONDS), conf |
| 76 | + .getLong(ROLLING_UPGRADE_CHORE_DELAY_SECONDS_KEY, |
| 77 | + DEFAULT_ROLLING_UPGRADE_CHORE_DELAY_SECONDS), |
| 78 | + TimeUnit.SECONDS); |
| 79 | + this.procedureExecutor = procedureExecutor; |
| 80 | + this.tableDescriptors = tableDescriptors; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + protected void chore() { |
| 85 | + if (isCompletelyMigrateSFT(CONCURRENT_PROCEDURES_COUNT)) { |
| 86 | + LOG.info("All Rolling-Upgrade tasks are complete, shutdown RollingUpgradeChore!"); |
| 87 | + shutdown(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private boolean isCompletelyMigrateSFT(int concurrentCount){ |
| 92 | + Iterator<MigrateStoreFileTrackerProcedure> iter = processingProcs.iterator(); |
| 93 | + while(iter.hasNext()){ |
| 94 | + MigrateStoreFileTrackerProcedure proc = iter.next(); |
| 95 | + if(procedureExecutor.isFinished(proc.getProcId())){ |
| 96 | + iter.remove(); |
| 97 | + } |
| 98 | + } |
| 99 | + // No new migration procedures will be submitted until |
| 100 | + // all procedures executed last time are completed. |
| 101 | + if (!processingProcs.isEmpty()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + Map<String, TableDescriptor> migrateSFTTables; |
| 106 | + try { |
| 107 | + migrateSFTTables = tableDescriptors.getAll().entrySet().stream().filter(entry -> { |
| 108 | + TableDescriptor td = entry.getValue(); |
| 109 | + return StringUtils.isEmpty(td.getValue(StoreFileTrackerFactory.TRACKER_IMPL)); |
| 110 | + }).limit(concurrentCount).collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue())); |
| 111 | + } catch (IOException e) { |
| 112 | + LOG.warn("Failed to migrate StoreFileTracker", e); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + if (migrateSFTTables.isEmpty()) { |
| 117 | + LOG.info("There is no table to migrate StoreFileTracker!"); |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + for (Map.Entry<String, TableDescriptor> entry : migrateSFTTables.entrySet()) { |
| 122 | + TableDescriptor tableDescriptor = entry.getValue(); |
| 123 | + MigrateStoreFileTrackerProcedure proc = |
| 124 | + new MigrateStoreFileTrackerProcedure(procedureExecutor.getEnvironment(), tableDescriptor); |
| 125 | + procedureExecutor.submitProcedure(proc); |
| 126 | + processingProcs.add(proc); |
| 127 | + } |
| 128 | + return false; |
| 129 | + } |
| 130 | +} |
0 commit comments