Object readResolve() { return of(time, unit); } }
public String toString(long currentTimeMillis) { return MoreObjects.toStringHelper(getClass().getSimpleName()) .add("grantId", grantId.toString(Character.MAX_RADIX)) .add("createdAt", SimpleTimeDuration.of(creationDateMs, TimeUnit.MILLISECONDS)) .add("expiresIn", SimpleTimeDuration.of(expirationDateMs, TimeUnit.MILLISECONDS)) .add("lockCount", lockMap.size()) .add("firstLock", lockMap.entries().iterator().next()) .add("versionId", versionId) .toString(); }
/** * Returns the maximum amount of time that can be passed to * {@link LockRequest.Builder#timeoutAfter(TimeDuration)}. The default value * is 10 minutes. */ @Value.Default public TimeDuration getMaxAllowedLockTimeout() { return SimpleTimeDuration.of(10, TimeUnit.MINUTES); }
/** * Returns the maximum permitted clock drift between the server and any * client. The default value is 5 seconds. */ @Value.Default public TimeDuration getMaxAllowedClockDrift() { return SimpleTimeDuration.of(5, TimeUnit.SECONDS); }
/** * Returns the maximum amount of time a lock is usually held for. * The default value is 1 hour. */ @Value.Default public TimeDuration getMaxNormalLockAge() { return SimpleTimeDuration.of(1, TimeUnit.HOURS); }
/** * Returns the maximum amount of time that may be passed to * {@link LockRequest.Builder#blockForAtMost(TimeDuration)}. The default * value is 60 seconds. * * @deprecated this value is no longer used or respected. */ @Deprecated @Value.Default public TimeDuration getMaxAllowedBlockingDuration() { return SimpleTimeDuration.of(60, TimeUnit.SECONDS); }
public String toString(long currentTimeMillis) { return MoreObjects.toStringHelper(getClass().getSimpleName()) .add("tokenId", tokenId.toString(Character.MAX_RADIX)) .add("client", client) .add("createdAt", SimpleTimeDuration.of(creationDateMs, TimeUnit.MILLISECONDS)) .add("expiresIn", SimpleTimeDuration.of(expirationDateMs - currentTimeMillis, TimeUnit.MILLISECONDS)) .add("lockCount", lockMap.size()) .add("firstLock", lockMap.entries().iterator().next()) .add("versionId", versionId) .add("requestingThread", requestingThread) .toString(); }
/** * These grants should not be constructed by users. Only the lock service should hand them out. */ public HeldLocksGrant(BigInteger grantId, long creationDateMs, long expirationDateMs, SortedLockCollection<LockDescriptor> lockMap, TimeDuration lockTimeout, @Nullable Long versionId) { this.grantId = Preconditions.checkNotNull(grantId, "grantId should not be null"); this.creationDateMs = creationDateMs; this.expirationDateMs = expirationDateMs; this.lockMap = lockMap; this.lockTimeout = SimpleTimeDuration.of(lockTimeout); this.versionId = versionId; Preconditions.checkArgument(!this.lockMap.isEmpty()); }
/** * Instructs the lock server to release these locks if a refresh request * has not been received for the period of time represented by the * <code>lockTimeout</code> parameter. The default value is controlled * by {@link #LockRequest#getDefaultLockTimeout()}. * You may not call this method multiple times. */ public Builder timeoutAfter(TimeDuration newLockTimeout) { Preconditions.checkArgument(newLockTimeout.toMillis() > 0); if ((lockMap == null) || (this.lockTimeout != null)) { throw new IllegalStateException(); } this.lockTimeout = SimpleTimeDuration.of(newLockTimeout); return this; }
/** * This should only be created by the Lock Service. */ public HeldLocksToken(BigInteger tokenId, LockClient client, long creationDateMs, long expirationDateMs, SortedLockCollection<LockDescriptor> lockMap, TimeDuration lockTimeout, @Nullable Long versionId, String requestingThread) { this.tokenId = Preconditions.checkNotNull(tokenId, "tokenId should not be null"); this.client = Preconditions.checkNotNull(client, "client should not be null"); this.creationDateMs = creationDateMs; this.expirationDateMs = expirationDateMs; this.lockMap = lockMap; this.lockTimeout = SimpleTimeDuration.of(lockTimeout); this.versionId = versionId; this.requestingThread = requestingThread; Preconditions.checkArgument(!this.lockMap.isEmpty()); }
SerializationProxy(LockServerOptions lockServerOptions) { isStandaloneServer = lockServerOptions.isStandaloneServer(); maxAllowedLockTimeout = SimpleTimeDuration.of( lockServerOptions.getMaxAllowedLockTimeout()); maxAllowedClockDrift = SimpleTimeDuration.of( lockServerOptions.getMaxAllowedClockDrift()); maxAllowedBlockingDuration = SimpleTimeDuration.of( lockServerOptions.getMaxAllowedBlockingDuration()); maxNormalLockAge = SimpleTimeDuration.of( lockServerOptions.getMaxNormalLockAge()); randomBitCount = lockServerOptions.getRandomBitCount(); lockStateLoggerDir = lockServerOptions.getLockStateLoggerDir(); slowLogTriggerMillis = lockServerOptions.slowLogTriggerMillis(); }
private LockServiceImpl(LockServerOptions options, Runnable callOnClose) { Preconditions.checkNotNull(options); this.callOnClose = callOnClose; isStandaloneServer = options.isStandaloneServer(); maxAllowedLockTimeout = SimpleTimeDuration.of(options.getMaxAllowedLockTimeout()); maxAllowedClockDrift = SimpleTimeDuration.of(options.getMaxAllowedClockDrift()); maxNormalLockAge = SimpleTimeDuration.of(options.getMaxNormalLockAge()); lockStateLoggerDir = options.getLockStateLoggerDir(); slowLogTriggerMillis = options.slowLogTriggerMillis(); executor.execute(() -> { Thread.currentThread().setName("Held Locks Token Reaper"); reapLocks(lockTokenReaperQueue, heldLocksTokenMap); }); executor.execute(() -> { Thread.currentThread().setName("Held Locks Grant Reaper"); reapLocks(lockGrantReaperQueue, heldLocksGrantMap); }); }
/** * Instructs the lock server to block for at most the given duration * when acquiring locks. By default, the lock server will block * indefinitely. You may not call this method multiple times, and you * may not call both {@link #doNotBlock()} and this method. */ public Builder blockForAtMost(TimeDuration newBlockingDuration) { Preconditions.checkNotNull(newBlockingDuration, "newBlockingDuration should not be null"); TimeDuration realBlockingDuration = SimpleTimeDuration.of(newBlockingDuration); Preconditions.checkArgument(realBlockingDuration.toNanos() >= 0); if (realBlockingDuration.toNanos() == 0) { return doNotBlock(); } if ((lockMap == null) || (blockingMode != null)) { throw new IllegalStateException(); } blockingMode = BlockingMode.BLOCK_UNTIL_TIMEOUT; this.blockingDuration = realBlockingDuration; return this; }
private static HeldLocksToken getHeldLocksToken(BigInteger tokenId) { long creationDateMs = System.currentTimeMillis(); long expirationDateMs = creationDateMs - 1; TimeDuration lockTimeout = SimpleTimeDuration.of(0, TimeUnit.SECONDS); long versionId = 0L; return new HeldLocksToken( tokenId, LockClient.of("fake lock client"), creationDateMs, expirationDateMs, LOCK_DESCRIPTORS, lockTimeout, versionId, "Dummy thread"); } }
@Test public void testSerialisationAndDeserialisationOfHeldLocksGrant() throws Exception { ImmutableSortedMap<LockDescriptor, LockMode> lockDescriptorLockMode = LockServiceTestUtils .getLockDescriptorLockMode(ImmutableList.of("lock1", "lock2")); HeldLocksGrant heldLocksGrant = new HeldLocksGrant( BigInteger.ONE, System.currentTimeMillis(), System.currentTimeMillis() + 10L, LockCollections.of(lockDescriptorLockMode), SimpleTimeDuration.of(100, TimeUnit.SECONDS), 10L); ObjectMapper mapper = new ObjectMapper(); String serializedForm = mapper.writeValueAsString(heldLocksGrant); HeldLocksGrant deserialzedlockServerOptions = mapper.readValue(serializedForm, HeldLocksGrant.class); assertEquals(heldLocksGrant, deserialzedlockServerOptions); }
private com.palantir.lock.LockRequest toLegacyLockRequest(LockRequest request) { SortedMap<LockDescriptor, LockMode> locks = buildLockMap(request.getLockDescriptors(), LockMode.WRITE); return com.palantir.lock.LockRequest.builder(locks) .blockForAtMost(SimpleTimeDuration.of(request.getAcquireTimeoutMs(), TimeUnit.MILLISECONDS)) .build(); }
@Test public void testSerializationDeserialization() throws Exception { SortedMap<LockDescriptor, LockMode> lockMap = ImmutableSortedMap.of(StringLockDescriptor.of("foo"), LockMode.READ); HeldLocksToken heldLocksToken = new HeldLocksToken( BigInteger.valueOf(0), LockClient.of("foo"), 0, 0, LockCollections.of(lockMap), SimpleTimeDuration.of(1, TimeUnit.SECONDS), 0L, "Dummy Thread"); HeldLocksToken deserializedHeldLocksToken = mapper.readValue(mapper.writeValueAsString(heldLocksToken), HeldLocksToken.class); assertThat(deserializedHeldLocksToken, is(heldLocksToken)); }
@Test public void testSimpleRefresh() throws InterruptedException { Builder builder = LockRequest.builder(ImmutableSortedMap.of(lock1, LockMode.WRITE)); builder.timeoutAfter(SimpleTimeDuration.of(5, TimeUnit.SECONDS)); LockResponse lock = server.lockWithFullLockResponse(LockClient.ANONYMOUS, builder.build()); Thread.sleep(10000); Set<HeldLocksToken> refreshTokens = server.refreshTokens(ImmutableList.of(lock.getToken())); Assert.assertEquals(1, refreshTokens.size()); } }
@Test public void lockDelegatesToLockService() throws InterruptedException { com.palantir.lock.LockRequest legacyRequest = com.palantir.lock.LockRequest.builder(buildLockMap(LockMode.WRITE)) .blockForAtMost(SimpleTimeDuration.of(TIMEOUT, TimeUnit.MILLISECONDS)) .build(); when(lockService.lock(LockClient.ANONYMOUS.getClientId(), legacyRequest)).thenReturn(LOCK_REFRESH_TOKEN); assertEquals(LockResponse.successful(LOCK_TOKEN_V2), timelock.lock( LockRequest.of(ImmutableSet.of(LOCK_A, LOCK_B), TIMEOUT))); verify(lockService).lock(LockClient.ANONYMOUS.getClientId(), legacyRequest); }
@Test public void setsGlobalDefaultLockTimeout() { TimeDuration expectedTimeout = SimpleTimeDuration.of(47, TimeUnit.SECONDS); AtlasDbConfig atlasDbConfig = ImmutableAtlasDbConfig.builder() .keyValueService(new InMemoryAtlasDbConfig()) .defaultLockTimeoutSeconds((int) expectedTimeout.getTime()) .build(); TransactionManagers.builder() .config(atlasDbConfig) .userAgent("test") .globalMetricsRegistry(new MetricRegistry()) .globalTaggedMetricRegistry(DefaultTaggedMetricRegistry.getDefault()) .registrar(environment) .build() .serializable(); assertEquals(expectedTimeout, LockRequest.getDefaultLockTimeout()); LockRequest lockRequest = LockRequest .builder(ImmutableSortedMap.of(StringLockDescriptor.of("foo"), LockMode.WRITE)).build(); assertEquals(expectedTimeout, lockRequest.getLockTimeout()); }