Skip to content

Commit

Permalink
Geode 7424 create index (apache#4458)
Browse files Browse the repository at this point in the history
co-authored-by: Jinmei Liao <[email protected]>
  • Loading branch information
2 people authored and jinmeiliao committed Dec 13, 2019
1 parent 7d471f9 commit c4fc290
Show file tree
Hide file tree
Showing 35 changed files with 931 additions and 245 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,27 @@
import org.junit.ClassRule;
import org.junit.Test;

import org.apache.geode.cache.configuration.RegionConfig;
import org.apache.geode.distributed.internal.InternalConfigurationPersistenceService;
import org.apache.geode.lang.Identifiable;
import org.apache.geode.management.api.ClusterManagementService;
import org.apache.geode.management.client.ClusterManagementServiceBuilder;
import org.apache.geode.management.configuration.Index;
import org.apache.geode.management.configuration.IndexType;
import org.apache.geode.management.configuration.Region;
import org.apache.geode.management.configuration.RegionType;
import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;
import org.apache.geode.test.junit.rules.GeodeDevRestClient;
import org.apache.geode.test.junit.rules.MemberStarterRule;
import org.apache.geode.test.junit.rules.RequiresGeodeHome;

/**
* this test is to make sure:
* 1. we have a test to verify the jq filter we add to the controller
* 2. the JQFilter we added is valid
* this test is to make sure: 1. we have a test to verify the jq filter we add to the controller 2.
* the JQFilter we added is valid
*/
public class JQFilterVerificationDUnitTest {

@ClassRule
public static ClusterStartupRule cluster = new ClusterStartupRule();

private static MemberVM locator, server;
private static ClusterManagementService cms;

@ClassRule
public static RequiresGeodeHome requiresGeodeHome = new RequiresGeodeHome();

Expand All @@ -68,28 +64,21 @@ public class JQFilterVerificationDUnitTest {

@BeforeClass
public static void beforeClass() throws IOException {
locator = cluster.startLocatorVM(0, l -> l.withHttpService());
server = cluster.startServerVM(1, locator.getPort());
cms = ClusterManagementServiceBuilder.buildWithHostAddress()
MemberVM locator = cluster.startLocatorVM(0, MemberStarterRule::withHttpService);
cluster.startServerVM(1, locator.getPort());
ClusterManagementService cms = ClusterManagementServiceBuilder.buildWithHostAddress()
.setHostAddress("localhost", locator.getHttpPort()).build();
Region region = new Region();
region.setName("regionA");
region.setType(RegionType.REPLICATE);
cms.create(region);
// create an index using CPS api since CMS doesn't support creating index yet
locator.invoke(() -> {
InternalConfigurationPersistenceService cps =
ClusterStartupRule.getLocator().getConfigurationPersistenceService();
cps.updateCacheConfig("cluster", cc -> {
RegionConfig regionA = Identifiable.find(cc.getRegions(), "regionA");
RegionConfig.Index index = new RegionConfig.Index();
index.setName("index1");
index.setFromClause("regionA");
index.setExpression("id");
regionA.getIndexes().add(index);
return cc;
});
});

Index index1 = new Index();
index1.setName("index1");
index1.setExpression("id");
index1.setRegionPath("/regionA");
index1.setIndexType(IndexType.RANGE);
cms.create(index1);

client = new GeodeDevRestClient("/management", "localhost", locator.getHttpPort(), false);
JsonNode jsonObject =
Expand All @@ -111,7 +100,7 @@ public static void beforeClass() throws IOException {
}

@AfterClass
public static void afterClass() throws Exception {
public static void afterClass() {
// after all the tests are done, the map has nothing left.
assertThat(apiWithJQFilters).hasSize(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,47 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.SoftAssertions.assertSoftly;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;

import org.apache.geode.cache.query.QueryService;
import org.apache.geode.management.api.ClusterManagementGetResult;
import org.apache.geode.management.api.ClusterManagementListResult;
import org.apache.geode.management.api.ClusterManagementService;
import org.apache.geode.management.api.ConfigurationResult;
import org.apache.geode.management.client.ClusterManagementServiceBuilder;
import org.apache.geode.management.configuration.Index;
import org.apache.geode.management.configuration.IndexType;
import org.apache.geode.management.configuration.Region;
import org.apache.geode.management.configuration.RegionType;
import org.apache.geode.management.runtime.RuntimeInfo;
import org.apache.geode.management.runtime.IndexInfo;
import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;
import org.apache.geode.test.junit.rules.GfshCommandRule;
import org.apache.geode.test.junit.rules.MemberStarterRule;

public class ListIndexManagementDUnitTest {
private static MemberVM locator, server;

private Region regionConfig;
private Index index;

@ClassRule
public static ClusterStartupRule lsRule = new ClusterStartupRule();

@ClassRule
public static GfshCommandRule gfsh = new GfshCommandRule();

private static ClusterManagementService cms;

@BeforeClass
public static void beforeclass() throws Exception {
locator = lsRule.startLocatorVM(0, l -> l.withHttpService());
server = lsRule.startServerVM(1, locator.getPort());
public static void beforeClass() {
MemberVM locator = lsRule.startLocatorVM(0, MemberStarterRule::withHttpService);
MemberVM server1 = lsRule.startServerVM(1, locator.getPort());
MemberVM server2 = lsRule.startServerVM(2, locator.getPort());

cms = ClusterManagementServiceBuilder.buildWithHostAddress()
.setHostAddress("localhost", locator.getHttpPort())
Expand All @@ -63,20 +67,38 @@ public static void beforeclass() throws Exception {
config.setName("region1");
config.setType(RegionType.REPLICATE);
cms.create(config);
locator.waitUntilRegionIsReadyOnExactlyThisManyServers("/region1", 1);

gfsh.connectAndVerify(locator);
gfsh.executeAndAssertThat(
"create index --name=index1 --type=key --expression=id --region=/region1")
.statusIsSuccess();
gfsh.executeAndAssertThat(
"create index --name=index2 --type=key --expression=key --region=/region1")
.statusIsSuccess();
locator.waitUntilRegionIsReadyOnExactlyThisManyServers("/region1", 2);

Index index1 = new Index();
index1.setName("index1");
index1.setExpression("id");
index1.setRegionPath("/region1");
index1.setIndexType(IndexType.KEY);
cms.create(index1);

Index index2 = new Index();
index2.setName("index2");
index2.setExpression("key");
index2.setRegionPath("/region1");
index2.setIndexType(IndexType.KEY);
cms.create(index2);

// make sure indexes are created on each server
MemberVM.invokeInEveryMember(() -> {
QueryService queryService =
Objects.requireNonNull(ClusterStartupRule.getCache()).getQueryService();
Collection<org.apache.geode.cache.query.Index> indexes = queryService.getIndexes();
assertThat(indexes).extracting(org.apache.geode.cache.query.Index::getName)
.containsExactlyInAnyOrder("index1", "index2");
assertThat(indexes.stream().findFirst()
.filter(index -> index.getRegion().getName().equals("region1")).isPresent()).isTrue();
}, server1, server2);
}

@Before
public void before() {
regionConfig = new Region();
index = new Index();
}

@Test
Expand All @@ -101,111 +123,107 @@ public void getNonExistRegion() {

@Test
public void listIndexForOneRegion() {
Index index = new Index();
index.setRegionPath("region1");
ClusterManagementListResult<Index, RuntimeInfo> list = cms.list(index);
ClusterManagementListResult<Index, IndexInfo> list = cms.list(index);
List<Index> result = list.getConfigResult();
assertThat(result).hasSize(2);
}

@Test
public void listAllIndex() throws Exception {
Index index = new Index();
ClusterManagementListResult<Index, RuntimeInfo> list = cms.list(index);
public void listAllIndex() {
ClusterManagementListResult<Index, IndexInfo> list = cms.list(index);
List<Index> result = list.getConfigResult();
assertThat(result).hasSize(2);
}

@Test
public void getIndex() {
Index index = new Index();
index.setRegionPath("region1");
index.setRegionPath("/region1");
index.setName("index1");
Index runtimeIndex = cms.get(index).getConfigResult();
assertThat(runtimeIndex.getRegionName()).isEqualTo("region1");
assertThat(runtimeIndex.getName()).isEqualTo("index1");
assertThat(runtimeIndex.getRegionPath()).isEqualTo("/region1");
assertThat(runtimeIndex.getExpression()).isEqualTo("id");
ConfigurationResult<Index, RuntimeInfo> wrapper = cms.get(index).getResult();
Index indexConfig = wrapper.getConfiguration();
assertThat(indexConfig.getLinks().getLinks()).containsKey("region");
assertThat(indexConfig.getLinks().getLinks().get("region")).endsWith("regions/region1");
ClusterManagementGetResult<Index, IndexInfo> clusterManagementGetResult = cms.get(index);
Index indexConfig = clusterManagementGetResult.getConfigResult();
List<IndexInfo> runtimeResult = clusterManagementGetResult.getRuntimeResult();

assertSoftly(softly -> {
softly.assertThat(indexConfig.getRegionName()).isEqualTo("region1");
softly.assertThat(indexConfig.getName()).isEqualTo("index1");
softly.assertThat(indexConfig.getRegionPath()).isEqualTo("/region1");
softly.assertThat(indexConfig.getExpression()).isEqualTo("id");
ConfigurationResult<Index, IndexInfo> configurationResult = cms.get(index).getResult();
Index indexConfigTwo = configurationResult.getConfiguration();
softly.assertThat(indexConfigTwo.getLinks().getLinks()).containsKey("region");
softly.assertThat(indexConfigTwo.getLinks().getLinks().get("region"))
.endsWith("regions/region1");
softly.assertThat(runtimeResult).extracting(IndexInfo::getMemberName)
.containsExactlyInAnyOrder("server-1", "server-2");
});
}

@Test
public void getIndexWithoutIndexId() {
Index index = new Index();
index.setRegionPath("region1");
assertThatThrownBy(() -> cms.get(index)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Unable to construct the URI ");
}

@Test
public void getIndexWithoutRegionNameAndIndexId() {
Index index = new Index();
assertThatThrownBy(() -> cms.get(index)).isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("Unable to construct the URI ");
}

@Test
public void getIndexWithoutRegionName() {
Index index = new Index();
index.setName("index1");
ClusterManagementListResult<Index, RuntimeInfo> list = cms.list(index);
List<Index> result = list.getConfigResult();
assertThat(result).hasSize(1);
Index runtimeIndex = result.get(0);
assertThat(runtimeIndex.getRegionName()).isEqualTo("region1");
assertThat(runtimeIndex.getName()).isEqualTo("index1");
assertThat(runtimeIndex.getRegionPath()).isEqualTo("/region1");
assertThat(runtimeIndex.getExpression()).isEqualTo("id");
assertThatThrownBy(() -> cms.get(index))
.hasMessageContaining("Error while extracting response for type");
}

@Test
public void listIndexWithoutRegionName() {
Index index = new Index();
index.setName("index1");
ClusterManagementListResult<Index, RuntimeInfo> list = cms.list(index);
List<Index> result = list.getConfigResult();
assertThat(result).hasSize(1);
Index runtimeIndex = result.get(0);
assertThat(runtimeIndex.getRegionName()).isEqualTo("region1");
assertThat(runtimeIndex.getName()).isEqualTo("index1");
assertThat(runtimeIndex.getRegionPath()).isEqualTo("/region1");
assertThat(runtimeIndex.getExpression()).isEqualTo("id");
assertListIndexResult(index);
}

@Test
public void listIndexesWithIdFilter() {
Index index = new Index();
index.setRegionPath("region1");
index.setName("index1");
ClusterManagementListResult<Index, RuntimeInfo> list = cms.list(index);
assertListIndexResult(index);
}

private void assertListIndexResult(Index index) {
ClusterManagementListResult<Index, IndexInfo> list = cms.list(index);
List<Index> result = list.getConfigResult();
assertThat(result).hasSize(1);
Index runtimeIndex = result.get(0);
assertThat(runtimeIndex.getRegionName()).isEqualTo("region1");
assertThat(runtimeIndex.getName()).isEqualTo("index1");
assertThat(runtimeIndex.getRegionPath()).isEqualTo("/region1");
assertThat(runtimeIndex.getExpression()).isEqualTo("id");
List<IndexInfo> runtimeResult = list.getRuntimeResult();
assertSoftly(softly -> {
softly.assertThat(result).hasSize(1);
Index indexConfig = result.get(0);
softly.assertThat(indexConfig.getRegionName()).isEqualTo("region1");
softly.assertThat(indexConfig.getName()).isEqualTo("index1");
softly.assertThat(indexConfig.getRegionPath()).isEqualTo("/region1");
softly.assertThat(indexConfig.getExpression()).isEqualTo("id");
softly.assertThat(runtimeResult).extracting(IndexInfo::getMemberName)
.containsExactlyInAnyOrder("server-1", "server-2");
});
}

@Test
public void getNonExistingIndex() {
Index index = new Index();
index.setRegionPath("region1");
index.setName("index333");
assertThatThrownBy(() -> cms.get(index)).hasMessageContaining("ENTITY_NOT_FOUND");
}

@Test
public void listNonExistingIndexesWithIdFilter() {
Index index = new Index();
index.setRegionPath("region1");
index.setName("index333");
ClusterManagementListResult<Index, RuntimeInfo> list = cms.list(index);
ClusterManagementListResult<Index, IndexInfo> list = cms.list(index);
List<Index> result = list.getConfigResult();
assertThat(result).hasSize(0);
assertThat(list.isSuccessful()).isTrue();
assertSoftly(softly -> {
softly.assertThat(result).hasSize(0);
softly.assertThat(list.isSuccessful()).isTrue();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,7 @@ javadoc/org/apache/geode/management/package-tree.html
javadoc/org/apache/geode/management/runtime/CacheServerInfo.html
javadoc/org/apache/geode/management/runtime/DeploymentInfo.html
javadoc/org/apache/geode/management/runtime/GatewayReceiverInfo.html
javadoc/org/apache/geode/management/runtime/IndexInfo.html
javadoc/org/apache/geode/management/runtime/MemberInformation.html
javadoc/org/apache/geode/management/runtime/OperationResult.html
javadoc/org/apache/geode/management/runtime/PdxInfo.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,6 @@ public class IndexManager {
@MutableForTesting
public static boolean IS_TEST_EXPANSION = false;



/**
* System property to maintain the ReverseMap to take care in-place modification of the objects by
* the application. In case of in-place modification the EntryEvent will not have the old-value,
Expand Down
Loading

0 comments on commit c4fc290

Please sign in to comment.