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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.named.redisson;
020
021import javax.inject.Named;
022import javax.inject.Singleton;
023
024import java.time.Duration;
025import java.util.concurrent.ConcurrentHashMap;
026import java.util.concurrent.ConcurrentMap;
027import java.util.concurrent.TimeUnit;
028
029import org.eclipse.aether.named.NamedLock;
030import org.eclipse.aether.named.NamedLockKey;
031import org.eclipse.aether.named.support.AdaptedSemaphoreNamedLock;
032import org.redisson.api.RSemaphore;
033
034/**
035 * Provider of {@link RedissonSemaphoreNamedLockFactory} using Redisson and {@link org.redisson.api.RSemaphore}.
036 */
037@Singleton
038@Named(RedissonSemaphoreNamedLockFactory.NAME)
039public class RedissonSemaphoreNamedLockFactory extends RedissonNamedLockFactorySupport {
040    public static final String NAME = "semaphore-redisson";
041
042    private static final String TYPED_NAME_PREFIX = NAME_PREFIX + NAME + ":";
043
044    private final ConcurrentMap<NamedLockKey, RSemaphore> semaphores;
045
046    public RedissonSemaphoreNamedLockFactory() {
047        this.semaphores = new ConcurrentHashMap<>();
048    }
049
050    @Override
051    protected AdaptedSemaphoreNamedLock createLock(final NamedLockKey key) {
052        RSemaphore semaphore = semaphores.computeIfAbsent(key, k -> {
053            RSemaphore result = redissonClient.getSemaphore(TYPED_NAME_PREFIX + k.name());
054            if (!result.trySetPermits(Integer.MAX_VALUE)) {
055                logger.warn("Failed to set permits on semaphore '{}'; it may be in a stale state", k.name());
056            }
057            return result;
058        });
059        return new AdaptedSemaphoreNamedLock(key, this, new RedissonSemaphore(semaphore));
060    }
061
062    @Override
063    protected void destroyLock(final NamedLock namedLock) {
064        if (namedLock instanceof AdaptedSemaphoreNamedLock) {
065            final NamedLockKey key = namedLock.key();
066            RSemaphore semaphore = semaphores.remove(key);
067            if (semaphore == null) {
068                throw new IllegalStateException("Semaphore expected, but does not exist: " + key);
069            }
070            /* There is no reasonable way to destroy the semaphore in Redis because we cannot know
071             * when the last process has stopped using it.
072             */
073        }
074    }
075
076    private static final class RedissonSemaphore implements AdaptedSemaphoreNamedLock.AdaptedSemaphore {
077        private final RSemaphore semaphore;
078
079        private RedissonSemaphore(final RSemaphore semaphore) {
080            this.semaphore = semaphore;
081        }
082
083        @Override
084        public boolean tryAcquire(final int perms, final long time, final TimeUnit unit) throws InterruptedException {
085            return semaphore.tryAcquire(perms, Duration.ofNanos(unit.toNanos(time)));
086        }
087
088        @Override
089        public void release(final int perms) {
090            semaphore.release(perms);
091        }
092    }
093}