You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by ji...@apache.org on 2018/05/09 15:13:36 UTC

[geode] branch develop updated: GEODE-4858: refactor ConfigurePdxCommand (#1933)

This is an automated email from the ASF dual-hosted git repository.

jinmeiliao pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new f1bca73  GEODE-4858: refactor ConfigurePdxCommand (#1933)
f1bca73 is described below

commit f1bca7378ffb56dc1e1c4706103cc4593a1826bb
Author: jinmeiliao <ji...@pivotal.io>
AuthorDate: Wed May 9 08:13:28 2018 -0700

    GEODE-4858: refactor ConfigurePdxCommand (#1933)
---
 .../geode/cache/configuration/DeclarableType.java  |  14 ++-
 .../internal/cli/commands/ConfigurePDXCommand.java | 109 ++++++++-------------
 .../cli/exceptions/EntityExistsException.java      |   3 +
 .../internal/cli/remote/CommandExecutor.java       |  10 +-
 .../geode/cache/configuration/CacheConfigTest.java |   6 +-
 .../cli/commands/ConfigurePDXCommandTest.java      |   4 +-
 6 files changed, 66 insertions(+), 80 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/cache/configuration/DeclarableType.java b/geode-core/src/main/java/org/apache/geode/cache/configuration/DeclarableType.java
index a47fce9..0f7de23 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/configuration/DeclarableType.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/configuration/DeclarableType.java
@@ -72,11 +72,15 @@ public class DeclarableType extends ClassNameType {
   }
 
   public DeclarableType(String className, String jsonProperties) {
-    ClassName cn = new ClassName(className, jsonProperties);
-    this.className = cn.getClassName();
-    Properties properties = cn.getInitProperties();
-    parameter = properties.stringPropertyNames().stream()
-        .map(k -> new ParameterType(k, properties.getProperty(k))).collect(Collectors.toList());
+    this(className, new ClassName(className, jsonProperties).getInitProperties());
+  }
+
+  public DeclarableType(String className, Properties properties) {
+    this.className = className;
+    if (properties != null) {
+      parameter = properties.stringPropertyNames().stream()
+          .map(k -> new ParameterType(k, properties.getProperty(k))).collect(Collectors.toList());
+    }
   }
 
   /**
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java
index dc73b5a..c6abb0f 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommand.java
@@ -16,28 +16,25 @@
 package org.apache.geode.management.internal.cli.commands;
 
 import java.util.Arrays;
-import java.util.List;
-import java.util.stream.Collectors;
 
 import org.springframework.shell.core.annotation.CliCommand;
 import org.springframework.shell.core.annotation.CliOption;
 
+import org.apache.geode.cache.configuration.CacheConfig;
 import org.apache.geode.cache.configuration.DeclarableType;
-import org.apache.geode.cache.configuration.ParameterType;
 import org.apache.geode.cache.configuration.PdxType;
-import org.apache.geode.distributed.ConfigurationPersistenceService;
 import org.apache.geode.management.cli.CliMetaData;
-import org.apache.geode.management.cli.Result;
+import org.apache.geode.management.cli.SingleGfshCommand;
 import org.apache.geode.management.internal.cli.AbstractCliAroundInterceptor;
 import org.apache.geode.management.internal.cli.GfshParseResult;
 import org.apache.geode.management.internal.cli.i18n.CliStrings;
-import org.apache.geode.management.internal.cli.result.InfoResultData;
-import org.apache.geode.management.internal.cli.result.ResultBuilder;
+import org.apache.geode.management.internal.cli.result.model.InfoResultModel;
+import org.apache.geode.management.internal.cli.result.model.ResultModel;
 import org.apache.geode.management.internal.security.ResourceOperation;
 import org.apache.geode.pdx.ReflectionBasedAutoSerializer;
 import org.apache.geode.security.ResourcePermission;
 
-public class ConfigurePDXCommand extends InternalGfshCommand {
+public class ConfigurePDXCommand extends SingleGfshCommand {
 
   protected ReflectionBasedAutoSerializer createReflectionBasedAutoSerializer(
       boolean checkPortability, String[] patterns) {
@@ -49,7 +46,7 @@ public class ConfigurePDXCommand extends InternalGfshCommand {
       interceptor = "org.apache.geode.management.internal.cli.commands.ConfigurePDXCommand$Interceptor")
   @ResourceOperation(resource = ResourcePermission.Resource.CLUSTER,
       operation = ResourcePermission.Operation.MANAGE)
-  public Result configurePDX(
+  public ResultModel configurePDX(
       @CliOption(key = CliStrings.CONFIGURE_PDX__READ__SERIALIZED,
           unspecifiedDefaultValue = "false",
           help = CliStrings.CONFIGURE_PDX__READ__SERIALIZED__HELP) Boolean readSerialized,
@@ -63,85 +60,66 @@ public class ConfigurePDXCommand extends InternalGfshCommand {
       @CliOption(key = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES,
           help = CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES__HELP) String[] portableClassesPatterns) {
 
-    Result result;
-    ReflectionBasedAutoSerializer autoSerializer = null;
-
     if (getConfigurationPersistenceService() == null) {
-      return ResultBuilder
-          .createUserErrorResult("Configure pdx failed because cluster configuration is disabled.");
+      return ResultModel
+          .createError("Configure pdx failed because cluster configuration is disabled.");
     }
 
-    InfoResultData infoResultData = ResultBuilder.createInfoResultData();
+    ResultModel result = new ResultModel();
+    InfoResultModel infoSection = result.addInfo();
 
     if (!getAllNormalMembers().isEmpty()) {
-      infoResultData.addLine(CliStrings.CONFIGURE_PDX__NORMAL__MEMBERS__WARNING);
+      infoSection.addLine(CliStrings.CONFIGURE_PDX__NORMAL__MEMBERS__WARNING);
     }
 
-    // Set persistent and the disk-store
+    PdxType pdxType = new PdxType();
+    pdxType.setIgnoreUnreadFields(ignoreUnreadFields);
+    pdxType.setReadSerialized(readSerialized);
+    infoSection.addLine(CliStrings.CONFIGURE_PDX__READ__SERIALIZED + " = " + readSerialized);
+    infoSection
+        .addLine(CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS + " = " + ignoreUnreadFields);
+
+    pdxType.setDiskStoreName(diskStore);
+    pdxType.setPersistent(diskStore != null);
+
     if (diskStore != null) {
-      infoResultData.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = true");
-      infoResultData.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + diskStore);
+      infoSection.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = true");
+      infoSection.addLine(CliStrings.CONFIGURE_PDX__DISKSTORE + " = " + diskStore);
     } else {
-      infoResultData.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = false");
+      infoSection.addLine(CliStrings.CONFIGURE_PDX__PERSISTENT + " = false");
     }
 
-    infoResultData.addLine(CliStrings.CONFIGURE_PDX__READ__SERIALIZED + " = " + readSerialized);
-    infoResultData
-        .addLine(CliStrings.CONFIGURE_PDX__IGNORE__UNREAD_FIELDS + " = " + ignoreUnreadFields);
-
-    // Auto Serializer Configuration
+    ReflectionBasedAutoSerializer autoSerializer = null;
     if (portableClassesPatterns != null) {
       autoSerializer = createReflectionBasedAutoSerializer(true, portableClassesPatterns);
-      infoResultData.addLine("PDX Serializer = " + autoSerializer.getClass().getName());
-      infoResultData.addLine("Portable Classes = " + Arrays.toString(portableClassesPatterns));
-    }
-
-    if (nonPortableClassesPatterns != null) {
+      infoSection.addLine("PDX Serializer = " + autoSerializer.getClass().getName());
+      infoSection.addLine("Portable Classes = " + Arrays.toString(portableClassesPatterns));
+    } else if (nonPortableClassesPatterns != null) {
       autoSerializer = createReflectionBasedAutoSerializer(false, nonPortableClassesPatterns);
-      infoResultData.addLine("PDX Serializer = " + autoSerializer.getClass().getName());
-      infoResultData
-          .addLine("Non Portable Classes = " + Arrays.toString(nonPortableClassesPatterns));
+      infoSection.addLine("PDX Serializer = " + autoSerializer.getClass().getName());
+      infoSection.addLine("Non Portable Classes = " + Arrays.toString(nonPortableClassesPatterns));
+    }
+    if (autoSerializer != null) {
+      pdxType.setPdxSerializer(new DeclarableType(ReflectionBasedAutoSerializer.class.getName(),
+          autoSerializer.getConfig()));
     }
 
-    result = ResultBuilder.buildResult(infoResultData);
-    ReflectionBasedAutoSerializer finalAutoSerializer = autoSerializer;
-    getConfigurationPersistenceService()
-        .updateCacheConfig(ConfigurationPersistenceService.CLUSTER_CONFIG, config -> {
-          if (config.getPdx() == null) {
-            config.setPdx(new PdxType());
-          }
-          config.getPdx().setReadSerialized(readSerialized);
-          config.getPdx().setIgnoreUnreadFields(ignoreUnreadFields);
-          config.getPdx().setDiskStoreName(diskStore);
-          config.getPdx().setPersistent(diskStore != null);
-
-          if (portableClassesPatterns != null || nonPortableClassesPatterns != null) {
-            DeclarableType pdxSerializer = new DeclarableType();
-            pdxSerializer.setClassName(ReflectionBasedAutoSerializer.class.getName());
-
-            List<ParameterType> parameters =
-                finalAutoSerializer.getConfig().entrySet().stream().map(entry -> {
-                  ParameterType parameterType = new ParameterType();
-                  parameterType.setName((String) entry.getKey());
-                  parameterType.setString((String) entry.getValue());
-                  return parameterType;
-                }).collect(Collectors.toList());
-            pdxSerializer.getParameter().addAll(parameters);
-
-            config.getPdx().setPdxSerializer(pdxSerializer);
-          }
-          return config;
-        });
+    result.setConfigObject(pdxType);
     return result;
   }
 
+  @Override
+  public void updateClusterConfig(String group, CacheConfig config, Object configObject) {
+    config.setPdx((PdxType) configObject);
+  }
+
   /**
    * Interceptor to validate command parameters.
    */
   public static class Interceptor extends AbstractCliAroundInterceptor {
 
     @Override
-    public Result preExecution(GfshParseResult parseResult) {
+    public ResultModel preExecution(GfshParseResult parseResult) {
       String[] portableClassesPatterns = (String[]) parseResult
           .getParamValue(CliStrings.CONFIGURE_PDX__PORTABLE__AUTO__SERIALIZER__CLASSES);
       String[] nonPortableClassesPatterns =
@@ -149,10 +127,9 @@ public class ConfigurePDXCommand extends InternalGfshCommand {
 
       if ((nonPortableClassesPatterns != null && nonPortableClassesPatterns.length > 0)
           && (portableClassesPatterns != null && portableClassesPatterns.length > 0)) {
-        return ResultBuilder.createUserErrorResult(CliStrings.CONFIGURE_PDX__ERROR__MESSAGE);
+        return ResultModel.createError(CliStrings.CONFIGURE_PDX__ERROR__MESSAGE);
       }
-
-      return ResultBuilder.createInfoResult("");
+      return ResultModel.createInfo("");
     }
   }
 }
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/exceptions/EntityExistsException.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/exceptions/EntityExistsException.java
index 49bcc05..dd44715 100644
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/exceptions/EntityExistsException.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/exceptions/EntityExistsException.java
@@ -18,6 +18,9 @@
 package org.apache.geode.management.internal.cli.exceptions;
 
 public class EntityExistsException extends EntityNotFoundException {
+  public EntityExistsException(String message) {
+    super(message);
+  }
 
   public EntityExistsException(String message, boolean statusOK) {
     super(message, statusOK);
diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java
index 0a156ce..247286e 100755
--- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java
+++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/remote/CommandExecutor.java
@@ -115,9 +115,15 @@ public class CommandExecutor {
     // if command result is ok, we will need to see if we need to update cluster configuration
     InfoResultModel infoResultModel = resultModel.addInfo();
     ConfigurationPersistenceService ccService = gfshCommand.getConfigurationPersistenceService();
-    if (parseResult.getParamValue("member") != null || ccService == null) {
+    if (ccService == null) {
       infoResultModel.addLine(
-          "Cluster configuration is not updated because the command is executed on specific member.");
+          "Cluster configuration service is not running. Configuration change is not persisted.");
+      return resultModel;
+    }
+
+    if (parseResult.getParamValue("member") != null) {
+      infoResultModel.addLine(
+          "Configuration change is not persisted because the command is executed on specific member.");
       return resultModel;
     }
 
diff --git a/geode-core/src/test/java/org/apache/geode/cache/configuration/CacheConfigTest.java b/geode-core/src/test/java/org/apache/geode/cache/configuration/CacheConfigTest.java
index 9a4ee7a..da510ce 100644
--- a/geode-core/src/test/java/org/apache/geode/cache/configuration/CacheConfigTest.java
+++ b/geode-core/src/test/java/org/apache/geode/cache/configuration/CacheConfigTest.java
@@ -37,11 +37,9 @@ public class CacheConfigTest {
   private String regionXml;
   private DeclarableType declarableWithString;
   private String declarableWithStringXml;
-  private ClassNameType classNameType;
   private String classNameTypeXml;
   private DeclarableType declarableWithParam;
   private String declarableWithParamXml;
-  private String stringTypeXml;
   private String cacheXml;
 
   @Before
@@ -56,7 +54,6 @@ public class CacheConfigTest {
     regionConfig.setRefid("REPLICATE");
     regionXml = "<region name=\"regionA\" refid=\"REPLICATE\">";
 
-    classNameType = new ClassNameType("my.className");
     classNameTypeXml = "<class-name>my.className</class-name>";
     declarableWithString = new DeclarableType("my.className", "{'key':'value'}");
     declarableWithStringXml =
@@ -68,8 +65,6 @@ public class CacheConfigTest {
     declarableWithParam.getParameter().add(param);
     declarableWithParamXml = classNameTypeXml + "<parameter name=\"key\"><declarable>"
         + declarableWithStringXml + "</declarable></parameter>";
-
-    stringTypeXml = "<string>string value</string>";
   }
 
   @After
@@ -168,6 +163,7 @@ public class CacheConfigTest {
 
     RegionAttributesType regionAttributes = cacheConfig.getRegionAttributes().get(0);
     assertThat(regionAttributes.getCacheListener().get(0)).isEqualTo(declarableWithString);
+    assertThat(regionAttributes.getCompressor().toString()).isEqualTo("my.className");
     assertThat(regionAttributes.getCacheLoader()).isEqualTo(declarableWithString);
     assertThat(regionAttributes.getCacheWriter()).isEqualTo(declarableWithString);
     assertThat(regionAttributes.getRegionTimeToLive().getExpirationAttributes().getCustomExpiry())
diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommandTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommandTest.java
index d377b60..ace35f7 100644
--- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommandTest.java
+++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/ConfigurePDXCommandTest.java
@@ -100,7 +100,7 @@ public class ConfigurePDXCommandTest {
         .statusIsError().containsOutput("Error while processing command")
         .containsOutput("Can't create ReflectionBasedAutoSerializer.");
 
-    verify(command, times(0)).persistClusterConfiguration(any(), any());
+    verify(command, times(0)).updateClusterConfig(any(), any(), any());
   }
 
   @Test
@@ -112,7 +112,7 @@ public class ConfigurePDXCommandTest {
         .statusIsError().containsOutput(
             "The autoserializer cannot support both portable and non-portable classes at the same time.");
 
-    verify(command, times(0)).persistClusterConfiguration(any(), any());
+    verify(command, times(0)).updateClusterConfig(any(), any(), any());
   }
 
   @Test

-- 
To stop receiving notification emails like this one, please contact
jinmeiliao@apache.org.