Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GEODE-6174: RegionConfigRealizer should be idempotent for create #3515

Merged
merged 1 commit into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.management.internal.rest;

import static org.apache.geode.test.junit.assertions.ClusterManagementResultAssert.assertManagementResult;

import org.junit.ClassRule;
import org.junit.Test;

import org.apache.geode.cache.configuration.RegionConfig;
import org.apache.geode.cache.configuration.RegionType;
import org.apache.geode.management.api.ClusterManagementResult;
import org.apache.geode.management.api.ClusterManagementService;
import org.apache.geode.management.client.ClusterManagementServiceProvider;
import org.apache.geode.test.dunit.rules.ClusterStartupRule;
import org.apache.geode.test.dunit.rules.MemberVM;

public class RegionManagementOnMultipleGroup {
@ClassRule
public static ClusterStartupRule cluster = new ClusterStartupRule();

@Test
public void createRegion() throws Exception {
MemberVM locator = cluster.startLocatorVM(0, l -> l.withHttpService());

cluster.startServerVM(1, "group1", locator.getPort());
cluster.startServerVM(2, "group2", locator.getPort());
cluster.startServerVM(3, "group1,group2", locator.getPort());

ClusterManagementService cms =
ClusterManagementServiceProvider.getService("localhost", locator.getHttpPort());
RegionConfig region = new RegionConfig();
region.setName("test");
region.setGroup("group1");
region.setType(RegionType.REPLICATE);

ClusterManagementResult result = cms.create(region);
assertManagementResult(result).isSuccessful().hasMemberStatus().containsOnlyKeys("server-1",
"server-3");

// create the same region on group2 will be successful even though the region already exists
// on one member
region.setGroup("group2");
assertManagementResult(cms.create(region)).isSuccessful().hasMemberStatus()
.containsOnlyKeys("server-2", "server-3");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.apache.geode.management.internal.configuration.realizers;

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

import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -53,4 +54,26 @@ public void sanityCheck() throws Exception {
assertThat(region.getAttributes().getDataPolicy()).isEqualTo(DataPolicy.REPLICATE);
assertThat(region.getAttributes().getScope()).isEqualTo(Scope.DISTRIBUTED_ACK);
}

@Test
public void create2ndTime() throws Exception {
config.setName("foo");
config.setType(RegionType.REPLICATE);
realizer.create(config, server.getCache());

// the 2nd time with same name and type will not throw an error
realizer.create(config, server.getCache());
}

@Test
public void createDifferentRegion2ndTime() throws Exception {
config.setName("bar");
config.setType(RegionType.REPLICATE);
realizer.create(config, server.getCache());

// the 2nd time with different type will throw an error
config.setType(RegionType.PARTITION);
assertThatThrownBy(() -> realizer.create(config, server.getCache()))
.isInstanceOf(IllegalStateException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.geode.cache.ExpirationAction;
import org.apache.geode.cache.ExpirationAttributes;
import org.apache.geode.cache.Region;
import org.apache.geode.cache.RegionExistsException;
import org.apache.geode.cache.RegionFactory;
import org.apache.geode.cache.Scope;
import org.apache.geode.cache.configuration.DeclarableType;
Expand All @@ -49,7 +50,11 @@ public RegionConfigRealizer() {}
@Override
public void create(RegionConfig regionConfig, Cache cache) {
RegionFactory factory = getRegionFactory(cache, regionConfig.getRegionAttributes());
factory.create(regionConfig.getName());
try {
factory.create(regionConfig.getName());
} catch (RegionExistsException e) {
// since we are trying to create this region, ignore this exception
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@

import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.ListAssert;
import org.assertj.core.api.MapAssert;

import org.apache.geode.management.api.ClusterManagementResult;
import org.apache.geode.management.api.Status;
import org.apache.geode.management.configuration.RuntimeCacheElement;

public class ClusterManagementResultAssert
Expand Down Expand Up @@ -50,6 +52,10 @@ public ClusterManagementResultAssert containsStatusMessage(String statusMessage)
return this;
}

public MapAssert<String, Status> hasMemberStatus() {
return assertThat(actual.getMemberStatuses());
}

public ListAssert<RuntimeCacheElement> hasListResult() {
return assertThat(actual.getResult());
}
Expand Down