Skip to content
This repository was archived by the owner on Nov 14, 2024. It is now read-only.

Refactor Logger to SafeLogger, part 1 #5619

Merged
merged 11 commits into from
Sep 13, 2021
Merged

Conversation

unneon
Copy link
Contributor

@unneon unneon commented Sep 2, 2021

Goals (and why): Replace most Logger usage in favor of SafeLogger, to unblock #5597.

Implementation Description (bullets): Each SafeArg vs UnsafeArg decision was made manually, quite conservatively in favor of UnsafeArg due to the large number of changes. For now, only log instances with simple logger declarations were processed.

Testing (What was existing testing like? What have you done to improve it?): Very few places in code test log messages. Nothing was done to improve this.

Concerns (what feedback would you like?): Thoroughly check that all SafeArg instances are actually safe.

Where should we start reviewing?: Wherever.

Priority (whenever / two weeks / yesterday): Not urgent, but blocks baseline upgrade.

@changelog-app
Copy link

changelog-app bot commented Sep 2, 2021

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Refactor Logger to SafeLogger, part 1

Check the box to generate changelog(s)

  • Generate changelog entry

Copy link
Contributor

@gmaretic gmaretic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This must have been enjoyable to write!

@@ -151,7 +151,7 @@ private void scheduleSweepRowTask(
// RejectedExecutionException are expected.
// The executor is shutdown when we already fetched all the values we were interested
// for the current iteration.
log.trace("Rejecting row {} because executor is closed", rows.get(rowIndex), e);
log.trace("Rejecting row {} because executor is closed", UnsafeArg.of("row", rows.get(rowIndex)), e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These kind of args are crap anyway because we are logging a reference only. I guess therefore it could be also logged as safe, even though the intention was almost certainly to log the hex representation of the byte array (and therefore unsafe). So this is not wrong, but I would drive by fix it and log the actual array

@@ -341,7 +342,7 @@ public void debugLogStateOfPool() {
activeCheckouts > 0 ? Integer.toString(activeCheckouts) : "(unknown)",
totalAllowed > 0 ? Integer.toString(totalAllowed) : "(not bounded)"));
}
log.debug("Current pool state: {}", currentState.toString());
log.debug("Current pool state: {}", UnsafeArg.of("currentState", currentState.toString()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why unsafe?

@@ -262,7 +263,7 @@ private void flushTask() {
}
} catch (Throwable t) {
if (!Thread.interrupted()) {
log.warn("Error occurred while flushing sweep stats: {}", t, t);
log.warn("Error occurred while flushing sweep stats: {}", UnsafeArg.of("throwable", t), t);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a benefit to attaching the throwable also as an arg?

@@ -331,7 +336,11 @@ private void flushWrites(Multiset<TableReference> writes, Set<TableReference> cl
// ignore problems when sweep or transaction tables don't exist
log.warn("Ignoring failed sweep stats flush due to ", e);
}
log.warn("Unable to flush sweep stats for writes {} and clears {}: ", writes, clears, e);
log.warn(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add a method to convert these in LoggingArgs?

SafeArg.of("numRangeBoundaries", numRangeBoundaries),
LoggingArgs.tableRef("srcTable", srcTable),
LoggingArgs.tableRef("destTable", destTable),
UnsafeArg.of("checkpoint", PtBytes.encodeHexString(checkpoint)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(this is how you encode the hex string)

@@ -190,7 +195,7 @@ private void writeStreamToFile(Transaction transaction, T id, StreamMetadata met
try {
tryWriteStreamToFile(transaction, id, metadata, fos);
} catch (IOException e) {
log.error("Could not finish streaming blocks to file for stream {}", id, e);
log.error("Could not finish streaming blocks to file for stream {}", UnsafeArg.of("stream", id), e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Stream ids are probably safe, but no need to change

@@ -248,7 +249,7 @@ public void validate() {
try {
entry.getValue().validate();
} catch (Exception e) {
log.error("Failed to validate table {}.", entry.getKey(), e);
log.error("Failed to validate table {}.", UnsafeArg.of("tableName", entry.getKey()), e);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are table names from schemas, probably should be safe to log? Again, fine to leave as is

@@ -128,7 +135,7 @@ public void truncate() {
public void checkDatabaseVersion() {
AgnosticResultSet result = conns.get().selectResultSetUnregisteredQuery("SHOW server_version");
String version = result.get(0).getString("server_version");
PostgresVersionCheck.checkDatabaseVersion(version, log);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not modify the method to take a SafeLogger instead? If you're worried about public API, you can always keep the old version of the method

SafeArg.of("tableCount", scrubTimestampToTableNameToCell.size()),
SafeArg.of("minTimestamp", minTimestamp),
SafeArg.of("maxTimestamp", maxTimestamp),
UnsafeArg.of("tables", tables));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also maybe use LoggingArgs

}
}

private Object[] constructSlowLockLogParams(String lockId, LockClient currentHolder, long durationMillis) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@bulldozer-bot bulldozer-bot bot merged commit 0b57644 into develop Sep 13, 2021
@bulldozer-bot bulldozer-bot bot deleted the mcegielka/safelogger branch September 13, 2021 17:12
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants