@Override public LockDescriptor getDescriptor() { return StringLockDescriptor.of("ImmutableTimestamp:" + timestamp); } }
protected LockAndUnlockContendedBenchmark(TimelockService timelock, int numClients, int numRequestsPerClient, int numDistinctLocks) { super(numClients, numRequestsPerClient); this.timelock = timelock; List<LockDescriptor> descriptors = Lists.newArrayListWithExpectedSize(numDistinctLocks); for (int i = 0; i < numDistinctLocks; i++) { descriptors.add(StringLockDescriptor.of(UUID.randomUUID().toString())); } lockDescriptors = ImmutableList.copyOf(descriptors); }
@Override public LockToken lock(String client, String lockName) throws InterruptedException { LockRequest lockRequest = LockRequest.of( ImmutableSet.of(StringLockDescriptor.of(lockName)), Long.MAX_VALUE, client); LockResponse lockResponse = timelockService.lock(lockRequest); Preconditions.checkState(lockResponse.wasSuccessful(), "Jepsen failed to lock a lock, but it would wait for Long.MAX_VALUE, so this is unexpected."); return lockResponse.getToken(); }
public static ImmutableSortedMap<LockDescriptor, LockMode> getLockDescriptorLockMode(List<String> descriptors) { ImmutableSortedMap.Builder<LockDescriptor, LockMode> builder = ImmutableSortedMap.naturalOrder(); for (String descriptor : descriptors) { LockDescriptor descriptor1 = StringLockDescriptor.of(descriptor); builder.put(descriptor1, LockMode.WRITE); } return builder.build(); } }
@BeforeClass public static void oneTimeSetup() throws Exception { lock = StringLockDescriptor.of("lock"); moduleFactory = new AtlasDbServicesFactory() { @Override public TestAtlasDbServices connect(ServicesConfigModule servicesConfigModule) { return DaggerTestAtlasDbServices.builder() .servicesConfigModule(servicesConfigModule) .build(); } }; }
public static Optional<TargetedSweeperLock> tryAcquire(int shard, TableMetadataPersistence.SweepStrategy strategy, TimelockService timeLock) { ShardAndStrategy shardStrategy = ShardAndStrategy.of(shard, strategy); LockDescriptor lock = StringLockDescriptor.of(shardStrategy.toText()); // We do not want the timeout to be too low to avoid a race condition where we give up too soon LockRequest request = LockRequest.of(ImmutableSet.of(lock), 100L); return timeLock.lock(request) .getTokenOrEmpty() .map(lockToken -> new TargetedSweeperLock(shardStrategy, timeLock, lockToken)); }
@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")); }
@Override public LockRefreshToken lock(String client, String lockName) throws InterruptedException { LockDescriptor descriptor = StringLockDescriptor.of(lockName); LockRequest request = LockRequest.builder(ImmutableSortedMap.of(descriptor, LockMode.WRITE)) .doNotBlock() .build(); return lockService.lock(client, request); }
@Test public void testSerialisationAndDeserialisationOfLockResponseWithLockHolders() throws Exception { HeldLocksToken token = LockServiceTestUtils.getFakeHeldLocksToken("client A", "Fake thread", new BigInteger("1"), "held-lock-1", "logger-lock"); Map<LockDescriptor, LockClient> lockHolders = ImmutableMap.of(StringLockDescriptor.of("lock_id"), LockClient.ANONYMOUS, StringLockDescriptor.of("lock_id2"), LockClient.of("client")); LockResponse response = new LockResponse(token, lockHolders); ObjectMapper mapper = new ObjectMapper(); LockResponse deserializedLockResponse = mapper.readValue(mapper.writeValueAsString(response), LockResponse.class); assertEquals(lockHolders, deserializedLockResponse.getLockHolders()); }
public void lockOrRefresh() throws InterruptedException { if (token != null) { Set<LockRefreshToken> refreshedTokens = lockService.refreshLockRefreshTokens(ImmutableList.of(token)); log.info("Refreshed an existing lock token for {} in a single lock service (token {}); got {}", getLockIdLoggingArg(), SafeArg.of("existingLockToken", token), SafeArg.of("refreshedTokens", refreshedTokens)); if (refreshedTokens.isEmpty()) { token = null; } } else { LockDescriptor lock = StringLockDescriptor.of(lockId); LockRequest request = LockRequest.builder( ImmutableSortedMap.of(lock, LockMode.WRITE)).doNotBlock().build(); token = lockService.lock(LockClient.ANONYMOUS.getClientId(), request); log.info("Attempted to acquire the lock {} in a single lock service; got {}", getLockIdLoggingArg(), SafeArg.of("acquiredToken", token)); } }
@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 void testEncodedLockDescriptors(String lockId) { assertThat(StringLockDescriptor.of(lockId).toString(), equalTo(expectedEncodedLockDescriptorToString(lockId))); testEncodedLockId(stringToBytes(lockId)); }
@Override public LockDescriptor getDescriptor() { return StringLockDescriptor.of("ImmutableTimestamp:" + timestamp); } }
private void testAsciiLockDescriptors(String lockId) { assertThat(StringLockDescriptor.of(lockId).toString(), equalTo(expectedLockDescriptorToString(lockId))); assertThat(ByteArrayLockDescriptor.of(stringToBytes(lockId)).toString(), equalTo(expectedLockDescriptorToString(lockId))); }
/** Sets up the tests. */ @Before public void setUp() { readWriteLock = new LockServerLock(StringLockDescriptor.of("lock"), new LockClientIndices()); anonymousReadLock = readWriteLock.get(LockClient.ANONYMOUS, LockMode.READ); anonymousWriteLock = readWriteLock.get(LockClient.ANONYMOUS, LockMode.WRITE); knownClientReadLock = readWriteLock.get(client, LockMode.READ); knownClientWriteLock = readWriteLock.get(client, LockMode.WRITE); barrier = new CyclicBarrier(2); }
@Test public void resilientToValidJsonBodyThatIsNotASerializableError() throws JsonProcessingException { LockRequest lockRequest = LockRequest.builder(ImmutableSortedMap.of(StringLockDescriptor.of(LOCK_ID), LockMode.WRITE)) .build(); Response response = createResponseForEntity(lockRequest); assertCanDecodeRuntimeException(response); }
@Before public void setUp() { server = LockRefreshingLockService.create(LockServiceImpl.create(LockServerOptions.builder() .isStandaloneServer(false) .build())); lock1 = StringLockDescriptor.of("lock1"); }
lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.READ); } else { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.WRITE); lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.READ); for (int i = 0; i < numLocks; ++i) { if (i % 2 == 0) { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.READ); lockMap = Maps.newTreeMap(); for (int i = 0; i < numLocks; ++i) { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.WRITE); for (int i = 0; i < numLocks; ++i) { if (i % 2 != 0) { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.WRITE); for (int i = 0; i < numLocks; ++i) { if (i % 2 == 0) { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.WRITE); } else { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.READ); for (int i = 0; i < numLocks; ++i) { if (i % 2 != 0) { lockMap.put(StringLockDescriptor.of("lock " + i), LockMode.READ);