Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested changes for 10805 #1

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/java/org/apache/cassandra/db/compaction/CompactionLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.*;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.*;
Expand All @@ -40,6 +44,8 @@
import org.codehaus.jackson.node.JsonNodeFactory;
import org.codehaus.jackson.node.ObjectNode;

import static com.google.common.io.Files.getNameWithoutExtension;

public class CompactionLogger
{
public interface Strategy
Expand Down Expand Up @@ -71,7 +77,6 @@ private interface CompactionStrategyAndTableFunction
private static final CompactionLogSerializer serializer = new CompactionLogSerializer();
private final ColumnFamilyStore cfs;
private final CompactionStrategyManager csm;
private final AtomicInteger identifier = new AtomicInteger(0);
private final Map<AbstractCompactionStrategy, String> compactionStrategyMapping = new ConcurrentHashMap<>();
private final AtomicBoolean enabled = new AtomicBoolean(false);

Expand Down Expand Up @@ -103,7 +108,14 @@ private ArrayNode arrayNode(Collection<SSTableReader> sstables, CompactionStrate

private String getId(AbstractCompactionStrategy strategy)
{
return compactionStrategyMapping.computeIfAbsent(strategy, s -> String.valueOf(identifier.getAndIncrement()));
if (strategy instanceof SizeTieredCompactionStrategy)
return "STCS";
else if (strategy instanceof LeveledCompactionStrategy)
return "LCS";
else if (strategy instanceof DateTieredCompactionStrategy)
return "DTCS";
else
return strategy.getName();
}

private JsonNode formatSSTables(AbstractCompactionStrategy strategy)
Expand All @@ -122,6 +134,7 @@ private JsonNode formatSSTable(AbstractCompactionStrategy strategy, SSTableReade
ObjectNode node = json.objectNode();
node.put("generation", sstable.descriptor.generation);
node.put("size", sstable.onDiskLength());
node.put("file", getNameWithoutExtension(sstable.getFilename()));
JsonNode logResult = strategy.strategyLogger().sstable(sstable);
if (logResult != null)
node.put("details", logResult);
Expand Down Expand Up @@ -204,8 +217,10 @@ public void compaction(long startTime, Collection<SSTableReader> input, long end
ObjectNode node = json.objectNode();
node.put("type", "compaction");
describe(node);
node.put("start", String.valueOf(startTime));
node.put("end", String.valueOf(endTime));
node.put("start", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(Instant.ofEpochMilli(startTime)
.atZone(ZoneId.systemDefault())));
node.put("end", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(Instant.ofEpochMilli(endTime)
.atZone(ZoneId.systemDefault())));
node.put("input", arrayNode(input, this::describe));
node.put("output", arrayNode(output, this::describe));
serializer.write(node);
Expand All @@ -214,7 +229,7 @@ public void compaction(long startTime, Collection<SSTableReader> input, long end

public void pending(AbstractCompactionStrategy strategy, int remaining)
{
if (enabled.get())
if (enabled.get() && remaining > 0)
{
ObjectNode node = json.objectNode();
node.put("type", "pending");
Expand Down Expand Up @@ -248,8 +263,9 @@ private static OutputStreamWriter createStream() throws IOException
return new OutputStreamWriter(Files.newOutputStream(compactionLog, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE));
}

public void write(JsonNode statement)
public void write(ObjectNode statement)
{
statement.put("date", DateTimeFormatter.ISO_OFFSET_DATE_TIME.format(ZonedDateTime.now()));
final String toWrite = statement.toString() + System.lineSeparator();
loggerService.execute(() -> {
try
Expand Down