/hazelcast/src/main/java/com/hazelcast/impl/SemaphoreProxyImpl.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 493 lines · 389 code · 89 blank · 15 comment · 22 complexity · 1ecfc18090ba93746a8d0ce7f369ee5f MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.hazelcast.impl;
  17. import com.hazelcast.core.HazelcastInstance;
  18. import com.hazelcast.core.InstanceDestroyedException;
  19. import com.hazelcast.core.Prefix;
  20. import com.hazelcast.core.RuntimeInterruptedException;
  21. import com.hazelcast.impl.base.FactoryAwareNamedProxy;
  22. import com.hazelcast.impl.monitor.LocalSemaphoreStatsImpl;
  23. import com.hazelcast.impl.monitor.SemaphoreOperationsCounter;
  24. import com.hazelcast.monitor.LocalSemaphoreStats;
  25. import com.hazelcast.nio.Data;
  26. import java.util.concurrent.Future;
  27. import java.util.concurrent.TimeUnit;
  28. import static com.hazelcast.nio.IOUtil.toData;
  29. public class SemaphoreProxyImpl extends FactoryAwareNamedProxy implements SemaphoreProxy {
  30. private transient SemaphoreProxy base = null;
  31. Data nameAsData = null;
  32. public SemaphoreProxyImpl(String name, FactoryImpl factory) {
  33. setName(name);
  34. setHazelcastInstance(factory);
  35. base = new SemaphoreProxyReal();
  36. }
  37. private void ensure() {
  38. factory.initialChecks();
  39. if (base == null) {
  40. base = (SemaphoreProxy) factory.getOrCreateProxyByName(name);
  41. }
  42. }
  43. public String getLongName() {
  44. return name;
  45. }
  46. public String getName() {
  47. return name.substring(Prefix.SEMAPHORE.length());
  48. }
  49. Data getNameAsData() {
  50. if (nameAsData == null) {
  51. nameAsData = toData(getName());
  52. }
  53. return nameAsData;
  54. }
  55. public Object getId() {
  56. ensure();
  57. return base.getId();
  58. }
  59. @Override
  60. public String toString() {
  61. return "Semaphore [" + getName() + "]";
  62. }
  63. @Override
  64. public boolean equals(Object o) {
  65. if (this == o)
  66. return true;
  67. if (o == null || getClass() != o.getClass())
  68. return false;
  69. SemaphoreProxyImpl that = (SemaphoreProxyImpl) o;
  70. return !(name != null ? !name.equals(that.name) : that.name != null);
  71. }
  72. @Override
  73. public int hashCode() {
  74. return name != null ? name.hashCode() : 0;
  75. }
  76. public InstanceType getInstanceType() {
  77. ensure();
  78. return base.getInstanceType();
  79. }
  80. public LocalSemaphoreStats getLocalSemaphoreStats() {
  81. ensure();
  82. return base.getLocalSemaphoreStats();
  83. }
  84. public SemaphoreOperationsCounter getOperationsCounter() {
  85. ensure();
  86. return base.getOperationsCounter();
  87. }
  88. public void acquire() throws InstanceDestroyedException, InterruptedException {
  89. ensure();
  90. base.acquire();
  91. }
  92. public void acquire(int permits) throws InstanceDestroyedException, InterruptedException {
  93. check(permits);
  94. ensure();
  95. base.acquire(permits);
  96. }
  97. public Future acquireAsync() {
  98. return doAsyncAcquire(1, false);
  99. }
  100. public Future acquireAsync(int permits) {
  101. check(permits);
  102. return doAsyncAcquire(permits, false);
  103. }
  104. public void acquireAttach() throws InstanceDestroyedException, InterruptedException {
  105. ensure();
  106. base.acquireAttach();
  107. }
  108. public void acquireAttach(int permits) throws InstanceDestroyedException, InterruptedException {
  109. check(permits);
  110. ensure();
  111. base.acquireAttach(permits);
  112. }
  113. public Future acquireAttachAsync() {
  114. return doAsyncAcquire(1, true);
  115. }
  116. public Future acquireAttachAsync(int permits) {
  117. check(permits);
  118. return doAsyncAcquire(permits, true);
  119. }
  120. public void attach() {
  121. ensure();
  122. base.attach();
  123. }
  124. public void attach(int permits) {
  125. check(permits);
  126. ensure();
  127. base.attach(permits);
  128. }
  129. public int attachedPermits() {
  130. ensure();
  131. return base.attachedPermits();
  132. }
  133. public int availablePermits() {
  134. ensure();
  135. return base.availablePermits();
  136. }
  137. public void detach() {
  138. ensure();
  139. base.detach();
  140. }
  141. public void detach(int permits) {
  142. check(permits);
  143. ensure();
  144. base.detach(permits);
  145. }
  146. public void destroy() {
  147. ensure();
  148. base.destroy();
  149. }
  150. public int drainPermits() {
  151. ensure();
  152. return base.drainPermits();
  153. }
  154. public void reducePermits(int permits) {
  155. check(permits);
  156. ensure();
  157. base.reducePermits(permits);
  158. }
  159. public void release() {
  160. ensure();
  161. base.release();
  162. }
  163. public void release(int permits) {
  164. check(permits);
  165. ensure();
  166. base.release(permits);
  167. }
  168. public void releaseDetach() {
  169. ensure();
  170. base.releaseDetach();
  171. }
  172. public void releaseDetach(int permits) {
  173. check(permits);
  174. ensure();
  175. base.releaseDetach(permits);
  176. }
  177. public boolean tryAcquire() {
  178. ensure();
  179. return base.tryAcquire();
  180. }
  181. public boolean tryAcquire(int permits) {
  182. check(permits);
  183. ensure();
  184. return base.tryAcquire(permits);
  185. }
  186. public boolean tryAcquire(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
  187. ensure();
  188. return base.tryAcquire(timeout, unit);
  189. }
  190. public boolean tryAcquire(int permits, long timeout, TimeUnit timeunit) throws InstanceDestroyedException, InterruptedException {
  191. check(permits, timeout, timeunit);
  192. ensure();
  193. return base.tryAcquire(permits, timeout, timeunit);
  194. }
  195. public boolean tryAcquireAttach() {
  196. ensure();
  197. return base.tryAcquireAttach();
  198. }
  199. public boolean tryAcquireAttach(int permits) {
  200. check(permits);
  201. ensure();
  202. return base.tryAcquireAttach(permits);
  203. }
  204. public boolean tryAcquireAttach(long timeout, TimeUnit timeunit) throws InstanceDestroyedException, InterruptedException {
  205. ensure();
  206. return base.tryAcquireAttach(timeout, timeunit);
  207. }
  208. public boolean tryAcquireAttach(int permits, long timeout, TimeUnit timeunit) throws InstanceDestroyedException, InterruptedException {
  209. check(permits, timeout, timeunit);
  210. ensure();
  211. return base.tryAcquireAttach(permits, timeout, timeunit);
  212. }
  213. private void check(int permits) {
  214. if (permits < 0)
  215. throw new IllegalArgumentException("Number of permits can not be negative: " + permits);
  216. }
  217. private void check(int permits, long timeout, TimeUnit timeunit) {
  218. check(permits);
  219. if (timeout < -1)
  220. throw new IllegalArgumentException("Invalid timeout value: " + timeout);
  221. if (timeunit == null) {
  222. throw new NullPointerException("TimeUnit can not be null.");
  223. }
  224. }
  225. private Future doAsyncAcquire(final Integer permits, final Boolean attach) {
  226. final SemaphoreProxyImpl semaphoreProxy = SemaphoreProxyImpl.this;
  227. AsyncCall call = new AsyncCall() {
  228. @Override
  229. protected void call() {
  230. try {
  231. if (attach)
  232. semaphoreProxy.acquireAttach(permits);
  233. else
  234. semaphoreProxy.acquire(permits);
  235. setResult(null);
  236. } catch (InterruptedException e) {
  237. setResult(e);
  238. } catch (InstanceDestroyedException e) {
  239. e.printStackTrace();
  240. }
  241. }
  242. @Override
  243. public boolean cancel(boolean mayInterruptIfRunning) {
  244. ConcurrentMapManager.MSemaphore msemaphore = factory.node.concurrentMapManager.new MSemaphore();
  245. return msemaphore.cancelAcquire(getNameAsData());
  246. }
  247. };
  248. factory.node.executorManager.executeAsync(call);
  249. return call;
  250. }
  251. private class SemaphoreProxyReal implements SemaphoreProxy {
  252. SemaphoreOperationsCounter operationsCounter = new SemaphoreOperationsCounter();
  253. public Object getId() {
  254. return name;
  255. }
  256. public InstanceType getInstanceType() {
  257. return InstanceType.SEMAPHORE;
  258. }
  259. public String getLongName() {
  260. return name;
  261. }
  262. public String getName() {
  263. return name.substring(Prefix.SEMAPHORE.length());
  264. }
  265. public void destroy() {
  266. newMSemaphore().destroy(getNameAsData());
  267. factory.destroyInstanceClusterWide(name, null);
  268. }
  269. public void acquire() throws InstanceDestroyedException, InterruptedException {
  270. acquire(1);
  271. }
  272. public void acquire(int permits) throws InstanceDestroyedException, InterruptedException {
  273. if (Thread.interrupted())
  274. throw new InterruptedException();
  275. try {
  276. doTryAcquire(permits, false, -1);
  277. } catch (RuntimeInterruptedException e) {
  278. throw new InterruptedException();
  279. }
  280. }
  281. public Future acquireAsync() {
  282. throw new UnsupportedOperationException();
  283. }
  284. public Future acquireAsync(int permits) {
  285. throw new UnsupportedOperationException();
  286. }
  287. public void acquireAttach() throws InstanceDestroyedException, InterruptedException {
  288. acquireAttach(1);
  289. }
  290. public void acquireAttach(int permits) throws InstanceDestroyedException, InterruptedException {
  291. if (Thread.interrupted())
  292. throw new InterruptedException();
  293. try {
  294. doTryAcquire(permits, true, -1);
  295. } catch (RuntimeInterruptedException e) {
  296. throw new InterruptedException();
  297. }
  298. }
  299. public Future acquireAttachAsync() {
  300. throw new UnsupportedOperationException();
  301. }
  302. public Future acquireAttachAsync(int permits) {
  303. throw new UnsupportedOperationException();
  304. }
  305. public void attach() {
  306. attach(1);
  307. }
  308. public void attach(int permits) {
  309. newMSemaphore().attachDetach(getNameAsData(), permits);
  310. }
  311. public int attachedPermits() {
  312. return newMSemaphore().getAttached(getNameAsData());
  313. }
  314. public int availablePermits() {
  315. return newMSemaphore().getAvailable(getNameAsData());
  316. }
  317. public void detach() {
  318. detach(1);
  319. }
  320. public void detach(int permits) {
  321. newMSemaphore().attachDetach(getNameAsData(), -permits);
  322. }
  323. public int drainPermits() {
  324. return newMSemaphore().drainPermits(getNameAsData());
  325. }
  326. public void release() {
  327. release(1);
  328. }
  329. public void release(int permits) {
  330. newMSemaphore().release(getNameAsData(), permits, false);
  331. }
  332. public void releaseDetach() {
  333. releaseDetach(1);
  334. }
  335. public void releaseDetach(int permits) {
  336. newMSemaphore().release(getNameAsData(), permits, true);
  337. }
  338. public boolean tryAcquire() {
  339. return tryAcquire(1);
  340. }
  341. public boolean tryAcquire(int permits) {
  342. try {
  343. return doTryAcquire(permits, false, 0);
  344. } catch (Throwable e) {
  345. return false;
  346. }
  347. }
  348. public boolean tryAcquire(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
  349. return tryAcquire(1, timeout, unit);
  350. }
  351. public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
  352. if (Thread.interrupted())
  353. throw new InterruptedException();
  354. try {
  355. return doTryAcquire(permits, false, unit.toMillis(timeout));
  356. } catch (RuntimeInterruptedException e) {
  357. throw new InterruptedException();
  358. }
  359. }
  360. public boolean tryAcquireAttach() {
  361. return tryAcquireAttach(1);
  362. }
  363. public boolean tryAcquireAttach(int permits) {
  364. try {
  365. return doTryAcquire(permits, true, 0);
  366. } catch (Throwable e) {
  367. return false;
  368. }
  369. }
  370. public boolean tryAcquireAttach(long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
  371. return tryAcquireAttach(1, timeout, unit);
  372. }
  373. public boolean tryAcquireAttach(int permits, long timeout, TimeUnit unit) throws InstanceDestroyedException, InterruptedException {
  374. if (Thread.interrupted())
  375. throw new InterruptedException();
  376. try {
  377. return doTryAcquire(permits, true, unit.toMillis(timeout));
  378. } catch (RuntimeInterruptedException e) {
  379. throw new InterruptedException();
  380. }
  381. }
  382. public void reducePermits(int permits) {
  383. newMSemaphore().reduce(getNameAsData(), permits);
  384. }
  385. public LocalSemaphoreStats getLocalSemaphoreStats() {
  386. LocalSemaphoreStatsImpl localSemaphoreStats = new LocalSemaphoreStatsImpl();
  387. localSemaphoreStats.setOperationStats(operationsCounter.getPublishedStats());
  388. return localSemaphoreStats;
  389. }
  390. public SemaphoreOperationsCounter getOperationsCounter() {
  391. return operationsCounter;
  392. }
  393. private ConcurrentMapManager.MSemaphore newMSemaphore() {
  394. ConcurrentMapManager.MSemaphore msemaphore = factory.node.concurrentMapManager.new MSemaphore();
  395. msemaphore.setOperationsCounter(operationsCounter);
  396. return msemaphore;
  397. }
  398. private boolean doTryAcquire(int permits, boolean attach, long timeout) throws InstanceDestroyedException {
  399. return newMSemaphore().tryAcquire(getNameAsData(), permits, attach, timeout);
  400. }
  401. public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
  402. }
  403. }
  404. }