Example usage for java.util.concurrent.locks ReentrantReadWriteLock readLock

List of usage examples for java.util.concurrent.locks ReentrantReadWriteLock readLock

Introduction

In this page you can find the example usage for java.util.concurrent.locks ReentrantReadWriteLock readLock.

Prototype

public ReentrantReadWriteLock.ReadLock readLock() 

Source Link

Usage

From source file:org.apache.tez.dag.app.rm.container.AMContainerImpl.java

public AMContainerImpl(Container container, ContainerHeartbeatHandler chh, TaskAttemptListener tal,
        AppContext appContext) {/*from w  w w. ja  v  a 2s  .  c o m*/
    ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    this.readLock = rwLock.readLock();
    this.writeLock = rwLock.writeLock();
    this.container = container;
    this.containerId = container.getId();
    this.eventHandler = appContext.getEventHandler();
    this.appContext = appContext;
    this.containerHeartbeatHandler = chh;
    this.taskAttemptListener = tal;
    this.failedAssignments = new LinkedList<TezTaskAttemptID>();

    this.noAllocationContainerTask = WAIT_TASK;
    this.stateMachine = stateMachineFactory.make(this);
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.recovery.RMStateStore.java

public RMStateStore() {
    super(RMStateStore.class.getName());
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    this.readLock = lock.readLock();
    this.writeLock = lock.writeLock();
    stateMachine = stateMachineFactory.make(this);
}

From source file:org.apache.hadoop.yarn.nodelabels.CommonNodeLabelsManager.java

public CommonNodeLabelsManager() {
    super(CommonNodeLabelsManager.class.getName());
    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    readLock = lock.readLock();
    writeLock = lock.writeLock();//  w ww  . j a v a  2  s . c  o m
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.rmnode.RMNodeImpl.java

public RMNodeImpl(NodeId nodeId, RMContext context, String hostName, int cmPort, int httpPort, Node node,
        Resource capability, String nodeManagerVersion) {
    this.nodeId = nodeId;
    this.context = context;
    this.hostName = hostName;
    this.commandPort = cmPort;
    this.httpPort = httpPort;
    this.totalCapability = capability;
    this.nodeAddress = hostName + ":" + cmPort;
    this.httpAddress = hostName + ":" + httpPort;
    this.node = node;
    this.healthReport = "Healthy";
    this.lastHealthReportTime = System.currentTimeMillis();
    this.nodeManagerVersion = nodeManagerVersion;
    this.timeStamp = 0;

    this.latestNodeHeartBeatResponse.setResponseId(0);

    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    this.readLock = lock.readLock();
    this.writeLock = lock.writeLock();

    this.stateMachine = stateMachineFactory.make(this);

    this.nodeUpdateQueue = new ConcurrentLinkedQueue<UpdatedContainerInfo>();

    this.containerAllocationExpirer = context.getContainerAllocationExpirer();
}

From source file:com.edgenius.wiki.rss.RSSServiceImpl.java

/**
 * @param spaceUid//from www. ja v  a 2s. c  o  m
 * @param spaceUname
 * @param viewer
 * @param skipSecurityCheck 
 * @return
 * @throws FeedException
 */
private Document getFeedDom(Integer spaceUid, String spaceUname, User viewer, boolean skipSecurityCheck)
        throws FeedException {
    ReentrantReadWriteLock lock = null;
    Document dom;
    try {

        File feedFile = new File(FileUtil.getFullPath(rssRoot.getFile().getAbsolutePath(), spaceUid + ".xml"));
        if (!feedFile.exists()) {
            createFeed(spaceUname);
        }

        //do read lock! must after createFeed possibility, otherwise, deadlock 
        lock = lockMap.get(spaceUid.toString());
        if (lock == null) {
            lock = new ReentrantReadWriteLock();
            lockMap.put(spaceUid.toString(), lock);
        }
        lock.readLock().lock();
        //!!! DON'T USE JDOM - although I test DOM, JDOM, DOM4J: JDOM is fastest. And DOM and DOM4J almost 2 time slow. But
        //!!! JDOM is not thread safe, an infinite looping can happen while this method reading different XML source, 
        //in different thread, and SAXBuild are different instance! The problem is mostly caused by NameSpace.getNamespace(), 
        //which using static HashMap and cause HashMap dead lock!!!
        DocumentBuilder builder = xmlBuilderPool.poll();
        if (builder == null) {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setCoalescing(true);
            factory.setIgnoringComments(true);
            builder = factory.newDocumentBuilder();
        }
        dom = builder.parse(feedFile);
        xmlBuilderPool.add(builder);

    } catch (Exception e) {
        log.error("Unable get feed " + spaceUname + " with excpetion ", e);
        throw new FeedException("Unable get feed " + spaceUname + " with excpetion " + e);
    } finally {
        if (lock != null)
            lock.readLock().unlock();
    }
    if (dom == null) {
        log.error("Unable get feed " + spaceUname);
        throw new FeedException("Unable get feed " + spaceUname);
    }

    //~~~~~~~~~~~~ Security filter
    if (!skipSecurityCheck) {
        //need filter out the page that viewer has not permission to read.  
        List<Node> forbidPageUuidList = new ArrayList<Node>();
        String pageUuid;
        Node ele;
        NodeList list = dom.getElementsByTagName(PageRSSModule.NS_PREFIX + ":" + PageRSSModule.PAGE_UUID);
        int len = list.getLength();
        for (int idx = 0; idx < len; idx++) {
            ele = list.item(idx);
            pageUuid = ele.getTextContent();
            if (!securityService.isAllowPageReading(spaceUname, pageUuid, viewer)) {
                log.info("User " + (viewer == null ? "anonymous" : viewer.getUsername())
                        + "  has not reading permission for pageUuid " + pageUuid + " on space " + spaceUname
                        + ". Feed item of this page is removed from RSS output.");
                forbidPageUuidList.add(ele.getParentNode());
            }

        }
        if (forbidPageUuidList.size() > 0) {
            NodeList cl = dom.getElementsByTagName(PageRSSModule.CHANNEL);
            if (cl.getLength() > 0) {
                //only one channel tag!
                Node channel = cl.item(0);
                for (Node element : forbidPageUuidList) {
                    channel.removeChild(element);
                }
            }
        }
    }
    return dom;
}

From source file:io.hops.hopsworks.common.security.CertificateMaterializer.java

private ReentrantReadWriteLock.ReadLock getReadLockForKey(MaterialKey key) {
    ReentrantReadWriteLock lock = getLockForKey(key, false);
    return lock != null ? lock.readLock() : null;
}

From source file:org.apache.tez.dag.app.dag.impl.TaskAttemptImpl.java

@SuppressWarnings("rawtypes")
public TaskAttemptImpl(TezTaskID taskId, int attemptNumber, EventHandler eventHandler, TaskAttemptListener tal,
        int partition, TezConfiguration conf, Token<JobTokenIdentifier> jobToken, Credentials credentials,
        Clock clock, TaskHeartbeatHandler taskHeartbeatHandler, AppContext appContext,
        ProcessorDescriptor processorDescriptor, TaskLocationHint locationHint, Resource resource,
        Map<String, LocalResource> localResources, Map<String, String> environment, String javaOpts,
        boolean isRescheduled) {
    ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    this.readLock = rwLock.readLock();
    this.writeLock = rwLock.writeLock();
    this.attemptId = TezBuilderUtils.newTaskAttemptId(taskId, attemptNumber);
    this.eventHandler = eventHandler;
    //Reported status
    this.partition = partition;
    this.conf = conf;
    this.jobToken = jobToken;
    this.credentials = credentials;
    this.clock = clock;
    this.taskHeartbeatHandler = taskHeartbeatHandler;
    this.appContext = appContext;
    this.taskResource = resource;
    this.reportedStatus = new TaskAttemptStatus();
    this.processorDescriptor = processorDescriptor;
    initTaskAttemptStatus(reportedStatus);
    RackResolver.init(conf);//from w ww.  j a  v  a 2  s.c om
    this.stateMachine = stateMachineFactory.make(this);
    this.locationHint = locationHint;
    this.localResources = localResources;
    this.environment = environment;
    this.javaOpts = javaOpts;
    this.isRescheduled = isRescheduled;
}

From source file:org.opencms.loader.CmsJspLoader.java

/**
 * Updates a JSP page in the "real" file system in case the VFS resource has changed.<p>
 * /*  www  . j  a  v a  2 s . co m*/
 * Also processes the <code>&lt;%@ cms %&gt;</code> tags before the JSP is written to the real FS.
 * Also recursively updates all files that are referenced by a <code>&lt;%@ cms %&gt;</code> tag 
 * on this page to make sure the file actually exists in the real FS. 
 * All <code>&lt;%@ include %&gt;</code> tags are parsed and the name in the tag is translated
 * from the OpenCms VFS path to the path in the real FS. 
 * The same is done for filenames in <code>&lt;%@ page errorPage=... %&gt;</code> tags.<p>
 * 
 * @param resource the requested JSP file resource in the VFS
 * @param controller the controller for the JSP integration
 * @param updatedFiles a Set containing all JSP pages that have been already updated
 * 
 * @return the file name of the updated JSP in the "real" FS
 * 
 * @throws ServletException might be thrown in the process of including the JSP 
 * @throws IOException might be thrown in the process of including the JSP 
 * @throws CmsLoaderException if the resource type can not be read
 */
public String updateJsp(CmsResource resource, CmsFlexController controller, Set<String> updatedFiles)
        throws IOException, ServletException, CmsLoaderException {

    String jspVfsName = resource.getRootPath();
    String extension;
    boolean isHardInclude;
    int loaderId = OpenCms.getResourceManager().getResourceType(resource.getTypeId()).getLoaderId();
    if ((loaderId == CmsJspLoader.RESOURCE_LOADER_ID) && (!jspVfsName.endsWith(JSP_EXTENSION))) {
        // this is a true JSP resource that does not end with ".jsp"
        extension = JSP_EXTENSION;
        isHardInclude = false;
    } else {
        // not a JSP resource or already ends with ".jsp"
        extension = "";
        // if this is a JSP we don't treat it as hard include
        isHardInclude = (loaderId != CmsJspLoader.RESOURCE_LOADER_ID);
    }

    String jspTargetName = CmsFileUtil.getRepositoryName(m_jspWebAppRepository, jspVfsName + extension,
            controller.getCurrentRequest().isOnline());

    // check if page was already updated
    if (updatedFiles.contains(jspTargetName)) {
        // no need to write the already included file to the real FS more then once
        return jspTargetName;
    }

    String jspPath = CmsFileUtil.getRepositoryName(m_jspRepository, jspVfsName + extension,
            controller.getCurrentRequest().isOnline());

    File d = new File(jspPath).getParentFile();
    if ((d == null) || (d.exists() && !(d.isDirectory() && d.canRead()))) {
        CmsMessageContainer message = Messages.get().container(Messages.LOG_ACCESS_DENIED_1, jspPath);
        LOG.error(message.key());
        // can not continue
        throw new ServletException(message.key());
    }

    if (!d.exists()) {
        // create directory structure
        d.mkdirs();
    }
    ReentrantReadWriteLock readWriteLock = getFileLock(jspVfsName);
    try {
        // get a read lock for this jsp
        readWriteLock.readLock().lock();
        File jspFile = new File(jspPath);
        // check if the JSP must be updated
        boolean mustUpdate = false;
        long jspModificationDate = 0;
        if (!jspFile.exists()) {
            // file does not exist in real FS
            mustUpdate = true;
            // make sure the parent folder exists
            File folder = jspFile.getParentFile();
            if (!folder.exists()) {
                boolean success = folder.mkdirs();
                if (!success) {
                    LOG.error(org.opencms.db.Messages.get().getBundle()
                            .key(org.opencms.db.Messages.LOG_CREATE_FOLDER_FAILED_1, folder.getAbsolutePath()));
                }
            }
        } else {
            jspModificationDate = jspFile.lastModified();
            if (jspModificationDate <= resource.getDateLastModified()) {
                // file in real FS is older then file in VFS
                mustUpdate = true;
            } else if (controller.getCurrentRequest().isDoRecompile()) {
                // recompile is forced with parameter
                mustUpdate = true;
            } else {
                // check if update is needed
                if (controller.getCurrentRequest().isOnline()) {
                    mustUpdate = !m_onlineJsps.containsKey(jspVfsName);
                } else {
                    mustUpdate = !m_offlineJsps.containsKey(jspVfsName);
                }
                // check strong links only if update is needed
                if (mustUpdate) {
                    // update strong link dependencies
                    mustUpdate = updateStrongLinks(resource, controller, updatedFiles);
                }
            }
        }
        if (mustUpdate) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITING_JSP_1, jspTargetName));
            }
            // jsp needs updating, acquire a write lock
            readWriteLock.readLock().unlock();
            readWriteLock.writeLock().lock();
            try {
                // check again if updating is still necessary as this might have happened while waiting for the write lock
                if (!jspFile.exists() || (jspModificationDate == jspFile.lastModified())) {
                    updatedFiles.add(jspTargetName);
                    byte[] contents;
                    String encoding;
                    try {
                        CmsObject cms = controller.getCmsObject();
                        contents = cms.readFile(resource).getContents();
                        // check the "content-encoding" property for the JSP, use system default if not found on path
                        encoding = cms.readPropertyObject(resource,
                                CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true).getValue();
                        if (encoding == null) {
                            encoding = OpenCms.getSystemInfo().getDefaultEncoding();
                        } else {
                            encoding = CmsEncoder.lookupEncoding(encoding.trim(), encoding);
                        }
                    } catch (CmsException e) {
                        controller.setThrowable(e, jspVfsName);
                        throw new ServletException(
                                Messages.get().getBundle().key(Messages.ERR_LOADER_JSP_ACCESS_1, jspVfsName),
                                e);
                    }

                    try {
                        // parse the JSP and modify OpenCms critical directives
                        contents = parseJsp(contents, encoding, controller, updatedFiles, isHardInclude);
                        if (LOG.isInfoEnabled()) {
                            // check for existing file and display some debug info
                            LOG.info(Messages.get().getBundle().key(Messages.LOG_JSP_PERMCHECK_4,
                                    new Object[] { jspFile.getAbsolutePath(), Boolean.valueOf(jspFile.exists()),
                                            Boolean.valueOf(jspFile.isFile()),
                                            Boolean.valueOf(jspFile.canWrite()) }));
                        }
                        // write the parsed JSP content to the real FS
                        synchronized (CmsJspLoader.class) {
                            // this must be done only one file at a time
                            FileOutputStream fs = new FileOutputStream(jspFile);
                            fs.write(contents);
                            fs.close();
                        }
                        if (controller.getCurrentRequest().isOnline()) {
                            m_onlineJsps.put(jspVfsName, Boolean.TRUE);
                        } else {
                            m_offlineJsps.put(jspVfsName, Boolean.TRUE);
                        }
                        if (LOG.isInfoEnabled()) {
                            LOG.info(Messages.get().getBundle().key(Messages.LOG_UPDATED_JSP_2, jspTargetName,
                                    jspVfsName));
                        }
                    } catch (FileNotFoundException e) {
                        throw new ServletException(Messages.get().getBundle()
                                .key(Messages.ERR_LOADER_JSP_WRITE_1, jspFile.getName()), e);
                    }
                }
            } finally {
                readWriteLock.readLock().lock();
                readWriteLock.writeLock().unlock();
            }
        }

        // update "last modified" and "expires" date on controller
        controller.updateDates(jspFile.lastModified(), CmsResource.DATE_EXPIRED_DEFAULT);
    } finally {
        //m_processingFiles.remove(jspVfsName);
        readWriteLock.readLock().unlock();
    }

    return jspTargetName;
}

