Search in sources :

Example 1 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class TakeSnapshotHandler method process.

/**
   * Execute the core common portions of taking a snapshot. The {@link #snapshotRegions(List)}
   * call should get implemented for each snapshot flavor.
   */
@Override
@edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "REC_CATCH_EXCEPTION", justification = "Intentional")
public void process() {
    String msg = "Running " + snapshot.getType() + " table snapshot " + snapshot.getName() + " " + eventType + " on table " + snapshotTable;
    LOG.info(msg);
    ReentrantLock lock = snapshotManager.getLocks().acquireLock(snapshot.getName());
    status.setStatus(msg);
    try {
        // If regions move after this meta scan, the region specific snapshot should fail, triggering
        // an external exception that gets captured here.
        // write down the snapshot info in the working directory
        SnapshotDescriptionUtils.writeSnapshotInfo(snapshot, workingDir, fs);
        snapshotManifest.addTableDescriptor(this.htd);
        monitor.rethrowException();
        List<Pair<HRegionInfo, ServerName>> regionsAndLocations;
        if (TableName.META_TABLE_NAME.equals(snapshotTable)) {
            regionsAndLocations = new MetaTableLocator().getMetaRegionsAndLocations(server.getZooKeeper());
        } else {
            regionsAndLocations = MetaTableAccessor.getTableRegionsAndLocations(server.getConnection(), snapshotTable, false);
        }
        // run the snapshot
        snapshotRegions(regionsAndLocations);
        monitor.rethrowException();
        // extract each pair to separate lists
        Set<String> serverNames = new HashSet<>();
        for (Pair<HRegionInfo, ServerName> p : regionsAndLocations) {
            if (p != null && p.getFirst() != null && p.getSecond() != null) {
                HRegionInfo hri = p.getFirst();
                if (hri.isOffline() && (hri.isSplit() || hri.isSplitParent()))
                    continue;
                serverNames.add(p.getSecond().toString());
            }
        }
        // flush the in-memory state, and write the single manifest
        status.setStatus("Consolidate snapshot: " + snapshot.getName());
        snapshotManifest.consolidate();
        // verify the snapshot is valid
        status.setStatus("Verifying snapshot: " + snapshot.getName());
        verifier.verifySnapshot(this.workingDir, serverNames);
        // complete the snapshot, atomically moving from tmp to .snapshot dir.
        completeSnapshot(this.snapshotDir, this.workingDir, this.fs);
        msg = "Snapshot " + snapshot.getName() + " of table " + snapshotTable + " completed";
        status.markComplete(msg);
        LOG.info(msg);
        metricsSnapshot.addSnapshot(status.getCompletionTimestamp() - status.getStartTime());
    } catch (Exception e) {
        // FindBugs: REC_CATCH_EXCEPTION
        status.abort("Failed to complete snapshot " + snapshot.getName() + " on table " + snapshotTable + " because " + e.getMessage());
        String reason = "Failed taking snapshot " + ClientSnapshotDescriptionUtils.toString(snapshot) + " due to exception:" + e.getMessage();
        LOG.error(reason, e);
        ForeignException ee = new ForeignException(reason, e);
        monitor.receive(ee);
        // need to mark this completed to close off and allow cleanup to happen.
        cancel(reason);
    } finally {
        LOG.debug("Launching cleanup of working dir:" + workingDir);
        try {
            // it.
            if (fs.exists(workingDir) && !this.fs.delete(workingDir, true)) {
                LOG.error("Couldn't delete snapshot working directory:" + workingDir);
            }
        } catch (IOException e) {
            LOG.error("Couldn't delete snapshot working directory:" + workingDir);
        }
        lock.unlock();
        tableLock.release();
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) IOException(java.io.IOException) SnapshotCreationException(org.apache.hadoop.hbase.snapshot.SnapshotCreationException) ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) KeeperException(org.apache.zookeeper.KeeperException) CancellationException(java.util.concurrent.CancellationException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) ServerName(org.apache.hadoop.hbase.ServerName) ForeignException(org.apache.hadoop.hbase.errorhandling.ForeignException) Pair(org.apache.hadoop.hbase.util.Pair) HashSet(java.util.HashSet)

Example 2 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class EnableTableProcedure method markRegionsOnline.

/**
   * Mark offline regions of the table online
   * @param env MasterProcedureEnv
   * @param tableName the target table
   * @return whether the operation is fully completed or being interrupted.
   * @throws IOException
   */
