|
| 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 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.accumulo.server.util; |
| 20 | + |
| 21 | +import org.apache.accumulo.core.Constants; |
| 22 | +import org.apache.accumulo.core.cli.Help; |
| 23 | +import org.apache.accumulo.core.conf.Property; |
| 24 | +import org.apache.accumulo.core.conf.SiteConfiguration; |
| 25 | +import org.apache.accumulo.core.data.InstanceId; |
| 26 | +import org.apache.accumulo.core.fate.zookeeper.ServiceLock; |
| 27 | +import org.apache.accumulo.core.fate.zookeeper.ServiceLock.ServiceLockPath; |
| 28 | +import org.apache.accumulo.core.fate.zookeeper.ZooReaderWriter; |
| 29 | +import org.apache.accumulo.core.fate.zookeeper.ZooUtil.NodeExistsPolicy; |
| 30 | +import org.apache.accumulo.core.volume.VolumeConfiguration; |
| 31 | +import org.apache.accumulo.server.fs.VolumeManager; |
| 32 | +import org.apache.accumulo.server.security.SecurityUtil; |
| 33 | +import org.apache.accumulo.start.spi.KeywordExecutable; |
| 34 | +import org.apache.hadoop.conf.Configuration; |
| 35 | +import org.apache.hadoop.fs.Path; |
| 36 | +import org.apache.zookeeper.KeeperException; |
| 37 | +import org.slf4j.Logger; |
| 38 | +import org.slf4j.LoggerFactory; |
| 39 | + |
| 40 | +import com.beust.jcommander.JCommander; |
| 41 | +import com.beust.jcommander.Parameter; |
| 42 | +import com.google.auto.service.AutoService; |
| 43 | + |
| 44 | +@AutoService(KeywordExecutable.class) |
| 45 | +public class UpgradeUtil implements KeywordExecutable { |
| 46 | + |
| 47 | + private static final Logger LOG = LoggerFactory.getLogger(UpgradeUtil.class); |
| 48 | + |
| 49 | + static class Opts extends Help { |
| 50 | + @Parameter(names = "--prepare", |
| 51 | + description = "prepare an older version instance for an upgrade to a newer non-bugfix release." |
| 52 | + + " This command should be run using the older version of software after the instance is shut down.") |
| 53 | + boolean prepare = false; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public String keyword() { |
| 58 | + return "upgrade"; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public String description() { |
| 63 | + return "utility used to perform various upgrade steps for an Accumulo instance"; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void execute(String[] args) throws Exception { |
| 68 | + Opts opts = new Opts(); |
| 69 | + opts.parseArgs(keyword(), args); |
| 70 | + |
| 71 | + if (!opts.prepare) { |
| 72 | + new JCommander(opts).usage(); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + var siteConf = SiteConfiguration.auto(); |
| 77 | + // Login as the server on secure HDFS |
| 78 | + if (siteConf.getBoolean(Property.INSTANCE_RPC_SASL_ENABLED)) { |
| 79 | + SecurityUtil.serverLogin(siteConf); |
| 80 | + } |
| 81 | + |
| 82 | + String volDir = VolumeConfiguration.getVolumeUris(siteConf).iterator().next(); |
| 83 | + Path instanceDir = new Path(volDir, "instance_id"); |
| 84 | + InstanceId iid = VolumeManager.getInstanceIDFromHdfs(instanceDir, new Configuration()); |
| 85 | + ZooReaderWriter zoo = new ZooReaderWriter(siteConf); |
| 86 | + |
| 87 | + if (opts.prepare) { |
| 88 | + final String zUpgradepath = Constants.ZROOT + "/" + iid + Constants.ZPREPARE_FOR_UPGRADE; |
| 89 | + try { |
| 90 | + if (zoo.exists(zUpgradepath)) { |
| 91 | + zoo.delete(zUpgradepath); |
| 92 | + } |
| 93 | + } catch (KeeperException | InterruptedException e) { |
| 94 | + throw new IllegalStateException("Error creating or checking for " + zUpgradepath |
| 95 | + + " node in zookeeper: " + e.getMessage(), e); |
| 96 | + } |
| 97 | + |
| 98 | + LOG.info("Upgrade specified, validating that Manager is stopped"); |
| 99 | + final ServiceLockPath mgrPath = |
| 100 | + ServiceLock.path(Constants.ZROOT + "/" + iid + Constants.ZMANAGER_LOCK); |
| 101 | + try { |
| 102 | + if (ServiceLock.getLockData(zoo.getZooKeeper(), mgrPath) != null) { |
| 103 | + throw new IllegalStateException( |
| 104 | + "Manager is running, shut it down and retry this operation"); |
| 105 | + } |
| 106 | + } catch (KeeperException | InterruptedException e) { |
| 107 | + throw new IllegalStateException("Error trying to determine if Manager lock is held", e); |
| 108 | + } |
| 109 | + |
| 110 | + LOG.info("Checking for existing fate transactions"); |
| 111 | + try { |
| 112 | + // Adapted from UpgradeCoordinator.abortIfFateTransactions |
| 113 | + if (!zoo.getChildren(Constants.ZFATE).isEmpty()) { |
| 114 | + throw new IllegalStateException("Cannot complete upgrade preparation" |
| 115 | + + " because FATE transactions exist. You can start a tserver, but" |
| 116 | + + " not the Manager, then use the shell to delete completed" |
| 117 | + + " transactions and fail pending or in-progress transactions." |
| 118 | + + " Once all of the FATE transactions have been removed you can" |
| 119 | + + " retry this operation."); |
| 120 | + } |
| 121 | + } catch (KeeperException | InterruptedException e) { |
| 122 | + throw new IllegalStateException("Error checking for existing FATE transactions", e); |
| 123 | + } |
| 124 | + |
| 125 | + LOG.info("Creating {} node in zookeeper, servers will be prevented from" |
| 126 | + + " starting while this node exists", zUpgradepath); |
| 127 | + try { |
| 128 | + zoo.putPersistentData(zUpgradepath, new byte[0], NodeExistsPolicy.SKIP); |
| 129 | + } catch (KeeperException | InterruptedException e) { |
| 130 | + throw new IllegalStateException("Error creating " + zUpgradepath |
| 131 | + + " node in zookeeper. Check for any issues and retry.", e); |
| 132 | + } |
| 133 | + |
| 134 | + LOG.info("Forcing removal of all server locks"); |
| 135 | + new ZooZap().zap(siteConf, "-manager", "-compaction-coordinators", "-tservers", "-compactors", |
| 136 | + "-sservers"); |
| 137 | + |
| 138 | + LOG.info("Instance {} prepared for upgrade. Server processes will not start while" |
| 139 | + + " in this state. To undo this state and abort upgrade preparations delete" |
| 140 | + + " the zookeeper node: {}. If you abort and restart the instance, then you " |
| 141 | + + " should re-run this utility before upgrading.", iid.canonical(), zUpgradepath); |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments