Jordan Halterman | 4743258 | 2018-01-25 16:56:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 org.onosproject.store.primitives; |
| 17 | |
| 18 | import java.time.Duration; |
| 19 | import java.util.Optional; |
| 20 | import java.util.concurrent.CompletableFuture; |
| 21 | import java.util.concurrent.ExecutionException; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | import java.util.concurrent.TimeoutException; |
| 24 | |
| 25 | import org.onosproject.store.service.AsyncDistributedLock; |
| 26 | import org.onosproject.store.service.DistributedLock; |
| 27 | import org.onosproject.store.service.StorageException; |
| 28 | import org.onosproject.store.service.Synchronous; |
| 29 | import org.onosproject.store.service.Version; |
| 30 | |
| 31 | /** |
| 32 | * Default implementation for a {@code DistributedLock} backed by a {@link AsyncDistributedLock}. |
| 33 | */ |
| 34 | public class DefaultDistributedLock extends Synchronous<AsyncDistributedLock> implements DistributedLock { |
| 35 | |
| 36 | private final AsyncDistributedLock asyncLock; |
| 37 | private final long operationTimeoutMillis; |
| 38 | |
| 39 | public DefaultDistributedLock(AsyncDistributedLock asyncLock, long operationTimeoutMillis) { |
| 40 | super(asyncLock); |
| 41 | this.asyncLock = asyncLock; |
| 42 | this.operationTimeoutMillis = operationTimeoutMillis; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public Version lock() { |
| 47 | return complete(asyncLock.lock()); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public Optional<Version> tryLock() { |
| 52 | return complete(asyncLock.tryLock()); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public Optional<Version> tryLock(Duration timeout) { |
| 57 | return complete(asyncLock.tryLock(timeout)); |
| 58 | } |
| 59 | |
| 60 | @Override |
| 61 | public void unlock() { |
| 62 | complete(asyncLock.unlock()); |
| 63 | } |
| 64 | |
| 65 | private <T> T complete(CompletableFuture<T> future) { |
| 66 | if (operationTimeoutMillis == -1) { |
| 67 | return future.join(); |
| 68 | } |
| 69 | try { |
| 70 | return future.get(operationTimeoutMillis, TimeUnit.MILLISECONDS); |
| 71 | } catch (InterruptedException e) { |
| 72 | Thread.currentThread().interrupt(); |
| 73 | throw new StorageException.Interrupted(); |
| 74 | } catch (TimeoutException e) { |
| 75 | throw new StorageException.Timeout(); |
| 76 | } catch (ExecutionException e) { |
| 77 | throw new StorageException(e.getCause()); |
| 78 | } |
| 79 | } |
| 80 | } |