private Builder(SortedMap<LockDescriptor, LockMode> lockMap) { this(LockCollections.of(lockMap)); }
/** * These grants should not be constructed by users. Only the lock service should hand them out. */ public HeldLocksGrant(BigInteger grantId) { this.grantId = Preconditions.checkNotNull(grantId, "grantId should not be null"); creationDateMs = System.currentTimeMillis(); expirationDateMs = -1; lockMap = LockCollections.of(); lockTimeout = null; versionId = null; }
public static HeldLocksToken getFakeHeldLocksToken(String clientName, String requestingThread, BigInteger tokenId, String... descriptors) { ImmutableSortedMap<LockDescriptor, LockMode> lockDescriptorLockMode = getLockDescriptorLockMode(Arrays.asList(descriptors)); return new HeldLocksToken(tokenId, LockClient.of(clientName), System.currentTimeMillis(), System.currentTimeMillis(), LockCollections.of(lockDescriptorLockMode), LockRequest.getDefaultLockTimeout(), 0L, requestingThread); }
@JsonCreator SerializationProxy(@JsonProperty("grantId") BigInteger grantId, @JsonProperty("creationDateMs") long creationDateMs, @JsonProperty("expirationDateMs") long expirationDateMs, @JsonProperty("locksWithMode") List<LockWithMode> lockMap, @JsonProperty("lockTimeout") SimpleTimeDuration lockTimeout, @JsonProperty("versionId") Long versionId) { this.grantId = grantId; this.creationDateMs = creationDateMs; this.expirationDateMs = expirationDateMs; ImmutableSortedMap.Builder<LockDescriptor, LockMode> localLockMapBuilder = ImmutableSortedMap.naturalOrder(); for (LockWithMode lock : lockMap) { localLockMapBuilder.put(lock.getLockDescriptor(), lock.getLockMode()); } this.lockMap = LockCollections.of(localLockMapBuilder.build()); this.lockTimeout = lockTimeout; this.versionId = versionId; }
@JsonCreator SerializationProxy(@JsonProperty("locks") List<LockWithMode> locks, @JsonProperty("lockTimeout") TimeDuration lockTimeout, @JsonProperty("lockGroupBehavior") LockGroupBehavior lockGroupBehavior, @JsonProperty("blockingMode") BlockingMode blockingMode, @JsonProperty("blockingDuration") TimeDuration blockingDuration, @JsonProperty("versionId") Long versionId, @JsonProperty("creatingThreadName") String creatingThreadName) { ImmutableSortedMap.Builder<LockDescriptor, LockMode> localLockMapBuilder = ImmutableSortedMap.naturalOrder(); for (LockWithMode lock : locks) { localLockMapBuilder.put(lock.getLockDescriptor(), lock.getLockMode()); } this.lockMap = LockCollections.of(localLockMapBuilder.build()); this.lockTimeout = lockTimeout; this.lockGroupBehavior = lockGroupBehavior; this.blockingMode = blockingMode; this.blockingDuration = blockingDuration; this.versionId = versionId; this.creatingThreadName = creatingThreadName; }
@JsonCreator SerializationProxy(@JsonProperty("tokenId") BigInteger tokenId, @JsonProperty("client") LockClient client, @JsonProperty("creationDateMs") long creationDateMs, @JsonProperty("expirationDateMs") long expirationDateMs, @JsonProperty("locks") List<LockWithMode> locks, @JsonProperty("lockTimeout") TimeDuration lockTimeout, @JsonProperty("versionId") Long versionId, @JsonProperty("requestingThread") String requestingThread) { ImmutableSortedMap.Builder<LockDescriptor, LockMode> localLockMapBuilder = ImmutableSortedMap.naturalOrder(); for (LockWithMode lock : locks) { localLockMapBuilder.put(lock.getLockDescriptor(), lock.getLockMode()); } this.lockMap = LockCollections.of(localLockMapBuilder.build()); this.tokenId = Preconditions.checkNotNull(tokenId, "tokenId"); this.client = Preconditions.checkNotNull(client, "client"); this.creationDateMs = creationDateMs; this.expirationDateMs = expirationDateMs; this.lockTimeout = SimpleTimeDuration.of(lockTimeout); this.versionId = versionId; this.requestingThread = requestingThread; Preconditions.checkArgument(!this.lockMap.isEmpty()); }
@Override public Set<LockRefreshToken> refreshLockRefreshTokens(Iterable<LockRefreshToken> tokens) { List<HeldLocksToken> fakeTokens = Lists.newArrayList(); LockDescriptor fakeLockDesc = StringLockDescriptor.of("refreshLockRefreshTokens"); SortedLockCollection<LockDescriptor> fakeLockSet = LockCollections.of(ImmutableSortedMap.of(fakeLockDesc, LockMode.READ)); for (LockRefreshToken token : tokens) { fakeTokens.add(new HeldLocksToken( token.getTokenId(), LockClient.ANONYMOUS, 0L, 0L, fakeLockSet, maxAllowedLockTimeout, 0L, "UnknownThread-refreshLockRefreshTokens")); } return ImmutableSet.copyOf(Iterables.transform(refreshTokens(fakeTokens), HeldLocksTokens.getRefreshTokenFun())); }
@Override public boolean unlockSimple(SimpleHeldLocksToken token) { Preconditions.checkNotNull(token); LockDescriptor fakeLockDesc = StringLockDescriptor.of("unlockSimple"); SortedLockCollection<LockDescriptor> fakeLockSet = LockCollections.of(ImmutableSortedMap.of(fakeLockDesc, LockMode.READ)); return unlock(new HeldLocksToken( token.getTokenId(), LockClient.ANONYMOUS, token.getCreationDateMs(), 0L, fakeLockSet, maxAllowedLockTimeout, 0L, "UnknownThread-unlockSimple")); }
@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); }
@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)); }
private Builder(SortedMap<LockDescriptor, LockMode> lockMap) { this(LockCollections.of(lockMap)); }
LockCollections.of(ImmutableSortedMap.of(DESCRIPTOR_1, LockMode.WRITE))) .blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)) .build(); LockRequest request2 = LockRequest.builder( LockCollections.of(ImmutableSortedMap.of(DESCRIPTOR_2, LockMode.WRITE))) .blockForAtMost(SimpleTimeDuration.of(1000, TimeUnit.MILLISECONDS)) .build(); "held-lock-3"); heldLocksTokenMap.putIfAbsent(token, LockServiceImpl.HeldLocks.of(token, LockCollections.of())); heldLocksTokenMap.putIfAbsent(token2, LockServiceImpl.HeldLocks.of(token2, LockCollections.of()));
/** * These grants should not be constructed by users. Only the lock service should hand them out. */ public HeldLocksGrant(BigInteger grantId) { this.grantId = Preconditions.checkNotNull(grantId, "grantId should not be null"); creationDateMs = System.currentTimeMillis(); expirationDateMs = -1; lockMap = LockCollections.of(); lockTimeout = null; versionId = null; }
private HeldLocksToken getExpiredHeldLocksToken() { ImmutableSortedMap.Builder<LockDescriptor, LockMode> builder = ImmutableSortedMap.naturalOrder(); builder.put( AtlasRowLockDescriptor.of( TransactionConstants.TRANSACTION_TABLE.getQualifiedName(), TransactionConstants.getValueForTimestamp(0L)), LockMode.WRITE); long creationDateMs = System.currentTimeMillis(); long expirationDateMs = creationDateMs - 1; TimeDuration lockTimeout = SimpleTimeDuration.of(0, TimeUnit.SECONDS); long versionId = 0L; return new HeldLocksToken( BigInteger.ZERO, lockClient, creationDateMs, expirationDateMs, LockCollections.of(builder.build()), lockTimeout, versionId, "Dummy thread"); }
Assert.assertNotNull(readWriteToken); Assert.assertEquals(client, readWriteToken.getClient()); Assert.assertEquals(LockCollections.of(ImmutableSortedMap.copyOf(lockMap)), readWriteToken.getLockDescriptors()); Assert.assertNotNull(token); Assert.assertEquals(client, token.getClient()); Assert.assertEquals(LockCollections.of(ImmutableSortedMap.copyOf(lockMap)), token.getLockDescriptors()); Assert.assertEquals(LockCollections.of(ImmutableSortedMap.copyOf(lockMap)), token.getLockDescriptors()); server.unlock(token); Assert.assertEquals(LockCollections.of(ImmutableSortedMap.copyOf(lockMap)), token.getLockDescriptors()); server.unlock(token);
@JsonCreator SerializationProxy(@JsonProperty("locks") List<LockWithMode> locks, @JsonProperty("lockTimeout") TimeDuration lockTimeout, @JsonProperty("lockGroupBehavior") LockGroupBehavior lockGroupBehavior, @JsonProperty("blockingMode") BlockingMode blockingMode, @JsonProperty("blockingDuration") TimeDuration blockingDuration, @JsonProperty("versionId") Long versionId, @JsonProperty("creatingThreadName") String creatingThreadName) { ImmutableSortedMap.Builder<LockDescriptor, LockMode> localLockMapBuilder = ImmutableSortedMap.naturalOrder(); for (LockWithMode lock : locks) { localLockMapBuilder.put(lock.getLockDescriptor(), lock.getLockMode()); } this.lockMap = LockCollections.of(localLockMapBuilder.build()); this.lockTimeout = lockTimeout; this.lockGroupBehavior = lockGroupBehavior; this.blockingMode = blockingMode; this.blockingDuration = blockingDuration; this.versionId = versionId; this.creatingThreadName = creatingThreadName; }
@JsonCreator SerializationProxy(@JsonProperty("grantId") BigInteger grantId, @JsonProperty("creationDateMs") long creationDateMs, @JsonProperty("expirationDateMs") long expirationDateMs, @JsonProperty("locksWithMode") List<LockWithMode> lockMap, @JsonProperty("lockTimeout") SimpleTimeDuration lockTimeout, @JsonProperty("versionId") Long versionId) { this.grantId = grantId; this.creationDateMs = creationDateMs; this.expirationDateMs = expirationDateMs; ImmutableSortedMap.Builder<LockDescriptor, LockMode> localLockMapBuilder = ImmutableSortedMap.naturalOrder(); for (LockWithMode lock : lockMap) { localLockMapBuilder.put(lock.getLockDescriptor(), lock.getLockMode()); } this.lockMap = LockCollections.of(localLockMapBuilder.build()); this.lockTimeout = lockTimeout; this.versionId = versionId; }
@JsonCreator SerializationProxy(@JsonProperty("tokenId") BigInteger tokenId, @JsonProperty("client") LockClient client, @JsonProperty("creationDateMs") long creationDateMs, @JsonProperty("expirationDateMs") long expirationDateMs, @JsonProperty("locks") List<LockWithMode> locks, @JsonProperty("lockTimeout") TimeDuration lockTimeout, @JsonProperty("versionId") Long versionId, @JsonProperty("requestingThread") String requestingThread) { ImmutableSortedMap.Builder<LockDescriptor, LockMode> localLockMapBuilder = ImmutableSortedMap.naturalOrder(); for (LockWithMode lock : locks) { localLockMapBuilder.put(lock.getLockDescriptor(), lock.getLockMode()); } this.lockMap = LockCollections.of(localLockMapBuilder.build()); this.tokenId = Preconditions.checkNotNull(tokenId, "tokenId"); this.client = Preconditions.checkNotNull(client, "client"); this.creationDateMs = creationDateMs; this.expirationDateMs = expirationDateMs; this.lockTimeout = SimpleTimeDuration.of(lockTimeout); this.versionId = versionId; this.requestingThread = requestingThread; Preconditions.checkArgument(!this.lockMap.isEmpty()); }
Assert.assertNotNull(token); Assert.assertEquals(LockClient.ANONYMOUS, token.getClient()); Assert.assertEquals(LockCollections.of(ImmutableSortedMap.of(lock1, LockMode.WRITE)), token.getLockDescriptors()); try { server.unlockAndFreeze(token); Assert.assertNotNull(token2); Assert.assertEquals(client, token2.getClient()); Assert.assertEquals(LockCollections.of(ImmutableSortedMap.of(lock1, LockMode.WRITE)), token2.getLockDescriptors()); server.unlockAndFreeze(token2); token2 = server.lockWithFullLockResponse(client, request).getToken();