private LockRefreshToken lockAnonymous(com.palantir.lock.LockRequest request) { try { return lockService.lock(LockClient.ANONYMOUS.getClientId(), request); } catch (InterruptedException ex) { Thread.currentThread().interrupt(); throw new RuntimeException(ex); } }
@Provides @Singleton public LockClient provideLockClient() { return LockClient.of("atlas instance"); }
@Test public void testPathParamSerDeOfLockClient() throws Exception { LockClient lockClient = LockClient.of("xyz"); String serializedForm = lockClient.toString(); LockClient lockClient1 = new LockClient(serializedForm); assertEquals(lockClient, lockClient1); }
@Override public Set<HeldLocksToken> getTokens(LockClient client) { Preconditions.checkNotNull(client); if (client.isAnonymous()) { throw new IllegalArgumentException("client must not be anonymous"); } else if (client.equals(INTERNAL_LOCK_GRANT_CLIENT)) { throw new IllegalArgumentException("Illegal client!"); } ImmutableSet.Builder<HeldLocksToken> tokens = ImmutableSet.builder(); synchronized (lockClientMultimap) { for (HeldLocksToken token : lockClientMultimap.get(client)) { @Nullable HeldLocks<HeldLocksToken> heldLocks = heldLocksTokenMap.get(token); if ((heldLocks != null) && !isFrozen(heldLocks.locks.getKeys())) { tokens.add(token); } } } ImmutableSet<HeldLocksToken> tokenSet = tokens.build(); if (log.isTraceEnabled()) { log.trace(".getTokens({}) returns {}", client, Iterables.transform(tokenSet, TOKEN_TO_ID)); } return tokenSet; }
@Test public void testSerializationDeserialization() throws Exception { LockClient lockClient = new LockClient("foo"); LockClient deserializedLockClient = mapper.readValue(mapper.writeValueAsString(lockClient), LockClient.class); assertThat(lockClient.getClientId(), is(deserializedLockClient.getClientId())); assertThat(lockClient.isAnonymous(), is(deserializedLockClient.isAnonymous())); }
String longString = String.copyValueOf(longChar); Assert.assertEquals(LockClient.of(null), LockClient.ANONYMOUS); Assert.assertEquals(LockClient.of(LockClient.ANONYMOUS.getClientId()), LockClient.ANONYMOUS); client = LockClient.of(longString); Assert.assertEquals(longString, client.getClientId()); try { lock = StringLockDescriptor.of(""); LockClient client2 = LockClient.of("another client"); HeldLocksToken token2 = server.lockWithFullLockResponse(client2, request2).getToken(); Assert.assertNull(token2);
@Test public void testPathParamSerDeOfAnonymousLockClient() throws Exception { LockClient lockClient = LockClient.ANONYMOUS; String serializedForm = lockClient.toString(); LockClient lockClient1 = new LockClient(serializedForm); assertEquals(lockClient, lockClient1); } }
/** * Returns a {@code LockClient} instance for the given client ID. * * Returns LockClient.ANONYMOUS if {@code clientId} is {@code null} or * the empty string */ public static LockClient of(String clientId) { if (Strings.isNullOrEmpty(clientId)) { return ANONYMOUS; } Preconditions.checkArgument(!clientId.equals(INTERNAL_LOCK_GRANT_CLIENT_ID)); return new LockClient(clientId); }
private Map<String, Object> synthesizeRequestState( Map<LockClient, Set<LockRequest>> outstandingLockRequests, Map<LockDescriptor, ClientAwareReadWriteLock> descriptorToLockMap) { LockServiceStateDebugger debugger = new LockServiceStateDebugger(outstandingLockRequests, descriptorToLockMap); Multimap<LockClient, LockServiceStateDebugger.LockRequestProgress> progressMultimap = debugger.getSuspectedLockProgress(); return nameObjectForYamlConversion(SYNTHESIZED_REQUEST_STATE_TITLE, progressMultimap.asMap().entrySet().stream() .collect(Collectors.toMap( entry -> entry.getKey().toString(), entry -> entry.getValue().stream().map( lockRequestProgress -> SanitizedLockRequestProgress.create( lockRequestProgress, lockDescriptorMapper, entry.getKey().toString())) .collect(Collectors.toList())))); }
if (client.isAnonymous()) { heldLocksTokenMap.put(token, heldLocks); lockTokenReaperQueue.add(token);
try { while (true) { LockClient client = LockClient.of("client" + clientID); LockRequest request; HeldLocksToken token; } else { numSuccess.set(numSuccess.get() + 1); Assert.assertEquals(Integer.toString(clientID), token.getClient().getClientId()); Assert.assertEquals(request.getLockDescriptors(), token.getLockDescriptors()); try {
/** * Returns a {@code LockClient} instance for the given client ID. * * Returns LockClient.ANONYMOUS if {@code clientId} is {@code null} or * the empty string */ public static LockClient of(String clientId) { if (Strings.isNullOrEmpty(clientId)) { return ANONYMOUS; } Preconditions.checkArgument(!clientId.equals(INTERNAL_LOCK_GRANT_CLIENT_ID)); return new LockClient(clientId); }
private void logLockAcquisitionFailure(Map<LockDescriptor, LockClient> failedLocks) { final String logMessage = "Current holders of the first {} of {} total failed locks were: {}"; List<String> lockDescriptions = Lists.newArrayList(); Iterator<Entry<LockDescriptor, LockClient>> entries = failedLocks.entrySet().iterator(); for (int i = 0; i < MAX_FAILED_LOCKS_TO_LOG && entries.hasNext(); i++) { Entry<LockDescriptor, LockClient> entry = entries.next(); lockDescriptions.add( String.format("Lock: %s, Holder: %s", entry.getKey().toString(), entry.getValue().toString())); } requestLogger.trace( logMessage, SafeArg.of("numLocksLogged", Math.min(MAX_FAILED_LOCKS_TO_LOG, failedLocks.size())), SafeArg.of("numLocksFailed", failedLocks.size()), UnsafeArg.of("lockDescriptions", lockDescriptions)); }
private HeldLocksToken createHeldLocksToken(LockClient client, SortedLockCollection<LockDescriptor> lockDescriptorMap, LockCollection<? extends ClientAwareReadWriteLock> heldLocksMap, TimeDuration lockTimeout, @Nullable Long versionId, String requestThread) { while (true) { BigInteger tokenId = new BigInteger(RANDOM_BIT_COUNT, randomPool.getSecureRandom()); long expirationDateMs = currentTimeMillis() + lockTimeout.toMillis(); HeldLocksToken token = new HeldLocksToken(tokenId, client, currentTimeMillis(), expirationDateMs, lockDescriptorMap, lockTimeout, versionId, requestThread); HeldLocks<HeldLocksToken> heldLocks = HeldLocks.of(token, heldLocksMap); if (heldLocksTokenMap.putIfAbsent(token, heldLocks) == null) { lockTokenReaperQueue.add(token); if (!client.isAnonymous()) { lockClientMultimap.put(client, token); } return token; } log.error("Lock ID collision! " + "Count of held tokens = {}" + "; random bit count = {}", SafeArg.of("heldTokenCount", heldLocksTokenMap.size()), SafeArg.of("randomBitCount", RANDOM_BIT_COUNT)); } }
Object readResolve() { if (Strings.isNullOrEmpty(clientId)) { return ANONYMOUS; } if (clientId.equals(INTERNAL_LOCK_GRANT_CLIENT_ID)) { return INTERNAL_LOCK_GRANT_CLIENT; } return of(clientId); } }
private long getImmutableTimestampInternal(long ts) { Long minLocked = lockService.getMinLockedInVersionId(immutableTsLockClient.getClientId()); return minLocked == null ? ts : minLocked; }
@Provides @Singleton public LockClient provideLockClient() { return LockClient.of("atlas instance"); }
private static HeldLocksToken acquireLock(LockService lockService, LockRequest lockRequest) { int failureCount = 0; while (true) { HeldLocksToken response; try { response = lockService.lockAndGetHeldLocks(LockClient.ANONYMOUS.getClientId(), lockRequest); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } if (response == null) { RuntimeException ex = new LockAcquisitionException( "Failed to lock using the provided lock request: " + lockRequest); log.warn("Could not lock successfully", ex); ++failureCount; if (failureCount >= NUM_RETRIES) { log.warn("Failing after {} tries", failureCount, ex); throw ex; } } else { return response; } } }
@Override public LockRefreshToken lock(String client, LockRequest request) throws InterruptedException { LockRefreshToken result = lock(LockClient.of(client), request); return result; }
private List<SimpleLockRequest> getDescriptorSimpleRequestMap(LockClient client, LockRequest request) { return request.getLocks().stream() .map(lock -> SimpleLockRequest.of(request, this.lockDescriptorMapper.getDescriptorMapping(lock.getLockDescriptor()), lock.getLockMode(), client.getClientId())) .collect(Collectors.toList()); }