Repository: geode Updated Branches: refs/heads/develop 6619a3de9 -> ad3e35e76
GEODE-11: add Fixed PR dunit tests for gemfire lucene Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/ad3e35e7 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/ad3e35e7 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/ad3e35e7 Branch: refs/heads/develop Commit: ad3e35e76a0a19f523c9fb77ef6cc7074c587172 Parents: 6619a3d Author: zhouxh <[email protected]> Authored: Wed Jan 25 17:43:39 2017 -0800 Committer: zhouxh <[email protected]> Committed: Wed Jan 25 18:07:23 2017 -0800 ---------------------------------------------------------------------- .../lucene/LuceneIndexCreationDUnitTest.java | 43 ++++---- .../LuceneIndexCreationOnFixedPRDUnitTest.java | 49 +++++++++ .../LuceneQueriesPeerFixedPRDUnitTest.java | 39 +++++++ .../cache/lucene/test/LuceneTestUtilities.java | 101 +++++++++++++++++++ 4 files changed, 213 insertions(+), 19 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/ad3e35e7/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationDUnitTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationDUnitTest.java index e58f713..6a02a25 100644 --- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationDUnitTest.java +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationDUnitTest.java @@ -227,7 +227,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { dataStore2.invoke(() -> initDataStore(createIndex)); } - private final Object[] getIndexes() { + protected final Object[] getIndexes() { return $(new Object[] {getFieldsIndexWithOneField()}, new Object[] {getFieldsIndexWithTwoFields()}, new Object[] {get2FieldsIndexes()}, new Object[] {getAnalyzersIndexWithOneField()}, @@ -272,7 +272,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { - private final Object[] getXmlAndExceptionMessages() { + protected final Object[] getXmlAndExceptionMessages() { return $( new Object[] {"verifyDifferentFieldsFails", CANNOT_CREATE_LUCENE_INDEX_DIFFERENT_FIELDS}, new Object[] {"verifyDifferentFieldAnalyzerSizesFails1", @@ -312,12 +312,17 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { dataStore2.invoke(() -> initDataStore(createIndex2)); } - private String getXmlFileForTest(String testName) { + protected String getXmlFileForTest(String testName) { return TestUtil.getResourcePath(getClass(), - getClass().getSimpleName() + "." + testName + ".cache.xml"); + getClassSimpleName() + "." + testName + ".cache.xml"); } - private void initDataStore(SerializableRunnableIF createIndex, String message) throws Exception { + protected String getClassSimpleName() { + return getClass().getSimpleName(); + } + + protected void initDataStore(SerializableRunnableIF createIndex, String message) + throws Exception { createIndex.run(); try { getCache().createRegionFactory(RegionShortcut.PARTITION).create(REGION_NAME); @@ -327,11 +332,11 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { } } - private void initCache(String cacheXmlFileName) throws FileNotFoundException { + protected void initCache(String cacheXmlFileName) throws FileNotFoundException { getCache().loadCacheXml(new FileInputStream(cacheXmlFileName)); } - private void initCache(String cacheXmlFileName, String message) throws FileNotFoundException { + protected void initCache(String cacheXmlFileName, String message) throws FileNotFoundException { try { getCache().loadCacheXml(new FileInputStream(cacheXmlFileName)); fail("Should not have been able to create cache"); @@ -340,21 +345,21 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { } } - private SerializableRunnableIF getFieldsIndexWithOneField() { + protected SerializableRunnableIF getFieldsIndexWithOneField() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); luceneService.createIndex(INDEX_NAME, REGION_NAME, "field1"); }; } - private SerializableRunnableIF getFieldsIndexWithTwoFields() { + protected SerializableRunnableIF getFieldsIndexWithTwoFields() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); luceneService.createIndex(INDEX_NAME, REGION_NAME, "field1", "field2"); }; } - private SerializableRunnableIF get2FieldsIndexes() { + protected SerializableRunnableIF get2FieldsIndexes() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); luceneService.createIndex(INDEX_NAME + "_1", REGION_NAME, "field1"); @@ -362,7 +367,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private SerializableRunnableIF getMultipleIndexes(final int numberOfIndexes) { + protected SerializableRunnableIF getMultipleIndexes(final int numberOfIndexes) { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); for (int count = 1; count <= numberOfIndexes; count++) { @@ -371,13 +376,13 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private void verifyIndexList(final int expectedSize) { + protected void verifyIndexList(final int expectedSize) { LuceneService luceneService = LuceneServiceProvider.get(getCache()); Collection<LuceneIndex> indexList = luceneService.getAllIndexes(); assertEquals(indexList.size(), expectedSize); } - private void verifyIndexes(final int numberOfIndexes) { + protected void verifyIndexes(final int numberOfIndexes) { LuceneService luceneService = LuceneServiceProvider.get(getCache()); for (int count = 1; count <= numberOfIndexes; count++) { assertEquals(luceneService.getIndex(INDEX_NAME + "_" + count, REGION_NAME).getName(), @@ -385,7 +390,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { } } - private SerializableRunnableIF getAnalyzersIndexWithNullField1() { + protected SerializableRunnableIF getAnalyzersIndexWithNullField1() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); Map<String, Analyzer> analyzers = new HashMap<>(); @@ -395,7 +400,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private SerializableRunnableIF getAnalyzersIndexWithNullField2() { + protected SerializableRunnableIF getAnalyzersIndexWithNullField2() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); Map<String, Analyzer> analyzers = new HashMap<>(); @@ -405,7 +410,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private SerializableRunnableIF getAnalyzersIndexWithOneField( + protected SerializableRunnableIF getAnalyzersIndexWithOneField( Class<? extends Analyzer> analyzerClass) { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); @@ -415,7 +420,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private SerializableRunnableIF getAnalyzersIndexWithOneField() { + protected SerializableRunnableIF getAnalyzersIndexWithOneField() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); Map<String, Analyzer> analyzers = new HashMap<>(); @@ -424,7 +429,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private SerializableRunnableIF getAnalyzersIndexWithTwoFields() { + protected SerializableRunnableIF getAnalyzersIndexWithTwoFields() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); Map<String, Analyzer> analyzers = new HashMap<>(); @@ -434,7 +439,7 @@ public class LuceneIndexCreationDUnitTest extends LuceneDUnitTest { }; } - private SerializableRunnableIF getAnalyzersIndexWithTwoFields2() { + protected SerializableRunnableIF getAnalyzersIndexWithTwoFields2() { return () -> { LuceneService luceneService = LuceneServiceProvider.get(getCache()); Map<String, Analyzer> analyzers = new HashMap<>(); http://git-wip-us.apache.org/repos/asf/geode/blob/ad3e35e7/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationOnFixedPRDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationOnFixedPRDUnitTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationOnFixedPRDUnitTest.java new file mode 100644 index 0000000..7de12e3 --- /dev/null +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneIndexCreationOnFixedPRDUnitTest.java @@ -0,0 +1,49 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.cache.lucene; + +import org.apache.geode.cache.lucene.test.LuceneTestUtilities; +import org.apache.geode.test.dunit.SerializableRunnableIF; +import org.apache.geode.test.junit.categories.DistributedTest; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import junitparams.JUnitParamsRunner; + +@Category(DistributedTest.class) +@RunWith(JUnitParamsRunner.class) +public class LuceneIndexCreationOnFixedPRDUnitTest extends LuceneIndexCreationDUnitTest { + @Override + protected void initDataStore(SerializableRunnableIF createIndex) throws Exception { + createIndex.run(); + LuceneTestUtilities.initDataStoreForFixedPR(getCache()); + } + + protected void initDataStore(SerializableRunnableIF createIndex, String message) + throws Exception { + try { + initDataStore(createIndex); + fail("Should not have been able to create index"); + } catch (IllegalStateException e) { + assertEquals(message, e.getMessage()); + } + } + + protected String getClassSimpleName() { + return getClass().getSuperclass().getSimpleName(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/ad3e35e7/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneQueriesPeerFixedPRDUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneQueriesPeerFixedPRDUnitTest.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneQueriesPeerFixedPRDUnitTest.java new file mode 100644 index 0000000..dd9fa6b --- /dev/null +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/LuceneQueriesPeerFixedPRDUnitTest.java @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.cache.lucene; + +import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.REGION_NAME; + +import org.apache.geode.cache.lucene.test.LuceneTestUtilities; +import org.apache.geode.test.dunit.SerializableRunnableIF; +import org.apache.geode.test.junit.categories.DistributedTest; +import org.junit.experimental.categories.Category; + +@Category(DistributedTest.class) +public class LuceneQueriesPeerFixedPRDUnitTest extends LuceneQueriesPRBase { + + @Override + protected void initAccessor(SerializableRunnableIF createIndex) throws Exception { + createIndex.run(); + getCache(); + LuceneTestUtilities.createFixedPartitionedRegion(getCache(), REGION_NAME, null, 0); + } + + @Override + protected void initDataStore(SerializableRunnableIF createIndex) throws Exception { + createIndex.run(); + LuceneTestUtilities.initDataStoreForFixedPR(getCache()); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/ad3e35e7/geode-lucene/src/test/java/org/apache/geode/cache/lucene/test/LuceneTestUtilities.java ---------------------------------------------------------------------- diff --git a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/test/LuceneTestUtilities.java b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/test/LuceneTestUtilities.java index 21c0bbc..c4081c5 100644 --- a/geode-lucene/src/test/java/org/apache/geode/cache/lucene/test/LuceneTestUtilities.java +++ b/geode-lucene/src/test/java/org/apache/geode/cache/lucene/test/LuceneTestUtilities.java @@ -14,16 +14,25 @@ */ package org.apache.geode.cache.lucene.test; +import static org.apache.geode.cache.lucene.test.LuceneTestUtilities.REGION_NAME; import static org.junit.Assert.*; +import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.function.Consumer; +import org.apache.geode.cache.AttributesFactory; import org.apache.geode.cache.Cache; +import org.apache.geode.cache.EntryOperation; +import org.apache.geode.cache.FixedPartitionAttributes; +import org.apache.geode.cache.FixedPartitionResolver; +import org.apache.geode.cache.PartitionAttributesFactory; +import org.apache.geode.cache.Region; import org.apache.geode.cache.asyncqueue.AsyncEventQueue; import org.apache.geode.cache.asyncqueue.internal.AsyncEventQueueImpl; import org.apache.geode.cache.lucene.LuceneIndex; @@ -34,10 +43,14 @@ import org.apache.geode.cache.lucene.LuceneService; import org.apache.geode.cache.lucene.LuceneServiceProvider; import org.apache.geode.cache.lucene.internal.LuceneIndexForPartitionedRegion; import org.apache.geode.cache.lucene.internal.LuceneServiceImpl; +import org.apache.geode.cache.persistence.PartitionOfflineException; +import org.apache.geode.internal.cache.ForceReattemptException; import org.apache.geode.internal.cache.LocalRegion; import org.apache.geode.internal.cache.wan.AbstractGatewaySender; import org.apache.geode.pdx.JSONFormatter; import org.apache.geode.pdx.PdxInstance; +import org.apache.geode.test.dunit.IgnoredException; +import org.apache.geode.test.dunit.VM; public class LuceneTestUtilities { public static final String INDEX_NAME = "index"; @@ -65,6 +78,94 @@ public class LuceneTestUtilities { public static final String CANNOT_CREATE_LUCENE_INDEX_DIFFERENT_INDEXES_3 = "Cannot create Region /region with [index#_region] async event ids because another cache has the same region defined with [] async event ids"; + public static String Quarter1 = "Q1"; + public static String Quarter2 = "Q2"; + public static String Quarter3 = "Q3"; + public static String Quarter4 = "Q4"; + + public static void initDataStoreForFixedPR(final Cache cache) throws Exception { + List<FixedPartitionAttributes> fpaList = new ArrayList<FixedPartitionAttributes>(); + int vmNum = VM.getCurrentVMNum(); + if (vmNum % 2 == 0) { + FixedPartitionAttributes fpa1 = FixedPartitionAttributes.createFixedPartition(Quarter1, true); + FixedPartitionAttributes fpa2 = + FixedPartitionAttributes.createFixedPartition(Quarter2, false); + fpaList.clear(); + fpaList.add(fpa1); + fpaList.add(fpa2); + } else { + FixedPartitionAttributes fpa1 = + FixedPartitionAttributes.createFixedPartition(Quarter1, false); + FixedPartitionAttributes fpa2 = FixedPartitionAttributes.createFixedPartition(Quarter2, true); + fpaList.clear(); + fpaList.add(fpa1); + fpaList.add(fpa2); + } + + createFixedPartitionedRegion(cache, REGION_NAME, fpaList, 40); + } + + public static void createFixedPartitionedRegion(final Cache cache, String regionName, + List<FixedPartitionAttributes> fpaList, int localMaxMemory) { + List<String> allPartitions = new ArrayList(); + if (fpaList != null) { + for (FixedPartitionAttributes fpa : fpaList) { + allPartitions.add(fpa.getPartitionName()); + } + } else { + allPartitions.add("Q1"); + allPartitions.add("Q2"); + } + + AttributesFactory fact = new AttributesFactory(); + + PartitionAttributesFactory pfact = new PartitionAttributesFactory(); + pfact.setTotalNumBuckets(16); + pfact.setRedundantCopies(1); + pfact.setLocalMaxMemory(localMaxMemory); + if (fpaList != null) { + for (FixedPartitionAttributes fpa : fpaList) { + pfact.addFixedPartitionAttributes(fpa); + } + } + pfact.setPartitionResolver(new MyFixedPartitionResolver(allPartitions)); + fact.setPartitionAttributes(pfact.create()); + Region r = cache.createRegionFactory(fact.create()).create(regionName); + assertNotNull(r); + } + + static class MyFixedPartitionResolver implements FixedPartitionResolver { + + private final List<String> allPartitions; + + public MyFixedPartitionResolver(final List<String> allPartitions) { + this.allPartitions = allPartitions; + } + + @Override + public String getPartitionName(final EntryOperation opDetails, + @Deprecated final Set targetPartitions) { + int hash = Math.abs(opDetails.getKey().hashCode() % allPartitions.size()); + return allPartitions.get(hash); + } + + @Override + public Object getRoutingObject(final EntryOperation opDetails) { + return opDetails.getKey(); + } + + @Override + public String getName() { + return getClass().getName(); + } + + @Override + public void close() { + + } + + } + public static void verifyInternalRegions(LuceneService luceneService, Cache cache, Consumer<LocalRegion> verify) { // Get index
