001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 * 
010 *      http://www.apache.org/licenses/LICENSE-2.0
011 * 
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.oozie.service;
019
020import org.apache.oozie.util.Instrumentable;
021import org.apache.oozie.util.Instrumentation;
022import org.apache.oozie.lock.LockToken;
023import org.apache.oozie.lock.MemoryLocks;
024
025/**
026 * Service that provides in-memory locks.  Assumes no other Oozie servers are using the database.
027 */
028public class MemoryLocksService implements Service, Instrumentable {
029    protected static final String INSTRUMENTATION_GROUP = "locks";
030    private MemoryLocks locks;
031
032    /**
033     * Initialize the memory locks service
034     *
035     * @param services services instance.
036     */
037    @Override
038    public void init(Services services) throws ServiceException {
039        locks = new MemoryLocks();
040    }
041
042    /**
043     * Destroy the memory locks service.
044     */
045    @Override
046    public void destroy() {
047        locks = null;
048    }
049
050    /**
051     * Return the public interface for the memory locks services
052     *
053     * @return {@link MemoryLocksService}.
054     */
055    @Override
056    public Class<? extends Service> getInterface() {
057        return MemoryLocksService.class;
058    }
059
060    /**
061     * Instruments the memory locks service.
062     *
063     * @param instr instance to instrument the memory locks service to.
064     */
065    public void instrument(Instrumentation instr) {
066        final MemoryLocks finalLocks = this.locks;
067        instr.addVariable(INSTRUMENTATION_GROUP, "locks", new Instrumentation.Variable<Long>() {
068            public Long getValue() {
069                return (long) finalLocks.size();
070            }
071        });
072    }
073
074    /**
075     * Obtain a READ lock for a source.
076     *
077     * @param resource resource name.
078     * @param wait time out in milliseconds to wait for the lock, -1 means no timeout and 0 no wait.
079     * @return the lock token for the resource, or <code>null</code> if the lock could not be obtained.
080     * @throws InterruptedException thrown if the thread was interrupted while waiting.
081     */
082    public LockToken getReadLock(String resource, long wait) throws InterruptedException {
083        return locks.getReadLock(resource, wait);
084    }
085
086    /**
087     * Obtain a WRITE lock for a source.
088     *
089     * @param resource resource name.
090     * @param wait time out in milliseconds to wait for the lock, -1 means no timeout and 0 no wait.
091     * @return the lock token for the resource, or <code>null</code> if the lock could not be obtained.
092     * @throws InterruptedException thrown if the thread was interrupted while waiting.
093     */
094    public LockToken getWriteLock(String resource, long wait) throws InterruptedException {
095        return locks.getWriteLock(resource, wait);
096    }
097}