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

CASSANDRA-20177 use Apache Datasketches for cardinality computation #3767

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions .build/cassandra-deps-template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@
<groupId>com.clearspring.analytics</groupId>
<artifactId>stream</artifactId>
</dependency>
<dependency>
<groupId>org.apache.datasketches</groupId>
<artifactId>datasketches-java</artifactId>
</dependency>
<dependency>
<groupId>org.apache.datasketches</groupId>
<artifactId>datasketches-memory</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
Expand Down
10 changes: 10 additions & 0 deletions .build/parent-pom-template.xml
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,16 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.datasketches</groupId>
<artifactId>datasketches-java</artifactId>
<version>6.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.datasketches</groupId>
<artifactId>datasketches-memory</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
Expand Down
59 changes: 46 additions & 13 deletions src/java/org/apache/cassandra/io/sstable/format/SSTableReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.clearspring.analytics.stream.cardinality.CardinalityMergeException;
import com.clearspring.analytics.stream.cardinality.ICardinality;
import org.apache.cassandra.concurrent.ExecutorPlus;
import org.apache.cassandra.concurrent.ScheduledExecutorPlus;
import org.apache.cassandra.concurrent.ScheduledExecutors;
Expand Down Expand Up @@ -84,6 +82,8 @@
import org.apache.cassandra.io.sstable.SSTableReadsListener;
import org.apache.cassandra.io.sstable.format.SSTableFormat.Components;
import org.apache.cassandra.io.sstable.metadata.CompactionMetadata;
import org.apache.cassandra.io.sstable.metadata.ICardinality;
import org.apache.cassandra.io.sstable.metadata.ICardinality.CardinalityMergeException;
import org.apache.cassandra.io.sstable.metadata.StatsMetadata;
import org.apache.cassandra.io.util.ChannelProxy;
import org.apache.cassandra.io.util.CheckedFunction;
Expand All @@ -103,6 +103,7 @@
import org.apache.cassandra.utils.JVMStabilityInspector;
import org.apache.cassandra.utils.NativeLibrary;
import org.apache.cassandra.utils.OutputHandler;
import org.apache.cassandra.utils.Pair;
import org.apache.cassandra.utils.Throwables;
import org.apache.cassandra.utils.TimeUUID;
import org.apache.cassandra.utils.concurrent.OpOrder;
Expand Down Expand Up @@ -288,8 +289,49 @@ public static long getApproximateKeyCount(Iterable<SSTableReader> sstables)
if (Iterables.isEmpty(sstables))
return count;

Pair<ICardinality, Boolean> result = null;
if (allSSTablesOnSameCardinality(sstables))
result = getApproximateKeyCountInternal(sstables);

if (result != null)
{
ICardinality cardinality = result.left;
boolean failed = result.right;

if (cardinality != null && !failed)
count = cardinality.cardinality();
}

// if something went wrong above or cardinality is not available, calculate using index summary
if (count < 0)
{
count = 0;
for (SSTableReader sstable : sstables)
count += sstable.estimatedKeys();
}
return count;
}

private static boolean allSSTablesOnSameCardinality(Iterable<SSTableReader> sstables)
{
Iterator<SSTableReader> iterator = sstables.iterator();

boolean hasSameCardinality = iterator.next().descriptor.version.hasLegacyCardinality();

for (SSTableReader sstable : sstables)
{
if (sstable.descriptor.version.hasLegacyCardinality() != hasSameCardinality)
return false;
}

return true;
}

private static Pair<ICardinality, Boolean> getApproximateKeyCountInternal(Iterable<SSTableReader> sstables)
{
boolean failed = false;
ICardinality cardinality = null;

for (SSTableReader sstable : sstables)
{
if (sstable.openReason == OpenReason.EARLY)
Expand All @@ -310,7 +352,7 @@ public static long getApproximateKeyCount(Iterable<SSTableReader> sstables)
if (cardinality == null)
cardinality = metadata.cardinalityEstimator;
else
cardinality = cardinality.merge(metadata.cardinalityEstimator);
cardinality = cardinality.merge(metadata.cardinalityEstimator.getCardinality());
}
catch (IOException e)
{
Expand All @@ -325,17 +367,8 @@ public static long getApproximateKeyCount(Iterable<SSTableReader> sstables)
break;
}
}
if (cardinality != null && !failed)
count = cardinality.cardinality();

// if something went wrong above or cardinality is not available, calculate using index summary
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is still used, just refactored (see the code above), this would happen when not all SSTables are on same format.

if (count < 0)
{
count = 0;
for (SSTableReader sstable : sstables)
count += sstable.estimatedKeys();
}
return count;
return Pair.create(cardinality, failed);
}

public static SSTableReader open(SSTable.Owner owner, Descriptor descriptor)
Expand Down
2 changes: 2 additions & 0 deletions src/java/org/apache/cassandra/io/sstable/format/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ protected Version(SSTableFormat format, String version)
public abstract boolean hasIsTransient();

