Share heap-based transaction row allocation across readers
This commit is contained in:
1 parent
d31fe15000
commit
8d4ec071e5
4 files changed
+122
-90
No files matched your search
+9
-46
@@ -10,8 +10,6 @@
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.ftr;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventList;
|
||||
@@ -20,7 +18,7 @@ import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.TxRowAllocator;
|
||||
import com.minres.scviewer.database.tx.ITxEvent;
|
||||
|
||||
/**
|
||||
@@ -126,7 +124,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
* @return the width
|
||||
*/
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
public synchronized int getRowCount() {
|
||||
if (rowCount<0)
|
||||
calculateConcurrency();
|
||||
return rowCount;
|
||||
@@ -140,46 +138,11 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
/**
|
||||
* Calculate concurrency.
|
||||
*/
|
||||
void calculateConcurrency() {
|
||||
if (rowCount>=0)
|
||||
return;
|
||||
ArrayList<Long> rowEndTime = new ArrayList<>();
|
||||
HashMap<Long, Integer> rowByTxId = new HashMap<>();
|
||||
for(EventEntry entry: getEvents()) {
|
||||
for(IEvent evt:entry.events) {
|
||||
TxEvent txEvt = (TxEvent) evt;
|
||||
ITx tx = txEvt.getTransaction();
|
||||
int rowIdx = 0;
|
||||
switch(evt.getKind()) {
|
||||
case END: //TODO: might throw NPE in concurrent execution
|
||||
Long txId = txEvt.getTransaction().getId();
|
||||
Integer row = rowByTxId.get(txId);
|
||||
if(row!=null) {
|
||||
txEvt.setConcurrencyIndex(row);
|
||||
rowByTxId.remove(txId);
|
||||
}
|
||||
break;
|
||||
case SINGLE:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
break;
|
||||
case BEGIN:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
rowByTxId.put(tx.getId(), rowIdx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
rowCount=rowEndTime.size()>0?rowEndTime.size():1;
|
||||
getChildNodes().parallelStream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
||||
}
|
||||
synchronized void calculateConcurrency() {
|
||||
if (rowCount >= 0) return;
|
||||
int count = TxRowAllocator.assign(getEvents(), (event, row) -> ((TxEvent) event).setConcurrencyIndex(row));
|
||||
for (com.minres.scviewer.database.IHierNode child : getChildNodes())
|
||||
((TxGenerator) child).calculateConcurrency();
|
||||
rowCount = count;
|
||||
}
|
||||
}
|
||||
+9
-44
@@ -11,8 +11,6 @@
|
||||
*******************************************************************************/
|
||||
package com.minres.scviewer.database.text;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventList;
|
||||
@@ -21,7 +19,7 @@ import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
import com.minres.scviewer.database.IWaveform;
|
||||
import com.minres.scviewer.database.WaveformType;
|
||||
import com.minres.scviewer.database.tx.ITx;
|
||||
import com.minres.scviewer.database.tx.TxRowAllocator;
|
||||
import com.minres.scviewer.database.tx.ITxEvent;
|
||||
|
||||
/**
|
||||
@@ -137,7 +135,7 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
* @return the width
|
||||
*/
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
public synchronized int getRowCount() {
|
||||
if (rowCount<0)
|
||||
calculateConcurrency();
|
||||
return rowCount;
|
||||
@@ -150,44 +148,11 @@ abstract class AbstractTxStream extends HierNode implements IWaveform {
|
||||
/**
|
||||
* Calculate concurrency.
|
||||
*/
|
||||
void calculateConcurrency() {
|
||||
if (rowCount>=0)
|
||||
return;
|
||||
ArrayList<Long> rowEndTime = new ArrayList<>();
|
||||
HashMap<Long, Integer> rowByTxId = new HashMap<>();
|
||||
for(EventEntry entry: events) {
|
||||
for(IEvent evt:entry.events) {
|
||||
TxEvent txEvt = (TxEvent) evt;
|
||||
ITx tx = txEvt.getTransaction();
|
||||
int rowIdx = 0;
|
||||
switch(evt.getKind()) {
|
||||
case END:
|
||||
Long txId = txEvt.getTransaction().getId();
|
||||
txEvt.setConcurrencyIndex(rowByTxId.get(txId));
|
||||
rowByTxId.remove(txId);
|
||||
break;
|
||||
case SINGLE:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
break;
|
||||
case BEGIN:
|
||||
for (; rowIdx < rowEndTime.size() && rowEndTime.get(rowIdx)>tx.getBeginTime(); rowIdx++);
|
||||
if (rowEndTime.size() <= rowIdx)
|
||||
rowEndTime.add(tx.getEndTime());
|
||||
else
|
||||
rowEndTime.set(rowIdx, tx.getEndTime());
|
||||
((TxEvent) evt).setConcurrencyIndex(rowIdx);
|
||||
rowByTxId.put(tx.getId(), rowIdx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
rowCount=rowEndTime.size()>0?rowEndTime.size():1;
|
||||
//getChildNodes().parallelStream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
||||
getChildNodes().stream().forEach(c -> ((TxGenerator)c).calculateConcurrency());
|
||||
}
|
||||
synchronized void calculateConcurrency() {
|
||||
if (rowCount >= 0) return;
|
||||
int count = TxRowAllocator.assign(getEvents(), (event, row) -> ((TxEvent) event).setConcurrencyIndex(row));
|
||||
for (com.minres.scviewer.database.IHierNode child : getChildNodes())
|
||||
((TxGenerator) child).calculateConcurrency();
|
||||
rowCount = count;
|
||||
}
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.minres.scviewer.database.tx;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.IdentityHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.function.ObjIntConsumer;
|
||||
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventKind;
|
||||
import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.IEventList;
|
||||
|
||||
/** Assigns the lowest available row in O(N log C), where C is peak concurrency. */
|
||||
public final class TxRowAllocator {
|
||||
private record Active(long end, int row) { }
|
||||
private TxRowAllocator() { }
|
||||
|
||||
public static int assign(IEventList events, ObjIntConsumer<ITxEvent> setRow) {
|
||||
PriorityQueue<Active> active = new PriorityQueue<>((a, b) -> {
|
||||
int cmp = Long.compareUnsigned(a.end(), b.end());
|
||||
return cmp != 0 ? cmp : Integer.compare(a.row(), b.row());
|
||||
});
|
||||
PriorityQueue<Integer> free = new PriorityQueue<>();
|
||||
Map<Long, Integer> rowByTx = new HashMap<>();
|
||||
Map<ITxEvent, Integer> assignments = new IdentityHashMap<>();
|
||||
int count = 0;
|
||||
for (EventEntry entry : events) {
|
||||
for (IEvent raw : entry.events) {
|
||||
ITxEvent event = (ITxEvent) raw;
|
||||
ITx tx = event.getTransaction();
|
||||
if (tx == null) throw new IllegalStateException("Missing transaction for " + event);
|
||||
if (event.getKind() == EventKind.END) {
|
||||
Integer row = rowByTx.remove(tx.getId());
|
||||
if (row == null) throw new IllegalStateException("End without begin for transaction " + tx.getId());
|
||||
assignments.put(event, row);
|
||||
continue;
|
||||
}
|
||||
while (!active.isEmpty() && Long.compareUnsigned(active.peek().end(), tx.getBeginTime()) <= 0)
|
||||
free.add(active.remove().row());
|
||||
int row = free.isEmpty() ? count++ : free.remove();
|
||||
active.add(new Active(tx.getEndTime(), row));
|
||||
assignments.put(event, row);
|
||||
if (event.getKind() == EventKind.BEGIN) rowByTx.put(tx.getId(), row);
|
||||
}
|
||||
}
|
||||
// Validate the whole layout before changing any event indexes.
|
||||
assignments.forEach((event, row) -> setRow.accept(event, row));
|
||||
return Math.max(1, count);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.minres.scviewer.database.test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import org.junit.Test;
|
||||
import com.minres.scviewer.database.EventKind;
|
||||
import com.minres.scviewer.database.EventEntry;
|
||||
import com.minres.scviewer.database.EventList;
|
||||
import com.minres.scviewer.database.IEvent;
|
||||
import com.minres.scviewer.database.tx.TxRowAllocator;
|
||||
|
||||
public class TxRowAllocatorTest {
|
||||
@Test
|
||||
public void randomizedAllocationMatchesLowestFreeRow() {
|
||||
Random random = new Random(42);
|
||||
EventList events = new EventList();
|
||||
for (int i = 0; i < 1000; ++i) {
|
||||
int begin = random.nextInt(1000), end = begin + random.nextInt(100);
|
||||
TxTestSupport.Tx tx = new TxTestSupport.Tx(i, begin, end);
|
||||
events.put(begin, new TxTestSupport.Event(tx, begin == end ? EventKind.SINGLE : EventKind.BEGIN));
|
||||
if (begin != end) events.put(end, new TxTestSupport.Event(tx, EventKind.END));
|
||||
}
|
||||
int count = TxRowAllocator.assign(events, (e, row) -> ((TxTestSupport.Event)e).row = row);
|
||||
List<Long> ends = new ArrayList<>();
|
||||
java.util.Map<Long, Integer> expected = new java.util.HashMap<>();
|
||||
for (EventEntry entry : events) for (IEvent raw : entry.events) {
|
||||
TxTestSupport.Event event = (TxTestSupport.Event) raw;
|
||||
if (event.kind == EventKind.END) {
|
||||
assertEquals((int) expected.remove(event.tx.id), event.row);
|
||||
} else {
|
||||
int row = 0;
|
||||
while (row < ends.size() && ends.get(row) > event.tx.begin) row++;
|
||||
if (row == ends.size()) ends.add(event.tx.end);
|
||||
else ends.set(row, event.tx.end);
|
||||
assertEquals(row, event.row);
|
||||
if (event.kind == EventKind.BEGIN) expected.put(event.tx.id, row);
|
||||
}
|
||||
}
|
||||
assertEquals(ends.size(), count);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void invalidLayoutDoesNotPublishPartialIndexes() {
|
||||
TxTestSupport.Tx tx = new TxTestSupport.Tx(1, 0, 10);
|
||||
TxTestSupport.Event end = new TxTestSupport.Event(tx, EventKind.END);
|
||||
EventList events = new EventList();events.put(10, end);
|
||||
try { TxRowAllocator.assign(events, (e, row) -> ((TxTestSupport.Event)e).row = row); fail(); }
|
||||
catch (IllegalStateException expected) { assertEquals(-1, end.row); }
|
||||
assertEquals(1, TxRowAllocator.assign(new EventList(), (e, row) -> fail()));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user