Java Code Examples for org.apache.hadoop.hbase.client.RegionInfo#getReplicaId()

The following examples show how to use org.apache.hadoop.hbase.client.RegionInfo#getReplicaId() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HRegionFileSystem.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Create a new Region on file-system.
 * @param conf the {@link Configuration} to use
 * @param fs {@link FileSystem} from which to add the region
 * @param tableDir {@link Path} to where the table is being stored
 * @param regionInfo {@link RegionInfo} for region to be added
 * @throws IOException if the region creation fails due to a FileSystem exception.
 */
public static HRegionFileSystem createRegionOnFileSystem(final Configuration conf,
    final FileSystem fs, final Path tableDir, final RegionInfo regionInfo) throws IOException {
  HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);

  // We only create a .regioninfo and the region directory if this is the default region replica
  if (regionInfo.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID) {
    Path regionDir = regionFs.getRegionDir();
    if (fs.exists(regionDir)) {
      LOG.warn("Trying to create a region that already exists on disk: " + regionDir);
    } else {
      // Create the region directory
      if (!createDirOnFileSystem(fs, conf, regionDir)) {
        LOG.warn("Unable to create the region directory: " + regionDir);
        throw new IOException("Unable to create region directory: " + regionDir);
      }
    }

    // Write HRI to a file in case we need to recover hbase:meta
    regionFs.writeRegionInfoOnFilesystem(false);
  } else {
    if (LOG.isDebugEnabled())
      LOG.debug("Skipping creation of .regioninfo file for " + regionInfo);
  }
  return regionFs;
}
 
Example 2
Source File: WALFactory.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @param region the region which we want to get a WAL for it. Could be null.
 */
public WAL getWAL(RegionInfo region) throws IOException {
  // use different WAL for hbase:meta
  if (region != null && region.isMetaRegion() &&
    region.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID) {
    return getMetaProvider().getWAL(region);
  } else {
    return provider.getWAL(region);
  }
}
 
Example 3
Source File: WALSplitUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Check whether there is recovered.edits in the region dir
 * @param conf conf
 * @param regionInfo the region to check
 * @return true if recovered.edits exist in the region dir
 */
public static boolean hasRecoveredEdits(final Configuration conf, final RegionInfo regionInfo)
    throws IOException {
  // No recovered.edits for non default replica regions
  if (regionInfo.getReplicaId() != RegionInfo.DEFAULT_REPLICA_ID) {
    return false;
  }
  // Only default replica region can reach here, so we can use regioninfo
  // directly without converting it to default replica's regioninfo.
  Path regionWALDir =
    CommonFSUtils.getWALRegionDir(conf, regionInfo.getTable(), regionInfo.getEncodedName());
  Path regionDir = FSUtils.getRegionDirFromRootDir(CommonFSUtils.getRootDir(conf), regionInfo);
  Path wrongRegionWALDir =
    CommonFSUtils.getWrongWALRegionDir(conf, regionInfo.getTable(), regionInfo.getEncodedName());
  FileSystem walFs = CommonFSUtils.getWALFileSystem(conf);
  FileSystem rootFs = CommonFSUtils.getRootDirFileSystem(conf);
  NavigableSet<Path> files = getSplitEditFilesSorted(walFs, regionWALDir);
  if (!files.isEmpty()) {
    return true;
  }
  files = getSplitEditFilesSorted(rootFs, regionDir);
  if (!files.isEmpty()) {
    return true;
  }
  files = getSplitEditFilesSorted(walFs, wrongRegionWALDir);
  return !files.isEmpty();
}
 
Example 4
Source File: RegionStateStore.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void visitMetaEntry(final RegionStateVisitor visitor, final Result result)
    throws IOException {
  final RegionLocations rl = CatalogFamilyFormat.getRegionLocations(result);
  if (rl == null) return;

  final HRegionLocation[] locations = rl.getRegionLocations();
  if (locations == null) return;

  for (int i = 0; i < locations.length; ++i) {
    final HRegionLocation hrl = locations[i];
    if (hrl == null) continue;

    final RegionInfo regionInfo = hrl.getRegion();
    if (regionInfo == null) continue;

    final int replicaId = regionInfo.getReplicaId();
    final State state = getRegionState(result, regionInfo);

    final ServerName lastHost = hrl.getServerName();
    ServerName regionLocation = MetaTableAccessor.getTargetServerName(result, replicaId);
    final long openSeqNum = hrl.getSeqNum();

    // TODO: move under trace, now is visible for debugging
    LOG.info(
      "Load hbase:meta entry region={}, regionState={}, lastHost={}, " +
        "regionLocation={}, openSeqNum={}",
      regionInfo.getEncodedName(), state, lastHost, regionLocation, openSeqNum);
    visitor.visitRegionState(result, regionInfo, state, regionLocation, lastHost, openSeqNum);
  }
}
 
