Skip to content
This repository was archived by the owner on Jul 7, 2020. It is now read-only.

Threadsafing HLL #39

Open
wants to merge 2 commits into
base: master
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.clearspring.analytics.stream.cardinality;

import java.util.concurrent.atomic.AtomicIntegerArray;

public class RegisterSet
{
public final static int LOG2_BITS_PER_WORD = 6;
Expand All @@ -24,7 +26,7 @@ public class RegisterSet
public final int count;
public final int size;

private final int[] M;
private final AtomicIntegerArray M;

public RegisterSet(int count)
{
Expand All @@ -40,22 +42,22 @@ public RegisterSet(int count, int[] initialValues)
{
if (bits == 0)
{
this.M = new int[1];
this.M = new AtomicIntegerArray(1);
}
else if (bits % Integer.SIZE == 0)
{
this.M = new int[bits];
this.M = new AtomicIntegerArray(bits);
}
else
{
this.M = new int[bits + 1];
this.M = new AtomicIntegerArray(bits + 1);
}
}
else
{
this.M = initialValues;
this.M = new AtomicIntegerArray(initialValues);
}
this.size = this.M.length;
this.size = this.M.length();
}

public static int getBits(int count)
Expand All @@ -67,14 +69,17 @@ public void set(int position, int value)
{
int bucketPos = position / LOG2_BITS_PER_WORD;
int shift = REGISTER_SIZE * (position - (bucketPos * LOG2_BITS_PER_WORD));
this.M[bucketPos] = (this.M[bucketPos] & ~(0x1f << shift)) | (value << shift);
int currentVal;
do {
currentVal = this.M.get(bucketPos);
} while(!this.M.compareAndSet(bucketPos, currentVal, (currentVal & ~(0x1f << shift)) | (value << shift)));
}

public int get(int position)
{
int bucketPos = position / LOG2_BITS_PER_WORD;
int shift = REGISTER_SIZE * (position - (bucketPos * LOG2_BITS_PER_WORD));
return (this.M[bucketPos] & (0x1f << shift)) >>> shift;
return (this.M.get(bucketPos) & (0x1f << shift)) >>> shift;
}

public boolean updateIfGreater(int position, int value)
Expand All @@ -84,37 +89,51 @@ public boolean updateIfGreater(int position, int value)
int mask = 0x1f << shift;

// Use long to avoid sign issues with the left-most shift
long curVal = this.M[bucket] & mask;
int curM;
long newVal = value << shift;
if (curVal < newVal) {
this.M[bucket] = (int)((this.M[bucket] & ~mask) | newVal);
return true;
} else {
return false;
long curVal;
while(true) {
curM = this.M.get(bucket);
curVal = curM & mask;
if (curVal < newVal) {
if (this.M.compareAndSet(bucket, curM, (int)((curM & ~mask) | newVal))) {
return true;
}
} else {
return false;
}
}
}

public void merge(RegisterSet that)
{
for (int bucket = 0; bucket < M.length; bucket++)
for (int bucket = 0; bucket < M.length(); bucket++)
{
int word = 0;
for (int j = 0; j < LOG2_BITS_PER_WORD; j++)
int thisM;
int thatM;
do
{
int mask = 0x1f << (REGISTER_SIZE * j);

int thisVal = (this.M[bucket] & mask);
int thatVal = (that.M[bucket] & mask);
word |= (thisVal < thatVal) ? thatVal : thisVal;
}
this.M[bucket] = word;
thisM = this.M.get(bucket);
thatM = that.M.get(bucket);
for (int j = 0; j < LOG2_BITS_PER_WORD; j++)
{
int mask = 0x1f << (REGISTER_SIZE * j);

int thisVal = (thisM & mask);
int thatVal = (thatM & mask);
word |= (thisVal < thatVal) ? thatVal : thisVal;
}
} while(!this.M.compareAndSet(bucket, thisM, word));
}
}

public int[] bits()
{
int[] copy = new int[size];
System.arraycopy(M, 0, copy, 0, M.length);
for (int i = 0; i < size; i++) {
copy[i] = M.get(i);
}
return copy;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

package com.clearspring.analytics.stream.cardinality;

import com.google.common.collect.Lists;
import com.google.common.hash.HashFunction;
import com.google.common.hash.Hashing;

import org.junit.Ignore;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;

import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -75,6 +79,38 @@ public void testHighCardinality()
System.out.println(err);
assertTrue(err < .1);
}

@Test
public void testConcurrentUpdate() throws Exception
{
long start = System.currentTimeMillis();
final HyperLogLog hyperLogLog = new HyperLogLog(10);
final int size = 1000;
final int perThread = 10000;
List<Future<?>> futures = Lists.newArrayListWithCapacity(1000);
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(8);
for (int i = 0; i < size; i++)
{
final int base = i * perThread;
futures.add(executor.submit(new Runnable() {
@Override
public void run() {
for (int n = 0; n < perThread; n++) {
hyperLogLog.offer(TestICardinality.streamElement(base + n));
}
}
}));
}
for (Future<?> f : futures)
{
f.get();
}
System.out.println("concurrent time: " + (System.currentTimeMillis() - start));
long estimate = hyperLogLog.cardinality();
double err = Math.abs(estimate - size * perThread) / (double) (size * perThread);
System.out.println(err);
assertTrue(err < .1);
}

@Test
public void testHighCardinality_withDefinedRSD()
Expand Down