public abstract boolean hasMetadataChecksum();

public abstract boolean hasLegacyCardinality();

/**
* This format raises the legacy int year 2038 limit to 2106 by using an uint instead
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ public BigTableWriter.Builder builder(Descriptor descriptor)

static class BigVersion extends Version
{
public static final String current_version = DatabaseDescriptor.getStorageCompatibilityMode().isBefore(5) ? "nb" : "oa";
public static final String current_version = DatabaseDescriptor.getStorageCompatibilityMode().isBefore(5) ? "nb" : "ob";
public static final String earliest_supported_version = "ma";

// ma (3.0.0): swap bf hash order
Expand Down Expand Up @@ -469,6 +469,7 @@ static class BigVersion extends Version
private final boolean hasKeyRange;
private final boolean hasUintDeletionTime;
private final boolean hasTokenSpaceCoverage;
private final boolean hasLegacyCardinality;

/**
* CASSANDRA-9067: 4.0 bloom filter representation changed (two longs just swapped)
Expand Down Expand Up @@ -501,6 +502,7 @@ static class BigVersion extends Version
hasKeyRange = version.compareTo("oa") >= 0;
hasUintDeletionTime = version.compareTo("oa") >= 0;
hasTokenSpaceCoverage = version.compareTo("oa") >= 0;
hasLegacyCardinality = version.compareTo("ob") < 0;
}

@Override
Expand Down Expand Up @@ -551,6 +553,12 @@ public boolean hasMetadataChecksum()
return hasMetadataChecksum;
}

@Override
public boolean hasLegacyCardinality()
{
return hasLegacyCardinality;
}

@Override
public boolean hasOldBfFormat()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,12 @@ public boolean hasMetadataChecksum()
return true;
}

@Override
public boolean hasLegacyCardinality()
{
return true;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

TODO

}

@Override
public boolean hasOldBfFormat()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
import java.io.IOException;

import com.clearspring.analytics.stream.cardinality.HyperLogLogPlus;
import com.clearspring.analytics.stream.cardinality.ICardinality;

import org.apache.cassandra.db.TypeSizes;
import org.apache.cassandra.io.sstable.format.Version;
import org.apache.cassandra.io.util.DataInputPlus;
import org.apache.cassandra.io.util.DataOutputPlus;
import org.apache.cassandra.utils.ByteArrayUtil;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.datasketches.hll.HllSketch;

/**
* Compaction related SSTable metadata.
Expand Down Expand Up @@ -87,7 +87,17 @@ public void serialize(Version version, CompactionMetadata component, DataOutputP

public CompactionMetadata deserialize(Version version, DataInputPlus in) throws IOException
{
ICardinality cardinality = HyperLogLogPlus.Builder.build(ByteBufferUtil.readBytes(in, in.readInt()));
ICardinality cardinality;

if (version.hasLegacyCardinality())
cardinality = new LegacyCardinality(HyperLogLogPlus.Builder.build(ByteBufferUtil.readBytes(in, in.readInt())));
else
{
byte[] bytes = ByteBufferUtil.readBytes(in, in.readInt());
HllSketch hllSketch = HllSketch.heapify(bytes);
cardinality = new DefaultCardinality(hllSketch);
}

return new CompactionMetadata(cardinality);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.cassandra.io.sstable.metadata;

import java.io.IOException;

import org.apache.datasketches.hll.HllSketch;
import org.apache.datasketches.hll.Union;

public class DefaultCardinality implements ICardinality<HllSketch>
{
private static final int DEFAULT_LG_MAX_K = 13;

private final HllSketch hllSketch;

public DefaultCardinality()
{
this(new HllSketch(HllSketch.DEFAULT_LG_K));
}

public DefaultCardinality(HllSketch hllSketch)
{
this.hllSketch = hllSketch;
}

@Override
public void offerHashed(long hashed)
{
hllSketch.update(hashed);
}

@Override
public long cardinality()
{
return (long) hllSketch.getEstimate();
}

@Override
public int sizeof()
{
return hllSketch.toUpdatableByteArray().length;
}

@Override
public byte[] getBytes() throws IOException
{
return hllSketch.toUpdatableByteArray();
}

@Override
public HllSketch getCardinality()
{
return hllSketch;
}

@Override
public ICardinality<HllSketch> merge(HllSketch cardinality) throws CardinalityMergeException
{
Union union = new Union(DEFAULT_LG_MAX_K);

union.update(hllSketch);
union.update(cardinality);

return new DefaultCardinality(union.getResult());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.cassandra.io.sstable.metadata;

import java.io.IOException;

public interface ICardinality<T>
{
void offerHashed(long hashed);

long cardinality();

int sizeof();

byte[] getBytes() throws IOException;

ICardinality<T> merge(T cardinality) throws CardinalityMergeException;

T getCardinality();

class CardinalityMergeException extends Exception
{
public CardinalityMergeException(Throwable cause)
{
super(cause);
}
}
}
Loading