Skip to content

Commit

Permalink
make tool compatible with Cassandra 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
smiklosovic committed Dec 14, 2022
1 parent ecd7065 commit c3d52db
Show file tree
Hide file tree
Showing 16 changed files with 79 additions and 50 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Commands:
cfstats Detailed statistics about cells in a column family
pstats Partition size statistics for a column family
purge Statistics about reclaimable data for a column family
sstables Print out metadata for sstables the belong to a column family
sstables Print out metadata for sstables that belong to a column family
summary Summary information about all column families including how much of the data is repaired
```

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>

<groupId>com.instaclustr</groupId>
<artifactId>ic-sstable-tools-4.0.0</artifactId>
<artifactId>ic-sstable-tools-4.1.0</artifactId>
<version>1.0.0</version>

<name>Instaclustr SSTable Tools</name>
Expand Down Expand Up @@ -88,7 +88,7 @@
<dependency>
<groupId>org.apache.cassandra</groupId>
<artifactId>cassandra-all</artifactId>
<version>4.0.0</version>
<version>4.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/deb/control/control
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ Version: [[version]]
Section: misc
Priority: optional
Architecture: all
Depends: cassandra (>= 4.0)
Depends: cassandra (>= 4.1)
Maintainer: [[maintainer]]
2 changes: 1 addition & 1 deletion src/main/java/com/instaclustr/sstabletools/Histogram.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected int size() {
}

/**
* Snapshot histogram.
* @return snapshot histogram.
*/
public Snapshot snapshot() {
final int s = size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public final class ProgressBar {
* Construct progress bar.
*
* @param title Progress bar title
* @param interactive whether bar is interactive or not
*/
public ProgressBar(String title, boolean interactive) {
this.title = title;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.instaclustr.sstabletools;

import org.apache.cassandra.db.DecoratedKey;
import org.apache.cassandra.io.sstable.SSTableId;

import java.util.ArrayList;
import java.util.Comparator;
Expand All @@ -26,7 +27,7 @@ public int compare(PurgeStatistics o1, PurgeStatistics o2) {
/**
* List of generations the key belongs to.
*/
public List<Integer> generations = new ArrayList<>();
public List<SSTableId> ssTableIds = new ArrayList<>();

/**
* Size in bytes of current partition.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void run() {
cfProxy.formatKey(stats.key),
Util.humanReadableByteCount(stats.size),
Util.humanReadableByteCount(stats.reclaimable),
stats.generations.toString()
stats.ssTableIds.toString()
);
}
System.out.println(tb);
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/instaclustr/sstabletools/SSTableMetadata.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.instaclustr.sstabletools;

import org.apache.cassandra.io.sstable.SSTableId;

import java.util.Comparator;

import static com.instaclustr.sstabletools.Util.compareIds;

/**
* Metadata statistics about sstable.
*/
Expand All @@ -10,30 +14,30 @@ public class SSTableMetadata {
@Override
public int compare(SSTableMetadata o1, SSTableMetadata o2) {
int cmp = Long.compare(o1.minTimestamp, o2.minTimestamp);
return cmp == 0 ? Integer.compare(o1.generation, o2.generation) : cmp;
return compareIds(cmp, o1.ssTableId, o2.ssTableId);
}
};

public final static Comparator<SSTableMetadata> TWCS_COMPARATOR = new Comparator<SSTableMetadata>() {
@Override
public int compare(SSTableMetadata o1, SSTableMetadata o2) {
int cmp = Long.compare(o1.maxTimestamp, o2.maxTimestamp);
return cmp == 0 ? Integer.compare(o1.generation, o2.generation) : cmp;
return compareIds(cmp, o1.ssTableId, o2.ssTableId);
}
};

public final static Comparator<SSTableMetadata> GENERATION_COMPARATOR = new Comparator<SSTableMetadata>() {
@Override
public int compare(SSTableMetadata o1, SSTableMetadata o2) {
return Integer.compare(o1.generation, o2.generation);
return compareIds(0, o1.ssTableId, o2.ssTableId);
}
};

public final static Comparator<SSTableMetadata> LEVEL_COMPARATOR = new Comparator<SSTableMetadata>() {
@Override
public int compare(SSTableMetadata o1, SSTableMetadata o2) {
int cmp = Long.compare(o1.level, o2.level);
return cmp == 0 ? Integer.compare(o1.generation, o2.generation) : cmp;
return compareIds(cmp, o1.ssTableId, o2.ssTableId);
}
};

Expand All @@ -45,7 +49,7 @@ public int compare(SSTableMetadata o1, SSTableMetadata o2) {
/**
* SSTable generation.
*/
public int generation;
public SSTableId ssTableId;

public long minTimestamp;

Expand Down
21 changes: 13 additions & 8 deletions src/main/java/com/instaclustr/sstabletools/SSTableStatistics.java
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
package com.instaclustr.sstabletools;

import org.apache.cassandra.io.sstable.SSTableId;
import org.apache.cassandra.io.sstable.SequenceBasedSSTableId;
import org.apache.cassandra.io.sstable.UUIDBasedSSTableId;

import java.util.Comparator;

/**
* SSTable statistics.
*/
public class SSTableStatistics {

public final static Comparator<SSTableStatistics> LIVENESS_COMPARATOR = new Comparator<SSTableStatistics>() {
@Override
public int compare(SSTableStatistics o1, SSTableStatistics o2) {
int cmp = Long.compare(o1.getLiveness(), o2.getLiveness());
return cmp == 0 ? Integer.compare(o1.generation, o2.generation) : cmp;
return Util.compareIds(cmp, o1.ssTableId, o2.ssTableId);
}
};

public final static Comparator<SSTableStatistics> DTCS_COMPARATOR = new Comparator<SSTableStatistics>() {
@Override
public int compare(SSTableStatistics o1, SSTableStatistics o2) {
int cmp = Long.compare(o1.minTimestamp, o2.minTimestamp);
return cmp == 0 ? Integer.compare(o1.generation, o2.generation) : cmp;
return Util.compareIds(cmp, o1.ssTableId, o2.ssTableId);
}
};

public final static Comparator<SSTableStatistics> TWCS_COMPARATOR = new Comparator<SSTableStatistics>() {
@Override
public int compare(SSTableStatistics o1, SSTableStatistics o2) {
int cmp = Long.compare(o1.maxTimestamp, o2.maxTimestamp);
return cmp == 0 ? Integer.compare(o1.generation, o2.generation) : cmp;
return Util.compareIds(cmp, o1.ssTableId, o2.ssTableId);
}
};

/**
* SSTable generation.
* SSTable id.
*/
public int generation;
public SSTableId ssTableId;

/**
* File name of SSTable Data.db.
Expand Down Expand Up @@ -123,15 +128,15 @@ public int compare(SSTableStatistics o1, SSTableStatistics o2) {
/**
* Construct statistics record for SSTable.
*
* @param generation SSTable Generation
* @param ssTableId SSTable id
* @param filename Filename of Data.db
* @param uncompressedLength Uncompressed length of SSTable Data.db in bytes
* @param minTimestamp Minimum timestamp of SSTable
* @param maxTimestamp Maximum timestamp of SSTable
* @param level SSTable LTCS level
*/
public SSTableStatistics(int generation, String filename, long uncompressedLength, long minTimestamp, long maxTimestamp, int level) {
this.generation = generation;
public SSTableStatistics(SSTableId ssTableId, String filename, long uncompressedLength, long minTimestamp, long maxTimestamp, int level) {
this.ssTableId = ssTableId;
this.filename = filename;
this.size = uncompressedLength;
this.minTimestamp = minTimestamp;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/instaclustr/sstabletools/Util.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.instaclustr.sstabletools;

import org.apache.cassandra.io.sstable.SSTableId;
import org.apache.cassandra.io.sstable.SequenceBasedSSTableId;
import org.apache.cassandra.io.sstable.UUIDBasedSSTableId;

import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.TimeZone;
Expand All @@ -25,6 +29,16 @@ public final class Util {
rand = new Random();
}

public static int compareIds(int cmp, SSTableId o1, SSTableId o2) {
if (o1 instanceof UUIDBasedSSTableId) {
return cmp == 0 ? ((UUIDBasedSSTableId) o1).compareTo((UUIDBasedSSTableId) o2) : cmp;
} else if (o1 instanceof SequenceBasedSSTableId) {
return cmp == 0 ? ((SequenceBasedSSTableId) o1).compareTo((SequenceBasedSSTableId) o2) : cmp;
} else {
throw new IllegalStateException("Unable to process SSTableId of type " + o1.getClass());
}
}

public static String humanReadableByteCount(long bytes) {
return humanReadableByteCount(bytes, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.*;
import java.util.stream.Collectors;

/**
* Proxy to Cassandra 3.0 backend.
* Proxy to Cassandra 4.1 backend.
*/
public class CassandraBackend implements CassandraProxy {
private static final CassandraBackend singleton = new CassandraBackend();
Expand All @@ -34,19 +35,22 @@ public static CassandraProxy getInstance() {

static {
Util.initDatabaseDescriptor();
Schema.instance.loadFromDisk(false);
Schema.instance.loadFromDisk();
}

private CassandraBackend() {}

public List<String> getKeyspaces() {
List<String> names = new ArrayList<>(Schema.instance.getNonSystemKeyspaces());
Collections.sort(names);
return names;
return Schema.instance.getNonLocalStrategyKeyspaces()
.stream()
.map(ksmd -> ksmd.name).sorted().collect(Collectors.toList());
}

public List<String> getColumnFamilies(String ksName) {
KeyspaceMetadata ksMetaData = Schema.instance.getKeyspaceMetadata(ksName);
if (ksMetaData == null) {
throw new IllegalStateException("Unknown keyspace " + ksMetaData.name);
}
List<String> names = new ArrayList<>(ksMetaData.tables.size() + ksMetaData.views.size());
for (TableMetadata cfMetaData : ksMetaData.tablesAndViews()) {
names.add(cfMetaData.name);
Expand All @@ -58,7 +62,7 @@ public List<String> getColumnFamilies(String ksName) {
private ColumnFamilyStore getStore(String ksName, String cfName) {
// Start by validating keyspace name
if (Schema.instance.getKeyspaceMetadata(ksName) == null) {
System.err.println(String.format("Reference to nonexistent keyspace: %s!", ksName));
System.err.printf("Reference to nonexistent keyspace: %s!%n", ksName);
System.exit(1);
}
Keyspace keyspace = Keyspace.open(ksName);
Expand All @@ -74,9 +78,9 @@ private ColumnFamilyStore getStore(String ksName, String cfName) {
try {
return keyspace.getColumnFamilyStore(baseName);
} catch (Throwable t) {
System.err.println(String.format(
"The provided column family is not part of this cassandra keyspace: keyspace = %s, column family = %s",
ksName, cfName));
System.err.printf(
"The provided column family is not part of this cassandra keyspace: keyspace = %s, column family = %s%n",
ksName, cfName);
System.exit(1);
}
return null;
Expand All @@ -90,7 +94,7 @@ public List<SSTableMetadata> getSSTableMetadata(String ksName, String cfName) {
SSTableMetadata tableMetadata = new SSTableMetadata();
File dataFile = new File(table.descriptor.filenameFor(Component.DATA));
tableMetadata.filename = dataFile.getName();
tableMetadata.generation = table.descriptor.generation;
tableMetadata.ssTableId = table.descriptor.id;
try {
tableMetadata.fileTimestamp = Files.getLastModifiedTime(dataFile.toPath()).toMillis();
} catch (IOException e) {
Expand Down Expand Up @@ -131,7 +135,7 @@ public ColumnFamilyProxy getColumnFamily(String ksName, String cfName, String sn
snapshotName,
filter);
} catch (Throwable t) {
System.err.println(String.format("Error retrieving snapshot for %s.%s", ksName, cfName));
System.err.printf("Error retrieving snapshot for %s.%s%n", ksName, cfName);
System.exit(1);
}

Expand All @@ -144,7 +148,7 @@ public Class getCompactionClass(String ksName, String cfName) {
TableMetadata metaData = Schema.instance.getTableMetadata(ksName, cfName);
return metaData.params.compaction.klass();
} catch (Throwable t) {
System.err.println(String.format("Error retrieving snapshot for %s.%s", ksName, cfName));
System.err.printf("Error retrieving snapshot for %s.%s%n", ksName, cfName);
System.exit(1);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.instaclustr.sstabletools.cassandra;

import com.google.common.util.concurrent.RateLimiter;
import com.instaclustr.sstabletools.*;
import org.apache.cassandra.db.ColumnFamilyStore;
import org.apache.cassandra.db.DecoratedKey;
Expand All @@ -9,6 +8,7 @@

import java.io.File;
import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -66,7 +66,7 @@ public ColumnFamilyBackend(AbstractType<?> keyValidator,
this.clearSnapshot = false;
} else {
snapshotName = Util.generateSnapshotName();
cfStore.snapshotWithoutFlush(snapshotName, null, true, null);
cfStore.snapshotWithoutMemtable(snapshotName, null, true, null, null, Instant.now());
this.clearSnapshot = true;
}
this.snapshotName = snapshotName;
Expand All @@ -91,7 +91,7 @@ public Collection<SSTableReader> getIndexReaders() {
File dataFile = new File(sstable.descriptor.filenameFor(Component.DATA));
readers.add(new IndexReader(
new SSTableStatistics(
sstable.descriptor.generation,
sstable.descriptor.id,
dataFile.getName(),
sstable.uncompressedLength(),
sstable.getMinTimestamp(),
Expand All @@ -101,7 +101,9 @@ public Collection<SSTableReader> getIndexReaders() {
sstable.descriptor.version,
sstable.getPartitioner()
));
} catch (Throwable t) {}
} catch (Throwable t) {

}
}
return readers;
}
Expand All @@ -114,7 +116,7 @@ public Collection<SSTableReader> getDataReaders() {
File dataFile = new File(sstable.descriptor.filenameFor(Component.DATA));
readers.add(new DataReader(
new SSTableStatistics(
sstable.descriptor.generation,
sstable.descriptor.id,
dataFile.getName(),
sstable.uncompressedLength(),
sstable.getMinTimestamp(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ public class DataReader extends AbstractSSTableReader {
private long position;

/**
* Construct a reader for Index.db sstable file.
* Construct a reader for Data.db sstable file.
*
* @param tableStats SSTable statistics.
* @param scanner scanner of sstables
* @param gcGrace gc_grace of table
*/
public DataReader(SSTableStatistics tableStats, ISSTableScanner scanner, int gcGrace) {
this.tableStats = tableStats;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class IndexReader extends AbstractSSTableReader {
*
* @param tableStats SSTable statistics.
* @param reader Reader to Index.db file.
* @param version Version of SSTable
* @param partitioner The sstable partitioner.
*/
public IndexReader(SSTableStatistics tableStats, RandomAccessReader reader, Version version, IPartitioner partitioner) {
Expand Down
Loading

0 comments on commit c3d52db

Please sign in to comment.