Skip to content

Commit

Permalink
GEODE-7425 delete index (apache#4503)
Browse files Browse the repository at this point in the history
Co-authored-by: Joris Melchior <[email protected]>
Co-authored-by: Jinmei Liao <[email protected]>
Co-authored-by: Darrel Schneider <[email protected]>
  • Loading branch information
3 people authored and jinmeiliao committed Dec 21, 2019
1 parent 5ec5dd1 commit e50c5e3
Show file tree
Hide file tree
Showing 12 changed files with 648 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
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.ClusterManagementRealizationResult;
import org.apache.geode.management.api.ClusterManagementService;
import org.apache.geode.management.api.ConfigurationResult;
import org.apache.geode.management.client.ClusterManagementServiceBuilder;
Expand All @@ -46,18 +47,20 @@
public class ListIndexManagementDUnitTest {

private Region regionConfig;
private Index index;
private Index indexConfig;

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

private static ClusterManagementService cms;
private static MemberVM locator;

@BeforeClass
public static void beforeClass() {
MemberVM locator = lsRule.startLocatorVM(0, MemberStarterRule::withHttpService);
locator = lsRule.startLocatorVM(0, MemberStarterRule::withHttpService);
MemberVM server1 = lsRule.startServerVM(1, locator.getPort());
MemberVM server2 = lsRule.startServerVM(2, locator.getPort());
MemberVM server3 = lsRule.startServerVM(3, "group1", locator.getPort());

cms = ClusterManagementServiceBuilder.buildWithHostAddress()
.setHostAddress("localhost", locator.getHttpPort())
Expand All @@ -67,7 +70,7 @@ public static void beforeClass() {
config.setName("region1");
config.setType(RegionType.REPLICATE);
cms.create(config);
locator.waitUntilRegionIsReadyOnExactlyThisManyServers("/region1", 2);
locator.waitUntilRegionIsReadyOnExactlyThisManyServers("/region1", 3);

Index index1 = new Index();
index1.setName("index1");
Expand All @@ -92,104 +95,110 @@ public static void beforeClass() {
.containsExactlyInAnyOrder("index1", "index2");
assertThat(indexes.stream().findFirst()
.filter(index -> index.getRegion().getName().equals("region1")).isPresent()).isTrue();
}, server1, server2);
}, server1, server2, server3);
}

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

