#539 - Polishing.

Add author tags. Simplify dependency setup. Replace logger declarations with Lombok's CommonsLog. Simplify test annotations.

Disable WAN module as the WAN server does not stop after running tests. Reformat code.
This commit is contained in:
Mark Paluch
2020-02-27 10:21:46 +01:00
parent fa0021cffb
commit 54f7146e0f
121 changed files with 1160 additions and 1092 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.Data;
@@ -29,6 +28,7 @@ import java.io.Serializable;
*/
@Data
public class Address implements Serializable {
private String street;
private String city;
private String country;

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.Data;
@@ -48,4 +47,4 @@ public class Customer implements Serializable {
this.lastName = lastName;
this.addresses = Arrays.asList(addresses);
}
}
}

View File

@@ -13,10 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import org.springframework.data.repository.CrudRepository;
/**
* @author Patrick Johnson
*/
public interface CustomerRepository extends CrudRepository<Customer, Long> {
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.Data;
@@ -28,10 +27,11 @@ import java.io.Serializable;
*/
@Data
public class EmailAddress implements Serializable {
private String value;
public EmailAddress(String value) {
this.value = value;
}
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.Data;
@@ -41,4 +40,4 @@ public class LineItem implements Serializable {
public BigDecimal calcTotal() {
return product.getPrice().multiply(BigDecimal.valueOf(amount));
}
}
}

View File

@@ -13,18 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.Region;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.annotation.Id;
import org.springframework.data.gemfire.mapping.annotation.Region;
/**
* Orders object used in the examples
*
@@ -34,8 +34,8 @@ import java.util.List;
@Data
@Region("Orders")
public class Order implements Serializable {
@Id
private Long id;
@Id private Long id;
private Long customerId;
private Address billingAddress;
private Address shippingAddress;
@@ -69,4 +69,4 @@ public class Order implements Serializable {
public void add(LineItem lineItem) {
lineItems.add(lineItem);
}
}
}

View File

@@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import org.springframework.data.repository.CrudRepository;
public interface OrderRepository extends CrudRepository<Order, Long> {
}
/**
* @author Patrick Johnson
*/
public interface OrderRepository extends CrudRepository<Order, Long> {}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.Data;

View File

@@ -13,10 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import org.springframework.data.repository.CrudRepository;
public interface ProductRepository extends CrudRepository<Product, Long> {
}
/**
* @author Patrick Johnson
*/
public interface ProductRepository extends CrudRepository<Product, Long> {}

View File

@@ -13,12 +13,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import lombok.extern.apachecommons.CommonsLog;
import java.math.BigDecimal;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import org.apache.geode.cache.Region;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.WebApplicationType;
@@ -26,26 +31,20 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.Bean;
import java.math.BigDecimal;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
@SpringBootApplication(scanBasePackageClasses = StorageServerConfig.class)
/**
* @author Patrick Johnson
*/
@SpringBootApplication
@CommonsLog
public class StorageServer {
private Logger logger = LoggerFactory.getLogger(this.getClass());
public static void main(String[] args) {
new SpringApplicationBuilder(StorageServer.class)
.web(WebApplicationType.NONE)
.build()
.run(args);
new SpringApplicationBuilder(StorageServer.class).web(WebApplicationType.NONE).build().run(args);
}
@Bean
public ApplicationRunner runner(CustomerRepository customerRepository, OrderRepository orderRepository,
ProductRepository productRepository, @Qualifier("Products") Region<Long, Product> products) {
ProductRepository productRepository, @Qualifier("Products") Region<Long, Product> products) {
return args -> {
createCustomerData(customerRepository);
@@ -53,40 +52,34 @@ public class StorageServer {
createOrders(productRepository, orderRepository);
logger.info("Completed creating orders ");
log.info("Completed creating orders ");
};
}
private void createOrders(ProductRepository productRepository, OrderRepository orderRepository) {
Random random = new Random(System.nanoTime());
Address address = new Address("it", "doesn't", "matter");
LongStream.rangeClosed(1, 10).forEach((orderId) ->
LongStream.rangeClosed(1, 300).forEach((customerId) -> {
Order order = new Order(orderId, customerId, address);
IntStream.rangeClosed(0, random.nextInt(3) + 1).forEach((lineItemCount) -> {
int quantity = random.nextInt(3) + 1;
long productId = random.nextInt(3) + 1;
order.add(new LineItem(productRepository.findById(productId).get(), quantity));
});
orderRepository.save(order);
}));
LongStream.rangeClosed(1, 10).forEach((orderId) -> LongStream.rangeClosed(1, 300).forEach((customerId) -> {
Order order = new Order(orderId, customerId, address);
IntStream.rangeClosed(0, random.nextInt(3) + 1).forEach((lineItemCount) -> {
int quantity = random.nextInt(3) + 1;
long productId = random.nextInt(3) + 1;
order.add(new LineItem(productRepository.findById(productId).get(), quantity));
});
orderRepository.save(order);
}));
}
private void createProducts(ProductRepository productRepository) {
productRepository.save(new Product(1L, "Apple iPod", new BigDecimal("99.99"),
"An Apple portable music player"));
productRepository.save(new Product(2L, "Apple iPad", new BigDecimal("499.99"),
"An Apple tablet device"));
Product macbook = new Product(3L, "Apple macBook", new BigDecimal("899.99"),
"An Apple notebook computer");
productRepository.save(new Product(1L, "Apple iPod", new BigDecimal("99.99"), "An Apple portable music player"));
productRepository.save(new Product(2L, "Apple iPad", new BigDecimal("499.99"), "An Apple tablet device"));
Product macbook = new Product(3L, "Apple macBook", new BigDecimal("899.99"), "An Apple notebook computer");
macbook.addAttribute("warranty", "included");
productRepository.save(macbook);
}
private void createCustomerData(CustomerRepository customerRepository) {
LongStream.rangeClosed(0, 300)
.parallel()
.forEach(customerId ->
customerRepository.save(new Customer(customerId, new EmailAddress(customerId + "@2.com"), "John" + customerId, "Smith" + customerId)));
LongStream.rangeClosed(0, 300).parallel().forEach(customerId -> customerRepository.save(
new Customer(customerId, new EmailAddress(customerId + "@2.com"), "John" + customerId, "Smith" + customerId)));
}
}
}

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import org.apache.geode.cache.DataPolicy;
@@ -33,11 +32,14 @@ import org.springframework.data.gemfire.config.annotation.CacheServerApplication
import org.springframework.data.gemfire.config.annotation.EnableOffHeap;
import org.springframework.data.gemfire.repository.config.EnableGemfireRepositories;
/**
* @author Patrick Johnson
*/
@Configuration
@ComponentScan
@CacheServerApplication(logLevel = "error")
@EnableGemfireRepositories(basePackageClasses = CustomerRepository.class)
@EnableOffHeap(memorySize = "8192m", regionNames = "Products")
@EnableOffHeap(memorySize = "512m", regionNames = "Products")
public class StorageServerConfig {
@Bean
@@ -47,22 +49,23 @@ public class StorageServerConfig {
@Bean
RegionAttributesFactoryBean<Long, Order> regionAttributes(PartitionAttributes<Long, Order> partitionAttributes) {
final RegionAttributesFactoryBean<Long, Order> regionAttributesFactoryBean = new RegionAttributesFactoryBean<>();
RegionAttributesFactoryBean<Long, Order> regionAttributesFactoryBean = new RegionAttributesFactoryBean<>();
regionAttributesFactoryBean.setPartitionAttributes(partitionAttributes);
return regionAttributesFactoryBean;
}
@Bean
PartitionAttributesFactoryBean<Long, Order> partitionAttributes() {
final PartitionAttributesFactoryBean<Long, Order> partitionAttributesFactoryBean = new PartitionAttributesFactoryBean<>();
PartitionAttributesFactoryBean<Long, Order> partitionAttributesFactoryBean = new PartitionAttributesFactoryBean<>();
partitionAttributesFactoryBean.setTotalNumBuckets(11);
partitionAttributesFactoryBean.setRedundantCopies(1);
return partitionAttributesFactoryBean;
}
@Bean("Orders")
PartitionedRegionFactoryBean<Long, Order> createOrderRegion(GemFireCache gemFireCache, RegionAttributes<Long, Order> regionAttributes) {
final PartitionedRegionFactoryBean<Long, Order> partitionedRegionFactoryBean = new PartitionedRegionFactoryBean<>();
PartitionedRegionFactoryBean<Long, Order> createOrderRegion(GemFireCache gemFireCache,
RegionAttributes<Long, Order> regionAttributes) {
PartitionedRegionFactoryBean<Long, Order> partitionedRegionFactoryBean = new PartitionedRegionFactoryBean<>();
partitionedRegionFactoryBean.setCache(gemFireCache);
partitionedRegionFactoryBean.setRegionName("Orders");
partitionedRegionFactoryBean.setDataPolicy(DataPolicy.PARTITION);
@@ -72,7 +75,7 @@ public class StorageServerConfig {
@Bean("Products")
ReplicatedRegionFactoryBean<Long, Product> createProductRegion(GemFireCache gemFireCache) {
final ReplicatedRegionFactoryBean<Long, Product> replicatedRegionFactoryBean = new ReplicatedRegionFactoryBean<>();
ReplicatedRegionFactoryBean<Long, Product> replicatedRegionFactoryBean = new ReplicatedRegionFactoryBean<>();
replicatedRegionFactoryBean.setCache(gemFireCache);
replicatedRegionFactoryBean.setRegionName("Products");
replicatedRegionFactoryBean.setDataPolicy(DataPolicy.REPLICATE);
@@ -81,11 +84,11 @@ public class StorageServerConfig {
@Bean("Customers")
ReplicatedRegionFactoryBean<Long, Customer> createCustomerRegion(GemFireCache gemFireCache, Compressor compressor) {
final ReplicatedRegionFactoryBean<Long, Customer> replicatedRegionFactoryBean = new ReplicatedRegionFactoryBean<>();
ReplicatedRegionFactoryBean<Long, Customer> replicatedRegionFactoryBean = new ReplicatedRegionFactoryBean<>();
replicatedRegionFactoryBean.setCache(gemFireCache);
replicatedRegionFactoryBean.setRegionName("Customers");
replicatedRegionFactoryBean.setDataPolicy(DataPolicy.REPLICATE);
replicatedRegionFactoryBean.setCompressor(compressor);
return replicatedRegionFactoryBean;
}
}
}

View File

@@ -13,39 +13,37 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.springdata.geode.server.storage;
import static org.assertj.core.api.Assertions.*;
import javax.annotation.Resource;
import org.apache.geode.cache.Cache;
import org.apache.geode.cache.Region;
import org.apache.geode.compression.SnappyCompressor;
import org.apache.geode.internal.cache.GemFireCacheImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Patrick Johnson
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = StorageServer.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
@SpringBootTest
public class StorageServerTests {
@Resource(name = "Customers")
private Region<Long, Customer> customers;
@Resource(name = "Orders")
private Region<Long, Order> orders;
@Resource(name = "Customers") private Region<Long, Customer> customers;
@Resource(name = "Products")
private Region<Long, Product> products;
@Resource(name = "Orders") private Region<Long, Order> orders;
@Autowired
Cache cache;
@Resource(name = "Products") private Region<Long, Product> products;
@Autowired Cache cache;
@Test
public void partitionAttributesConfiguredCorrectly() {
@@ -55,9 +53,13 @@ public class StorageServerTests {
@Test
public void compressorIsEnabled() {
assertThat(customers.getAttributes().getCompressor()).isInstanceOf(SnappyCompressor.class);
GemFireCacheImpl impl = (GemFireCacheImpl) cache;
assertThat(impl.getCachePerfStats().getTotalPostCompressedBytes()).isLessThan(impl.getCachePerfStats().getTotalPreCompressedBytes());
assertThat(impl.getCachePerfStats().getTotalPostCompressedBytes())
.isLessThan(impl.getCachePerfStats().getTotalPreCompressedBytes());
}
@Test
@@ -65,4 +67,4 @@ public class StorageServerTests {
assertThat(products.getAttributes().getOffHeap()).isTrue();
assertThat(customers.getAttributes().getOffHeap()).isFalse();
}
}
}