Example 5
Source File: EnableTableProcedure.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * @return Maximum region replica id found in passed list of regions.
 */
private static int getMaxReplicaId(List<RegionInfo> regions) {
  int max = 0;
  for (RegionInfo regionInfo: regions) {
    if (regionInfo.getReplicaId() > max) {
      // Iterating through all the list to identify the highest replicaID region.
      // We can stop after checking with the first set of regions??
      max = regionInfo.getReplicaId();
    }
  }
  return max;

}
 
Example 6
Source File: HbckRegionInfo.java    From hbase with Apache License 2.0 5 votes vote down vote up
public synchronized void addServer(RegionInfo regionInfo, ServerName serverName) {
  OnlineEntry rse = new OnlineEntry(regionInfo, serverName) ;
  this.deployedEntries.add(rse);
  this.deployedOn.add(serverName);
  // save the replicaId that we see deployed in the cluster
  this.deployedReplicaId = regionInfo.getReplicaId();
  this.primaryHRIForDeployedReplica =
      RegionReplicaUtil.getRegionInfoForDefaultReplica(regionInfo);
}
 
Example 7
Source File: HRegionFileSystem.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Open Region from file-system.
 * @param conf the {@link Configuration} to use
 * @param fs {@link FileSystem} from which to add the region
 * @param tableDir {@link Path} to where the table is being stored
 * @param regionInfo {@link RegionInfo} for region to be added
 * @param readOnly True if you don't want to edit the region data
 * @throws IOException if the region creation fails due to a FileSystem exception.
 */
public static HRegionFileSystem openRegionFromFileSystem(final Configuration conf,
    final FileSystem fs, final Path tableDir, final RegionInfo regionInfo, boolean readOnly)
    throws IOException {
  HRegionFileSystem regionFs = new HRegionFileSystem(conf, fs, tableDir, regionInfo);
  Path regionDir = regionFs.getRegionDir();

  if (!fs.exists(regionDir)) {
    LOG.warn("Trying to open a region that do not exists on disk: " + regionDir);
    throw new IOException("The specified region do not exists on disk: " + regionDir);
  }

  if (!readOnly) {
    // Cleanup temporary directories
    regionFs.cleanupTempDir();
    regionFs.cleanupSplitsDir();
    regionFs.cleanupMergesDir();

    // If it doesn't exists, Write HRI to a file, in case we need to recover hbase:meta
    // Only create HRI if we are the default replica
    if (regionInfo.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID) {
      regionFs.checkRegionInfoOnFilesystem();
    } else {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Skipping creation of .regioninfo file for " + regionInfo);
      }
    }
  }

  return regionFs;
}
 
Example 8
Source File: TestRegionReplicaReplicationEndpointNoMaster.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public void postWALWrite(ObserverContext<? extends WALCoprocessorEnvironment> ctx,
                         RegionInfo info, WALKey logKey, WALEdit logEdit) throws IOException {
  // only keep primary region's edits
  if (logKey.getTableName().equals(tableName) && info.getReplicaId() == 0) {
    // Presume type is a WALKeyImpl
    entries.add(new Entry((WALKeyImpl)logKey, logEdit));
  }
}
 
Example 9
Source File: RegionStateStore.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void updateUserRegionLocation(RegionInfo regionInfo, State state,
    ServerName regionLocation, long openSeqNum,
     long pid) throws IOException {
  long time = EnvironmentEdgeManager.currentTime();
  final int replicaId = regionInfo.getReplicaId();
  final Put put = new Put(CatalogFamilyFormat.getMetaKeyForRegion(regionInfo), time);
  MetaTableAccessor.addRegionInfo(put, regionInfo);
  final StringBuilder info =
    new StringBuilder("pid=").append(pid).append(" updating hbase:meta row=")
      .append(regionInfo.getEncodedName()).append(", regionState=").append(state);
  if (openSeqNum >= 0) {
    Preconditions.checkArgument(state == State.OPEN && regionLocation != null,
        "Open region should be on a server");
    MetaTableAccessor.addLocation(put, regionLocation, openSeqNum, replicaId);
    // only update replication barrier for default replica
    if (regionInfo.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID &&
      hasGlobalReplicationScope(regionInfo.getTable())) {
      MetaTableAccessor.addReplicationBarrier(put, openSeqNum);
      info.append(", repBarrier=").append(openSeqNum);
    }
    info.append(", openSeqNum=").append(openSeqNum);
    info.append(", regionLocation=").append(regionLocation);
  } else if (regionLocation != null) {
    // Ideally, if no regionLocation, write null to the hbase:meta but this will confuse clients
    // currently; they want a server to hit. TODO: Make clients wait if no location.
    put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
        .setRow(put.getRow())
        .setFamily(HConstants.CATALOG_FAMILY)
        .setQualifier(CatalogFamilyFormat.getServerNameColumn(replicaId))
        .setTimestamp(put.getTimestamp())
        .setType(Cell.Type.Put)
        .setValue(Bytes.toBytes(regionLocation.getServerName()))
        .build());
    info.append(", regionLocation=").append(regionLocation);
  }
  put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY)
      .setRow(put.getRow())
      .setFamily(HConstants.CATALOG_FAMILY)
      .setQualifier(getStateColumn(replicaId))
      .setTimestamp(put.getTimestamp())
      .setType(Cell.Type.Put)
      .setValue(Bytes.toBytes(state.name()))
      .build());
  LOG.info(info.toString());
  updateRegionLocation(regionInfo, state, put);
}
 
