Skip to content

Commit d7a46b5

Browse files
aweisbergbelliottsmith
authored andcommitted
Deterministic Simulator thread ids
Also: Don't ignore self reconcile test patch by Ariel Weisberg; reviewed by David Capwell for CASSANDRA-20841
1 parent dddbe1a commit d7a46b5

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

test/simulator/main/org/apache/cassandra/simulator/systems/InterceptibleThread.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ public String toString()
189189
final Runnable onTermination;
190190
private final InterceptorOfGlobalMethods interceptorOfGlobalMethods;
191191
private final LocalTime time;
192+
private final long id;
192193

193194
// this is set before the thread's execution begins/continues; events and cessation are reported back to this
194195
private InterceptorOfConsequences interceptor;
@@ -205,7 +206,7 @@ public String toString()
205206
// perform any non-deterministic actions
206207
private int determinismDepth;
207208

208-
public InterceptibleThread(ThreadGroup group, Runnable target, String name, Object extraToStringInfo, Runnable onTermination, InterceptorOfGlobalMethods interceptorOfGlobalMethods, LocalTime time)
209+
public InterceptibleThread(ThreadGroup group, Runnable target, String name, Object extraToStringInfo, Runnable onTermination, InterceptorOfGlobalMethods interceptorOfGlobalMethods, LocalTime time, long id)
209210
{
210211
super(group, target, name);
211212
this.onTermination = onTermination;
@@ -214,6 +215,13 @@ public InterceptibleThread(ThreadGroup group, Runnable target, String name, Obje
214215
// group is nulled on termination, and we need it for reporting purposes, so save the toString
215216
this.toString = "Thread[" + name + ',' + getPriority() + ',' + group.getName() + ']';
216217
this.extraToStringInfo = extraToStringInfo;
218+
this.id = id;
219+
}
220+
221+
@Override
222+
public long getId()
223+
{
224+
return id;
217225
}
218226

219227
public boolean park(long waitTime, WaitTimeKind waitTimeKind)

test/simulator/main/org/apache/cassandra/simulator/systems/InterceptibleThreadFactory.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.Serializable;
2222
import java.util.concurrent.ThreadFactory;
23+
import java.util.function.Supplier;
2324

2425
import org.apache.cassandra.concurrent.NamedThreadFactory;
2526

@@ -28,7 +29,7 @@ public interface InterceptibleThreadFactory extends ThreadFactory
2829
public interface MetaFactory<F extends ThreadFactory> extends Serializable
2930
{
3031
F create(String id, int priority, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
31-
ThreadGroup threadGroup, Runnable onTermination, SimulatedTime.LocalTime time, InterceptingExecutorFactory parent, Object extraToStringInfo);
32+
ThreadGroup threadGroup, Runnable onTermination, SimulatedTime.LocalTime time, InterceptingExecutorFactory parent, Object extraToStringInfo, Supplier<Long> idSupplier);
3233
}
3334

3435
public static class ConcreteInterceptibleThreadFactory extends NamedThreadFactory implements InterceptibleThreadFactory
@@ -37,16 +38,18 @@ public static class ConcreteInterceptibleThreadFactory extends NamedThreadFactor
3738
final Runnable onTermination;
3839
final SimulatedTime.LocalTime time;
3940
final Object extraToStringInfo;
41+
final Supplier<Long> idSupplier;
4042

4143
public ConcreteInterceptibleThreadFactory(String id, int priority, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
4244
ThreadGroup threadGroup, Runnable onTermination, SimulatedTime.LocalTime time,
43-
InterceptingExecutorFactory parent, Object extraToStringInfo)
45+
InterceptingExecutorFactory parent, Object extraToStringInfo, Supplier<Long> idSupplier)
4446
{
4547
super(id, priority, contextClassLoader, threadGroup, uncaughtExceptionHandler);
4648
this.onTermination = onTermination;
4749
this.time = time;
4850
this.parent = parent;
4951
this.extraToStringInfo = extraToStringInfo;
52+
this.idSupplier = idSupplier;
5053
}
5154

