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 */ 018 019package org.apache.oozie.service; 020 021import org.apache.oozie.util.Instrumentable; 022import org.apache.oozie.util.Instrumentation; 023import org.apache.oozie.lock.LockToken; 024import org.apache.oozie.lock.MemoryLocks; 025 026/** 027 * Service that provides in-memory locks. Assumes no other Oozie servers are using the database. 028 */ 029public class MemoryLocksService implements Service, Instrumentable { 030 protected static final String INSTRUMENTATION_GROUP = "locks"; 031 private MemoryLocks locks; 032 033 /** 034 * Initialize the memory locks service 035 * 036 * @param services services instance. 037 */ 038 @Override 039 public void init(Services services) throws ServiceException { 040 locks = new MemoryLocks(); 041 } 042 043 /** 044 * Destroy the memory locks service. 045 */ 046 @Override 047 public void destroy() { 048 locks = null; 049 } 050 051 /** 052 * Return the public interface for the memory locks services 053 * 054 * @return {@link MemoryLocksService}. 055 */ 056 @Override 057 public Class<? extends Service> getInterface() { 058 return MemoryLocksService.class; 059 } 060 061 /** 062 * Instruments the memory locks service. 063 * 064 * @param instr instance to instrument the memory locks service to. 065 */ 066 public void instrument(Instrumentation instr) { 067 final MemoryLocks finalLocks = this.locks; 068 instr.addVariable(INSTRUMENTATION_GROUP, "locks", new Instrumentation.Variable<Long>() { 069 public Long getValue() { 070 return (long) finalLocks.size(); 071 } 072 }); 073 } 074 075 /** 076 * Obtain a READ lock for a source. 077 * 078 * @param resource resource name. 079 * @param wait time out in milliseconds to wait for the lock, -1 means no timeout and 0 no wait. 080 * @return the lock token for the resource, or <code>null</code> if the lock could not be obtained. 081 * @throws InterruptedException thrown if the thread was interrupted while waiting. 082 */ 083 public LockToken getReadLock(String resource, long wait) throws InterruptedException { 084 return locks.getReadLock(resource, wait); 085 } 086 087 /** 088 * Obtain a WRITE lock for a source. 089 * 090 * @param resource resource name. 091 * @param wait time out in milliseconds to wait for the lock, -1 means no timeout and 0 no wait. 092 * @return the lock token for the resource, or <code>null</code> if the lock could not be obtained. 093 * @throws InterruptedException thrown if the thread was interrupted while waiting. 094 */ 095 public LockToken getWriteLock(String resource, long wait) throws InterruptedException { 096 return locks.getWriteLock(resource, wait); 097 } 098}