| 
 | 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 | +package org.apache.hadoop.hdfs.server.federation.router;  | 
 | 19 | + | 
 | 20 | +import org.apache.hadoop.classification.InterfaceAudience;  | 
 | 21 | +import org.apache.hadoop.classification.InterfaceStability;  | 
 | 22 | +import org.apache.hadoop.conf.Configuration;  | 
 | 23 | +import org.apache.hadoop.fs.Path;  | 
 | 24 | +import org.apache.hadoop.hdfs.server.federation.resolver.RemoteLocation;  | 
 | 25 | +import org.apache.hadoop.tools.fedbalance.DistCpProcedure;  | 
 | 26 | +import org.apache.hadoop.tools.fedbalance.FedBalanceConfigs;  | 
 | 27 | +import org.apache.hadoop.tools.fedbalance.FedBalanceContext;  | 
 | 28 | +import org.apache.hadoop.tools.fedbalance.TrashProcedure;  | 
 | 29 | +import org.apache.hadoop.tools.fedbalance.procedure.BalanceJob;  | 
 | 30 | +import org.apache.hadoop.tools.fedbalance.procedure.BalanceProcedure;  | 
 | 31 | +import org.apache.hadoop.tools.fedbalance.procedure.BalanceProcedureScheduler;  | 
 | 32 | + | 
 | 33 | +import java.io.IOException;  | 
 | 34 | +import java.util.List;  | 
 | 35 | +import java.util.concurrent.atomic.AtomicInteger;  | 
 | 36 | + | 
 | 37 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_FORCE_CLOSE_OPEN_FILE;  | 
 | 38 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_FORCE_CLOSE_OPEN_FILE_DEFAULT;  | 
 | 39 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_MAP;  | 
 | 40 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_BANDWIDTH;  | 
 | 41 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_DELAY;  | 
 | 42 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_DELAY_DEFAULT;  | 
 | 43 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_DIFF;  | 
 | 44 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_DIFF_DEFAULT;  | 
 | 45 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_TRASH;  | 
 | 46 | +import static org.apache.hadoop.hdfs.server.federation.router.RBFConfigKeys.DFS_ROUTER_FEDERATION_RENAME_TRASH_DEFAULT;  | 
 | 47 | +import static org.apache.hadoop.tools.fedbalance.FedBalance.DISTCP_PROCEDURE;  | 
 | 48 | +import static org.apache.hadoop.tools.fedbalance.FedBalance.TRASH_PROCEDURE;  | 
 | 49 | +import static org.apache.hadoop.tools.fedbalance.FedBalance.NO_MOUNT;  | 
 | 50 | + | 
 | 51 | +import org.slf4j.Logger;  | 
 | 52 | +import org.slf4j.LoggerFactory;  | 
 | 53 | + | 
 | 54 | +/**  | 
 | 55 | + * Rename across router based federation namespaces.  | 
 | 56 | + */  | 
 | 57 | +@InterfaceAudience.Private  | 
 | 58 | +@InterfaceStability.Unstable  | 
 | 59 | +public class RouterFederationRename {  | 
 | 60 | + | 
 | 61 | +  private static final Logger LOG =  | 
 | 62 | +      LoggerFactory.getLogger(RouterFederationRename.class.getName());  | 
 | 63 | +  private final RouterRpcServer rpcServer;  | 
 | 64 | +  private final Configuration conf;  | 
 | 65 | +  private final AtomicInteger routerRenameCounter = new AtomicInteger();  | 
 | 66 | +  public enum RouterRenameOption {  | 
 | 67 | +    NONE, DISTCP  | 
 | 68 | +  }  | 
 | 69 | + | 
 | 70 | +  public RouterFederationRename(RouterRpcServer rpcServer, Configuration conf) {  | 
 | 71 | +    this.rpcServer = rpcServer;  | 
 | 72 | +    this.conf = conf;  | 
 | 73 | +  }  | 
 | 74 | + | 
 | 75 | +  /**  | 
 | 76 | +   * Router federation rename across namespaces.  | 
 | 77 | +   *  | 
 | 78 | +   * @param src the source path. There is no mount point under the src path.  | 
 | 79 | +   * @param dst the dst path.  | 
 | 80 | +   * @param srcLocations the remote locations of src.  | 
 | 81 | +   * @param dstLocations the remote locations of dst.  | 
 | 82 | +   * @throws IOException if rename fails.  | 
 | 83 | +   * @return true if rename succeeds.  | 
 | 84 | +   */  | 
 | 85 | +  boolean routerFedRename(final String src, final String dst,  | 
 | 86 | +      final List<RemoteLocation> srcLocations,  | 
 | 87 | +      final List<RemoteLocation> dstLocations) throws IOException {  | 
 | 88 | +    if (!rpcServer.isEnableRenameAcrossNamespace()) {  | 
 | 89 | +      throw new IOException("Rename of " + src + " to " + dst  | 
 | 90 | +          + " is not allowed, no eligible destination in the same namespace was"  | 
 | 91 | +          + " found");  | 
 | 92 | +    }  | 
 | 93 | +    if (srcLocations.size() != 1 || dstLocations.size() != 1) {  | 
 | 94 | +      throw new IOException("Rename of " + src + " to " + dst + " is not"  | 
 | 95 | +          + " allowed. The remote location should be exactly one.");  | 
 | 96 | +    }  | 
 | 97 | +    RemoteLocation srcLoc = srcLocations.get(0);  | 
 | 98 | +    RemoteLocation dstLoc = dstLocations.get(0);  | 
 | 99 | +    // Build and submit router federation rename job.  | 
 | 100 | +    BalanceJob job = buildRouterRenameJob(srcLoc.getNameserviceId(),  | 
 | 101 | +        dstLoc.getNameserviceId(), srcLoc.getDest(), dstLoc.getDest());  | 
 | 102 | +    BalanceProcedureScheduler scheduler = rpcServer.getFedRenameScheduler();  | 
 | 103 | +    countIncrement();  | 
 | 104 | +    try {  | 
 | 105 | +      scheduler.submit(job);  | 
 | 106 | +      LOG.info("Rename {} to {} from namespace {} to {}. JobId={}.", src, dst,  | 
 | 107 | +          srcLoc.getNameserviceId(), dstLoc.getNameserviceId(), job.getId());  | 
 | 108 | +      scheduler.waitUntilDone(job);  | 
 | 109 | +      if (job.getError() != null) {  | 
 | 110 | +        throw new IOException("Rename of " + src + " to " + dst + " failed.",  | 
 | 111 | +            job.getError());  | 
 | 112 | +      }  | 
 | 113 | +      return true;  | 
 | 114 | +    } finally {  | 
 | 115 | +      countDecrement();  | 
 | 116 | +    }  | 
 | 117 | +  }  | 
 | 118 | + | 
 | 119 | +  /**  | 
 | 120 | +   * Build router federation rename job moving data from src to dst.  | 
 | 121 | +   * @param srcNs the source namespace id.  | 
 | 122 | +   * @param dstNs the dst namespace id.  | 
 | 123 | +   * @param src the source path.  | 
 | 124 | +   * @param dst the dst path.  | 
 | 125 | +   */  | 
 | 126 | +  private BalanceJob buildRouterRenameJob(String srcNs, String dstNs,  | 
 | 127 | +      String src, String dst) throws IOException {  | 
 | 128 | +    checkConfiguration(conf);  | 
 | 129 | +    Path srcPath = new Path("hdfs://" + srcNs + src);  | 
 | 130 | +    Path dstPath = new Path("hdfs://" + dstNs + dst);  | 
 | 131 | +    boolean forceCloseOpen =  | 
 | 132 | +        conf.getBoolean(DFS_ROUTER_FEDERATION_RENAME_FORCE_CLOSE_OPEN_FILE,  | 
 | 133 | +            DFS_ROUTER_FEDERATION_RENAME_FORCE_CLOSE_OPEN_FILE_DEFAULT);  | 
 | 134 | +    int map = conf.getInt(DFS_ROUTER_FEDERATION_RENAME_MAP, -1);  | 
 | 135 | +    int bandwidth = conf.getInt(DFS_ROUTER_FEDERATION_RENAME_BANDWIDTH, -1);  | 
 | 136 | +    long delay = conf.getLong(DFS_ROUTER_FEDERATION_RENAME_DELAY,  | 
 | 137 | +        DFS_ROUTER_FEDERATION_RENAME_DELAY_DEFAULT);  | 
 | 138 | +    int diff = conf.getInt(DFS_ROUTER_FEDERATION_RENAME_DIFF,  | 
 | 139 | +        DFS_ROUTER_FEDERATION_RENAME_DIFF_DEFAULT);  | 
 | 140 | +    String trashPolicy = conf.get(DFS_ROUTER_FEDERATION_RENAME_TRASH,  | 
 | 141 | +        DFS_ROUTER_FEDERATION_RENAME_TRASH_DEFAULT);  | 
 | 142 | +    FedBalanceConfigs.TrashOption trashOpt =  | 
 | 143 | +        FedBalanceConfigs.TrashOption.valueOf(trashPolicy.toUpperCase());  | 
 | 144 | +    // Construct job context.  | 
 | 145 | +    FedBalanceContext context =  | 
 | 146 | +        new FedBalanceContext.Builder(srcPath, dstPath, NO_MOUNT, conf)  | 
 | 147 | +            .setForceCloseOpenFiles(forceCloseOpen)  | 
 | 148 | +            .setUseMountReadOnly(true)  | 
 | 149 | +            .setMapNum(map)  | 
 | 150 | +            .setBandwidthLimit(bandwidth)  | 
 | 151 | +            .setTrash(trashOpt)  | 
 | 152 | +            .setDelayDuration(delay)  | 
 | 153 | +            .setDiffThreshold(diff)  | 
 | 154 | +            .build();  | 
 | 155 | + | 
 | 156 | +    LOG.info(context.toString());  | 
 | 157 | +    // Construct the balance job.  | 
 | 158 | +    BalanceJob.Builder<BalanceProcedure> builder = new BalanceJob.Builder<>();  | 
 | 159 | +    DistCpProcedure dcp =  | 
 | 160 | +        new DistCpProcedure(DISTCP_PROCEDURE, null, delay, context);  | 
 | 161 | +    builder.nextProcedure(dcp);  | 
 | 162 | +    TrashProcedure tp =  | 
 | 163 | +        new TrashProcedure(TRASH_PROCEDURE, null, delay, context);  | 
 | 164 | +    builder.nextProcedure(tp);  | 
 | 165 | +    return builder.build();  | 
 | 166 | +  }  | 
 | 167 | + | 
 | 168 | +  public int getRouterFederationRenameCount() {  | 
 | 169 | +    return routerRenameCounter.get();  | 
 | 170 | +  }  | 
 | 171 | + | 
 | 172 | +  void countIncrement() {  | 
 | 173 | +    routerRenameCounter.incrementAndGet();  | 
 | 174 | +  }  | 
 | 175 | + | 
 | 176 | +  void countDecrement() {  | 
 | 177 | +    routerRenameCounter.decrementAndGet();  | 
 | 178 | +  }  | 
 | 179 | + | 
 | 180 | +  static void checkConfiguration(Configuration conf) throws IOException {  | 
 | 181 | +    int map = conf.getInt(DFS_ROUTER_FEDERATION_RENAME_MAP, -1);  | 
 | 182 | +    int bandwidth = conf.getInt(DFS_ROUTER_FEDERATION_RENAME_BANDWIDTH, -1);  | 
 | 183 | +    long delay = conf.getLong(DFS_ROUTER_FEDERATION_RENAME_DELAY,  | 
 | 184 | +        DFS_ROUTER_FEDERATION_RENAME_DELAY_DEFAULT);  | 
 | 185 | +    int diff = conf.getInt(DFS_ROUTER_FEDERATION_RENAME_DIFF,  | 
 | 186 | +        DFS_ROUTER_FEDERATION_RENAME_DIFF_DEFAULT);  | 
 | 187 | +    if (map < 0) {  | 
 | 188 | +      throw new IOException("map=" + map + " is negative. Please check "  | 
 | 189 | +          + DFS_ROUTER_FEDERATION_RENAME_MAP);  | 
 | 190 | +    } else if (bandwidth < 0) {  | 
 | 191 | +      throw new IOException(  | 
 | 192 | +          "bandwidth=" + bandwidth + " is negative. Please check "  | 
 | 193 | +              + DFS_ROUTER_FEDERATION_RENAME_BANDWIDTH);  | 
 | 194 | +    } else if (delay < 0) {  | 
 | 195 | +      throw new IOException("delay=" + delay + " is negative. Please check "  | 
 | 196 | +          + DFS_ROUTER_FEDERATION_RENAME_DELAY);  | 
 | 197 | +    } else if (diff < 0) {  | 
 | 198 | +      throw new IOException("diff=" + diff + " is negative. Please check "  | 
 | 199 | +          + DFS_ROUTER_FEDERATION_RENAME_DIFF);  | 
 | 200 | +    }  | 
 | 201 | +  }  | 
 | 202 | +}  | 
0 commit comments