View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.named.redisson;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.time.Duration;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.ConcurrentMap;
27  import java.util.concurrent.TimeUnit;
28  
29  import org.eclipse.aether.named.NamedLock;
30  import org.eclipse.aether.named.NamedLockKey;
31  import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
32  import org.redisson.api.RSemaphore;
33  
34  /**
35   * Provider of {@link RedissonSemaphoreNamedLockFactory} using Redisson and {@link org.redisson.api.RSemaphore}.
36   */
37  @Singleton
38  @Named(RedissonSemaphoreNamedLockFactory.NAME)
39  public class RedissonSemaphoreNamedLockFactory extends RedissonNamedLockFactorySupport {
40      public static final String NAME = "semaphore-redisson";
41  
42      private static final String TYPED_NAME_PREFIX = NAME_PREFIX + NAME + ":";
43  
44      private final ConcurrentMap<NamedLockKey, RSemaphore> semaphores;
45  
46      public RedissonSemaphoreNamedLockFactory() {
47          this.semaphores = new ConcurrentHashMap<>();
48      }
49  
50      @Override
51      protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) {
52          RSemaphore semaphore = semaphores.computeIfAbsent(key, k -> {
53              RSemaphore result = redissonClient.getSemaphore(TYPED_NAME_PREFIX + k.name());
54              if (!result.trySetPermits(Integer.MAX_VALUE)) {
55                  logger.warn("Failed to set permits on semaphore '{}'; it may be in a stale state", k.name());
56              }
57              return result;
58          });
59          return new AdaptedSemaphoreNamedLock(key, this, new RedissonSemaphore(semaphore));
60      }
61  
62      @Override
63      protected void destroyLock(final NamedLock namedLock) {
64          if (namedLock instanceof AdaptedSemaphoreNamedLock) {
65              final NamedLockKey key = namedLock.key();
66              RSemaphore semaphore = semaphores.remove(key);
67              if (semaphore == null) {
68                  throw new IllegalStateException("Semaphore expected, but does not exist: " + key);
69              }
70              /* There is no reasonable way to destroy the semaphore in Redis because we cannot know
71               * when the last process has stopped using it.
72               */
73          }
74      }
75  
76      private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore {
77          private final RSemaphore semaphore;
78  
79          private RedissonSemaphore(final RSemaphore semaphore) {
80              this.semaphore = semaphore;
81          }
82  
83          @Override
84          public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
85              return semaphore.tryAcquire(perms, Duration.ofNanos(unit.toNanos(time)));
86          }
87  
88          @Override
89          public void release(final int perms) {
90              semaphore.release(perms);
91          }
92      }
93  }