Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
Closed
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 @@ -36,8 +36,6 @@
import java.util.List;

public class TableStats implements ProtoObject<TableStatsProto>, Cloneable, GsonObject {
private TableStatsProto.Builder builder = TableStatsProto.newBuilder();

@Expose private Long numRows = null; // required
@Expose private Long numBytes = null; // required
@Expose private Integer numBlocks = null; // optional
Expand Down Expand Up @@ -178,7 +176,6 @@ public int hashCode() {

public Object clone() throws CloneNotSupportedException {
TableStats stat = (TableStats) super.clone();
stat.builder = CatalogProtos.TableStatsProto.newBuilder();
stat.numRows = numRows != null ? numRows : null;
stat.numBytes = numBytes != null ? numBytes : null;
stat.numBlocks = numBlocks != null ? numBlocks : null;
Expand Down Expand Up @@ -241,11 +238,7 @@ public String toJson() {

@Override
public TableStatsProto getProto() {
if (builder == null) {
builder = CatalogProtos.TableStatsProto.newBuilder();
} else {
builder.clear();
}
TableStatsProto.Builder builder = CatalogProtos.TableStatsProto.newBuilder();

builder.setNumRows(this.numRows);
builder.setNumBytes(this.numBytes);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@

import org.apache.tajo.catalog.Column;
import org.apache.tajo.catalog.json.CatalogGsonHelper;
import org.apache.tajo.catalog.proto.CatalogProtos.TableStatsProto;
import org.apache.tajo.common.TajoDataTypes.Type;
import org.apache.tajo.datum.TextDatum;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class TestTableStat {
@Test
Expand Down Expand Up @@ -76,4 +84,51 @@ public void tableStatEquals(TableStats s1, TableStats s2) {
assertEquals(s1.getColumnStats().get(i), s2.getColumnStats().get(i));
}
}

@Test
public void testGetProtoThreadSafe() throws Exception {
final TableStats tableStats = new TableStats();

List<ColumnStats> columnStatsList = new ArrayList<ColumnStats>();
for (int i = 0; i < 3; i++) {
Column column = new Column("col_" + (i + 1), Type.TEXT);
ColumnStats columnStats = new ColumnStats(column);
columnStats.setMinValue(new TextDatum(i + ""));
columnStats.setMaxValue(new TextDatum((100 - i) + ""));

columnStatsList.add(columnStats);
}
tableStats.setColumnStats(columnStatsList);

int numThread = 10;
final CountDownLatch latch = new CountDownLatch(numThread);
final AtomicBoolean success = new AtomicBoolean(true);
for (int i = 0; i < numThread; i++) {
Thread t = new Thread() {
public void run() {
for (int j = 0; j < 100; j++) {
try {
TableStatsProto proto = tableStats.getProto();
if (tableStats.getColumnStats().size() != proto.getColStatList().size()) {
success.set(false);
break;
}
} catch (Exception e) {
success.set(false);
}
}

latch.countDown();
}
};

t.start();
}

latch.await();

if (!success.get()) {
fail("TableStats returns different column ststs");
}
}
}