Search in sources :

Example 6 with Lock

use of java.util.concurrent.locks.Lock in project hbase by apache.

the class AssignmentManager method cleanOutCrashedServerReferences.

/**
   * Clean out crashed server removing any assignments.
   * @param sn Server that went down.
   * @return list of regions in transition on this server
   */
public List<HRegionInfo> cleanOutCrashedServerReferences(final ServerName sn) {
    // Clean out any existing assignment plans for this server
    synchronized (this.regionPlans) {
        for (Iterator<Map.Entry<String, RegionPlan>> i = this.regionPlans.entrySet().iterator(); i.hasNext(); ) {
            Map.Entry<String, RegionPlan> e = i.next();
            ServerName otherSn = e.getValue().getDestination();
            // The name will be null if the region is planned for a random assign.
            if (otherSn != null && otherSn.equals(sn)) {
                // Use iterator's remove else we'll get CME
                i.remove();
            }
        }
    }
    List<HRegionInfo> rits = regionStates.serverOffline(sn);
    for (Iterator<HRegionInfo> it = rits.iterator(); it.hasNext(); ) {
        HRegionInfo hri = it.next();
        String encodedName = hri.getEncodedName();
        // We need a lock on the region as we could update it
        Lock lock = locker.acquireLock(encodedName);
        try {
            RegionState regionState = regionStates.getRegionTransitionState(encodedName);
            if (regionState == null || (regionState.getServerName() != null && !regionState.isOnServer(sn)) || !RegionStates.isOneOfStates(regionState, State.PENDING_OPEN, State.OPENING, State.FAILED_OPEN, State.FAILED_CLOSE, State.OFFLINE)) {
                LOG.info("Skip " + regionState + " since it is not opening/failed_close" + " on the dead server any more: " + sn);
                it.remove();
            } else {
                if (tableStateManager.isTableState(hri.getTable(), TableState.State.DISABLED, TableState.State.DISABLING)) {
                    regionStates.regionOffline(hri);
                    it.remove();
                    continue;
                }
                // Mark the region offline and assign it again by SSH
                regionStates.updateRegionState(hri, State.OFFLINE);
            }
        } finally {
            lock.unlock();
        }
    }
    return rits;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ServerName(org.apache.hadoop.hbase.ServerName) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NavigableMap(java.util.NavigableMap) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 7 with Lock

use of java.util.concurrent.locks.Lock in project hbase by apache.

the class AssignmentManager method onRegionReadyToMerge.

private String onRegionReadyToMerge(final RegionState current, final HRegionInfo hri, final ServerName serverName, final RegionStateTransition transition) {
    // If the region is in merge_new state, it could be an RPC retry.
    if (current != null && !current.isMergingNewOnServer(serverName)) {
        return "Merging daughter region already exists, p=" + current;
    }
    if (!((HMaster) server).getSplitOrMergeTracker().isSplitOrMergeEnabled(MasterSwitchType.MERGE)) {
        return "merge switch is off!";
    }
    // Just return in case of retrying
    if (current != null) {
        return null;
    }
    final HRegionInfo a = HRegionInfo.convert(transition.getRegionInfo(1));
    final HRegionInfo b = HRegionInfo.convert(transition.getRegionInfo(2));
    Set<String> encodedNames = new HashSet<>(2);
    encodedNames.add(a.getEncodedName());
    encodedNames.add(b.getEncodedName());
    Map<String, Lock> locks = locker.acquireLocks(encodedNames);
    try {
        RegionState rs_a = regionStates.getRegionState(a);
        RegionState rs_b = regionStates.getRegionState(b);
        if (rs_a == null || !rs_a.isOpenedOnServer(serverName) || rs_b == null || !rs_b.isOpenedOnServer(serverName)) {
            return "Some daughter is not in a state to merge on " + serverName + ", a=" + rs_a + ", b=" + rs_b;
        }
        regionStates.updateRegionState(a, State.MERGING);
        regionStates.updateRegionState(b, State.MERGING);
        regionStates.createRegionState(hri, State.MERGING_NEW, serverName, null);
        return null;
    } finally {
        for (Lock lock : locks.values()) {
            lock.unlock();
        }
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HashSet(java.util.HashSet) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock)

Example 8 with Lock

use of java.util.concurrent.locks.Lock in project hbase by apache.

the class ServerCrashProcedure method calcRegionsToAssign.

/**
   * Figure out what we need to assign. Should be idempotent.
   * @param env
   * @return List of calculated regions to assign; may be empty or null.
   * @throws IOException
   */
private List<HRegionInfo> calcRegionsToAssign(final MasterProcedureEnv env) throws IOException {
    AssignmentManager am = env.getMasterServices().getAssignmentManager();
    List<HRegionInfo> regionsToAssignAggregator = new ArrayList<>();
    int replicaCount = env.getMasterConfiguration().getInt(HConstants.META_REPLICAS_NUM, HConstants.DEFAULT_META_REPLICA_NUM);
    for (int i = 1; i < replicaCount; i++) {
        HRegionInfo metaHri = RegionReplicaUtil.getRegionInfoForReplica(HRegionInfo.FIRST_META_REGIONINFO, i);
        if (am.isCarryingMetaReplica(this.serverName, metaHri)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Reassigning meta replica" + metaHri + " that was on " + this.serverName);
            }
            regionsToAssignAggregator.add(metaHri);
        }
    }
    // Clean out anything in regions in transition.
    List<HRegionInfo> regionsInTransition = am.cleanOutCrashedServerReferences(serverName);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Reassigning " + size(this.regionsOnCrashedServer) + " region(s) that " + (serverName == null ? "null" : serverName) + " was carrying (and " + regionsInTransition.size() + " regions(s) that were opening on this server)");
    }
    regionsToAssignAggregator.addAll(regionsInTransition);
    // Iterate regions that were on this server and figure which of these we need to reassign
    if (this.regionsOnCrashedServer != null && !this.regionsOnCrashedServer.isEmpty()) {
        RegionStates regionStates = am.getRegionStates();
        for (HRegionInfo hri : this.regionsOnCrashedServer) {
            if (regionsInTransition.contains(hri))
                continue;
            String encodedName = hri.getEncodedName();
            Lock lock = am.acquireRegionLock(encodedName);
            try {
                RegionState rit = regionStates.getRegionTransitionState(hri);
                if (processDeadRegion(hri, am)) {
                    ServerName addressFromAM = regionStates.getRegionServerOfRegion(hri);
                    if (addressFromAM != null && !addressFromAM.equals(this.serverName)) {
                        // If this region is in transition on the dead server, it must be
                        // opening or pending_open, which should have been covered by
                        // AM#cleanOutCrashedServerReferences
                        LOG.info("Skip assigning " + hri.getRegionNameAsString() + " because opened on " + addressFromAM.getServerName());
                        continue;
                    }
                    if (rit != null) {
                        if (rit.getServerName() != null && !rit.isOnServer(this.serverName)) {
                            // Skip regions that are in transition on other server
                            LOG.info("Skip assigning region in transition on other server" + rit);
                            continue;
                        }
                        LOG.info("Reassigning region " + rit + " and clearing zknode if exists");
                        regionStates.updateRegionState(hri, RegionState.State.OFFLINE);
                    } else if (regionStates.isRegionInState(hri, RegionState.State.SPLITTING_NEW, RegionState.State.MERGING_NEW)) {
                        regionStates.updateRegionState(hri, RegionState.State.OFFLINE);
                    }
                    regionsToAssignAggregator.add(hri);
                // TODO: The below else if is different in branch-1 from master branch.
                } else if (rit != null) {
                    if ((rit.isClosing() || rit.isFailedClose() || rit.isOffline()) && am.getTableStateManager().isTableState(hri.getTable(), TableState.State.DISABLED, TableState.State.DISABLING) || am.getReplicasToClose().contains(hri)) {
                        // If the table was partially disabled and the RS went down, we should clear the
                        // RIT and remove the node for the region.
                        // The rit that we use may be stale in case the table was in DISABLING state
                        // but though we did assign we will not be clearing the znode in CLOSING state.
                        // Doing this will have no harm. See HBASE-5927
                        regionStates.updateRegionState(hri, RegionState.State.OFFLINE);
                        am.offlineDisabledRegion(hri);
                    } else {
                        LOG.warn("THIS SHOULD NOT HAPPEN: unexpected region in transition " + rit + " not to be assigned by SSH of server " + serverName);
                    }
                }
            } finally {
                lock.unlock();
            }
        }
    }
    return regionsToAssignAggregator;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) RegionState(org.apache.hadoop.hbase.master.RegionState) RegionStates(org.apache.hadoop.hbase.master.RegionStates) ServerName(org.apache.hadoop.hbase.ServerName) AssignmentManager(org.apache.hadoop.hbase.master.AssignmentManager) ArrayList(java.util.ArrayList) Lock(java.util.concurrent.locks.Lock)

