ndimiduk commented on code in PR #6370:
URL: https://github.com/apache/hbase/pull/6370#discussion_r1923615193


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/snapshot/SnapshotRegionLocator.java:
##########
@@ -0,0 +1,205 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.snapshot;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import org.apache.commons.lang3.NotImplementedException;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HRegionLocation;
+import org.apache.hadoop.hbase.ServerName;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.client.RegionInfoBuilder;
+import org.apache.hadoop.hbase.client.RegionLocator;
+import org.apache.hadoop.hbase.util.Bytes;
+import org.apache.yetus.audience.InterfaceAudience;
+
+import org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos;
+import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
+
+/**
+ * {@link RegionLocator} built using the most recent full backup's snapshot 
manifest for a given
+ * table. Useful for aligning any subsequent incremental backups along the 
same splits as the full
+ * backup
+ */
[email protected]
+public final class SnapshotRegionLocator implements RegionLocator {
+
+  private static final String SNAPSHOT_MANIFEST_DIR_PREFIX =
+    "region.locator.snapshot.manifest.dir.";
+
+  private static final ServerName FAKE_SERVER_NAME =
+    ServerName.parseServerName("www.example.net,1234,1212121212");
+
+  private final TableName tableName;
+  private final TreeMap<byte[], HRegionReplicas> regions;
+
+  private final List<HRegionLocation> rawLocations;
+
+  public static SnapshotRegionLocator create(Configuration conf, TableName 
table)
+    throws IOException {
+    Path workingDir = new Path(conf.get(getSnapshotManifestDir(table)));
+    FileSystem fs = workingDir.getFileSystem(conf);
+    SnapshotProtos.SnapshotDescription desc =
+      SnapshotDescriptionUtils.readSnapshotInfo(fs, workingDir);
+    SnapshotManifest manifest = SnapshotManifest.open(conf, fs, workingDir, 
desc);
+
+    TableName tableName = manifest.getTableDescriptor().getTableName();
+    TreeMap<byte[], HRegionReplicas> replicas = new 
TreeMap<>(Bytes.BYTES_COMPARATOR);
+    List<HRegionLocation> rawLocations = new ArrayList<>();
+
+    for (SnapshotProtos.SnapshotRegionManifest region : 
manifest.getRegionManifests()) {
+      HBaseProtos.RegionInfo ri = region.getRegionInfo();
+      byte[] key = ri.getStartKey().toByteArray();
+
+      SnapshotHRegionLocation location = toLocation(ri, tableName);
+      rawLocations.add(location);
+      HRegionReplicas hrr = replicas.get(key);
+
+      if (hrr == null) {
+        hrr = new HRegionReplicas(location);
+      } else {
+        hrr.addReplica(location);
+      }
+
+      replicas.put(key, hrr);
+    }
+
+    return new SnapshotRegionLocator(tableName, replicas, rawLocations);
+  }
+
+  private SnapshotRegionLocator(TableName tableName, TreeMap<byte[], 
HRegionReplicas> regions,
+    List<HRegionLocation> rawLocations) {
+    this.tableName = tableName;
+    this.regions = regions;
+    this.rawLocations = rawLocations;
+  }
+
+  @Override
+  public HRegionLocation getRegionLocation(byte[] row, int replicaId, boolean 
reload)
+    throws IOException {
+    return regions.floorEntry(row).getValue().getReplica(replicaId);
+  }
+
+  @Override
+  public List<HRegionLocation> getRegionLocations(byte[] row, boolean reload) 
throws IOException {
+    return List.of(getRegionLocation(row, reload));
+  }
+
+  @Override
+  public void clearRegionLocationCache() {
+
+  }
+
+  @Override
+  public List<HRegionLocation> getAllRegionLocations() throws IOException {
+    return rawLocations;
+  }
+
+  @Override
+  public TableName getName() {
+    return tableName;
+  }
+
+  @Override
+  public void close() throws IOException {
+
+  }
+
+  public static boolean shouldUseSnapshotRegionLocator(Configuration conf, 
TableName table) {
+    return conf.get(getSnapshotManifestDir(table)) != null;
+  }
+
+  public static void setSnapshotManifestDir(Configuration conf, String dir, 
TableName table) {
+    conf.set(getSnapshotManifestDir(table), dir);
+  }
+
+  private static String getSnapshotManifestDir(TableName table) {
+    return SNAPSHOT_MANIFEST_DIR_PREFIX
+      + table.getNameWithNamespaceInclAsString().replaceAll("-", "_");

Review Comment:
   Oh this isn't a directory path, it's a configuration key? My bad.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to