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 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.catalog;
 
 import java.util.List;
 
 
Writes region and assignment information to hbase:meta. TODO: Put MetaReader and MetaEditor together; doesn't make sense having them distinct. see HBASE-3475.
 
 public class MetaEditor {
   // TODO: Strip CatalogTracker from this class.  Its all over and in the end
   // its only used to get its Configuration so we can get associated
   // Connection.
   private static final Log LOG = LogFactory.getLog(MetaEditor.class);

  
Generates and returns a Put containing the region into for the catalog table
 
   public static Put makePutFromRegionInfo(HRegionInfo regionInfo)
   throws IOException {
     Put put = new Put(regionInfo.getRegionName());
     addRegionInfo(putregionInfo);
     return put;
   }

  
Generates and returns a Delete containing the region info for the catalog table
 
   public static Delete makeDeleteFromRegionInfo(HRegionInfo regionInfo) {
     if (regionInfo == null) {
       throw new IllegalArgumentException("Can't make a delete for null region");
     }
     Delete delete = new Delete(regionInfo.getRegionName());
     return delete;
   }

  
Adds split daughters to the Put
 
   public static Put addDaughtersToPut(Put putHRegionInfo splitAHRegionInfo splitB) {
     if (splitA != null) {
       put.addImmutable(
     }
     if (splitB != null) {
       put.addImmutable(
     }
     return put;
   }

  
Put the passed p to the hbase:meta table.

Parameters:
ct CatalogTracker on whose back we will ride the edit.
p Put to add to hbase:meta
Throws:
java.io.IOException
  static void putToMetaTable(final CatalogTracker ctfinal Put p)
  throws IOException {
    put(MetaReader.getMetaHTable(ct), p);
  }

  
Put the passed p to a catalog table.

Parameters:
ct CatalogTracker on whose back we will ride the edit.
p Put to add
Throws:
java.io.IOException
  static void putToCatalogTable(final CatalogTracker ctfinal Put p)
  throws IOException {
    put(MetaReader.getCatalogHTable(ct), p);
  }

  

Parameters:
t Table to use (will be closed when done).
p
Throws:
java.io.IOException
  private static void put(final HTable tfinal Put pthrows IOException {
    try {
      t.put(p);
    } finally {
      t.close();
    }
  }

  
Put the passed ps to the hbase:meta table.

Parameters:
ct CatalogTracker on whose back we will ride the edit.
ps Put to add to hbase:meta
Throws:
java.io.IOException
  public static void putsToMetaTable(final CatalogTracker ctfinal List<Putps)
  throws IOException {
    HTable t = MetaReader.getMetaHTable(ct);
    try {
      t.put(ps);
    } finally {
      t.close();
    }
  }

  
Delete the passed d from the hbase:meta table.

Parameters:
ct CatalogTracker on whose back we will ride the edit.
d Delete to add to hbase:meta
Throws:
java.io.IOException
  static void deleteFromMetaTable(final CatalogTracker ctfinal Delete d)
      throws IOException {
    List<Deletedels = new ArrayList<Delete>(1);
    dels.add(d);
    deleteFromMetaTable(ctdels);
  }

  
Delete the passed deletes from the hbase:meta table.

Parameters:
ct CatalogTracker on whose back we will ride the edit.
deletes Deletes to add to hbase:meta This list should support #remove.
Throws:
java.io.IOException
  public static void deleteFromMetaTable(final CatalogTracker ctfinal List<Deletedeletes)
      throws IOException {
    HTable t = MetaReader.getMetaHTable(ct);
    try {
      t.delete(deletes);
    } finally {
      t.close();
    }
  }

  
Execute the passed mutations against hbase:meta table.

Parameters:
ct CatalogTracker on whose back we will ride the edit.
mutations Puts and Deletes to execute on hbase:meta
Throws:
java.io.IOException
  public static void mutateMetaTable(final CatalogTracker ctfinal List<Mutationmutations)
      throws IOException {
    HTable t = MetaReader.getMetaHTable(ct);
    try {
      t.batch(mutations);
    } catch (InterruptedException e) {
      ie.initCause(e);
      throw ie;
    } finally {
      t.close();
    }
  }

  
Adds a hbase:meta row for the specified new region.

Parameters:
regionInfo region information
Throws:
java.io.IOException if problem connecting or updating meta
  public static void addRegionToMeta(CatalogTracker catalogTracker,
      HRegionInfo regionInfo)
  throws IOException {
    putToMetaTable(catalogTrackermakePutFromRegionInfo(regionInfo));
    .info("Added " + regionInfo.getRegionNameAsString());
  }

  
Adds a hbase:meta row for the specified new region to the given catalog table. The HTable is not flushed or closed.

Parameters:
meta the HTable for META
regionInfo region information
Throws:
java.io.IOException if problem connecting or updating meta
  public static void addRegionToMeta(HTable metaHRegionInfo regionInfothrows IOException {
    addRegionToMeta(metaregionInfonullnull);
  }

  
Adds a (single) hbase:meta row for the specified new region and its daughters. Note that this does not add its daughter's as different rows, but adds information about the daughters in the same row as the parent. Use splitRegion(org.apache.hadoop.hbase.catalog.CatalogTracker,org.apache.hadoop.hbase.HRegionInfo,org.apache.hadoop.hbase.HRegionInfo,org.apache.hadoop.hbase.HRegionInfo,org.apache.hadoop.hbase.ServerName) if you want to do that.

Parameters:
meta the HTable for META
regionInfo region information
splitA first split daughter of the parent regionInfo
splitB second split daughter of the parent regionInfo
Throws:
java.io.IOException if problem connecting or updating meta
  public static void addRegionToMeta(HTable metaHRegionInfo regionInfo,
      HRegionInfo splitAHRegionInfo splitBthrows IOException {
    Put put = makePutFromRegionInfo(regionInfo);
    addDaughtersToPut(putsplitAsplitB);
    meta.put(put);
    if (.isDebugEnabled()) {
      .debug("Added " + regionInfo.getRegionNameAsString());
    }
  }

  
Adds a (single) hbase:meta row for the specified new region and its daughters. Note that this does not add its daughter's as different rows, but adds information about the daughters in the same row as the parent. Use splitRegion(org.apache.hadoop.hbase.catalog.CatalogTracker,org.apache.hadoop.hbase.HRegionInfo,org.apache.hadoop.hbase.HRegionInfo,org.apache.hadoop.hbase.HRegionInfo,org.apache.hadoop.hbase.ServerName) if you want to do that.

Parameters:
catalogTracker CatalogTracker on whose back we will ride the edit.
regionInfo region information
splitA first split daughter of the parent regionInfo
splitB second split daughter of the parent regionInfo
Throws:
java.io.IOException if problem connecting or updating meta
  public static void addRegionToMeta(CatalogTracker catalogTrackerHRegionInfo regionInfo,
      HRegionInfo splitAHRegionInfo splitBthrows IOException {
    HTable meta = MetaReader.getMetaHTable(catalogTracker);
    try {
      addRegionToMeta(metaregionInfosplitAsplitB);
    } finally {
      meta.close();
    }
  }

  
Adds a hbase:meta row for each of the specified new regions.

Parameters:
catalogTracker CatalogTracker
regionInfos region information list
Throws:
java.io.IOException if problem connecting or updating meta
  public static void addRegionsToMeta(CatalogTracker catalogTracker,
      List<HRegionInforegionInfos)
  throws IOException {
    List<Putputs = new ArrayList<Put>();
    for (HRegionInfo regionInfo : regionInfos) {
      puts.add(makePutFromRegionInfo(regionInfo));
    }
    putsToMetaTable(catalogTrackerputs);
    .info("Added " + puts.size());
  }

  
Adds a daughter region entry to meta.

Parameters:
regionInfo the region to put
sn the location of the region
openSeqNum the latest sequence number obtained when the region was open
  public static void addDaughter(final CatalogTracker catalogTracker,
      final HRegionInfo regionInfofinal ServerName snfinal long openSeqNum)
    Put put = new Put(regionInfo.getRegionName());
    addRegionInfo(putregionInfo);
    if (sn != null) {
      addLocation(putsnopenSeqNum);
    }
    putToMetaTable(catalogTrackerput);
    .info("Added daughter " + regionInfo.getEncodedName() +
      (sn == null", serverName=null"", serverName=" + sn.toString()));
  }

  
Merge the two regions into one in an atomic operation. Deletes the two merging regions in hbase:meta and adds the merged region with the information of two merging regions.

Parameters:
catalogTracker the catalog tracker
mergedRegion the merged region
regionA
regionB
sn the location of the region
Throws:
java.io.IOException
  public static void mergeRegions(final CatalogTracker catalogTracker,
      HRegionInfo mergedRegionHRegionInfo regionAHRegionInfo regionB,
      ServerName snthrows IOException {
    HTable meta = MetaReader.getMetaHTable(catalogTracker);
    try {
      HRegionInfo copyOfMerged = new HRegionInfo(mergedRegion);
      // Put for parent
      Put putOfMerged = makePutFromRegionInfo(copyOfMerged);
          regionA.toByteArray());
          regionB.toByteArray());
      // Deletes for merging regions
      Delete deleteA = makeDeleteFromRegionInfo(regionA);
      Delete deleteB = makeDeleteFromRegionInfo(regionB);
      // The merged is a new region, openSeqNum = 1 is fine.
      addLocation(putOfMergedsn, 1);
      byte[] tableRow = Bytes.toBytes(mergedRegion.getRegionNameAsString()
          + .);
      multiMutate(metatableRowputOfMergeddeleteAdeleteB);
    } finally {
      meta.close();
    }
  }

  
Splits the region into two in an atomic operation. Offlines the parent region with the information that it is split into two, and also adds the daughter regions. Does not add the location information to the daughter regions since they are not open yet.

Parameters:
catalogTracker the catalog tracker
parent the parent region which is split
splitA Split daughter region A
splitB Split daughter region A
sn the location of the region
  public static void splitRegion(final CatalogTracker catalogTracker,
      HRegionInfo parentHRegionInfo splitAHRegionInfo splitB,
      ServerName snthrows IOException {
    HTable meta = MetaReader.getMetaHTable(catalogTracker);
    try {
      HRegionInfo copyOfParent = new HRegionInfo(parent);
      copyOfParent.setOffline(true);
      copyOfParent.setSplit(true);
      //Put for parent
      Put putParent = makePutFromRegionInfo(copyOfParent);
      addDaughtersToPut(putParentsplitAsplitB);
      //Puts for daughters
      Put putA = makePutFromRegionInfo(splitA);
      Put putB = makePutFromRegionInfo(splitB);
      addLocation(putAsn, 1); //these are new regions, openSeqNum = 1 is fine.
      addLocation(putBsn, 1);
      byte[] tableRow = Bytes.toBytes(parent.getRegionNameAsString() + .);
      multiMutate(metatableRowputParentputAputB);
    } finally {
      meta.close();
    }
  }

  
Performs an atomic multi-Mutate operation against the given table.
  private static void multiMutate(HTable tablebyte[] rowMutation... mutationsthrows IOException {
    CoprocessorRpcChannel channel = table.coprocessorService(row);
    MutateRowsRequest.Builder mmrBuilder = MutateRowsRequest.newBuilder();
    for (Mutation mutation : mutations) {
      if (mutation instanceof Put) {
        mmrBuilder.addMutationRequest(ProtobufUtil.toMutation(.mutation));
      } else if (mutation instanceof Delete) {
        mmrBuilder.addMutationRequest(ProtobufUtil.toMutation(.mutation));
      } else {
        throw new DoNotRetryIOException("multi in MetaEditor doesn't support "
            + mutation.getClass().getName());
      }
    }
        MultiRowMutationService.newBlockingStub(channel);
    try {
      service.mutateRows(nullmmrBuilder.build());
    } catch (ServiceException ex) {
      ProtobufUtil.toIOException(ex);
    }
  }


  
Updates the location of the specified hbase:meta region in ROOT to be the specified server hostname and startcode.

Uses passed catalog tracker to get a connection to the server hosting ROOT and makes edits to that region.

Parameters:
catalogTracker catalog tracker
regionInfo region to update location of
sn Server name
openSeqNum the latest sequence number obtained when the region was open
Throws:
java.io.IOException
java.net.ConnectException Usually because the regionserver carrying hbase:meta is down.
java.lang.NullPointerException Because no -ROOT- server connection
  public static void updateMetaLocation(CatalogTracker catalogTracker,
      HRegionInfo regionInfoServerName snlong openSeqNum)
    updateLocation(catalogTrackerregionInfosnopenSeqNum);
  }

  
Updates the location of the specified region in hbase:meta to be the specified server hostname and startcode.

Uses passed catalog tracker to get a connection to the server hosting hbase:meta and makes edits to that region.

Parameters:
catalogTracker catalog tracker
regionInfo region to update location of
sn Server name
Throws:
java.io.IOException
  public static void updateRegionLocation(CatalogTracker catalogTracker,
      HRegionInfo regionInfoServerName snlong updateSeqNum)
  throws IOException {
    updateLocation(catalogTrackerregionInfosnupdateSeqNum);
  }

  
Updates the location of the specified region to be the specified server.

Connects to the specified server which should be hosting the specified catalog region name to perform the edit.

Parameters:
catalogTracker
regionInfo region to update location of
sn Server name
openSeqNum the latest sequence number obtained when the region was open
Throws:
java.io.IOException In particular could throw java.net.ConnectException if the server is down on other end.
  private static void updateLocation(final CatalogTracker catalogTracker,
      HRegionInfo regionInfoServerName snlong openSeqNum)
  throws IOException {
    Put put = new Put(regionInfo.getRegionName());
    addLocation(putsnopenSeqNum);
    putToCatalogTable(catalogTrackerput);
    .info("Updated row " + regionInfo.getRegionNameAsString() +
      " with server=" + sn);
  }

  
Deletes the specified region from META.

Parameters:
catalogTracker
regionInfo region to be deleted from META
Throws:
java.io.IOException
  public static void deleteRegion(CatalogTracker catalogTracker,
      HRegionInfo regionInfo)
  throws IOException {
    Delete delete = new Delete(regionInfo.getRegionName());
    deleteFromMetaTable(catalogTrackerdelete);
    .info("Deleted " + regionInfo.getRegionNameAsString());
  }

  
Deletes the specified regions from META.

Parameters:
catalogTracker
regionsInfo list of regions to be deleted from META
Throws:
java.io.IOException
  public static void deleteRegions(CatalogTracker catalogTracker,
      List<HRegionInforegionsInfothrows IOException {
    List<Deletedeletes = new ArrayList<Delete>(regionsInfo.size());
    for (HRegionInfo hriregionsInfo) {
      deletes.add(new Delete(hri.getRegionName()));
    }
    deleteFromMetaTable(catalogTrackerdeletes);
    .info("Deleted " + regionsInfo);
  }

  
Adds and Removes the specified regions from hbase:meta

Parameters:
catalogTracker
regionsToRemove list of regions to be deleted from META
regionsToAdd list of regions to be added to META
Throws:
java.io.IOException
  public static void mutateRegions(CatalogTracker catalogTracker,
      final List<HRegionInforegionsToRemovefinal List<HRegionInforegionsToAdd)
      throws IOException {
    List<Mutationmutation = new ArrayList<Mutation>();
    if (regionsToRemove != null) {
      for (HRegionInfo hriregionsToRemove) {
        mutation.add(new Delete(hri.getRegionName()));
      }
    }
    if (regionsToAdd != null) {
      for (HRegionInfo hriregionsToAdd) {
        mutation.add(makePutFromRegionInfo(hri));
      }
    }
    mutateMetaTable(catalogTrackermutation);
    if (regionsToRemove != null && regionsToRemove.size() > 0) {
      .debug("Deleted " + regionsToRemove);
    }
    if (regionsToAdd != null && regionsToAdd.size() > 0) {
      .debug("Added " + regionsToAdd);
    }
  }

  
Overwrites the specified regions from hbase:meta

Parameters:
catalogTracker
regionInfos list of regions to be added to META
Throws:
java.io.IOException
  public static void overwriteRegions(CatalogTracker catalogTracker,
      List<HRegionInforegionInfosthrows IOException {
    deleteRegions(catalogTrackerregionInfos);
    // Why sleep? This is the easiest way to ensure that the previous deletes does not
    // eclipse the following puts, that might happen in the same ts from the server.
    // See HBASE-9906, and HBASE-9879. Once either HBASE-9879, HBASE-8770 is fixed,
    // or HBASE-9905 is fixed and meta uses seqIds, we do not need the sleep.
    Threads.sleep(20);
    addRegionsToMeta(catalogTrackerregionInfos);
    .info("Overwritten " + regionInfos);
  }

  
Deletes merge qualifiers for the specified merged region.

Parameters:
catalogTracker
mergedRegion
Throws:
java.io.IOException
  public static void deleteMergeQualifiers(CatalogTracker catalogTracker,
      final HRegionInfo mergedRegionthrows IOException {
    Delete delete = new Delete(mergedRegion.getRegionName());
    deleteFromMetaTable(catalogTrackerdelete);
    .info("Deleted references in merged region "
        + mergedRegion.getRegionNameAsString() + ", qualifier="
        + Bytes.toStringBinary(.) + " and qualifier="
        + Bytes.toStringBinary(.));
  }
  private static Put addRegionInfo(final Put pfinal HRegionInfo hri)
  throws IOException {
        hri.toByteArray());
    return p;
  }
  private static Put addLocation(final Put pfinal ServerName snlong openSeqNum) {
    // using regionserver's local time as the timestamp of Put.
    // See: HBASE-11536
    long now = EnvironmentEdgeManager.currentTimeMillis();
      Bytes.toBytes(sn.getHostAndPort()));
      Bytes.toBytes(sn.getStartcode()));
        Bytes.toBytes(openSeqNum));
    return p;
  }
New to GrepCode? Check out our FAQ X