Example 9 with Lock

use of java.util.concurrent.locks.Lock in project hbase by apache.

the class CoprocessorClassLoader method getClassLoader.

/**
   * Get a CoprocessorClassLoader for a coprocessor jar path from cache.
   * If not in cache, create one.
   *
   * @param path the path to the coprocessor jar file to load classes from
   * @param parent the parent class loader for exempted classes
   * @param pathPrefix a prefix used in temp path name to store the jar file locally
   * @param conf the configuration used to create the class loader, if needed
   * @return a CoprocessorClassLoader for the coprocessor jar path
   * @throws IOException
   */
public static CoprocessorClassLoader getClassLoader(final Path path, final ClassLoader parent, final String pathPrefix, final Configuration conf) throws IOException {
    CoprocessorClassLoader cl = getIfCached(path);
    String pathStr = path.toString();
    if (cl != null) {
        LOG.debug("Found classloader " + cl + " for " + pathStr);
        return cl;
    }
    if (path.getFileSystem(conf).isFile(path) && !pathStr.endsWith(".jar")) {
        throw new IOException(pathStr + ": not a jar file?");
    }
    Lock lock = locker.acquireLock(pathStr);
    try {
        cl = getIfCached(path);
        if (cl != null) {
            LOG.debug("Found classloader " + cl + " for " + pathStr);
            return cl;
        }
        cl = AccessController.doPrivileged(new PrivilegedAction<CoprocessorClassLoader>() {

            @Override
            public CoprocessorClassLoader run() {
                return new CoprocessorClassLoader(parent);
            }
        });
        cl.init(path, pathPrefix, conf);
        // Cache class loader as a weak value, will be GC'ed when no reference left
        CoprocessorClassLoader prev = classLoadersCache.putIfAbsent(path, cl);
        if (prev != null) {
            // Lost update race, use already added class loader
            LOG.warn("THIS SHOULD NOT HAPPEN, a class loader" + " is already cached for " + pathStr);
            cl = prev;
        }
        return cl;
    } finally {
        lock.unlock();
    }
}
Also used : PrivilegedAction(java.security.PrivilegedAction) IOException(java.io.IOException) Lock(java.util.concurrent.locks.Lock)