From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.ContainerManagerImpl.java

public ContainerManagerImpl(Context context, ContainerExecutor exec, DeletionService deletionContext,
        NodeStatusUpdater nodeStatusUpdater, NodeManagerMetrics metrics, LocalDirsHandlerService dirsHandler) {
    super(ContainerManagerImpl.class.getName());
    this.context = context;
    this.dirsHandler = dirsHandler;

    // ContainerManager level dispatcher.
    dispatcher = new AsyncDispatcher();
    this.deletionService = deletionContext;
    this.metrics = metrics;

    rsrcLocalizationSrvc = createResourceLocalizationService(exec, deletionContext, context);
    addService(rsrcLocalizationSrvc);//from  w w  w .  j ava  2s. c  o  m

    containersLauncher = createContainersLauncher(context, exec);
    addService(containersLauncher);

    this.exec = exec;

    this.nodeStatusUpdater = nodeStatusUpdater;

    // Start configurable services
    auxiliaryServices = new AuxServices();
    auxiliaryServices.registerServiceListener(this);
    addService(auxiliaryServices);

    this.containersMonitor = new ContainersMonitorImpl(exec, dispatcher, this.context);
    addService(this.containersMonitor);

    dispatcher.register(ContainerEventType.class, new ContainerEventDispatcher());
    dispatcher.register(ApplicationEventType.class, new ApplicationEventDispatcher());
    dispatcher.register(LocalizationEventType.class, rsrcLocalizationSrvc);
    dispatcher.register(AuxServicesEventType.class, auxiliaryServices);
    dispatcher.register(ContainersMonitorEventType.class, containersMonitor);
    dispatcher.register(ContainersLauncherEventType.class, containersLauncher);

    addService(dispatcher);

    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    this.readLock = lock.readLock();
    this.writeLock = lock.writeLock();

    this.cryptoMaterialUpdaterThreadPool = Executors.newFixedThreadPool(3, new ThreadFactoryBuilder()
            .setDaemon(true).setNameFormat("Container crypto material updater thread #%d").build());
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.rmapp.attempt.RMAppAttemptImpl.java

public RMAppAttemptImpl(ApplicationAttemptId appAttemptId, RMContext rmContext, YarnScheduler scheduler,
        ApplicationMasterService masterService, ApplicationSubmissionContext submissionContext,
        Configuration conf, boolean maybeLastAttempt, ResourceRequest amReq,
        BlacklistManager amBlacklistManager) {
    this.conf = conf;
    this.applicationAttemptId = appAttemptId;
    this.rmContext = rmContext;
    this.eventHandler = rmContext.getDispatcher().getEventHandler();
    this.submissionContext = submissionContext;
    this.scheduler = scheduler;
    this.masterService = masterService;

    ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    this.readLock = lock.readLock();
    this.writeLock = lock.writeLock();

    this.proxiedTrackingUrl = generateProxyUriWithScheme();
    this.maybeLastAttempt = maybeLastAttempt;
    this.stateMachine = stateMachineFactory.make(this);

    this.attemptMetrics = new RMAppAttemptMetrics(applicationAttemptId, rmContext);

    this.amReq = amReq;
    this.blacklistedNodesForAM = amBlacklistManager;
}