use of java.util.concurrent.Callable in project lucene-solr by apache.
the class TestDocBasedVersionConstraints method testConcurrentAdds.
/**
* Constantly hammer the same doc with multiple concurrent threads and diff versions,
* confirm that the highest version wins.
*/
public void testConcurrentAdds() throws Exception {
final int NUM_DOCS = atLeast(50);
final int MAX_CONCURENT = atLeast(10);
ExecutorService runner = ExecutorUtil.newMDCAwareFixedThreadPool(MAX_CONCURENT, new DefaultSolrThreadFactory("TestDocBasedVersionConstraints"));
// runner = Executors.newFixedThreadPool(1); // to test single threaded
try {
for (int id = 0; id < NUM_DOCS; id++) {
final int numAdds = TestUtil.nextInt(random(), 3, MAX_CONCURENT);
final int winner = TestUtil.nextInt(random(), 0, numAdds - 1);
final int winnerVersion = atLeast(100);
final boolean winnerIsDeleted = (0 == TestUtil.nextInt(random(), 0, 4));
List<Callable<Object>> tasks = new ArrayList<>(numAdds);
for (int variant = 0; variant < numAdds; variant++) {
final boolean iShouldWin = (variant == winner);
final long version = (iShouldWin ? winnerVersion : TestUtil.nextInt(random(), 1, winnerVersion - 1));
if ((iShouldWin && winnerIsDeleted) || (!iShouldWin && 0 == TestUtil.nextInt(random(), 0, 4))) {
tasks.add(delayedDelete("" + id, "" + version));
} else {
tasks.add(delayedAdd("id", "" + id, "name", "name" + id + "_" + variant, "my_version_l", "" + version));
}
}
runner.invokeAll(tasks);
final String expectedDoc = "{'id':'" + id + "','my_version_l':" + winnerVersion + (!winnerIsDeleted ? ",'name':'name" + id + "_" + winner + "'}" : "}");
assertJQ(req("qt", "/get", "id", "" + id, "fl", "id,name,my_version_l"), "=={'doc':" + expectedDoc + "}");
assertU(commit());
assertJQ(req("q", "id:" + id, "fl", "id,name,my_version_l"), "/response/numFound==1", "/response/docs==[" + expectedDoc + "]");
}
} finally {
ExecutorUtil.shutdownAndAwaitTermination(runner);
}
}
use of java.util.concurrent.Callable in project incubator-systemml by apache.
the class LibMatrixDNNHelper method getConv2dWorkers.
/**
* Factory method that returns list of callable tasks for performing conv2d
*
* @param params convolution parameters
* @return list of callable tasks for performing conv2d
* @throws DMLRuntimeException if error occurs
*/
public static ArrayList<Callable<Long>> getConv2dWorkers(ConvolutionParameters params) throws DMLRuntimeException {
ArrayList<Callable<Long>> ret = new ArrayList<Callable<Long>>();
// Try to create as many tasks as threads.
// Creating more tasks will help in tail, but would have additional overhead of maintaining the intermediate
// data structures such as im2col blocks.
int k = OptimizerUtils.getConstrainedNumThreads(params.numThreads);
int taskSize = (int) (Math.ceil((double) params.N / k));
// TODO: Decide here based on params whether to use LoopedIm2ColConv2dAllChannels or LoopedIm2ColConv2dOneChannel
// For now, let's stick to the existing approach of converting [1, CHW] to [CRS, PQ] as it allows matrix multiplication large enough matrix.
boolean allChannels = true;
ArrayList<MatrixBlock> filters = null;
if (!allChannels) {
filters = splitFilter(params);
}
boolean isEmptyDenseInput = !params.input1.isInSparseFormat() && params.input1.denseBlock == null;
for (int i = 0; i * taskSize < params.N; i++) {
if (LibMatrixDNN.isEligibleForConv2dSparse(params))
ret.add(new LibMatrixDNNConv2dHelper.SparseNativeConv2d(i * taskSize, Math.min((i + 1) * taskSize, params.N), params));
else if (!isEmptyDenseInput && allChannels)
ret.add(new LibMatrixDNNConv2dHelper.LoopedIm2ColConv2dAllChannels(i * taskSize, Math.min((i + 1) * taskSize, params.N), params));
else if (!isEmptyDenseInput && !allChannels)
ret.add(new LibMatrixDNNConv2dHelper.LoopedIm2ColConv2dOneChannel(i * taskSize, Math.min((i + 1) * taskSize, params.N), params, filters));
else
throw new DMLRuntimeException("Unsupported operator");
}
return ret;
}
use of java.util.concurrent.Callable in project geode by apache.
the class ClientAuthorizationTestCase method doOp.
protected static void doOp(OperationCode op, final int[] indices, final int flagsI, final int expectedResult) throws InterruptedException {
boolean operationOmitted = false;
final int flags = flagsI;
Region region = getRegion();
if ((flags & OpFlags.USE_SUBREGION) > 0) {
assertNotNull(region);
Region subregion = null;
if ((flags & OpFlags.NO_CREATE_SUBREGION) > 0) {
if ((flags & OpFlags.CHECK_NOREGION) > 0) {
// Wait for some time for DRF update to come
waitForCondition(() -> getSubregion() == null);
subregion = getSubregion();
assertNull(subregion);
return;
} else {
// Wait for some time for DRF update to come
waitForCondition(() -> getSubregion() != null);
subregion = getSubregion();
assertNotNull(subregion);
}
} else {
subregion = createSubregion(region);
}
assertNotNull(subregion);
region = subregion;
} else if ((flags & OpFlags.CHECK_NOREGION) > 0) {
// Wait for some time for region destroy update to come
waitForCondition(() -> getRegion() == null);
region = getRegion();
assertNull(region);
return;
} else {
assertNotNull(region);
}
final String[] keys = KEYS;
final String[] vals;
if ((flags & OpFlags.USE_NEWVAL) > 0) {
vals = NVALUES;
} else {
vals = VALUES;
}
InterestResultPolicy policy = InterestResultPolicy.KEYS_VALUES;
if ((flags & OpFlags.REGISTER_POLICY_NONE) > 0) {
policy = InterestResultPolicy.NONE;
}
final int numOps = indices.length;
System.out.println("Got doOp for op: " + op.toString() + ", numOps: " + numOps + ", indices: " + indicesToString(indices) + ", expect: " + expectedResult);
boolean exceptionOccurred = false;
boolean breakLoop = false;
if (op.isGet() || op.isContainsKey() || op.isKeySet() || op.isQuery() || op.isExecuteCQ()) {
Thread.sleep(PAUSE);
}
for (int indexIndex = 0; indexIndex < numOps; ++indexIndex) {
if (breakLoop) {
break;
}
int index = indices[indexIndex];
try {
final Object key = keys[index];
final Object expectedVal = vals[index];
if (op.isGet()) {
Object value = null;
// this is the case for testing GET_ALL
if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
breakLoop = true;
List keyList = new ArrayList(numOps);
Object searchKey;
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
searchKey = keys[keyNum];
keyList.add(searchKey);
// local invalidate some KEYS to force fetch of those KEYS from server
if ((flags & OpFlags.CHECK_NOKEY) > 0) {
AbstractRegionEntry entry = (AbstractRegionEntry) ((LocalRegion) region).getRegionEntry(searchKey);
System.out.println("" + keyNum + ": key is " + searchKey + " and entry is " + entry);
assertFalse(region.containsKey(searchKey));
} else {
if (keyNumIndex % 2 == 1) {
assertTrue(region.containsKey(searchKey));
region.localInvalidate(searchKey);
}
}
}
Map entries = region.getAll(keyList);
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
searchKey = keys[keyNum];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(entries.containsKey(searchKey));
} else {
assertTrue(entries.containsKey(searchKey));
value = entries.get(searchKey);
assertEquals(vals[keyNum], value);
}
}
break;
}
if ((flags & OpFlags.LOCAL_OP) > 0) {
Callable<Boolean> condition = new Callable<Boolean>() {
private Region region;
@Override
public Boolean call() throws Exception {
Object value = getLocalValue(region, key);
return (flags & OpFlags.CHECK_FAIL) > 0 ? !expectedVal.equals(value) : expectedVal.equals(value);
}
public Callable<Boolean> init(Region region) {
this.region = region;
return this;
}
}.init(region);
waitForCondition(condition);
value = getLocalValue(region, key);
} else if ((flags & OpFlags.USE_GET_ENTRY_IN_TX) > 0) {
getCache().getCacheTransactionManager().begin();
Entry e = region.getEntry(key);
// Also, check getAll()
ArrayList a = new ArrayList();
a.addAll(a);
region.getAll(a);
getCache().getCacheTransactionManager().commit();
value = e.getValue();
} else {
if ((flags & OpFlags.CHECK_NOKEY) > 0) {
assertFalse(region.containsKey(key));
} else {
assertTrue(region.containsKey(key) || ((LocalRegion) region).getRegionEntry(key).isTombstone());
region.localInvalidate(key);
}
value = region.get(key);
}
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(expectedVal.equals(value));
} else {
assertNotNull(value);
assertEquals(expectedVal, value);
}
} else if (op.isPut()) {
region.put(key, expectedVal);
} else if (op.isPutAll()) {
HashMap map = new HashMap();
for (int i = 0; i < indices.length; i++) {
map.put(keys[indices[i]], vals[indices[i]]);
}
region.putAll(map);
breakLoop = true;
} else if (op.isDestroy()) {
// }
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localDestroy(key);
} else {
region.destroy(key);
}
} else if (op.isInvalidate()) {
if (region.containsKey(key)) {
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localInvalidate(key);
} else {
region.invalidate(key);
}
}
} else if (op.isContainsKey()) {
boolean result;
if ((flags & OpFlags.LOCAL_OP) > 0) {
result = region.containsKey(key);
} else {
result = region.containsKeyOnServer(key);
}
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(result);
} else {
assertTrue(result);
}
} else if (op.isRegisterInterest()) {
if ((flags & OpFlags.USE_LIST) > 0) {
breakLoop = true;
// Register interest list in this case
List keyList = new ArrayList(numOps);
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
keyList.add(keys[keyNum]);
}
region.registerInterest(keyList, policy);
} else if ((flags & OpFlags.USE_REGEX) > 0) {
breakLoop = true;
region.registerInterestRegex("key[1-" + numOps + ']', policy);
} else if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
breakLoop = true;
region.registerInterest("ALL_KEYS", policy);
} else {
region.registerInterest(key, policy);
}
} else if (op.isUnregisterInterest()) {
if ((flags & OpFlags.USE_LIST) > 0) {
breakLoop = true;
// Register interest list in this case
List keyList = new ArrayList(numOps);
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
keyList.add(keys[keyNum]);
}
region.unregisterInterest(keyList);
} else if ((flags & OpFlags.USE_REGEX) > 0) {
breakLoop = true;
region.unregisterInterestRegex("key[1-" + numOps + ']');
} else if ((flags & OpFlags.USE_ALL_KEYS) > 0) {
breakLoop = true;
region.unregisterInterest("ALL_KEYS");
} else {
region.unregisterInterest(key);
}
} else if (op.isKeySet()) {
breakLoop = true;
Set keySet;
if ((flags & OpFlags.LOCAL_OP) > 0) {
keySet = region.keySet();
} else {
keySet = region.keySetOnServer();
}
assertNotNull(keySet);
if ((flags & OpFlags.CHECK_FAIL) == 0) {
assertEquals(numOps, keySet.size());
}
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(keySet.contains(keys[keyNum]));
} else {
assertTrue(keySet.contains(keys[keyNum]));
}
}
} else if (op.isQuery()) {
breakLoop = true;
SelectResults queryResults = region.query("SELECT DISTINCT * FROM " + region.getFullPath());
assertNotNull(queryResults);
Set queryResultSet = queryResults.asSet();
if ((flags & OpFlags.CHECK_FAIL) == 0) {
assertEquals(numOps, queryResultSet.size());
}
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(queryResultSet.contains(vals[keyNum]));
} else {
assertTrue(queryResultSet.contains(vals[keyNum]));
}
}
} else if (op.isExecuteCQ()) {
breakLoop = true;
QueryService queryService = getCache().getQueryService();
CqQuery cqQuery;
if ((cqQuery = queryService.getCq("cq1")) == null) {
CqAttributesFactory cqFact = new CqAttributesFactory();
cqFact.addCqListener(new AuthzCqListener());
CqAttributes cqAttrs = cqFact.create();
cqQuery = queryService.newCq("cq1", "SELECT * FROM " + region.getFullPath(), cqAttrs);
}
if ((flags & OpFlags.LOCAL_OP) > 0) {
// Interpret this as testing results using CqListener
final AuthzCqListener listener = (AuthzCqListener) cqQuery.getCqAttributes().getCqListener();
WaitCriterion ev = new WaitCriterion() {
@Override
public boolean done() {
if ((flags & OpFlags.CHECK_FAIL) > 0) {
return 0 == listener.getNumUpdates();
} else {
return numOps == listener.getNumUpdates();
}
}
@Override
public String description() {
return null;
}
};
waitForCriterion(ev, 3 * 1000, 200, true);
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertEquals(0, listener.getNumUpdates());
} else {
assertEquals(numOps, listener.getNumUpdates());
listener.checkPuts(vals, indices);
}
assertEquals(0, listener.getNumCreates());
assertEquals(0, listener.getNumDestroys());
assertEquals(0, listener.getNumOtherOps());
assertEquals(0, listener.getNumErrors());
} else {
SelectResults cqResults = cqQuery.executeWithInitialResults();
assertNotNull(cqResults);
Set cqResultValues = new HashSet();
for (Object o : cqResults.asList()) {
Struct s = (Struct) o;
cqResultValues.add(s.get("value"));
}
Set cqResultSet = cqResults.asSet();
if ((flags & OpFlags.CHECK_FAIL) == 0) {
assertEquals(numOps, cqResultSet.size());
}
for (int keyNumIndex = 0; keyNumIndex < numOps; ++keyNumIndex) {
int keyNum = indices[keyNumIndex];
if ((flags & OpFlags.CHECK_FAIL) > 0) {
assertFalse(cqResultValues.contains(vals[keyNum]));
} else {
assertTrue(cqResultValues.contains(vals[keyNum]));
}
}
}
} else if (op.isStopCQ()) {
breakLoop = true;
CqQuery cqQuery = getCache().getQueryService().getCq("cq1");
((AuthzCqListener) cqQuery.getCqAttributes().getCqListener()).reset();
cqQuery.stop();
} else if (op.isCloseCQ()) {
breakLoop = true;
CqQuery cqQuery = getCache().getQueryService().getCq("cq1");
((AuthzCqListener) cqQuery.getCqAttributes().getCqListener()).reset();
cqQuery.close();
} else if (op.isRegionClear()) {
breakLoop = true;
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localClear();
} else {
region.clear();
}
} else if (op.isRegionCreate()) {
breakLoop = true;
// Region subregion = createSubregion(region);
// subregion.createRegionOnServer();
// Create region on server using the DynamicRegionFactory
// Assume it has been already initialized
DynamicRegionFactory drf = DynamicRegionFactory.get();
Region subregion = drf.createDynamicRegion(regionName, SUBREGION_NAME);
assertEquals('/' + regionName + '/' + SUBREGION_NAME, subregion.getFullPath());
} else if (op.isRegionDestroy()) {
breakLoop = true;
if ((flags & OpFlags.LOCAL_OP) > 0) {
region.localDestroyRegion();
} else {
if ((flags & OpFlags.USE_SUBREGION) > 0) {
try {
DynamicRegionFactory.get().destroyDynamicRegion(region.getFullPath());
} catch (RegionDestroyedException ex) {
// harmless to ignore this
System.out.println("doOp: sub-region " + region.getFullPath() + " already destroyed");
operationOmitted = true;
}
} else {
region.destroyRegion();
}
}
} else {
fail("doOp: Unhandled operation " + op);
}
if (expectedResult != NO_EXCEPTION) {
if (!operationOmitted && !op.isUnregisterInterest()) {
fail("Expected an exception while performing operation op =" + op + "flags = " + OpFlags.description(flags));
}
}
} catch (Exception ex) {
exceptionOccurred = true;
if ((ex instanceof ServerConnectivityException || ex instanceof QueryInvocationTargetException || ex instanceof CqException) && (expectedResult == NOTAUTHZ_EXCEPTION) && (ex.getCause() instanceof NotAuthorizedException)) {
System.out.println("doOp: Got expected NotAuthorizedException when doing operation [" + op + "] with flags " + OpFlags.description(flags) + ": " + ex.getCause());
continue;
} else if (expectedResult == OTHER_EXCEPTION) {
System.out.println("doOp: Got expected exception when doing operation: " + ex.toString());
continue;
} else {
fail("doOp: Got unexpected exception when doing operation. Policy = " + policy + " flags = " + OpFlags.description(flags), ex);
}
}
}
if (!exceptionOccurred && !operationOmitted && expectedResult != NO_EXCEPTION) {
fail("Expected an exception while performing operation: " + op + " flags = " + OpFlags.description(flags));
}
}
use of java.util.concurrent.Callable in project geode by apache.
the class OneTaskOnlyDecoratorJUnitTest method testExecuteOnlyOnce.
/**
* Test to make sure we only execute the task once no matter how many times we schedule it.
*/
@Test
public void testExecuteOnlyOnce() throws Exception {
ScheduledExecutorService ex = Executors.newScheduledThreadPool(1);
MyConflationListener listener = new MyConflationListener();
OneTaskOnlyExecutor decorator = new OneTaskOnlyExecutor(ex, listener);
final CountDownLatch latch = new CountDownLatch(1);
ex.submit(new Callable() {
public Object call() throws Exception {
latch.await();
return null;
}
});
final AtomicInteger counter = new AtomicInteger();
Runnable increment = new Runnable() {
public void run() {
counter.incrementAndGet();
}
};
for (int i = 0; i < 50; i++) {
decorator.schedule(increment, 0, TimeUnit.SECONDS);
}
assertEquals(0, counter.get());
latch.countDown();
ex.shutdown();
ex.awaitTermination(60, TimeUnit.SECONDS);
assertEquals(1, counter.get());
assertEquals(49, listener.getDropCount());
}
use of java.util.concurrent.Callable in project geode by apache.
the class OutOfOffHeapMemoryDUnitTest method testOtherMembersSeeOutOfOffHeapMemoryMemberDisconnects.
@Test
public void testOtherMembersSeeOutOfOffHeapMemoryMemberDisconnects() {
final int vmCount = Host.getHost(0).getVMCount();
final String name = getRegionName();
final RegionShortcut shortcut = getRegionShortcut();
final int biggerVM = 0;
final int smallerVM = 1;
Host.getHost(0).getVM(smallerVM).invoke(new SerializableRunnable() {
public void run() {
OutOfOffHeapMemoryDUnitTest.isSmallerVM.set(true);
}
});
// create off-heap region in all members
for (int i = 0; i < vmCount; i++) {
Host.getHost(0).getVM(i).invoke(new SerializableRunnable() {
public void run() {
OutOfOffHeapMemoryDUnitTest.cache.set(getCache());
OutOfOffHeapMemoryDUnitTest.system.set(getSystem());
final Region<Object, Object> region = OutOfOffHeapMemoryDUnitTest.cache.get().createRegionFactory(shortcut).setOffHeap(true).create(name);
assertNotNull(region);
}
});
}
// make sure there are vmCount+1 members total
for (int i = 0; i < vmCount; i++) {
Host.getHost(0).getVM(i).invoke(new SerializableRunnable() {
public void run() {
assertFalse(OutOfOffHeapMemoryDUnitTest.cache.get().isClosed());
assertTrue(OutOfOffHeapMemoryDUnitTest.system.get().isConnected());
// +1 for locator
final int countMembersPlusLocator = vmCount + 1;
// -1 one for self
final int countOtherMembers = vmCount - 1;
assertEquals(countMembersPlusLocator, ((InternalDistributedSystem) OutOfOffHeapMemoryDUnitTest.system.get()).getDistributionManager().getDistributionManagerIds().size());
assertEquals(countOtherMembers, ((DistributedRegion) OutOfOffHeapMemoryDUnitTest.cache.get().getRegion(name)).getDistributionAdvisor().getNumProfiles());
}
});
}
// perform puts in bigger member until smaller member goes OOOHME
Host.getHost(0).getVM(biggerVM).invoke(new SerializableRunnable() {
public void run() {
final long TIME_LIMIT = 30 * 1000;
final StopWatch stopWatch = new StopWatch(true);
// -1 for self
int countOtherMembers = vmCount - 1;
// -1 for self, -1 for smallerVM
final int countOtherMembersMinusSmaller = vmCount - 1 - 1;
final Region<Object, Object> region = OutOfOffHeapMemoryDUnitTest.cache.get().getRegion(name);
for (int i = 0; countOtherMembers > countOtherMembersMinusSmaller; i++) {
region.put("key-" + i, new byte[1024]);
countOtherMembers = ((DistributedRegion) OutOfOffHeapMemoryDUnitTest.cache.get().getRegion(name)).getDistributionAdvisor().getNumProfiles();
assertTrue("puts failed to push member out of off-heap memory within time limit", stopWatch.elapsedTimeMillis() < TIME_LIMIT);
}
assertEquals("Member did not depart from OutOfOffHeapMemory", countOtherMembersMinusSmaller, countOtherMembers);
}
});
// verify that member with OOOHME closed
Host.getHost(0).getVM(smallerVM).invoke(new SerializableRunnable() {
public void run() {
assertTrue(OutOfOffHeapMemoryDUnitTest.cache.get().isClosed());
assertFalse(OutOfOffHeapMemoryDUnitTest.system.get().isConnected());
}
});
// verify that all other members noticed smaller member closed
for (int i = 0; i < vmCount; i++) {
if (i == smallerVM) {
continue;
}
Host.getHost(0).getVM(i).invoke(new SerializableRunnable() {
public void run() {
// +1 for locator, -1 for OOOHME
final int countMembersPlusLocator = vmCount + 1 - 1;
// member
// -1 for self, -1 for OOOHME member
final int countOtherMembers = vmCount - 1 - 1;
with().pollInterval(10, TimeUnit.MILLISECONDS).await().atMost(30, TimeUnit.SECONDS).until(numDistributionManagers(), equalTo(countMembersPlusLocator));
with().pollInterval(10, TimeUnit.MILLISECONDS).await().atMost(30, TimeUnit.SECONDS).until(numProfiles(), equalTo(countOtherMembers));
}
private Callable<Integer> numProfiles() {
return () -> {
DistributedRegion dr = (DistributedRegion) OutOfOffHeapMemoryDUnitTest.cache.get().getRegion(name);
return dr.getDistributionAdvisor().getNumProfiles();
};
}
private Callable<Integer> numDistributionManagers() {
return () -> {
InternalDistributedSystem ids = (InternalDistributedSystem) OutOfOffHeapMemoryDUnitTest.system.get();
return ids.getDistributionManager().getDistributionManagerIds().size();
};
}
});
}
}
Aggregations