Example 10 with Lock

use of java.util.concurrent.locks.Lock in project hive by apache.

the class HiveMetaStore method main.

/**
   * @param args
   */
public static void main(String[] args) throws Throwable {
    HiveConf.setLoadMetastoreConfig(true);
    final HiveConf conf = new HiveConf(HMSHandler.class);
    HiveMetastoreCli cli = new HiveMetastoreCli(conf);
    cli.parse(args);
    final boolean isCliVerbose = cli.isVerbose();
    // NOTE: It is critical to do this prior to initializing log4j, otherwise
    // any log specific settings via hiveconf will be ignored
    Properties hiveconf = cli.addHiveconfToSystemProperties();
    // use Hive's default log4j configuration
    if (System.getProperty("log4j.configurationFile") == null) {
        // before any of the other core hive classes are loaded
        try {
            LogUtils.initHiveLog4j();
        } catch (LogInitializationException e) {
            HMSHandler.LOG.warn(e.getMessage());
        }
    }
    HiveStringUtils.startupShutdownMessage(HiveMetaStore.class, args, LOG);
    try {
        String msg = "Starting hive metastore on port " + cli.port;
        HMSHandler.LOG.info(msg);
        if (cli.isVerbose()) {
            System.err.println(msg);
        }
        // set all properties specified on the command line
        for (Map.Entry<Object, Object> item : hiveconf.entrySet()) {
            conf.set((String) item.getKey(), (String) item.getValue());
        }
        // Add shutdown hook.
        ShutdownHookManager.addShutdownHook(new Runnable() {

            @Override
            public void run() {
                String shutdownMsg = "Shutting down hive metastore.";
                HMSHandler.LOG.info(shutdownMsg);
                if (isCliVerbose) {
                    System.err.println(shutdownMsg);
                }
                if (conf.getBoolVar(ConfVars.METASTORE_METRICS)) {
                    try {
                        MetricsFactory.close();
                    } catch (Exception e) {
                        LOG.error("error in Metrics deinit: " + e.getClass().getName() + " " + e.getMessage(), e);
                    }
                }
            }
        });
        //Start Metrics for Standalone (Remote) Mode
        if (conf.getBoolVar(ConfVars.METASTORE_METRICS)) {
            try {
                MetricsFactory.init(conf);
            } catch (Exception e) {
                // log exception, but ignore inability to start
                LOG.error("error in Metrics init: " + e.getClass().getName() + " " + e.getMessage(), e);
            }
        }
        Lock startLock = new ReentrantLock();
        Condition startCondition = startLock.newCondition();
        AtomicBoolean startedServing = new AtomicBoolean();
        startMetaStoreThreads(conf, startLock, startCondition, startedServing);
        startMetaStore(cli.getPort(), ShimLoader.getHadoopThriftAuthBridge(), conf, startLock, startCondition, startedServing);
    } catch (Throwable t) {
        // Catch the exception, log it and rethrow it.
        HMSHandler.LOG.error("Metastore Thrift Server threw an exception...", t);
        throw t;
    }
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) Properties(java.util.Properties) JDOException(javax.jdo.JDOException) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) TException(org.apache.thrift.TException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) Lock(java.util.concurrent.locks.Lock) ReentrantLock(java.util.concurrent.locks.ReentrantLock) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LogInitializationException(org.apache.hadoop.hive.common.LogUtils.LogInitializationException) HiveConf(org.apache.hadoop.hive.conf.HiveConf) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap)

Aggregations

Lock (java.util.concurrent.locks.Lock)717 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)171 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)144 ReentrantLock (java.util.concurrent.locks.ReentrantLock)96 Test (org.junit.Test)57 ArrayList (java.util.ArrayList)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)36 IOException (java.io.IOException)33 CountDownLatch (java.util.concurrent.CountDownLatch)30 HashMap (java.util.HashMap)22 Triple (org.apache.clerezza.commons.rdf.Triple)21 Map (java.util.Map)20 Ignite (org.apache.ignite.Ignite)17 Condition (java.util.concurrent.locks.Condition)15 OSBTreeBonsaiLocalException (com.orientechnologies.orient.core.exception.OSBTreeBonsaiLocalException)13 OAtomicOperation (com.orientechnologies.orient.core.storage.impl.local.paginated.atomicoperations.OAtomicOperation)13 HashSet (java.util.HashSet)13 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)13 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 OCacheEntry (com.orientechnologies.orient.core.storage.cache.OCacheEntry)11