private static boolean markRegionsOnline(final MasterProcedureEnv env, final TableName tableName) throws IOException {
    final AssignmentManager assignmentManager = env.getMasterServices().getAssignmentManager();
    final MasterServices masterServices = env.getMasterServices();
    final ServerManager serverManager = masterServices.getServerManager();
    boolean done = false;
    // Get the regions of this table. We're done when all listed
    // tables are onlined.
    List<Pair<HRegionInfo, ServerName>> tableRegionsAndLocations;
    if (TableName.META_TABLE_NAME.equals(tableName)) {
        tableRegionsAndLocations = new MetaTableLocator().getMetaRegionsAndLocations(masterServices.getZooKeeper());
    } else {
        tableRegionsAndLocations = MetaTableAccessor.getTableRegionsAndLocations(masterServices.getConnection(), tableName);
    }
    int countOfRegionsInTable = tableRegionsAndLocations.size();
    Map<HRegionInfo, ServerName> regionsToAssign = regionsToAssignWithServerName(env, tableRegionsAndLocations);
    // need to potentially create some regions for the replicas
    List<HRegionInfo> unrecordedReplicas = AssignmentManager.replicaRegionsNotRecordedInMeta(new HashSet<>(regionsToAssign.keySet()), masterServices);
    Map<ServerName, List<HRegionInfo>> srvToUnassignedRegs = assignmentManager.getBalancer().roundRobinAssignment(unrecordedReplicas, serverManager.getOnlineServersList());
    if (srvToUnassignedRegs != null) {
        for (Map.Entry<ServerName, List<HRegionInfo>> entry : srvToUnassignedRegs.entrySet()) {
            for (HRegionInfo h : entry.getValue()) {
                regionsToAssign.put(h, entry.getKey());
            }
        }
    }
    int offlineRegionsCount = regionsToAssign.size();
    LOG.info("Table '" + tableName + "' has " + countOfRegionsInTable + " regions, of which " + offlineRegionsCount + " are offline.");
    if (offlineRegionsCount == 0) {
        return true;
    }
    List<ServerName> onlineServers = serverManager.createDestinationServersList();
    Map<ServerName, List<HRegionInfo>> bulkPlan = env.getMasterServices().getAssignmentManager().getBalancer().retainAssignment(regionsToAssign, onlineServers);
    if (bulkPlan != null) {
        LOG.info("Bulk assigning " + offlineRegionsCount + " region(s) across " + bulkPlan.size() + " server(s), retainAssignment=true");
        BulkAssigner ba = new GeneralBulkAssigner(masterServices, bulkPlan, assignmentManager, true);
        try {
            if (ba.bulkAssign()) {
                done = true;
            }
        } catch (InterruptedException e) {
            LOG.warn("Enable operation was interrupted when enabling table '" + tableName + "'");
            // Preserve the interrupt.
            Thread.currentThread().interrupt();
        }
    } else {
        LOG.info("Balancer was unable to find suitable servers for table " + tableName + ", leaving unassigned");
    }
    return done;
}
Also used : ServerManager(org.apache.hadoop.hbase.master.ServerManager) GeneralBulkAssigner(org.apache.hadoop.hbase.master.GeneralBulkAssigner) AssignmentManager(org.apache.hadoop.hbase.master.AssignmentManager) MasterServices(org.apache.hadoop.hbase.master.MasterServices) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) ServerName(org.apache.hadoop.hbase.ServerName) BulkAssigner(org.apache.hadoop.hbase.master.BulkAssigner) GeneralBulkAssigner(org.apache.hadoop.hbase.master.GeneralBulkAssigner) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.apache.hadoop.hbase.util.Pair)

Example 3 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class TestMetaTableLocator method testVerifyMetaRegionLocationFails.

/**
   * Test get of meta region fails properly if nothing to connect to.
   * @throws IOException
   * @throws InterruptedException
   * @throws KeeperException
   * @throws ServiceException
   */