5255
@Override
@@ -60,7 +63,7 @@ protected synchronized InterceptibleThread newThread(ThreadGroup threadGroup, Ru
6063
{
6164
// Can not use NamedThreadFactory.globalPrefix() as this method runs in the App class loader and not the Instance class loader; the ThreadGroup's name can act as a proxy for this.
6265
String threadName = threadGroup.getName() + '_' + name;
63-
InterceptibleThread thread = new InterceptibleThread(threadGroup, runnable, threadName, extraToStringInfo, onTermination, parent.interceptorOfGlobalMethods, time);
66+
InterceptibleThread thread = new InterceptibleThread(threadGroup, runnable, threadName, extraToStringInfo, onTermination, parent.interceptorOfGlobalMethods, time, idSupplier.get());
6467
if (parent.isClosed)
6568
thread.trapInterrupts(false);
6669
return setupThread(thread);
@@ -72,7 +75,7 @@ public static class PlainThreadFactory extends NamedThreadFactory
7275
final Runnable onTermination;
7376

7477
public PlainThreadFactory(String id, int priority, ClassLoader contextClassLoader, Thread.UncaughtExceptionHandler uncaughtExceptionHandler,
75-
ThreadGroup threadGroup, Runnable onTermination, SimulatedTime.LocalTime time, InterceptingExecutorFactory parent, Object extraToStringInfo)
78+
ThreadGroup threadGroup, Runnable onTermination, SimulatedTime.LocalTime time, InterceptingExecutorFactory parent, Object extraToStringInfo, Supplier<Long> idSupplier)
7679
{
7780
super(id, priority, contextClassLoader, threadGroup, uncaughtExceptionHandler);
7881
this.onTermination = onTermination;

test/simulator/main/org/apache/cassandra/simulator/systems/InterceptingExecutorFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.concurrent.TimeUnit;
2828
import java.util.function.BiFunction;
2929
import java.util.function.Consumer;
30+
import java.util.function.Supplier;
3031

3132
import com.google.common.annotations.VisibleForTesting;
3233

@@ -187,15 +188,17 @@ public E build()
187188
final ClassLoader classLoader;
188189
final ThreadGroup threadGroup;
189190
final IIsolatedExecutor.DynamicFunction<Serializable> transferToInstance;
191+
final Supplier<Long> idSupplier;
190192
volatile boolean isClosed;
191193

192-
InterceptingExecutorFactory(SimulatedExecution simulatedExecution, InterceptorOfGlobalMethods interceptorOfGlobalMethods, ClassLoader classLoader, ThreadGroup threadGroup)
194+
InterceptingExecutorFactory(SimulatedExecution simulatedExecution, InterceptorOfGlobalMethods interceptorOfGlobalMethods, ClassLoader classLoader, ThreadGroup threadGroup, Supplier<Long> idSupplier)
193195
{
194196
this.simulatedExecution = simulatedExecution;
195197
this.interceptorOfGlobalMethods = interceptorOfGlobalMethods;
196198
this.classLoader = classLoader;
197199
this.threadGroup = threadGroup;
198200
this.transferToInstance = IsolatedExecutor.transferTo(classLoader);
201+
this.idSupplier = idSupplier;
199202
}
200203

201204
public InterceptibleThreadFactory factory(String name)
@@ -232,7 +235,7 @@ <F extends ThreadFactory> F factory(String name, Object extraInfo, ThreadGroup t
232235
else if (!this.threadGroup.parentOf(threadGroup)) throw new IllegalArgumentException();
233236
Runnable onTermination = transferToInstance.apply((SerializableRunnable)FastThreadLocal::removeAll);
234237
LocalTime time = transferToInstance.apply((SerializableCallable<LocalTime>) SimulatedTime.Global::current).call();
235-
return factory.create(name, Thread.NORM_PRIORITY, classLoader, uncaughtExceptionHandler, threadGroup, onTermination, time, this, extraInfo);
238+
return factory.create(name, Thread.NORM_PRIORITY, classLoader, uncaughtExceptionHandler, threadGroup, onTermination, time, this, extraInfo, idSupplier);
236239
}
237240

238241
@Override

test/simulator/main/org/apache/cassandra/simulator/systems/SimulatedExecution.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.concurrent.Callable;
2222
import java.util.concurrent.ScheduledFuture;
2323
import java.util.concurrent.atomic.AtomicInteger;
24+
import java.util.concurrent.atomic.AtomicLong;
2425
import java.util.function.Function;
2526

2627
import org.apache.cassandra.simulator.ActionList;
@@ -41,6 +42,7 @@
4142

4243
public class SimulatedExecution implements InterceptorOfExecution
4344
{
45+
private final AtomicLong idSupplier = new AtomicLong(0);
4446
static class NoExecutorMarker implements InterceptingExecutor
4547
{
4648
static final NoExecutorMarker INFINITE_LOOP = new NoExecutorMarker();
@@ -160,7 +162,7 @@ public SimulatedExecution()
160162

161163
public InterceptingExecutorFactory factory(InterceptorOfGlobalMethods interceptorOfGlobalMethods, ClassLoader classLoader, ThreadGroup threadGroup)
162164
{
163-
return new InterceptingExecutorFactory(this, interceptorOfGlobalMethods, classLoader, threadGroup);
165+
return new InterceptingExecutorFactory(this, interceptorOfGlobalMethods, classLoader, threadGroup, idSupplier::incrementAndGet);
164166
}
165167

166168
public InterceptExecution intercept()

test/simulator/test/org/apache/cassandra/simulator/test/ShortPaxosSimulationTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
package org.apache.cassandra.simulator.test;
2020

21-
import org.junit.Ignore;
2221
import org.junit.Test;
2322

2423
import org.apache.cassandra.simulator.paxos.PaxosSimulationRunner;
@@ -105,7 +104,6 @@ public void simulationTest()
105104
}
106105

107106
@Test
108-
@Ignore("fails due to OOM DirectMemory - unclear why")
109107
public void selfReconcileTest()
110108
{
111109
PaxosSimulationRunner.executeWithExceptionThrowing(new String[] { "reconcile",
@@ -114,7 +112,9 @@ public void selfReconcileTest()
114112
"-c", "2",
115113
"--cluster-action-limit", "2",
116114
"-s", "30",
117-
"--with-self" });
115+
"--with-self",
116+
"--with-rng", "0",
117+
"--with-time", "0",});
118118
}
119119
}
120120

0 commit comments

Comments
 (0)