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.ipc;
20  
21  import java.util.ArrayDeque;
22  import java.util.Collection;
23  import java.util.Objects;
24  import java.util.concurrent.TimeUnit;
25  import java.util.concurrent.TimeoutException;
26  
27  import org.eclipse.aether.named.NamedLockKey;
28  import org.eclipse.aether.named.support.LockUpgradeNotSupportedException;
29  import org.eclipse.aether.named.support.NamedLockSupport;
30  
31  /**
32   * IPC named locks.
33   *
34   * @since 2.0.1
35   */
36  class IpcNamedLock extends NamedLockSupport {
37  
38      final IpcClient client;
39      final Collection<String> keys;
40      final ThreadLocal<ArrayDeque<Ctx>> contexts;
41  
42      IpcNamedLock(NamedLockKey key, IpcNamedLockFactory factory, IpcClient client, Collection<String> keys) {
43          super(key, factory);
44          this.client = client;
45          this.keys = keys;
46          this.contexts = ThreadLocal.withInitial(ArrayDeque::new);
47      }
48  
49      @Override
50      public boolean doLockShared(long time, TimeUnit unit) {
51          ArrayDeque<Ctx> contexts = this.contexts.get();
52          if (!contexts.isEmpty()) {
53              contexts.push(new Ctx(false, null, true));
54              return true;
55          }
56          try {
57              String contextId = client.newContext(true, time, unit);
58              try {
59                  client.lock(Objects.requireNonNull(contextId), keys, time, unit);
60              } catch (TimeoutException e) {
61                  tryUnlock(contextId);
62                  return false;
63              }
64              contexts.push(new Ctx(true, contextId, true));
65              return true;
66          } catch (TimeoutException e) {
67              return false;
68          }
69      }
70  
71      @Override
72      public boolean doLockExclusively(long time, TimeUnit unit) {
73          ArrayDeque<Ctx> contexts = this.contexts.get();
74          if (!contexts.isEmpty()) {
75              // boxing: the outermost context already holds the server-side lock; only a shared-only stack is an
76              // upgrade, as in the other named lock implementations
77              if (contexts.stream().allMatch(c -> c.shared)) {
78                  throw new LockUpgradeNotSupportedException(this);
79              }
80              contexts.push(new Ctx(false, null, false));
81              return true;
82          }
83          try {
84              String contextId = client.newContext(false, time, unit);
85              try {
86                  client.lock(Objects.requireNonNull(contextId), keys, time, unit);
87              } catch (TimeoutException e) {
88                  tryUnlock(contextId);
89                  return false;
90              }
91              contexts.push(new Ctx(true, contextId, false));
92              return true;
93          } catch (TimeoutException e) {
94              return false;
95          }
96      }
97  
98      @Override
99      public void doUnlock() {
100         ArrayDeque<Ctx> contexts = this.contexts.get();
101         if (contexts.isEmpty()) {
102             throw new IllegalStateException("improper boxing");
103         }
104         Ctx ctx = contexts.pop();
105         if (ctx.acted) {
106             client.unlock(ctx.contextId);
107         }
108     }
109 
110     private void tryUnlock(String contextId) {
111         try {
112             client.unlock(contextId);
113         } catch (Exception e) {
114             // Best-effort cleanup: if unlock fails during timeout handling,
115             // we must not propagate the exception — doing so would crash the
116             // calling thread and skip latch countdowns in tests, causing hangs.
117             logger.warn("Unable to unlock (contextId = {}) after timeout", contextId, e);
118         }
119     }
120 
121     private record Ctx(boolean acted, String contextId, boolean shared) {}
122 }