@Test
public void testVerifyMetaRegionLocationFails() throws IOException, InterruptedException, KeeperException, ServiceException {
    ClusterConnection connection = Mockito.mock(ClusterConnection.class);
    ServiceException connectException = new ServiceException(new ConnectException("Connection refused"));
    final AdminProtos.AdminService.BlockingInterface implementation = Mockito.mock(AdminProtos.AdminService.BlockingInterface.class);
    Mockito.when(implementation.getRegionInfo((RpcController) Mockito.any(), (GetRegionInfoRequest) Mockito.any())).thenThrow(connectException);
    Mockito.when(connection.getAdmin(Mockito.any(ServerName.class))).thenReturn(implementation);
    RpcControllerFactory controllerFactory = Mockito.mock(RpcControllerFactory.class);
    Mockito.when(controllerFactory.newController()).thenReturn(Mockito.mock(HBaseRpcController.class));
    Mockito.when(connection.getRpcControllerFactory()).thenReturn(controllerFactory);
    ServerName sn = ServerName.valueOf("example.com", 1234, System.currentTimeMillis());
    MetaTableLocator.setMetaLocation(this.watcher, sn, RegionState.State.OPENING);
    assertFalse(new MetaTableLocator().verifyMetaRegionLocation(connection, watcher, 100));
    MetaTableLocator.setMetaLocation(this.watcher, sn, RegionState.State.OPEN);
    assertFalse(new MetaTableLocator().verifyMetaRegionLocation(connection, watcher, 100));
}
Also used : ClusterConnection(org.apache.hadoop.hbase.client.ClusterConnection) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) ServiceException(org.apache.hadoop.hbase.shaded.com.google.protobuf.ServiceException) RpcControllerFactory(org.apache.hadoop.hbase.ipc.RpcControllerFactory) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 4 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class TestMetaTableLocator method testNoTimeoutWaitForMeta.

/**
   * Test waiting on meat w/ no timeout specified.
   * @throws IOException
   * @throws InterruptedException
   * @throws KeeperException
   */
@Test
public void testNoTimeoutWaitForMeta() throws IOException, InterruptedException, KeeperException {
    final MetaTableLocator mtl = new MetaTableLocator();
    ServerName hsa = mtl.getMetaRegionLocation(watcher);
    assertNull(hsa);
    // Now test waiting on meta location getting set.
    Thread t = new WaitOnMetaThread();
    startWaitAliveThenWaitItLives(t, 1);
    // Set a meta location.
    MetaTableLocator.setMetaLocation(this.watcher, SN, RegionState.State.OPEN);
    hsa = SN;
    // Join the thread... should exit shortly.
    t.join();
    // Now meta is available.
    assertTrue(mtl.getMetaRegionLocation(watcher).equals(hsa));
}
Also used : MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) Test(org.junit.Test)

Example 5 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class TestMetaTableLocator method testInterruptWaitOnMeta.

/**
   * Test interruptable while blocking wait on meta.
   * @throws IOException
   * @throws ServiceException
   * @throws InterruptedException
   */
@Test
public void testInterruptWaitOnMeta() throws IOException, InterruptedException, ServiceException {
    final ClientProtos.ClientService.BlockingInterface client = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class);
    Mockito.when(client.get((RpcController) Mockito.any(), (GetRequest) Mockito.any())).thenReturn(GetResponse.newBuilder().build());
    final MetaTableLocator mtl = new MetaTableLocator();
    ServerName meta = new MetaTableLocator().getMetaRegionLocation(this.watcher);
    assertNull(meta);
    Thread t = new Thread() {

        @Override
        public void run() {
            try {
                mtl.waitMetaRegionLocation(watcher);
            } catch (InterruptedException e) {
                throw new RuntimeException("Interrupted", e);
            }
        }
    };
    t.start();
    while (!t.isAlive()) Threads.sleep(1);
    Threads.sleep(1);
    assertTrue(t.isAlive());
    mtl.stop();
    // Join the thread... should exit shortly.
    t.join();
}
Also used : MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) Test(org.junit.Test)

Aggregations

MetaTableLocator (org.apache.hadoop.hbase.zookeeper.MetaTableLocator)21 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)10 ServerName (org.apache.hadoop.hbase.ServerName)8 Pair (org.apache.hadoop.hbase.util.Pair)7 ZooKeeperWatcher (org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher)7 Test (org.junit.Test)6 IOException (java.io.IOException)3 ClusterConnection (org.apache.hadoop.hbase.client.ClusterConnection)3 NameStringPair (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)3 HashSet (java.util.HashSet)2 NotServingRegionException (org.apache.hadoop.hbase.NotServingRegionException)2 ForeignException (org.apache.hadoop.hbase.errorhandling.ForeignException)2 HBaseRpcController (org.apache.hadoop.hbase.ipc.HBaseRpcController)2 ServiceException (org.apache.hadoop.hbase.shaded.com.google.protobuf.ServiceException)2 FileNotFoundException (java.io.FileNotFoundException)1 ConnectException (java.net.ConnectException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1