@Override public String toString() { String lockIdAsString = getLockIdAsString(); return getClass().getSimpleName() + " [" + encodeToAscii(lockIdAsString) + "]"; }
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)); }
/** Returns a {@code LockDescriptor} instance for the given lock ID. */ @SuppressWarnings("checkstyle:jdkStandardCharsets") // StandardCharsets only in JDK 1.7+ public static LockDescriptor of(String lockId) { Preconditions.checkNotNull(lockId, "lockId should not be null"); Preconditions.checkArgument(!Strings.isNullOrEmpty(lockId)); return new LockDescriptor(lockId.getBytes(Charsets.UTF_8)); } }
private void assertDescriptorsNotPresentInFile(File logFile) { try { List<String> contents = Files.readAllLines(logFile.toPath()); String result = Strings.join(contents).with("\n"); assertThat(result).doesNotContain(DESCRIPTOR_1.getLockIdAsString()); assertThat(result).doesNotContain(DESCRIPTOR_2.getLockIdAsString()); } catch (IOException e) { throw new RuntimeException(e); } }
private LockRequest requestContaining(String table) { return argThat( argument -> argument != null && argument.getLockDescriptors().stream() .anyMatch(descriptor -> descriptor.getLockIdAsString().contains(table))); }
if (log.isDebugEnabled() || isSlowLogEnabled()) { long responseTimeMillis = System.currentTimeMillis() - startTime; logSlowLockAcquisition(entry.getKey().toString(), currentHolder, responseTimeMillis);
/** Tests identity operations */ @Test public void testIdentity() { Assert.assertEquals("a client", client.getClientId()); Assert.assertEquals("", LockClient.ANONYMOUS.getClientId()); Assert.assertEquals("lock1", lock1.getLockIdAsString()); Assert.assertEquals("lock2", lock2.getLockIdAsString()); }
@Override public String toString() { String lockIdAsString = getLockIdAsString(); return getClass().getSimpleName() + " [" + encodeToAscii(lockIdAsString) + "]"; }
/** Returns a {@code LockDescriptor} instance for the given table and row. */ public static LockDescriptor of(String tableName, byte[] rowName) { Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName)); Preconditions.checkNotNull(rowName, "rowName should not be null"); byte[] tableBytes = tableName.getBytes(); byte[] bytes = new byte[tableBytes.length + 1 + rowName.length]; System.arraycopy(tableBytes, 0, bytes, 0, tableBytes.length); System.arraycopy(rowName, 0, bytes, tableBytes.length + 1, rowName.length); return new LockDescriptor(bytes); } }
@Test public void testDescriptors() throws Exception { List<File> files = LockServiceTestUtils.logStateDirFiles(); Optional<File> descriptorsFile = files.stream().filter( file -> file.getName().startsWith(LockServiceStateLogger.DESCRIPTORS_FILE_PREFIX)).findFirst(); Map<String, String> descriptorsMap = new Yaml().loadAs(new FileInputStream(descriptorsFile.get()), Map.class); Set<LockDescriptor> allDescriptors = getAllDescriptors(); for (LockDescriptor descriptor : allDescriptors) { assertTrue("Existing descriptor can't be found in dumped descriptors", descriptorsMap.values().stream().anyMatch(descriptorFromFile -> descriptorFromFile.equals(descriptor.toString()))); } }
/** Tests that our objects have {@code toString()} methods defined. */ @Test public void testToStrings() { Assert.assertEquals("client", client.getClientId()); Assert.assertEquals("lock", readWriteLock.getDescriptor().getLockIdAsString()); assertGoodToString(readWriteLock); assertGoodToString(anonymousReadLock); assertGoodToString(knownClientWriteLock); }
/** Returns a {@code LockDescriptor} instance for the given table, row, and column. */ public static LockDescriptor of(String tableName, byte[] rowName, byte[] colName) { Preconditions.checkArgument(!Strings.isNullOrEmpty(tableName)); Preconditions.checkNotNull(rowName, "rowName should not be null"); Preconditions.checkNotNull(colName, "colName should not be null"); byte[] tableBytes = tableName.getBytes(); byte[] bytes = new byte[tableBytes.length + 1 + rowName.length + 1 + colName.length]; System.arraycopy(tableBytes, 0, bytes, 0, tableBytes.length); System.arraycopy(rowName, 0, bytes, tableBytes.length + 1, rowName.length); System.arraycopy(colName, 0, bytes, tableBytes.length + 1 + rowName.length + 1, colName.length); return new LockDescriptor(bytes); } }
Assert.assertEquals(longString, lock.getLockIdAsString());
/** Returns a {@code LockDescriptor} instance for the given lock ID. */ public static LockDescriptor of(byte[] bytes) { Preconditions.checkNotNull(bytes, "bytes cannot be null"); return new LockDescriptor(bytes.clone()); } }
private void testEncodedLockId(byte[] bytes) { assertThat(ByteArrayLockDescriptor.of(bytes).toString(), equalTo(expectedEncodedLockDescriptorToString(bytes))); }
/** Returns a {@code LockDescriptor} instance for the given table and row. */ public static LockDescriptor of(long timestamp) { return new LockDescriptor(Longs.toByteArray(timestamp)); } }
private void testEncodedLockDescriptors(String lockId) { assertThat(StringLockDescriptor.of(lockId).toString(), equalTo(expectedEncodedLockDescriptorToString(lockId))); testEncodedLockId(stringToBytes(lockId)); }
/** Returns a {@code LockDescriptor} instance for the given lock ID. */ @SuppressWarnings("checkstyle:jdkStandardCharsets") // StandardCharsets only in JDK 1.7+ public static LockDescriptor of(String lockId) { Preconditions.checkNotNull(lockId, "lockId should not be null"); Preconditions.checkArgument(!Strings.isNullOrEmpty(lockId)); return new LockDescriptor(lockId.getBytes(Charsets.UTF_8)); } }
@Test public void noRetryOnExpiredLockTokens() throws InterruptedException { HeldLocksToken expiredLockToken = getExpiredHeldLocksToken(); try { txManager.runTaskWithLocksWithRetry(ImmutableList.of(expiredLockToken), () -> null, (tx, locks) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); fail(); } catch (TransactionLockTimeoutNonRetriableException e) { LockDescriptor descriptor = Iterables.getFirst(expiredLockToken.getLockDescriptors(), null); assertThat(e.getMessage(), containsString(descriptor.toString())); assertThat(e.getMessage(), containsString("Retry is not possible.")); } }