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.internal.impl.synccontext.named;
020
021import java.util.ArrayDeque;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Deque;
025import java.util.concurrent.TimeUnit;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.SyncContext;
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.metadata.Metadata;
031import org.eclipse.aether.named.NamedLock;
032import org.eclipse.aether.named.NamedLockFactory;
033import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
034import org.eclipse.aether.util.ConfigUtils;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038import static java.util.Objects.requireNonNull;
039
040/**
041 * Adapter to adapt {@link NamedLockFactory} and {@link NamedLock} to {@link SyncContext}.
042 */
043public final class NamedLockFactoryAdapter {
044    public static final String TIME_KEY = "aether.syncContext.named.time";
045
046    public static final long DEFAULT_TIME = 30L;
047
048    public static final String TIME_UNIT_KEY = "aether.syncContext.named.time.unit";
049
050    public static final TimeUnit DEFAULT_TIME_UNIT = TimeUnit.SECONDS;
051
052    public static final String RETRY_KEY = "aether.syncContext.named.retry";
053
054    public static final int DEFAULT_RETRY = 1;
055
056    public static final String RETRY_WAIT_KEY = "aether.syncContext.named.retry.wait";
057
058    public static final long DEFAULT_RETRY_WAIT = 200L;
059
060    private final NameMapper nameMapper;
061
062    private final NamedLockFactory namedLockFactory;
063
064    public NamedLockFactoryAdapter(final NameMapper nameMapper, final NamedLockFactory namedLockFactory) {
065        this.nameMapper = requireNonNull(nameMapper);
066        this.namedLockFactory = requireNonNull(namedLockFactory);
067        // TODO: this is ad-hoc "validation", experimental and likely to change
068        if (this.namedLockFactory instanceof FileLockNamedLockFactory && !this.nameMapper.isFileSystemFriendly()) {
069            throw new IllegalArgumentException(
070                    "Misconfiguration: FileLockNamedLockFactory lock factory requires FS friendly NameMapper");
071        }
072    }
073
074    public SyncContext newInstance(final RepositorySystemSession session, final boolean shared) {
075        return new AdaptedLockSyncContext(session, shared, nameMapper, namedLockFactory);
076    }
077
078    /**
079     * @since 1.9.1
080     */
081    public NameMapper getNameMapper() {
082        return nameMapper;
083    }
084
085    /**
086     * @since 1.9.1
087     */
088    public NamedLockFactory getNamedLockFactory() {
089        return namedLockFactory;
090    }
091
092    public String toString() {
093        return getClass().getSimpleName()
094                + "(nameMapper=" + nameMapper
095                + ", namedLockFactory=" + namedLockFactory
096                + ")";
097    }
098
099    private static class AdaptedLockSyncContext implements SyncContext {
100        private static final Logger LOGGER = LoggerFactory.getLogger(AdaptedLockSyncContext.class);
101
102        private final RepositorySystemSession session;
103
104        private final boolean shared;
105
106        private final NameMapper lockNaming;
107
108        private final NamedLockFactory namedLockFactory;
109
110        private final long time;
111
112        private final TimeUnit timeUnit;
113
114        private final int retry;
115
116        private final long retryWait;
117
118        private final Deque<NamedLock> locks;
119
120        private AdaptedLockSyncContext(
121                final RepositorySystemSession session,
122                final boolean shared,
123                final NameMapper lockNaming,
124                final NamedLockFactory namedLockFactory) {
125            this.session = session;
126            this.shared = shared;
127            this.lockNaming = lockNaming;
128            this.namedLockFactory = namedLockFactory;
129            this.time = getTime(session);
130            this.timeUnit = getTimeUnit(session);
131            this.retry = getRetry(session);
132            this.retryWait = getRetryWait(session);
133            this.locks = new ArrayDeque<>();
134
135            if (time < 0L) {
136                throw new IllegalArgumentException(TIME_KEY + " value cannot be negative");
137            }
138            if (retry < 0L) {
139                throw new IllegalArgumentException(RETRY_KEY + " value cannot be negative");
140            }
141            if (retryWait < 0L) {
142                throw new IllegalArgumentException(RETRY_WAIT_KEY + " value cannot be negative");
143            }
144        }
145
146        private long getTime(final RepositorySystemSession session) {
147            return ConfigUtils.getLong(session, DEFAULT_TIME, TIME_KEY);
148        }
149
150        private TimeUnit getTimeUnit(final RepositorySystemSession session) {
151            return TimeUnit.valueOf(ConfigUtils.getString(session, DEFAULT_TIME_UNIT.name(), TIME_UNIT_KEY));
152        }
153
154        private int getRetry(final RepositorySystemSession session) {
155            return ConfigUtils.getInteger(session, DEFAULT_RETRY, RETRY_KEY);
156        }
157
158        private long getRetryWait(final RepositorySystemSession session) {
159            return ConfigUtils.getLong(session, DEFAULT_RETRY_WAIT, RETRY_WAIT_KEY);
160        }
161
162        @Override
163        public void acquire(Collection<? extends Artifact> artifacts, Collection<? extends Metadata> metadatas) {
164            Collection<String> keys = lockNaming.nameLocks(session, artifacts, metadatas);
165            if (keys.isEmpty()) {
166                return;
167            }
168
169            final int attempts = retry + 1;
170            final ArrayList<IllegalStateException> illegalStateExceptions = new ArrayList<>();
171            for (int attempt = 1; attempt <= attempts; attempt++) {
172                LOGGER.trace(
173                        "Attempt {}: Need {} {} lock(s) for {}", attempt, keys.size(), shared ? "read" : "write", keys);
174                int acquiredLockCount = 0;
175                try {
176                    if (attempt > 1) {
177                        Thread.sleep(retryWait);
178                    }
179                    for (String key : keys) {
180                        NamedLock namedLock = namedLockFactory.getLock(key);
181                        LOGGER.trace("Acquiring {} lock for '{}'", shared ? "read" : "write", key);
182
183                        boolean locked;
184                        if (shared) {
185                            locked = namedLock.lockShared(time, timeUnit);
186                        } else {
187                            locked = namedLock.lockExclusively(time, timeUnit);
188                        }
189
190                        if (!locked) {
191                            String timeStr = time + " " + timeUnit;
192                            LOGGER.trace(
193                                    "Failed to acquire {} lock for '{}' in {}",
194                                    shared ? "read" : "write",
195                                    key,
196                                    timeStr);
197
198                            namedLock.close();
199                            closeAll();
200                            illegalStateExceptions.add(new IllegalStateException(
201                                    "Attempt " + attempt + ": Could not acquire " + (shared ? "read" : "write")
202                                            + " lock for '" + namedLock.name() + "' in " + timeStr));
203                            break;
204                        } else {
205                            locks.push(namedLock);
206                            acquiredLockCount++;
207                        }
208                    }
209                } catch (InterruptedException e) {
210                    Thread.currentThread().interrupt();
211                    throw new RuntimeException(e);
212                }
213                LOGGER.trace("Attempt {}: Total locks acquired: {}", attempt, acquiredLockCount);
214                if (acquiredLockCount == keys.size()) {
215                    break;
216                }
217            }
218            if (!illegalStateExceptions.isEmpty()) {
219                IllegalStateException ex = new IllegalStateException("Could not acquire lock(s)");
220                illegalStateExceptions.forEach(ex::addSuppressed);
221                throw namedLockFactory.onFailure(ex);
222            }
223        }
224
225        private void closeAll() {
226            if (locks.isEmpty()) {
227                return;
228            }
229
230            // Release locks in reverse insertion order
231            int released = 0;
232            while (!locks.isEmpty()) {
233                try (NamedLock namedLock = locks.pop()) {
234                    LOGGER.trace("Releasing {} lock for '{}'", shared ? "read" : "write", namedLock.name());
235                    namedLock.unlock();
236                    released++;
237                }
238            }
239            LOGGER.trace("Total locks released: {}", released);
240        }
241
242        @Override
243        public void close() {
244            closeAll();
245        }
246    }
247}