@Test
public void listRegion() {
public void listRegion_succeeds_with_empty_filter() {
List<Region> result =
cms.list(new Region()).getConfigResult();
assertThat(result).hasSize(1);
}

@Test
public void getRegion() {
public void getRegion_succeeds_with_region_name_filter() {
regionConfig.setName("region1");
Region region = cms.get(regionConfig).getConfigResult();
assertThat(region).isNotNull();
}

@Test
public void getNonExistRegion() {
public void getRegion_fails_with_non_existent_region_name_in_filter() {
regionConfig.setName("notExist");
assertThatThrownBy(() -> cms.get(regionConfig)).hasMessageContaining("ENTITY_NOT_FOUND");
}

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

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

@Test
public void getIndex() {
index.setRegionPath("/region1");
index.setName("index1");
ClusterManagementGetResult<Index, IndexInfo> clusterManagementGetResult = cms.get(index);
public void getIndex_succeeds_with_index_name_and_region_name_filter() {
indexConfig.setRegionPath("/region1");
indexConfig.setName("index1");
ClusterManagementGetResult<Index, IndexInfo> clusterManagementGetResult = cms.get(indexConfig);
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();
softly.assertThat(indexConfig.getRegionName()).as("get index: region name")
.isEqualTo("region1");
softly.assertThat(indexConfig.getName()).as("get index: index name").isEqualTo("index1");
softly.assertThat(indexConfig.getRegionPath()).as("get index: region path")
.isEqualTo("/region1");
softly.assertThat(indexConfig.getExpression()).as("get index: expression").isEqualTo("id");
ConfigurationResult<Index, IndexInfo> configurationResult =
cms.get(this.indexConfig).getResult();
Index indexConfigTwo = configurationResult.getConfiguration();
softly.assertThat(indexConfigTwo.getLinks().getLinks()).containsKey("region");
softly.assertThat(indexConfigTwo.getLinks().getLinks()).as("get index: links key")
.containsKey("region");
softly.assertThat(indexConfigTwo.getLinks().getLinks().get("region"))
.as("get index: links value")
.endsWith("regions/region1");
softly.assertThat(runtimeResult).extracting(IndexInfo::getMemberName)
.containsExactlyInAnyOrder("server-1", "server-2");
.as("get index: runtime servers")
.containsExactlyInAnyOrder("server-1", "server-2", "server-3");
});
}

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

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

@Test
public void getIndexWithoutRegionName() {
index.setName("index1");
assertThatThrownBy(() -> cms.get(index))
public void getIndex_fails_when_region_name_is_missing_from_filter() {
indexConfig.setName("index1");
assertThatThrownBy(() -> cms.get(indexConfig))
.hasMessageContaining("Error while extracting response for type");
}

@Test
public void listIndexWithoutRegionName() {
index.setName("index1");
assertListIndexResult(index);
public void listIndex_succeeds_with_index_name_only_filter() {
indexConfig.setName("index1");
assertListIndexResult(indexConfig);
}

@Test
public void listIndexesWithIdFilter() {
index.setRegionPath("region1");
index.setName("index1");
assertListIndexResult(index);
public void listIndex_succeeds_with_region_name_and_index_name_filter() {
indexConfig.setRegionPath("region1");
indexConfig.setName("index1");
assertListIndexResult(indexConfig);
}

private void assertListIndexResult(Index index) {
Expand All @@ -199,31 +208,125 @@ private void assertListIndexResult(Index index) {
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(indexConfig.getRegionName()).as("list index: region name")
.isEqualTo("region1");
softly.assertThat(indexConfig.getName()).as("list index: index name").isEqualTo("index1");
softly.assertThat(indexConfig.getRegionPath()).as("list index: region path")
.isEqualTo("/region1");
softly.assertThat(indexConfig.getExpression()).as("list index: expression").isEqualTo("id");
softly.assertThat(runtimeResult).extracting(IndexInfo::getMemberName)
.containsExactlyInAnyOrder("server-1", "server-2");
.as("list index: runtime servers")
.containsExactlyInAnyOrder("server-1", "server-2", "server-3");
});
}

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

@Test
public void listNonExistingIndexesWithIdFilter() {
index.setRegionPath("region1");
index.setName("index333");
ClusterManagementListResult<Index, IndexInfo> list = cms.list(index);
public void listIndex_fails_with_wrong_index_name_in_filter() {
indexConfig.setRegionPath("region1");
indexConfig.setName("index333");
ClusterManagementListResult<Index, IndexInfo> list = cms.list(indexConfig);
List<Index> result = list.getConfigResult();
assertSoftly(softly -> {
softly.assertThat(result).hasSize(0);
softly.assertThat(list.isSuccessful()).isTrue();
softly.assertThat(result).as("list non existing: result size").hasSize(0);
softly.assertThat(list.isSuccessful()).as("list non existing: success").isTrue();
});
}

@Test
public void createAndDeleteIndex_success_for_specific_group() {
Region region = new Region();
region.setName("region2");
region.setType(RegionType.REPLICATE);
region.setGroup("group1");
cms.create(region);
locator.waitUntilRegionIsReadyOnExactlyThisManyServers("/region2", 1);

Index index = new Index();
index.setName("index");
index.setExpression("key");
index.setRegionPath("/region2");
index.setGroup("group1");
index.setIndexType(IndexType.KEY);
cms.create(index);

ClusterManagementGetResult<Index, IndexInfo> indexResult = cms.get(index);
Index fetchedIndexConfig = indexResult.getConfigResult();
List<IndexInfo> runtimeResult = indexResult.getRuntimeResult();
assertSoftly(softly -> {
softly.assertThat(fetchedIndexConfig.getRegionName()).as("index create: region name")
.isEqualTo("region2");
softly.assertThat(fetchedIndexConfig.getName()).as("index create: index name")
.isEqualTo("index");
softly.assertThat(fetchedIndexConfig.getRegionPath()).as("index create: region path")
.isEqualTo("/region2");
softly.assertThat(fetchedIndexConfig.getExpression()).as("index create: expression")
.isEqualTo("key");
softly.assertThat(runtimeResult).extracting(IndexInfo::getMemberName)
.as("index create: runtime server")
.containsExactlyInAnyOrder("server-3");
});

ClusterManagementRealizationResult deleteIndexResult = cms.delete(index);
region.setGroup(null);
ClusterManagementRealizationResult deleteRegionResult = cms.delete(region);
assertSoftly(softly -> {
softly.assertThat(deleteIndexResult.isSuccessful()).isTrue();
softly.assertThatThrownBy(() -> cms.get(index)).as("delete index confirmation")
.hasMessageContaining("Index 'index' does not exist");
softly.assertThat(deleteRegionResult.isSuccessful()).isTrue();
softly.assertThatThrownBy(() -> cms.get(region)).as("delete region confirmation")
.hasMessageContaining("Region 'region2' does not exist");
});
}

@Test
public void createAndDeleteIndex_success_for_cluster() {
Region region = new Region();
region.setName("region2");
region.setType(RegionType.REPLICATE);
cms.create(region);
locator.waitUntilRegionIsReadyOnExactlyThisManyServers("/region2", 3);

Index index = new Index();
index.setName("index.1");
index.setExpression("key");
index.setRegionPath("/region2");
index.setIndexType(IndexType.KEY);
cms.create(index);

ClusterManagementGetResult<Index, IndexInfo> indexResult = cms.get(index);
Index fetchedIndexConfig = indexResult.getConfigResult();
List<IndexInfo> runtimeResult = indexResult.getRuntimeResult();
assertSoftly(softly -> {
softly.assertThat(fetchedIndexConfig.getRegionName()).as("index create: region name")
.isEqualTo("region2");
softly.assertThat(fetchedIndexConfig.getName()).as("index create: index name")
.isEqualTo("index.1");
softly.assertThat(fetchedIndexConfig.getRegionPath()).as("index create: index path")
.isEqualTo("/region2");
softly.assertThat(fetchedIndexConfig.getExpression()).as("index create: index expression")
.isEqualTo("key");
softly.assertThat(runtimeResult).as("index create: runtime servers")
.extracting(IndexInfo::getMemberName)
.containsExactlyInAnyOrder("server-1", "server-2", "server-3");
});

ClusterManagementRealizationResult deleteIndexResult = cms.delete(index);
ClusterManagementRealizationResult deleteRegionResult = cms.delete(region);
assertSoftly(softly -> {
softly.assertThat(deleteIndexResult.isSuccessful()).isTrue();
softly.assertThatThrownBy(() -> cms.get(index)).as("index delete confirmation")
.hasMessageContaining("Index 'index.1' does not exist");
softly.assertThat(deleteRegionResult.isSuccessful()).isTrue();
softly.assertThatThrownBy(() -> cms.get(region)).as("region delete confirmation")
.hasMessageContaining("Region 'region2' does not exist");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public IndexConfigManager(ConfigurationPersistenceService persistenceService) {

@Override
public void add(Index config, CacheConfig existing) {
RegionConfig regionConfiguration = existing.findRegionConfiguration(config.getRegionName());
if (regionConfiguration == null) {
RegionConfig regionConfig = existing.findRegionConfiguration(config.getRegionName());
if (regionConfig == null) {
throw new IllegalArgumentException(
"Region provided does not exist: " + config.getRegionName());
}
RegionConfig.Index index = converter.fromConfigObject(config);
regionConfiguration.getIndexes().add(index);
regionConfig.getIndexes().add(index);
}

@Override
Expand All @@ -52,7 +52,20 @@ public void update(Index config, CacheConfig existing) {

@Override
public void delete(Index config, CacheConfig existing) {
throw new NotImplementedException("Not implemented yet");
RegionConfig regionConfig = existing.findRegionConfiguration(config.getRegionName());
if (regionConfig == null) {
throw new IllegalArgumentException(
"Region provided does not exist: " + config.getRegionName());
}
RegionConfig.Index index = regionConfig.getIndexes().stream()
.filter(item -> item.getName().equals(config.getName()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Index provided does not exist on Region: "
+ config.getRegionName()
+ ", "
+ config.getName()));

regionConfig.getIndexes().remove(index);
}

@Override
Expand Down
Loading

0 comments on commit e50c5e3

Please sign in to comment.