Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 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.catalog;
 import java.util.List;
A tool to migrate the data stored in hbase:meta table to pbuf serialization. Supports migrating from 0.92.x and 0.94.x to 0.96.x for the catalog table.

will be removed for the major release after 0.96.
 public class MetaMigrationConvertingToPB {
   private static final Log LOG = LogFactory.getLog(MetaMigrationConvertingToPB.class);
   private static class ConvertToPBMetaVisitor implements Visitor {
     private final MasterServices services;
     private long numMigratedRows;
     public ConvertToPBMetaVisitor(MasterServices services) {
       this. = services;
        = 0;
     public boolean visit(Result rthrows IOException {
       if (r ==  null || r.isEmpty()) return true;
       // Check info:regioninfo, info:splitA, and info:splitB.  Make sure all
       // have migrated HRegionInfos.
       byte [] hriBytes = getBytes(r.);
       // Presumes that an edit updating all three cells either succeeds or
       // doesn't -- that we don't have case of info:regioninfo migrated but not
       // info:splitA.
       if (isMigrated(hriBytes)) return true;
       // OK. Need to migrate this row in meta.
       //This will 'migrate' the HRI from 092.x and 0.94.x to 0.96+ by reading the
       //writable serialization
       HRegionInfo hri = parseFrom(hriBytes);
       // Now make a put to write back to meta.
       Put p =  MetaEditor.makePutFromRegionInfo(hri);
       // Now migrate info:splitA and info:splitB if they are not null
       MetaEditor.putToCatalogTable(this..getCatalogTracker(), p);
       if (.isDebugEnabled()) {
         .debug("Migrated " + Bytes.toString(p.getRow()));
       return true;
   static void migrateSplitIfNecessary(final Result rfinal Put pfinal byte [] which)
       throws IOException {
     byte [] hriSplitBytes = getBytes(rwhich);
     if (!isMigrated(hriSplitBytes)) {
       //This will 'migrate' the HRI from 092.x and 0.94.x to 0.96+ by reading the
       //writable serialization
       HRegionInfo hri = parseFrom(hriSplitBytes);
   static HRegionInfo parseFrom(byte[] hriBytesthrows IOException {
     try {
      return HRegionInfo.parseFrom(hriBytes);
    } catch (DeserializationException ex) {
      throw new IOException(ex);


r Result to dig in.
qualifier Qualifier to look at in the passed r.
Bytes for an HRegionInfo or null if no bytes or empty bytes found.
  static byte [] getBytes(final Result rfinal byte [] qualifier) {
    byte [] hriBytes = r.getValue(.qualifier);
    if (hriBytes == null || hriBytes.length <= 0) return null;
    return hriBytes;
  static boolean isMigrated(final byte [] hriBytes) {
    if (hriBytes == null || hriBytes.length <= 0) return true;
    return ProtobufUtil.isPBMagicPrefix(hriBytes);

Converting writable serialization to PB, if it is needed.

services MasterServices to get a handle on master
num migrated rows
Throws: or RuntimeException if something goes wrong
  public static long updateMetaIfNecessary(final MasterServices services)
  throws IOException {
    if (isMetaTableUpdated(services.getCatalogTracker())) {
      .info("META already up-to date with PB serialization");
      return 0;
    .info("META has Writable serializations, migrating hbase:meta to PB serialization");
    try {
      long rows = updateMeta(services);
      .info("META updated with PB serialization. Total rows updated: " + rows);
      return rows;
    } catch (IOException e) {
      .warn("Update hbase:meta with PB serialization failed." + "Master startup aborted.");
      throw e;

Update hbase:meta rows, converting writable serialization to PB

num migrated rows
  static long updateMeta(final MasterServices masterServicesthrows IOException {
    .info("Starting update of META");
    ConvertToPBMetaVisitor v = new ConvertToPBMetaVisitor(masterServices);
    MetaReader.fullScan(masterServices.getCatalogTracker(), v);
    .info("Finished update of META. Total rows updated:" + v.numMigratedRows);
    return v.numMigratedRows;


catalogTracker the catalog tracker
True if the meta table has been migrated.
  static boolean isMetaTableUpdated(final CatalogTracker catalogTrackerthrows IOException {
    List<Resultresults = MetaReader.fullScanOfMeta(catalogTracker);
    if (results == null || results.isEmpty()) {
      .info("hbase:meta doesn't have any entries to update.");
      return true;
    for (Result r : results) {
      if (!isMigrated(value)) {
        return false;
    return true;
New to GrepCode? Check out our FAQ X