Example 10
Source File: HBaseFsck.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Scan hbase:meta, adding all regions found to the regionInfo map.
 * @throws IOException if an error is encountered
 */
boolean loadMetaEntries() throws IOException {
  ClientMetaTableAccessor.Visitor visitor = new ClientMetaTableAccessor.Visitor() {
    int countRecord = 1;

    // comparator to sort KeyValues with latest modtime
    final Comparator<Cell> comp = new Comparator<Cell>() {
      @Override
      public int compare(Cell k1, Cell k2) {
        return Long.compare(k1.getTimestamp(), k2.getTimestamp());
      }
    };

    @Override
    public boolean visit(Result result) throws IOException {
      try {

        // record the latest modification of this META record
        long ts =  Collections.max(result.listCells(), comp).getTimestamp();
        RegionLocations rl = CatalogFamilyFormat.getRegionLocations(result);
        if (rl == null) {
          emptyRegionInfoQualifiers.add(result);
          errors.reportError(ERROR_CODE.EMPTY_META_CELL,
            "Empty REGIONINFO_QUALIFIER found in hbase:meta");
          return true;
        }
        ServerName sn = null;
        if (rl.getRegionLocation(RegionInfo.DEFAULT_REPLICA_ID) == null ||
            rl.getRegionLocation(RegionInfo.DEFAULT_REPLICA_ID).getRegion() == null) {
          emptyRegionInfoQualifiers.add(result);
          errors.reportError(ERROR_CODE.EMPTY_META_CELL,
            "Empty REGIONINFO_QUALIFIER found in hbase:meta");
          return true;
        }
        RegionInfo hri = rl.getRegionLocation(RegionInfo.DEFAULT_REPLICA_ID).getRegion();
        if (!(isTableIncluded(hri.getTable())
            || hri.isMetaRegion())) {
          return true;
        }
        PairOfSameType<RegionInfo> daughters = MetaTableAccessor.getDaughterRegions(result);
        for (HRegionLocation h : rl.getRegionLocations()) {
          if (h == null || h.getRegion() == null) {
            continue;
          }
          sn = h.getServerName();
          hri = h.getRegion();

          HbckRegionInfo.MetaEntry m = null;
          if (hri.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID) {
            m = new HbckRegionInfo.MetaEntry(hri, sn, ts, daughters.getFirst(),
                daughters.getSecond());
          } else {
            m = new HbckRegionInfo.MetaEntry(hri, sn, ts, null, null);
          }
          HbckRegionInfo previous = regionInfoMap.get(hri.getEncodedName());
          if (previous == null) {
            regionInfoMap.put(hri.getEncodedName(), new HbckRegionInfo(m));
          } else if (previous.getMetaEntry() == null) {
            previous.setMetaEntry(m);
          } else {
            throw new IOException("Two entries in hbase:meta are same " + previous);
          }
        }
        List<RegionInfo> mergeParents = MetaTableAccessor.getMergeRegions(result.rawCells());
        if (mergeParents != null) {
          for (RegionInfo mergeRegion : mergeParents) {
            if (mergeRegion != null) {
              // This region is already being merged
              HbckRegionInfo hbInfo = getOrCreateInfo(mergeRegion.getEncodedName());
              hbInfo.setMerged(true);
            }
          }
        }

        // show proof of progress to the user, once for every 100 records.
        if (countRecord % 100 == 0) {
          errors.progress();
        }
        countRecord++;
        return true;
      } catch (RuntimeException e) {
        LOG.error("Result=" + result);
        throw e;
      }
    }
  };
  if (!checkMetaOnly) {
    // Scan hbase:meta to pick up user regions
    MetaTableAccessor.fullScanRegions(connection, visitor);
  }

  errors.print("");
  return true;
}