From e3cf4881218e6f179327cd62432449a8f31560b6 Mon Sep 17 00:00:00 2001 From: Patrick Rhomberg Date: Tue, 2 May 2017 11:09:35 -0700 Subject: [PATCH] GEODE-2662: Gfsh displays field value on wrong line when receiving objects with missing fields DataCommandResult.buildTable refactored to scan for all necessary fields and build rows, padding with MISSING_VALUE as necessary. ServerStarterRule adjusted to build .withPDXPersistent() rather than take it as input to .startServer() Refactored a great deal for readability. --- geode-core/build.gradle | 4 +- .../internal/cli/commands/DataCommands.java | 484 ++++++++------- .../cli/domain/DataCommandResult.java | 554 ++++++++++-------- .../cli/functions/DataCommandFunction.java | 533 +++++++++-------- .../cli/result/TabularResultData.java | 74 +-- .../QueryDataInconsistencyDUnitTest.java | 18 +- .../GemfireDataCommandsDUnitTest.java | 28 +- .../DataCommandFunctionWithPDXJUnitTest.java | 220 +++++++ .../test/dunit/rules/ServerStarterRule.java | 13 +- 9 files changed, 1060 insertions(+), 868 deletions(-) create mode 100644 geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java diff --git a/geode-core/build.gradle b/geode-core/build.gradle index f07444af7baa..02971468da00 100755 --- a/geode-core/build.gradle +++ b/geode-core/build.gradle @@ -113,8 +113,7 @@ dependencies { compile 'commons-beanutils:commons-beanutils:' + project.'commons-beanutils.version' // https://mvnrepository.com/artifact/io.github.lukehutch/fast-classpath-scanner - compile 'io.github.lukehutch:fast-classpath-scanner:' + project.'fast-classpath-scanner.version' - + compile 'io.github.lukehutch:fast-classpath-scanner:' + project.'fast-classpath-scanner.version' compile project(':geode-common') @@ -127,7 +126,6 @@ dependencies { // Test Dependencies // External - testCompile 'com.google.guava:guava:' + project.'guava.version' testCompile 'com.jayway.jsonpath:json-path-assert:' + project.'json-path-assert.version' testCompile 'org.apache.bcel:bcel:' + project.'bcel.version' testRuntime 'org.apache.derby:derby:' + project.'derby.version' diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DataCommands.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DataCommands.java index 89db5d117f09..a38e54504e94 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DataCommands.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/commands/DataCommands.java @@ -14,27 +14,9 @@ */ package org.apache.geode.management.internal.cli.commands; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.StringTokenizer; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.apache.shiro.subject.Subject; -import org.springframework.shell.core.CommandMarker; -import org.springframework.shell.core.annotation.CliAvailabilityIndicator; -import org.springframework.shell.core.annotation.CliCommand; -import org.springframework.shell.core.annotation.CliOption; - +import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; import org.apache.geode.LogWriter; import org.apache.geode.cache.CacheClosedException; import org.apache.geode.cache.CacheFactory; @@ -79,6 +61,26 @@ import org.apache.geode.management.internal.security.ResourceOperation; import org.apache.geode.security.ResourcePermission.Operation; import org.apache.geode.security.ResourcePermission.Resource; +import org.apache.shiro.subject.Subject; +import org.springframework.shell.core.CommandMarker; +import org.springframework.shell.core.annotation.CliAvailabilityIndicator; +import org.springframework.shell.core.annotation.CliCommand; +import org.springframework.shell.core.annotation.CliOption; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.StringTokenizer; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; /** * @since GemFire 7.0 @@ -118,8 +120,8 @@ public Result rebalance( help = CliStrings.REBALANCE__SIMULATE__HELP) boolean simulate) { ExecutorService commandExecutors = Executors.newSingleThreadExecutor(); - List> commandResult = new ArrayList>(); - Result result = null; + List> commandResult = new ArrayList<>(); + Result result; try { commandResult.add(commandExecutors .submit(new ExecuteRebalanceWithTimeout(includeRegions, excludeRegions, simulate))); @@ -165,16 +167,16 @@ public Result executeRebalanceWithTimeout(String[] includeRegions, String[] excl Result result = null; try { - RebalanceOperation op = null; + RebalanceOperation op; - if (includeRegions != null && includeRegions.length > 0) { - CompositeResultData rebalanceResulteData = ResultBuilder.createCompositeResultData(); + if (ArrayUtils.isNotEmpty(includeRegions)) { + CompositeResultData rebalanceResultData = ResultBuilder.createCompositeResultData(); int index = 0; for (String regionName : includeRegions) { // To be removed after region Name specification with "/" is fixed - regionName = regionName.startsWith("/") == true ? regionName : ("/" + regionName); + regionName = regionName.startsWith("/") ? regionName : ("/" + regionName); Region region = cache.getRegion(regionName); if (region == null) { @@ -189,21 +191,18 @@ public Result executeRebalanceWithTimeout(String[] includeRegions, String[] excl Function rebalanceFunction = new RebalanceFunction(); Object[] functionArgs = new Object[3]; functionArgs[0] = simulate ? "true" : "false"; - Set setRegionName = new HashSet(); + Set setRegionName = new HashSet<>(); setRegionName.add(regionName); functionArgs[1] = setRegionName; - Set excludeRegionSet = new HashSet(); - if (excludeRegions != null && excludeRegions.length > 0) { - - for (String str : excludeRegions) { - excludeRegionSet.add(str); - } + Set excludeRegionSet = new HashSet<>(); + if (ArrayUtils.isNotEmpty(excludeRegions)) { + Collections.addAll(excludeRegionSet, excludeRegions); } functionArgs[2] = excludeRegionSet; - if (simulate == true) { - List resultList = null; + if (simulate) { + List resultList; try { resultList = (ArrayList) CliUtil .executeFunction(rebalanceFunction, functionArgs, member).getResult(); @@ -212,24 +211,24 @@ public Result executeRebalanceWithTimeout(String[] includeRegions, String[] excl .info(CliStrings.format( CliStrings.REBALANCE__MSG__EXCEPTION_IN_REBALANCE_FOR_MEMBER_0_Exception_1, member.getId(), ex.getMessage()), ex); - rebalanceResulteData.addSection() + rebalanceResultData.addSection() .addData(CliStrings.format( CliStrings.REBALANCE__MSG__EXCEPTION_IN_REBALANCE_FOR_MEMBER_0_Exception, member.getId()), ex.getMessage()); - result = ResultBuilder.buildResult(rebalanceResulteData); + result = ResultBuilder.buildResult(rebalanceResultData); continue; } - if (checkResultList(rebalanceResulteData, resultList, member) == true) { - result = ResultBuilder.buildResult(rebalanceResulteData); + if (checkResultList(rebalanceResultData, resultList, member)) { + result = ResultBuilder.buildResult(rebalanceResultData); continue; } List rstList = tokenize((String) resultList.get(0), ","); - result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResulteData, - (ArrayList) rstList, index, simulate, cache)); + result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResultData, + (ArrayList) rstList, index, true, cache)); } else { - List resultList = null; + List resultList; try { resultList = (ArrayList) CliUtil .executeFunction(rebalanceFunction, functionArgs, member).getResult(); @@ -238,47 +237,47 @@ public Result executeRebalanceWithTimeout(String[] includeRegions, String[] excl .info(CliStrings.format( CliStrings.REBALANCE__MSG__EXCEPTION_IN_REBALANCE_FOR_MEMBER_0_Exception_1, member.getId(), ex.getMessage()), ex); - rebalanceResulteData.addSection() + rebalanceResultData.addSection() .addData(CliStrings.format( CliStrings.REBALANCE__MSG__EXCEPTION_IN_REBALANCE_FOR_MEMBER_0_Exception, member.getId()), ex.getMessage()); - result = ResultBuilder.buildResult(rebalanceResulteData); + result = ResultBuilder.buildResult(rebalanceResultData); continue; } - if (checkResultList(rebalanceResulteData, resultList, member) == true) { - result = ResultBuilder.buildResult(rebalanceResulteData); + if (checkResultList(rebalanceResultData, resultList, member)) { + result = ResultBuilder.buildResult(rebalanceResultData); continue; } List rstList = tokenize((String) resultList.get(0), ","); - result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResulteData, - (ArrayList) rstList, index, simulate, cache)); + result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResultData, + (ArrayList) rstList, index, false, cache)); } } else { + ResourceManager manager = cache.getResourceManager(); RebalanceFactory rbFactory = manager.createRebalanceFactory(); - Set excludeRegionSet = new HashSet(); + Set excludeRegionSet = new HashSet<>(); if (excludeRegions != null) { - for (String excludeRegion : excludeRegions) - excludeRegionSet.add(excludeRegion); + Collections.addAll(excludeRegionSet, excludeRegions); } rbFactory.excludeRegions(excludeRegionSet); - Set includeRegionSet = new HashSet(); + Set includeRegionSet = new HashSet<>(); includeRegionSet.add(regionName); rbFactory.includeRegions(includeRegionSet); - if (simulate == true) { + if (simulate) { op = manager.createRebalanceFactory().simulate(); - result = ResultBuilder.buildResult(buildResultForRebalance(rebalanceResulteData, - op.getResults(), index, simulate, cache)); + result = ResultBuilder.buildResult(buildResultForRebalance(rebalanceResultData, + op.getResults(), index, true, cache)); } else { op = manager.createRebalanceFactory().start(); // Wait until the rebalance is complete and then get the results - result = ResultBuilder.buildResult(buildResultForRebalance(rebalanceResulteData, - op.getResults(), index, simulate, cache)); + result = ResultBuilder.buildResult(buildResultForRebalance(rebalanceResultData, + op.getResults(), index, false, cache)); } } index++; @@ -297,9 +296,9 @@ public Result executeRebalanceWithTimeout(String[] includeRegions, String[] excl } } - List tokenize(String str, String separator) { + private List tokenize(String str, String separator) { StringTokenizer st = new StringTokenizer(str, separator); - List rstList = new ArrayList(); + List rstList = new ArrayList<>(); while (st.hasMoreTokens()) { rstList.add(st.nextToken()); @@ -308,16 +307,15 @@ List tokenize(String str, String separator) { return rstList; } - boolean checkResultList(CompositeResultData rebalanceResulteData, List resultList, + private boolean checkResultList(CompositeResultData rebalanceResultData, List resultList, DistributedMember member) { boolean toContinueForOtherMembers = false; - if (resultList != null && !resultList.isEmpty()) { - for (int i = 0; i < resultList.size(); i++) { - Object object = resultList.get(i); + if (CollectionUtils.isNotEmpty(resultList)) { + for (Object object : resultList) { if (object instanceof Exception) { - rebalanceResulteData.addSection().addData( + rebalanceResultData.addSection().addData( CliStrings.format(CliStrings.REBALANCE__MSG__NO_EXECUTION, member.getId()), ((Exception) object).getMessage()); @@ -327,7 +325,7 @@ boolean checkResultList(CompositeResultData rebalanceResulteData, List resultLis toContinueForOtherMembers = true; break; } else if (object instanceof Throwable) { - rebalanceResulteData.addSection().addData( + rebalanceResultData.addSection().addData( CliStrings.format(CliStrings.REBALANCE__MSG__NO_EXECUTION, member.getId()), ((Throwable) object).getMessage()); @@ -341,7 +339,7 @@ boolean checkResultList(CompositeResultData rebalanceResulteData, List resultLis } else { LogWrapper.getInstance().info( "Rebalancing for member=" + member.getId() + ", resultList is either null or empty"); - rebalanceResulteData.addSection().addData("Rebalancing for member=" + member.getId(), + rebalanceResultData.addSection().addData("Rebalancing for member=" + member.getId(), ", resultList is either null or empty"); toContinueForOtherMembers = true; } @@ -349,15 +347,14 @@ boolean checkResultList(CompositeResultData rebalanceResulteData, List resultLis return toContinueForOtherMembers; } - Result executeRebalanceOnDS(InternalCache cache, String simulate, String[] excludeRegionsList) { + private Result executeRebalanceOnDS(InternalCache cache, String simulate, + String[] excludeRegionsList) { Result result = null; int index = 1; - CompositeResultData rebalanceResulteData = ResultBuilder.createCompositeResultData(); - List listExcludedRegion = new ArrayList(); + CompositeResultData rebalanceResultData = ResultBuilder.createCompositeResultData(); + List listExcludedRegion = new ArrayList<>(); if (excludeRegionsList != null) { - for (String str : excludeRegionsList) { - listExcludedRegion.add(str); - } + Collections.addAll(listExcludedRegion, excludeRegionsList); } List listMemberRegion = getMemberRegionList(cache, listExcludedRegion); @@ -377,29 +374,26 @@ Result executeRebalanceOnDS(InternalCache cache, String simulate, String[] exclu } } - if (flagToContinueWithRebalance == false) { + if (!flagToContinueWithRebalance) { return ResultBuilder .createInfoResult(CliStrings.REBALANCE__MSG__NO_REBALANCING_REGIONS_ON_DS); } - Iterator it1 = listMemberRegion.iterator(); - while (it1.hasNext() && flagToContinueWithRebalance) { + for (MemberPRInfo memberPR : listMemberRegion) { try { - MemberPRInfo memberPR = (MemberPRInfo) it1.next(); - // check if there are more than one members associated with region for - // rebalancing + // check if there are more than one members associated with region for rebalancing if (memberPR.dsMemberList.size() > 1) { for (int i = 0; i < memberPR.dsMemberList.size(); i++) { DistributedMember dsMember = memberPR.dsMemberList.get(i); Function rebalanceFunction = new RebalanceFunction(); Object[] functionArgs = new Object[3]; functionArgs[0] = simulate; - Set regionSet = new HashSet(); + Set regionSet = new HashSet<>(); regionSet.add(memberPR.region); functionArgs[1] = regionSet; - Set excludeRegionSet = new HashSet(); + Set excludeRegionSet = new HashSet<>(); functionArgs[2] = excludeRegionSet; List resultList = null; @@ -409,52 +403,51 @@ Result executeRebalanceOnDS(InternalCache cache, String simulate, String[] exclu resultList = (ArrayList) CliUtil .executeFunction(rebalanceFunction, functionArgs, dsMember).getResult(); - if (checkResultList(rebalanceResulteData, resultList, dsMember) == true) { - result = ResultBuilder.buildResult(rebalanceResulteData); + if (checkResultList(rebalanceResultData, resultList, dsMember)) { + result = ResultBuilder.buildResult(rebalanceResultData); continue; } List rstList = tokenize((String) resultList.get(0), ","); - result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResulteData, - (ArrayList) rstList, index, simulate.equals("true") ? true : false, cache)); + result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResultData, + (ArrayList) rstList, index, simulate.equals("true"), cache)); index++; - // Rebalancing for region is done so break and continue with - // other region + // Rebalancing for region is done so break and continue with other region break; } else { if (i == memberPR.dsMemberList.size() - 1) { - rebalanceResulteData.addSection().addData( + rebalanceResultData.addSection().addData( CliStrings.format( CliStrings.REBALANCE__MSG__NO_EXECUTION_FOR_REGION_0_ON_MEMBERS_1, memberPR.region, listOfAllMembers(memberPR.dsMemberList)), CliStrings.REBALANCE__MSG__MEMBERS_MIGHT_BE_DEPARTED); - result = ResultBuilder.buildResult(rebalanceResulteData); + result = ResultBuilder.buildResult(rebalanceResultData); } else { continue; } } } catch (Exception ex) { if (i == memberPR.dsMemberList.size() - 1) { - rebalanceResulteData.addSection().addData( + rebalanceResultData.addSection().addData( CliStrings.format( CliStrings.REBALANCE__MSG__NO_EXECUTION_FOR_REGION_0_ON_MEMBERS_1, memberPR.region, listOfAllMembers(memberPR.dsMemberList)), CliStrings.REBALANCE__MSG__REASON + ex.getMessage()); - result = ResultBuilder.buildResult(rebalanceResulteData); + result = ResultBuilder.buildResult(rebalanceResultData); } else { continue; } } - if (checkResultList(rebalanceResulteData, resultList, dsMember) == true) { - result = ResultBuilder.buildResult(rebalanceResulteData); + if (checkResultList(rebalanceResultData, resultList, dsMember)) { + result = ResultBuilder.buildResult(rebalanceResultData); continue; } List rstList = tokenize((String) resultList.get(0), ","); - result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResulteData, - (ArrayList) rstList, index, simulate.equals("true") ? true : false, cache)); + result = ResultBuilder.buildResult(toCompositeResultData(rebalanceResultData, + (ArrayList) rstList, index, simulate.equals("true"), cache)); index++; } } @@ -487,65 +480,59 @@ protected CompositeResultData toCompositeResultData(CompositeResultData rebalanc ArrayList rstlist, int index, boolean simulate, InternalCache cache) { // add only if there are any valid regions in results - if (rstlist.size() > resultItemCount && rstlist.get(resultItemCount) != null - && rstlist.get(resultItemCount).length() > 0) { + if (rstlist.size() > resultItemCount && StringUtils.isNotEmpty(rstlist.get(resultItemCount))) { TabularResultData table1 = rebalanceResulteData.addSection().addTable("Table" + index); String newLine = System.getProperty("line.separator"); StringBuilder resultStr = new StringBuilder(); resultStr.append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETCREATEBYTES); table1.accumulate("Value", rstlist.get(0)); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATEBYTES + " = " + rstlist.get(0)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATEBYTES).append(" = ") + .append(rstlist.get(0)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETCREATETIM); table1.accumulate("Value", rstlist.get(1)); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATETIM + " = " + rstlist.get(1)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATETIM).append(" = ") + .append(rstlist.get(1)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETCREATESCOMPLETED); table1.accumulate("Value", rstlist.get(2)); - resultStr - .append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATESCOMPLETED + " = " + rstlist.get(2)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATESCOMPLETED).append(" = ") + .append(rstlist.get(2)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERBYTES); table1.accumulate("Value", rstlist.get(3)); - resultStr - .append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERBYTES + " = " + rstlist.get(3)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERBYTES).append(" = ") + .append(rstlist.get(3)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERTIME); table1.accumulate("Value", rstlist.get(4)); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERTIME + " = " + rstlist.get(4)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERTIME).append(" = ") + .append(rstlist.get(4)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERSCOMPLETED); table1.accumulate("Value", rstlist.get(5)); - resultStr.append( - CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERSCOMPLETED + " = " + rstlist.get(5)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERSCOMPLETED).append(" = ") + .append(rstlist.get(5)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERTIME); table1.accumulate("Value", rstlist.get(6)); - resultStr - .append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERTIME + " = " + rstlist.get(6)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERTIME).append(" = ") + .append(rstlist.get(6)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERSCOMPLETED); table1.accumulate("Value", rstlist.get(7)); - resultStr.append( - CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERSCOMPLETED + " = " + rstlist.get(7)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERSCOMPLETED).append(" = ") + .append(rstlist.get(7)).append(newLine); table1.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALTIME); table1.accumulate("Value", rstlist.get(8)); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALTIME + " = " + rstlist.get(8)); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALTIME).append(" = ").append(rstlist.get(8)) + .append(newLine); - String headerText = null; + String headerText; if (simulate) { headerText = "Simulated partition regions "; } else { @@ -560,81 +547,73 @@ protected CompositeResultData toCompositeResultData(CompositeResultData rebalanc return rebalanceResulteData; } - CompositeResultData buildResultForRebalance(CompositeResultData rebalanceResulteData, + private CompositeResultData buildResultForRebalance(CompositeResultData rebalanceResultData, RebalanceResults results, int index, boolean simulate, InternalCache cache) { Set regions = results.getPartitionRebalanceDetails(); Iterator iterator = regions.iterator(); // add only if there are valid number of regions - if (regions.size() > 0 && ((PartitionRebalanceInfo) iterator.next()).getRegionPath() != null - && ((PartitionRebalanceInfo) iterator.next()).getRegionPath().length() > 0) { + if (regions.size() > 0 + && StringUtils.isNotEmpty(((PartitionRebalanceInfo) iterator.next()).getRegionPath())) { final TabularResultData resultData = - rebalanceResulteData.addSection().addTable("Table" + index); + rebalanceResultData.addSection().addTable("Table" + index); String newLine = System.getProperty("line.separator"); StringBuilder resultStr = new StringBuilder(); resultStr.append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETCREATEBYTES); resultData.accumulate("Value", results.getTotalBucketCreateBytes()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATEBYTES + " = " - + results.getTotalBucketCreateBytes()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATEBYTES).append(" = ") + .append(results.getTotalBucketCreateBytes()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETCREATETIM); resultData.accumulate("Value", results.getTotalBucketCreateTime()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATETIM + " = " - + results.getTotalBucketCreateTime()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATETIM).append(" = ") + .append(results.getTotalBucketCreateTime()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETCREATESCOMPLETED); resultData.accumulate("Value", results.getTotalBucketCreatesCompleted()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATESCOMPLETED + " = " - + results.getTotalBucketCreatesCompleted()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETCREATESCOMPLETED).append(" = ") + .append(results.getTotalBucketCreatesCompleted()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERBYTES); resultData.accumulate("Value", results.getTotalBucketTransferBytes()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERBYTES + " = " - + results.getTotalBucketTransferBytes()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERBYTES).append(" = ") + .append(results.getTotalBucketTransferBytes()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERTIME); resultData.accumulate("Value", results.getTotalBucketTransferTime()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERTIME + " = " - + results.getTotalBucketTransferTime()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERTIME).append(" = ") + .append(results.getTotalBucketTransferTime()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERSCOMPLETED); resultData.accumulate("Value", results.getTotalBucketTransfersCompleted()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERSCOMPLETED + " = " - + results.getTotalBucketTransfersCompleted()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALBUCKETTRANSFERSCOMPLETED).append(" = ") + .append(results.getTotalBucketTransfersCompleted()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERTIME); resultData.accumulate("Value", results.getTotalPrimaryTransferTime()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERTIME + " = " - + results.getTotalPrimaryTransferTime()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERTIME).append(" = ") + .append(results.getTotalPrimaryTransferTime()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERSCOMPLETED); resultData.accumulate("Value", results.getTotalPrimaryTransfersCompleted()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERSCOMPLETED + " = " - + results.getTotalPrimaryTransfersCompleted()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALPRIMARYTRANSFERSCOMPLETED).append(" = ") + .append(results.getTotalPrimaryTransfersCompleted()).append(newLine); resultData.accumulate("Rebalanced Stats", CliStrings.REBALANCE__MSG__TOTALTIME); resultData.accumulate("Value", results.getTotalTime()); - resultStr.append(CliStrings.REBALANCE__MSG__TOTALTIME + " = " + results.getTotalTime()); - resultStr.append(newLine); + resultStr.append(CliStrings.REBALANCE__MSG__TOTALTIME).append(" = ") + .append(results.getTotalTime()).append(newLine); Iterator it = regions.iterator(); - String headerText = null; + String headerText; if (simulate) { headerText = "Simulated partition regions "; @@ -650,7 +629,7 @@ CompositeResultData buildResultForRebalance(CompositeResultData rebalanceResulte cache.getLogger().info(headerText + resultStr); } - return rebalanceResulteData; + return rebalanceResultData; } public DistributedMember getAssociatedMembers(String region, final InternalCache cache) { @@ -660,7 +639,7 @@ public DistributedMember getAssociatedMembers(String region, final InternalCache DistributedMember member = null; if (bean == null) { - return member; + return null; } String[] membersName = bean.getMembers(); @@ -670,7 +649,7 @@ public DistributedMember getAssociatedMembers(String region, final InternalCache boolean matchFound = false; if (membersName.length > 1) { - while (it.hasNext() && matchFound == false) { + while (it.hasNext() && !matchFound) { DistributedMember dsmember = (DistributedMember) it.next(); for (String memberName : membersName) { if (MBeanJMXAdapter.getMemberNameOrId(dsmember).equals(memberName)) { @@ -684,8 +663,9 @@ public DistributedMember getAssociatedMembers(String region, final InternalCache return member; } - List getMemberRegionList(InternalCache cache, List listExcludedRegion) { - List listMemberPRInfo = new ArrayList(); + private List getMemberRegionList(InternalCache cache, + List listExcludedRegion) { + List listMemberPRInfo = new ArrayList<>(); String[] listDSRegions = ManagementService.getManagementService(cache).getDistributedSystemMXBean().listRegions(); final Set dsMembers = CliUtil.getAllMembers(cache); @@ -693,11 +673,10 @@ List getMemberRegionList(InternalCache cache, List listExc for (String regionName : listDSRegions) { // check for excluded regions boolean excludedRegionMatch = false; - Iterator it = listExcludedRegion.iterator(); - while (it.hasNext()) { + for (String aListExcludedRegion : listExcludedRegion) { // this is needed since region name may start with / or without it // also - String excludedRegion = it.next().trim(); + String excludedRegion = aListExcludedRegion.trim(); if (regionName.startsWith("/")) { if (!excludedRegion.startsWith("/")) { excludedRegion = "/" + excludedRegion; @@ -715,7 +694,7 @@ List getMemberRegionList(InternalCache cache, List listExc } } - if (excludedRegionMatch == true) { + if (excludedRegionMatch) { // ignore this region continue; } @@ -773,7 +752,7 @@ public Result exportData( this.securityService.authorizeRegionRead(regionName); final DistributedMember targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId); - Result result = null; + Result result; if (!filePath.endsWith(CliStrings.GEODE_DATA_FILE_EXTENSION)) { return ResultBuilder.createUserErrorResult(CliStrings @@ -826,13 +805,12 @@ public Result importData( unspecifiedDefaultValue = CliMetaData.ANNOTATION_NULL_VALUE, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.IMPORT_DATA__MEMBER__HELP) String memberNameOrId, - @CliOption(key = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS, mandatory = false, - unspecifiedDefaultValue = "false", + @CliOption(key = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS, unspecifiedDefaultValue = "false", help = CliStrings.IMPORT_DATA__INVOKE_CALLBACKS__HELP) boolean invokeCallbacks) { this.securityService.authorizeRegionWrite(regionName); - Result result = null; + Result result; try { final DistributedMember targetMember = CliUtil.getDistributedMemberByNameOrId(memberNameOrId); @@ -874,8 +852,7 @@ public Result importData( return result; } - @CliMetaData(shellOnly = false, - relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) + @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) @CliCommand(value = {CliStrings.PUT}, help = CliStrings.PUT__HELP) public Result put( @CliOption(key = {CliStrings.PUT__KEY}, mandatory = true, @@ -894,26 +871,28 @@ public Result put( this.securityService.authorizeRegionWrite(regionPath); InternalCache cache = getCache(); - DataCommandResult dataResult = null; - if (regionPath == null || regionPath.isEmpty()) { + DataCommandResult dataResult; + if (StringUtils.isEmpty(regionPath)) { return makePresentationResult(DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__REGIONNAME_EMPTY, false)); } - if (key == null || key.isEmpty()) - return makePresentationResult(dataResult = DataCommandResult.createPutResult(key, null, null, + if (StringUtils.isEmpty(key)) { + return makePresentationResult(DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__KEY_EMPTY, false)); + } - if (value == null || value.isEmpty()) - return makePresentationResult(dataResult = DataCommandResult.createPutResult(value, null, - null, CliStrings.PUT__MSG__VALUE_EMPTY, false)); + if (StringUtils.isEmpty(value)) { + return makePresentationResult(DataCommandResult.createPutResult(value, null, null, + CliStrings.PUT__MSG__VALUE_EMPTY, false)); + } @SuppressWarnings("rawtypes") Region region = cache.getRegion(regionPath); DataCommandFunction putfn = new DataCommandFunction(); if (region == null) { Set memberList = getRegionAssociatedMembers(regionPath, getCache(), false); - if (memberList != null && memberList.size() > 0) { + if (CollectionUtils.isNotEmpty(memberList)) { DataCommandRequest request = new DataCommandRequest(); request.setCommand(CliStrings.PUT); request.setValue(value); @@ -923,28 +902,30 @@ public Result put( request.setValueClass(valueClass); request.setPutIfAbsent(putIfAbsent); dataResult = callFunctionForRegion(request, putfn, memberList); - } else + } else { dataResult = DataCommandResult.createPutInfoResult(key, value, null, CliStrings.format(CliStrings.PUT__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false); + } } else { dataResult = putfn.put(key, value, putIfAbsent, keyClass, valueClass, regionPath); } dataResult.setKeyClass(keyClass); - if (valueClass != null) + if (valueClass != null) { dataResult.setValueClass(valueClass); + } return makePresentationResult(dataResult); } private Result makePresentationResult(DataCommandResult dataResult) { - if (dataResult != null) + if (dataResult != null) { return dataResult.toCommandResult(); - else + } else { return ResultBuilder.createGemFireErrorResult("Error executing data command"); + } } - @CliMetaData(shellOnly = false, - relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) + @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) @CliCommand(value = {CliStrings.GET}, help = CliStrings.GET__HELP) public Result get( @CliOption(key = {CliStrings.GET__KEY}, mandatory = true, @@ -962,23 +943,24 @@ public Result get( this.securityService.authorizeRegionRead(regionPath, key); InternalCache cache = getCache(); - DataCommandResult dataResult = null; + DataCommandResult dataResult; - if (regionPath == null || regionPath.isEmpty()) { - return makePresentationResult(dataResult = DataCommandResult.createGetResult(key, null, null, + if (StringUtils.isEmpty(regionPath)) { + return makePresentationResult(DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__REGIONNAME_EMPTY, false)); } - if (key == null || key.isEmpty()) - return makePresentationResult(dataResult = DataCommandResult.createGetResult(key, null, null, + if (StringUtils.isEmpty(key)) { + return makePresentationResult(DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__KEY_EMPTY, false)); + } @SuppressWarnings("rawtypes") Region region = cache.getRegion(regionPath); DataCommandFunction getfn = new DataCommandFunction(); if (region == null) { Set memberList = getRegionAssociatedMembers(regionPath, getCache(), false); - if (memberList != null && memberList.size() > 0) { + if (CollectionUtils.isNotEmpty(memberList)) { DataCommandRequest request = new DataCommandRequest(); request.setCommand(CliStrings.GET); request.setKey(key); @@ -988,25 +970,26 @@ public Result get( request.setLoadOnCacheMiss(loadOnCacheMiss); Subject subject = this.securityService.getSubject(); if (subject != null) { - request.setPrincipal((Serializable) subject.getPrincipal()); + request.setPrincipal(subject.getPrincipal()); } dataResult = callFunctionForRegion(request, getfn, memberList); - } else + } else { dataResult = DataCommandResult.createGetInfoResult(key, null, null, CliStrings.format(CliStrings.GET__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false); + } } else { dataResult = getfn.get(null, key, keyClass, valueClass, regionPath, loadOnCacheMiss); } dataResult.setKeyClass(keyClass); - if (valueClass != null) + if (valueClass != null) { dataResult.setValueClass(valueClass); + } return makePresentationResult(dataResult); } - @CliMetaData(shellOnly = false, - relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) + @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) @CliCommand(value = {CliStrings.LOCATE_ENTRY}, help = CliStrings.LOCATE_ENTRY__HELP) public Result locateEntry( @CliOption(key = {CliStrings.LOCATE_ENTRY__KEY}, mandatory = true, @@ -1024,20 +1007,21 @@ public Result locateEntry( this.securityService.authorizeRegionRead(regionPath, key); - DataCommandResult dataResult = null; + DataCommandResult dataResult; - if (regionPath == null || regionPath.isEmpty()) { - return makePresentationResult(dataResult = DataCommandResult.createLocateEntryResult(key, - null, null, CliStrings.LOCATE_ENTRY__MSG__REGIONNAME_EMPTY, false)); + if (StringUtils.isEmpty(regionPath)) { + return makePresentationResult(DataCommandResult.createLocateEntryResult(key, null, null, + CliStrings.LOCATE_ENTRY__MSG__REGIONNAME_EMPTY, false)); } - if (key == null || key.isEmpty()) - return makePresentationResult(dataResult = DataCommandResult.createLocateEntryResult(key, - null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_EMPTY, false)); + if (StringUtils.isEmpty(key)) { + return makePresentationResult(DataCommandResult.createLocateEntryResult(key, null, null, + CliStrings.LOCATE_ENTRY__MSG__KEY_EMPTY, false)); + } DataCommandFunction locateEntry = new DataCommandFunction(); Set memberList = getRegionAssociatedMembers(regionPath, getCache(), true); - if (memberList != null && memberList.size() > 0) { + if (CollectionUtils.isNotEmpty(memberList)) { DataCommandRequest request = new DataCommandRequest(); request.setCommand(CliStrings.LOCATE_ENTRY); request.setKey(key); @@ -1046,18 +1030,19 @@ public Result locateEntry( request.setValueClass(valueClass); request.setRecursive(recursive); dataResult = callFunctionForRegion(request, locateEntry, memberList); - } else + } else { dataResult = DataCommandResult.createLocateEntryInfoResult(key, null, null, CliStrings.format( CliStrings.LOCATE_ENTRY__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false); + } dataResult.setKeyClass(keyClass); - if (valueClass != null) + if (valueClass != null) { dataResult.setValueClass(valueClass); + } return makePresentationResult(dataResult); } - @CliMetaData(shellOnly = false, - relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) + @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) @CliCommand(value = {CliStrings.REMOVE}, help = CliStrings.REMOVE__HELP) public Result remove( @CliOption(key = {CliStrings.REMOVE__KEY}, help = CliStrings.REMOVE__KEY__HELP, @@ -1070,16 +1055,16 @@ public Result remove( @CliOption(key = {CliStrings.REMOVE__KEYCLASS}, help = CliStrings.REMOVE__KEYCLASS__HELP) String keyClass) { InternalCache cache = getCache(); - DataCommandResult dataResult = null; + DataCommandResult dataResult; - if (regionPath == null || regionPath.isEmpty()) { - return makePresentationResult(dataResult = DataCommandResult.createRemoveResult(key, null, - null, CliStrings.REMOVE__MSG__REGIONNAME_EMPTY, false)); + if (StringUtils.isEmpty(regionPath)) { + return makePresentationResult(DataCommandResult.createRemoveResult(key, null, null, + CliStrings.REMOVE__MSG__REGIONNAME_EMPTY, false)); } if (!removeAllKeys && (key == null)) { - return makePresentationResult(dataResult = DataCommandResult.createRemoveResult(key, null, - null, CliStrings.REMOVE__MSG__KEY_EMPTY, false)); + return makePresentationResult(DataCommandResult.createRemoveResult(null, null, null, + CliStrings.REMOVE__MSG__KEY_EMPTY, false)); } if (removeAllKeys) { @@ -1093,7 +1078,7 @@ public Result remove( DataCommandFunction removefn = new DataCommandFunction(); if (region == null) { Set memberList = getRegionAssociatedMembers(regionPath, getCache(), false); - if (memberList != null && memberList.size() > 0) { + if (CollectionUtils.isNotEmpty(memberList)) { DataCommandRequest request = new DataCommandRequest(); request.setCommand(CliStrings.REMOVE); request.setKey(key); @@ -1101,10 +1086,11 @@ public Result remove( request.setRemoveAllKeys(removeAllKeys ? "ALL" : null); request.setRegionName(regionPath); dataResult = callFunctionForRegion(request, removefn, memberList); - } else + } else { dataResult = DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND_ON_ALL_MEMBERS, regionPath), false); + } } else { dataResult = removefn.remove(key, keyClass, regionPath, removeAllKeys ? "ALL" : null); @@ -1114,17 +1100,15 @@ public Result remove( return makePresentationResult(dataResult); } - @CliMetaData(shellOnly = false, - relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) + @CliMetaData(relatedTopic = {CliStrings.TOPIC_GEODE_DATA, CliStrings.TOPIC_GEODE_REGION}) @MultiStepCommand @CliCommand(value = {CliStrings.QUERY}, help = CliStrings.QUERY__HELP) public Object query( @CliOption(key = CliStrings.QUERY__QUERY, help = CliStrings.QUERY__QUERY__HELP, mandatory = true) final String query, - @CliOption(key = CliStrings.QUERY__STEPNAME, mandatory = false, help = "Step name", + @CliOption(key = CliStrings.QUERY__STEPNAME, help = "Step name", unspecifiedDefaultValue = CliStrings.QUERY__STEPNAME__DEFAULTVALUE) String stepName, - @CliOption(key = CliStrings.QUERY__INTERACTIVE, mandatory = false, - help = CliStrings.QUERY__INTERACTIVE__HELP, + @CliOption(key = CliStrings.QUERY__INTERACTIVE, help = CliStrings.QUERY__INTERACTIVE__HELP, unspecifiedDefaultValue = "true") final boolean interactive) { if (!CliUtil.isGfshVM() && stepName.equals(CliStrings.QUERY__STEPNAME__DEFAULTVALUE)) { @@ -1156,18 +1140,12 @@ private static class MemberPRInfo { public String region; public MemberPRInfo() { - region = new String(); - dsMemberList = new ArrayList(); + region = ""; + dsMemberList = new ArrayList<>(); } public boolean equals(Object o2) { - if (o2 == null) { - return false; - } - if (this.region.equals(((MemberPRInfo) o2).region)) { - return true; - } - return false; + return o2 != null && this.region.equals(((MemberPRInfo) o2).region); } } @@ -1196,8 +1174,7 @@ public static DataCommandResult callFunctionForRegion(DataCommandRequest request FunctionService.onMembers(members).setArguments(request).execute(putfn); List list = (List) collector.getResult(); DataCommandResult result = null; - for (int i = 0; i < list.size(); i++) { - Object object = list.get(i); + for (Object object : list) { if (object instanceof Throwable) { Throwable error = (Throwable) object; result = new DataCommandResult(); @@ -1220,42 +1197,45 @@ public static DataCommandResult callFunctionForRegion(DataCommandRequest request public static Set getQueryRegionsAssociatedMembers(Set regions, final InternalCache cache, boolean returnAll) { LogWriter logger = cache.getLogger(); - Set members = null; + Set members; Set newMembers = null; Iterator iterator = regions.iterator(); - String region = (String) iterator.next(); + String region = iterator.next(); members = getRegionAssociatedMembers(region, cache, true); - if (logger.fineEnabled()) + if (logger.fineEnabled()) { logger.fine("Members for region " + region + " Members " + members); - List regionAndingList = new ArrayList(); + } + List regionAndingList = new ArrayList<>(); regionAndingList.add(region); if (regions.size() == 1) { newMembers = members; } else { - if (members != null && !members.isEmpty()) { + if (CollectionUtils.isNotEmpty(members)) { while (iterator.hasNext()) { region = iterator.next(); newMembers = getRegionAssociatedMembers(region, cache, true); if (newMembers == null) { - newMembers = new HashSet(); + newMembers = new HashSet<>(); } - if (logger.fineEnabled()) + if (logger.fineEnabled()) { logger.fine("Members for region " + region + " Members " + newMembers); + } regionAndingList.add(region); newMembers.retainAll(members); members = newMembers; - if (logger.fineEnabled()) + if (logger.fineEnabled()) { logger.fine( "Members after anding for regions " + regionAndingList + " List : " + newMembers); + } } } } - members = new HashSet(); - if (newMembers == null) + members = new HashSet<>(); + if (newMembers == null) { return members; - Iterator memberIterator = newMembers.iterator(); - while (memberIterator.hasNext()) { - members.add(memberIterator.next()); + } + for (DistributedMember newMember : newMembers) { + members.add(newMember); if (!returnAll) { return members; } @@ -1267,17 +1247,20 @@ public static Set getQueryRegionsAssociatedMembers(Set getRegionAssociatedMembers(String region, final InternalCache cache, boolean returnAll) { - DistributedMember member = null; + DistributedMember member; - if (region == null || region.isEmpty()) + if (StringUtils.isEmpty(region)) { return null; + } DistributedRegionMXBean bean = ManagementService.getManagementService(cache).getDistributedRegionMXBean(region); - if (bean == null)// try with slash ahead + if (bean == null) { + // try with slash ahead bean = ManagementService.getManagementService(cache) .getDistributedRegionMXBean(Region.SEPARATOR + region); + } if (bean == null) { return null; @@ -1285,11 +1268,11 @@ public static Set getRegionAssociatedMembers(String region, String[] membersName = bean.getMembers(); Set dsMembers = cache.getMembers(); - Set dsMembersWithThisMember = new HashSet(); + Set dsMembersWithThisMember = new HashSet<>(); dsMembersWithThisMember.addAll(dsMembers); dsMembersWithThisMember.add(cache.getDistributedSystem().getDistributedMember()); Iterator it = dsMembersWithThisMember.iterator(); - Set matchedMembers = new HashSet(); + Set matchedMembers = new HashSet<>(); if (membersName.length > 0) { while (it.hasNext()) { @@ -1321,11 +1304,13 @@ public static Object[] replaceGfshEnvVar(String query, Map gfshE int replacedVars = 0; while (!done) { int index1 = query.indexOf("${", startIndex); - if (index1 == -1) + if (index1 == -1) { break; + } int index2 = query.indexOf("}", index1); - if (index2 == -1) + if (index2 == -1) { break; + } String var = query.substring(index1 + 2, index2); String value = gfshEnvVarMap.get(var); if (value != null) { @@ -1333,8 +1318,9 @@ public static Object[] replaceGfshEnvVar(String query, Map gfshE replacedVars++; } startIndex = index2 + 1; - if (startIndex >= query.length()) + if (startIndex >= query.length()) { done = true; + } } return new Object[] {replacedVars, query}; } diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/domain/DataCommandResult.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/domain/DataCommandResult.java index 423d78124845..fe88fc98d397 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/domain/DataCommandResult.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/domain/DataCommandResult.java @@ -17,14 +17,7 @@ import static org.apache.geode.management.internal.cli.multistep.CLIMultiStepHelper.createBannerResult; import static org.apache.geode.management.internal.cli.multistep.CLIMultiStepHelper.createPageResult; -import java.io.DataInput; -import java.io.DataOutput; -import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - +import org.apache.commons.lang.StringUtils; import org.apache.geode.DataSerializer; import org.apache.geode.internal.ClassPathLoader; import org.apache.geode.management.cli.Result; @@ -38,20 +31,28 @@ import org.apache.geode.management.internal.cli.result.ResultBuilder; import org.apache.geode.management.internal.cli.result.TabularResultData; import org.apache.geode.management.internal.cli.util.JsonUtil; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.json.JSONObject; +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + /** - * Domain object used for Data Commands Functions - * - * TODO : Implement DataSerializable - * + * Domain object used for Data Commands Functions TODO : Implement DataSerializable */ public class DataCommandResult implements /* Data */ Serializable { - /** - * - */ + private static Logger logger = LogManager.getLogger(); + private static final long serialVersionUID = 1L; private String command; private Object putResult; @@ -66,7 +67,9 @@ public class DataCommandResult implements /* Data */ Serializable { public static final String RESULT_FLAG = "Result"; public static final String NUM_ROWS = "Rows"; - // Aggreagated Data. + public static final String MISSING_VALUE = ""; + + // Aggregated Data. private List locateEntryLocations; private KeyInfo locateEntryResult; private boolean hasResultForAggregation; @@ -91,21 +94,24 @@ public String toString() { if (isGet()) { sb.append(" Type : Get").append(NEW_LINE); sb.append(" Key : ").append(inputKey).append(NEW_LINE); - if (getResult != null) + if (getResult != null) { sb.append(" ReturnValue Class : ").append(getResult.getClass()).append(NEW_LINE); + } sb.append(" ReturnValue : ").append(getResult).append(NEW_LINE); } else if (isPut()) { sb.append(" Type : Put"); sb.append(" Key : ").append(inputKey).append(NEW_LINE); - if (putResult != null) + if (putResult != null) { sb.append(" ReturnValue Class : ").append(putResult.getClass()).append(NEW_LINE); + } sb.append(" ReturnValue : ").append(putResult).append(NEW_LINE); sb.append(" Value : ").append(inputValue).append(NEW_LINE); } else if (isRemove()) { sb.append(" Type : Remove"); sb.append(" Key : ").append(inputKey).append(NEW_LINE); - if (removeResult != null) + if (removeResult != null) { sb.append(" ReturnValue Class : ").append(removeResult.getClass()).append(NEW_LINE); + } sb.append(" ReturnValue : ").append(removeResult).append(NEW_LINE); } else if (isLocateEntry()) { sb.append(" Type : Locate Entry"); @@ -114,45 +120,31 @@ public String toString() { sb.append(" Results : ").append(locateEntryResult).append(NEW_LINE); sb.append(" Locations : ").append(locateEntryLocations).append(NEW_LINE); } - if (errorString != null) + if (errorString != null) { sb.append(" ERROR ").append(errorString); + } return sb.toString(); } public boolean isGet() { - if (CliStrings.GET.equals(command)) - return true; - else - return false; + return CliStrings.GET.equals(command); } public boolean isPut() { - if (CliStrings.PUT.equals(command)) - return true; - else - return false; + return CliStrings.PUT.equals(command); } public boolean isRemove() { - if (CliStrings.REMOVE.equals(command)) - return true; - else - return false; + return CliStrings.REMOVE.equals(command); } public boolean isLocateEntry() { - if (CliStrings.LOCATE_ENTRY.equals(command)) - return true; - else - return false; + return CliStrings.LOCATE_ENTRY.equals(command); } public boolean isSelect() { - if (CliStrings.QUERY.equals(command)) - return true; - else - return false; + return CliStrings.QUERY.equals(command); } public List getSelectResult() { @@ -393,11 +385,13 @@ public void setLocateEntryLocations(List locateEntryLocations) { public Result toCommandResult() { - if (keyClass == null || keyClass.isEmpty()) + if (StringUtils.isEmpty(keyClass)) { keyClass = "java.lang.String"; + } - if (valueClass == null || valueClass.isEmpty()) + if (StringUtils.isEmpty(valueClass)) { valueClass = "java.lang.String"; + } if (errorString != null) { // return ResultBuilder.createGemFireErrorResult(errorString); @@ -406,124 +400,140 @@ public Result toCommandResult() { section.addData("Message", errorString); section.addData(RESULT_FLAG, operationCompletedSuccessfully); return ResultBuilder.buildResult(data); + } + + CompositeResultData data = ResultBuilder.createCompositeResultData(); + SectionResultData section = data.addSection(); + TabularResultData table = section.addTable(); + + section.addData(RESULT_FLAG, operationCompletedSuccessfully); + if (infoString != null) { + section.addData("Message", infoString); + } + + if (isGet()) { + toCommandResult_isGet(section, table); + } else if (isLocateEntry()) { + toCommandResult_isLocate(section, table); + } else if (isPut()) { + toCommandResult_isPut(section, table); + } else if (isRemove()) { + toCommandResult_isRemove(section, table); + } else if (isSelect()) { + // its moved to its separate method + } + return ResultBuilder.buildResult(data); + } + + private void toCommandResult_isGet(SectionResultData section, TabularResultData table) { + section.addData("Key Class", getKeyClass()); + if (!isDeclaredPrimitive(keyClass)) { + addJSONStringToTable(table, inputKey); } else { - CompositeResultData data = ResultBuilder.createCompositeResultData(); - SectionResultData section = data.addSection(); - TabularResultData table = section.addTable(); + section.addData("Key", inputKey); + } - section.addData(RESULT_FLAG, operationCompletedSuccessfully); - if (infoString != null) - section.addData("Message", infoString); + section.addData("Value Class", getValueClass()); + if (!isDeclaredPrimitive(valueClass)) { + addJSONStringToTable(table, getResult); + } else { + section.addData("Value", getResult); + } + } - if (isGet()) { - - section.addData("Key Class", getKeyClass()); - if (!isDeclaredPrimitive(keyClass)) - addJSONStringToTable(table, inputKey); - else - section.addData("Key", inputKey); - - section.addData("Value Class", getValueClass()); - if (!isDeclaredPrimitive(valueClass)) - addJSONStringToTable(table, getResult); - else - section.addData("Value", getResult); - - - } else if (isLocateEntry()) { - - section.addData("Key Class", getKeyClass()); - if (!isDeclaredPrimitive(keyClass)) - addJSONStringToTable(table, inputKey); - else - section.addData("Key", inputKey); - - if (locateEntryLocations != null) { - TabularResultData locationTable = section.addTable(); - - int totalLocations = 0; - - for (KeyInfo info : locateEntryLocations) { - List locations = info.getLocations(); - - if (locations != null) { - if (locations.size() == 1) { - Object array[] = locations.get(0); - // String regionPath = (String)array[0]; - boolean found = (Boolean) array[1]; - if (found) { - totalLocations++; - boolean primary = (Boolean) array[3]; - String bucketId = (String) array[4]; - locationTable.accumulate("MemberName", info.getMemberName()); - locationTable.accumulate("MemberId", info.getMemberId()); - if (bucketId != null) {// PR - if (primary) - locationTable.accumulate("Primary", "*Primary PR*"); - else - locationTable.accumulate("Primary", "No"); - locationTable.accumulate("BucketId", bucketId); - } + private void toCommandResult_isLocate(SectionResultData section, TabularResultData table) { + + section.addData("Key Class", getKeyClass()); + if (!isDeclaredPrimitive(keyClass)) { + addJSONStringToTable(table, inputKey); + } else { + section.addData("Key", inputKey); + } + + if (locateEntryLocations != null) { + TabularResultData locationTable = section.addTable(); + + int totalLocations = 0; + + for (KeyInfo info : locateEntryLocations) { + List locations = info.getLocations(); + + if (locations != null) { + if (locations.size() == 1) { + Object array[] = locations.get(0); + // String regionPath = (String)array[0]; + boolean found = (Boolean) array[1]; + if (found) { + totalLocations++; + boolean primary = (Boolean) array[3]; + String bucketId = (String) array[4]; + locationTable.accumulate("MemberName", info.getMemberName()); + locationTable.accumulate("MemberId", info.getMemberId()); + if (bucketId != null) {// PR + if (primary) { + locationTable.accumulate("Primary", "*Primary PR*"); + } else { + locationTable.accumulate("Primary", "No"); } - } else { - for (Object[] array : locations) { - String regionPath = (String) array[0]; - boolean found = (Boolean) array[1]; - if (found) { - totalLocations++; - boolean primary = (Boolean) array[3]; - String bucketId = (String) array[4]; - locationTable.accumulate("MemberName", info.getMemberName()); - locationTable.accumulate("MemberId", info.getMemberId()); - locationTable.accumulate("RegionPath", regionPath); - if (bucketId != null) {// PR - if (primary) - locationTable.accumulate("Primary", "*Primary PR*"); - else - locationTable.accumulate("Primary", "No"); - locationTable.accumulate("BucketId", bucketId); - } + locationTable.accumulate("BucketId", bucketId); + } + } + } else { + for (Object[] array : locations) { + String regionPath = (String) array[0]; + boolean found = (Boolean) array[1]; + if (found) { + totalLocations++; + boolean primary = (Boolean) array[3]; + String bucketId = (String) array[4]; + locationTable.accumulate("MemberName", info.getMemberName()); + locationTable.accumulate("MemberId", info.getMemberId()); + locationTable.accumulate("RegionPath", regionPath); + if (bucketId != null) {// PR + if (primary) { + locationTable.accumulate("Primary", "*Primary PR*"); + } else { + locationTable.accumulate("Primary", "No"); } + locationTable.accumulate("BucketId", bucketId); } } } } - section.addData("Locations Found", totalLocations); - } else { - section.addData("Location Info ", "Could not find location information"); } + } + section.addData("Locations Found", totalLocations); + } else { + section.addData("Location Info ", "Could not find location information"); + } + } - } else if (isPut()) { - section.addData("Key Class", getKeyClass()); - - if (!isDeclaredPrimitive(keyClass)) { - addJSONStringToTable(table, inputKey); - } else - section.addData("Key", inputKey); - - section.addData("Value Class", getValueClass()); - if (!isDeclaredPrimitive(valueClass)) { - addJSONStringToTable(table, putResult); - } else - section.addData("Old Value", putResult); - - } else if (isRemove()) { - if (inputKey != null) {// avoids printing key when remove ALL is called - section.addData("Key Class", getKeyClass()); - if (!isDeclaredPrimitive(keyClass)) - addJSONStringToTable(table, inputKey); - else - section.addData("Key", inputKey); - } - /* - * if(valueClass!=null && !valueClass.isEmpty()){ section.addData("Value Class", - * getValueClass()); addJSONStringToTable(table,removeResult); }else - * section.addData("Value", removeResult); - */ - } else if (isSelect()) { - // its moved to its separate method + private void toCommandResult_isPut(SectionResultData section, TabularResultData table) { + section.addData("Key Class", getKeyClass()); + + if (!isDeclaredPrimitive(keyClass)) { + addJSONStringToTable(table, inputKey); + } else { + section.addData("Key", inputKey); + } + + section.addData("Value Class", getValueClass()); + if (!isDeclaredPrimitive(valueClass)) { + addJSONStringToTable(table, putResult); + } else { + section.addData("Old Value", putResult); + } + + } + + private void toCommandResult_isRemove(SectionResultData section, TabularResultData table) { + if (inputKey != null) {// avoids printing key when remove ALL is called + section.addData("Key Class", getKeyClass()); + if (!isDeclaredPrimitive(keyClass)) { + addJSONStringToTable(table, inputKey); + } else { + section.addData("Key", inputKey); } - return ResultBuilder.buildResult(data); } } @@ -555,8 +565,9 @@ public CompositeResultData toSelectCommandResult() { } if (this.selectResult != null) { section.addData(NUM_ROWS, this.selectResult.size()); - if (this.queryTraceString != null) + if (this.queryTraceString != null) { section.addData("Query Trace", this.queryTraceString); + } buildTable(table, 0, selectResult.size()); } } @@ -570,7 +581,7 @@ public CompositeResultData toSelectCommandResult() { */ @SuppressWarnings({"rawtypes", "unchecked"}) public Result pageResult(int startCount, int endCount, String step) { - List fields = new ArrayList(); + List fields = new ArrayList<>(); List values = new ArrayList(); fields.add(RESULT_FLAG); values.add(operationCompletedSuccessfully); @@ -592,8 +603,8 @@ public Result pageResult(int startCount, int endCount, String step) { if (selectResult != null) { try { TabularResultData table = ResultBuilder.createTabularResultData(); - String[] headers = null; - Object[][] rows = null; + String[] headers; + Object[][] rows; int rowCount = buildTable(table, startCount, endCount); GfJsonArray array = table.getHeaders(); headers = new String[array.size()]; @@ -619,36 +630,70 @@ public Result pageResult(int startCount, int endCount, String step) { Object valuesArray[] = {startCount, endCount}; return createPageResult(fieldsArray, valuesArray, step, headers, rows); } - } else + } else { return createBannerResult(fields, values, step); + } } } private int buildTable(TabularResultData table, int startCount, int endCount) { - int rowCount = 0; - // Introspect first using tabular data - for (int i = startCount; i <= endCount; i++) { - if (i >= selectResult.size()) - break; - else - rowCount++; - - SelectResultRow row = selectResult.get(i); - switch (row.type) { - case ROW_TYPE_BEAN: - addJSONStringToTable(table, row.value); - break; - case ROW_TYPE_STRUCT_RESULT: - addJSONStringToTable(table, row.value); - break; - case ROW_TYPE_PRIMITIVE: - table.accumulate(RESULT_FLAG, row.value); - break; + // Three steps: + // 1a. Convert each row object to a Json object. + // 1b. Build a list of keys that are used for each object + // 2. Pad MISSING_VALUE into Json objects for those data that are missing any particular key + // 3. Build the table from these Json objects. + + // 1. + int lastRowExclusive = Math.min(selectResult.size(), endCount + 1); + List paginatedRows = selectResult.subList(startCount, lastRowExclusive); + + List tableRows = new ArrayList<>(); + List rowsWithRealJsonObjects = new ArrayList<>(); + Set columns = new HashSet<>(); + + for (SelectResultRow row : paginatedRows) { + GfJsonObject object = new GfJsonObject(); + try { + if (row.value == null || MISSING_VALUE.equals(row.value)) { + object.put("Value", MISSING_VALUE); + } else if (row.type == ROW_TYPE_PRIMITIVE) { + object.put(RESULT_FLAG, row.value); + } else { + object = buildGfJsonFromRawObject(row.value); + rowsWithRealJsonObjects.add(object); + object.keys().forEachRemaining(columns::add); + } + tableRows.add(object); + } catch (GfJsonException e) { + JSONObject errJson = + new JSONObject().put("Value", "Error getting bean properties " + e.getMessage()); + tableRows.add(new GfJsonObject(errJson, false)); } } - return rowCount; + + // 2. + for (GfJsonObject tableRow : rowsWithRealJsonObjects) { + for (String key : columns) { + if (!tableRow.has(key)) { + try { + tableRow.put(key, MISSING_VALUE); + } catch (GfJsonException e) { + // TODO: Address this unlikely possibility. + logger.warn("Ignored GfJsonException:", e); + } + } + } + } + + // 3. + for (GfJsonObject jsonObject : tableRows) { + addJSONObjectToTable(table, jsonObject); + } + + return paginatedRows.size(); } + private boolean isDeclaredPrimitive(String keyClass2) { try { Class klass = ClassPathLoader.getLatest().forName(keyClass2); @@ -658,45 +703,6 @@ private boolean isDeclaredPrimitive(String keyClass2) { } } - private void addJSONStringToTable(TabularResultData table, Object object) { - if (object == null || "".equals(object)) { - table.accumulate("Value", ""); - } else { - try { - Class klass = object.getClass(); - GfJsonObject jsonObject = null; - if (String.class.equals(klass)) { - // InputString in JSON Form but with round brackets - String json = (String) object; - String newString = json.replaceAll("'", "\""); - if (newString.charAt(0) == '(') { - int len = newString.length(); - StringBuilder sb = new StringBuilder(); - sb.append("{").append(newString.substring(1, len - 1)).append("}"); - newString = sb.toString(); - } - jsonObject = new GfJsonObject(newString); - } else { - jsonObject = new GfJsonObject(object, true); - } - - Iterator keys = jsonObject.keys(); - while (keys.hasNext()) { - String k = keys.next(); - // filter out meta-field type-class used to identify java class of json obbject - if (!"type-class".equals(k)) { - Object value = jsonObject.get(k); - if (value != null) { - table.accumulate(k, getDomainValue(value)); - } - } - } - } catch (Exception e) { - table.accumulate("Value", "Error getting bean properties " + e.getMessage()); - } - } - } - private Object getDomainValue(Object value) { if (value instanceof String) { @@ -708,8 +714,9 @@ private Object getDomainValue(Object value) { } catch (Exception e) { return str; } - } else + } else { return str; + } } return value; } @@ -722,7 +729,6 @@ public void setInputQuery(Object inputQuery) { this.inputQuery = inputQuery; } - public static class KeyInfo implements /* Data */ Serializable { private String memberId; @@ -734,8 +740,9 @@ public static class KeyInfo implements /* Data */ Serializable { private ArrayList locations = null; public void addLocation(Object[] locationArray) { - if (this.locations == null) - locations = new ArrayList(); + if (this.locations == null) { + locations = new ArrayList<>(); + } locations.add(locationArray); } @@ -790,13 +797,14 @@ public String toString() { } public boolean hasLocation() { - if (locations == null) + if (locations == null) { return false; - else { + } else { for (Object[] array : locations) { boolean found = (Boolean) array[1]; - if (found) + if (found) { return true; + } } } return false; @@ -823,7 +831,6 @@ public void fromData(DataInput in) throws IOException, ClassNotFoundException { } } - public static final int ROW_TYPE_STRUCT_RESULT = 100; public static final int ROW_TYPE_BEAN = 200; public static final int ROW_TYPE_PRIMITIVE = 300; @@ -856,45 +863,98 @@ public void setValue(Object value) { } + public void aggregate(DataCommandResult result) { - if (isLocateEntry()) { - /* Right now only called for LocateEntry */ + /* Right now only called for LocateEntry */ + if (!isLocateEntry()) { + return; + } - if (this.locateEntryLocations == null) { - locateEntryLocations = new ArrayList(); - } + if (this.locateEntryLocations == null) { + locateEntryLocations = new ArrayList<>(); + } - if (result == null) {// self-transform result from single to aggregate when numMember==1 - if (this.locateEntryResult != null) { - locateEntryLocations.add(locateEntryResult); - // TODO : Decide whether to show value or not this.getResult = - // locateEntryResult.getValue(); - } - return; + if (result == null) {// self-transform result from single to aggregate when numMember==1 + if (this.locateEntryResult != null) { + locateEntryLocations.add(locateEntryResult); + // TODO : Decide whether to show value or not this.getResult = locateEntryResult.getValue(); } + return; + } + + if (result.errorString != null && !result.errorString.equals(errorString)) { + // append errorString only if differs + errorString = result.errorString + " " + errorString; + } + + // append message only when it differs for negative results + if (!operationCompletedSuccessfully && result.infoString != null + && !result.infoString.equals(infoString)) { + infoString = result.infoString; + } - if (result.errorString != null && !result.errorString.equals(errorString)) { - // append errorString only if differs - String newString = result.errorString + " " + errorString; - errorString = newString; + if (result.hasResultForAggregation) { + this.operationCompletedSuccessfully = true; + infoString = result.infoString; + if (result.locateEntryResult != null) { + locateEntryLocations.add(result.locateEntryResult); } + } + } - // append messsage only when it differs for negative results - if (!operationCompletedSuccessfully && result.infoString != null - && !result.infoString.equals(infoString)) { - infoString = result.infoString; + + private void addJSONObjectToTable(TabularResultData table, GfJsonObject object) { + Iterator keys; + + keys = object.keys(); + while (keys.hasNext()) { + String k = keys.next(); + // filter out meta-field type-class used to identify java class of json object + if (!"type-class".equals(k)) { + Object value = object.get(k); + + if (value != null) { + table.accumulate(k, getDomainValue(value)); + } } + } + } + + private GfJsonObject buildGfJsonFromRawObject(Object object) throws GfJsonException { + GfJsonObject jsonObject; + if (String.class.equals(object.getClass())) { + jsonObject = new GfJsonObject(sanitizeJsonString((String) object)); + } else { + jsonObject = new GfJsonObject(object, true); + } + + return jsonObject; + } + + private String sanitizeJsonString(String s) { + // InputString in JSON Form but with round brackets + String newString = s.replaceAll("'", "\""); + if (newString.charAt(0) == '(') { + int len = newString.length(); + newString = "{" + newString.substring(1, len - 1) + "}"; + } + return newString; + } - if (result.hasResultForAggregation /* && result.errorString==null */) { - this.operationCompletedSuccessfully = true;// override this - // result.operationCompletedSuccessfully - infoString = result.infoString; - if (result.locateEntryResult != null) - locateEntryLocations.add(result.locateEntryResult); + private void addJSONStringToTable(TabularResultData table, Object object) { + if (object == null || MISSING_VALUE.equals(object)) { + table.accumulate("Value", MISSING_VALUE); + } else { + try { + GfJsonObject jsonObject = buildGfJsonFromRawObject(object); + addJSONObjectToTable(table, jsonObject); + } catch (Exception e) { + table.accumulate("Value", "Error getting bean properties " + e.getMessage()); } } } + // @Override public void toData(DataOutput out) throws IOException { DataSerializer.writeString(command, out); @@ -935,5 +995,3 @@ public void fromData(DataInput in) throws IOException, ClassNotFoundException { } } - - diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java index 8620cffbb00d..e2164a3750a8 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/functions/DataCommandFunction.java @@ -14,23 +14,7 @@ */ package org.apache.geode.management.internal.cli.functions; -import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.atomic.AtomicInteger; - import org.apache.commons.lang.StringUtils; -import org.apache.logging.log4j.Logger; -import org.apache.shiro.subject.Subject; -import org.json.JSONArray; - -import org.apache.geode.cache.CacheClosedException; import org.apache.geode.cache.CacheFactory; import org.apache.geode.cache.DataPolicy; import org.apache.geode.cache.Region; @@ -80,6 +64,19 @@ import org.apache.geode.management.internal.cli.shell.Gfsh; import org.apache.geode.management.internal.cli.util.JsonUtil; import org.apache.geode.pdx.PdxInstance; +import org.apache.logging.log4j.Logger; +import org.apache.shiro.subject.Subject; +import org.json.JSONArray; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; /** * @since GemFire 7.0 @@ -138,29 +135,28 @@ public void execute(FunctionContext functionContext) { System.getProperty("memberName")); } DataCommandResult result = null; - if (request.isGet()) + if (request.isGet()) { result = get(request); - else if (request.isLocateEntry()) + } else if (request.isLocateEntry()) { result = locateEntry(request); - else if (request.isPut()) + } else if (request.isPut()) { result = put(request); - else if (request.isRemove()) + } else if (request.isRemove()) { result = remove(request); - else if (request.isSelect()) + } else if (request.isSelect()) { result = select(request); + } if (logger.isDebugEnabled()) { logger.debug("Result is {}", result); } functionContext.getResultSender().lastResult(result); - } catch (CacheClosedException e) { - e.printStackTrace(); - functionContext.getResultSender().sendException(e); } catch (Exception e) { - e.printStackTrace(); + logger.info("Exception occurred:", e); functionContext.getResultSender().sendException(e); } } + private InternalCache getCache() { return (InternalCache) CacheFactory.getAnyInstance(); } @@ -223,132 +219,131 @@ public void reset2() { @SuppressWarnings("rawtypes") private DataCommandResult select(Object principal, String queryString) { + InternalCache cache = getCache(); AtomicInteger nestedObjectCount = new AtomicInteger(0); - if (queryString != null && !queryString.isEmpty()) { - QueryService qs = cache.getQueryService(); - - // TODO : Find out if is this optimised use. Can you have something equivalent of parsed - // queries with names - // where name can be retrieved to avoid parsing every-time - Query query = qs.newQuery(queryString); - DefaultQuery tracedQuery = (DefaultQuery) query; - WrappedIndexTrackingQueryObserver queryObserver = null; - String queryVerboseMsg = null; - long startTime = -1; + if (StringUtils.isEmpty(queryString)) { + return DataCommandResult.createSelectInfoResult(null, null, -1, null, + CliStrings.QUERY__MSG__QUERY_EMPTY, false); + } + + QueryService qs = cache.getQueryService(); + + // TODO : Find out if is this optimised use. Can you have something equivalent of parsed + // queries with names where name can be retrieved to avoid parsing every-time + Query query = qs.newQuery(queryString); + DefaultQuery tracedQuery = (DefaultQuery) query; + WrappedIndexTrackingQueryObserver queryObserver = null; + String queryVerboseMsg = null; + long startTime = -1; + if (tracedQuery.isTraced()) { + startTime = NanoTimer.getTime(); + queryObserver = new WrappedIndexTrackingQueryObserver(); + QueryObserverHolder.setInstance(queryObserver); + } + List list = new ArrayList<>(); + + try { + Object results = query.execute(); if (tracedQuery.isTraced()) { - startTime = NanoTimer.getTime(); - queryObserver = new WrappedIndexTrackingQueryObserver(); - QueryObserverHolder.setInstance(queryObserver); + queryVerboseMsg = getLogMessage(queryObserver, startTime, queryString); + queryObserver.reset2(); } - List list = new ArrayList(); + if (results instanceof SelectResults) { + select_SelectResults((SelectResults) results, principal, list, nestedObjectCount); + } else { + select_NonSelectResults(results, list); + } + return DataCommandResult.createSelectResult(queryString, list, queryVerboseMsg, null, null, + true); + + } catch (FunctionDomainException | GfJsonException | QueryInvocationTargetException + | NameResolutionException | TypeMismatchException e) { + logger.warn(e.getMessage(), e); + return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, + e.getMessage(), false); + } finally { + if (queryObserver != null) { + QueryObserverHolder.reset(); + } + } + } + private void select_NonSelectResults(Object results, List list) { + if (logger.isDebugEnabled()) { + logger.debug("BeanResults : Bean Results class is {}", results.getClass()); + } + String str = toJson(results); + GfJsonObject jsonBean; + try { + jsonBean = new GfJsonObject(str); + } catch (GfJsonException e) { + logger.info("Exception occurred:", e); + jsonBean = new GfJsonObject(); try { - Object results = query.execute(); - if (tracedQuery.isTraced()) { - queryVerboseMsg = getLogMessage(queryObserver, startTime, queryString); - queryObserver.reset2(); + jsonBean.put("msg", e.getMessage()); + } catch (GfJsonException e1) { + logger.warn("Ignored GfJsonException:", e1); + } + } + if (logger.isDebugEnabled()) { + logger.debug("BeanResults : Adding bean json string : {}", jsonBean); + } + list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString())); + } + + private void select_SelectResults(SelectResults selectResults, Object principal, + List list, AtomicInteger nestedObjectCount) throws GfJsonException { + for (Object object : selectResults) { + // Post processing + object = securityService.postProcess(principal, null, null, object, false); + + if (object instanceof Struct) { + StructImpl impl = (StructImpl) object; + GfJsonObject jsonStruct = getJSONForStruct(impl, nestedObjectCount); + if (logger.isDebugEnabled()) { + logger.debug("SelectResults : Adding select json string : {}", jsonStruct); } - if (results instanceof SelectResults) { - SelectResults selectResults = (SelectResults) results; - for (Iterator iter = selectResults.iterator(); iter.hasNext();) { - Object object = iter.next(); - // Post processing - object = this.securityService.postProcess(principal, null, null, object, false); - - if (object instanceof Struct) { - StructImpl impl = (StructImpl) object; - GfJsonObject jsonStruct = getJSONForStruct(impl, nestedObjectCount); - if (logger.isDebugEnabled()) - logger.debug("SelectResults : Adding select json string : {}", jsonStruct); - list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_STRUCT_RESULT, - jsonStruct.toString())); - } else { - if (JsonUtil.isPrimitiveOrWrapper(object.getClass())) { - if (logger.isDebugEnabled()) - logger.debug("SelectResults : Adding select primitive : {}", object); - list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_PRIMITIVE, object)); - } else { - if (logger.isDebugEnabled()) - logger.debug("SelectResults : Bean Results class is {}", object.getClass()); - String str = toJson(object); - GfJsonObject jsonBean; - try { - jsonBean = new GfJsonObject(str); - } catch (GfJsonException e) { - logger.fatal(e.getMessage(), e); - jsonBean = new GfJsonObject(); - try { - jsonBean.put("msg", e.getMessage()); - } catch (GfJsonException e1) { - } - } - if (logger.isDebugEnabled()) - logger.debug("SelectResults : Adding bean json string : {}", jsonBean); - list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString())); - } - } - } - } else { - if (logger.isDebugEnabled()) - logger.debug("BeanResults : Bean Results class is {}", results.getClass()); - String str = toJson(results); - GfJsonObject jsonBean; + list.add( + new SelectResultRow(DataCommandResult.ROW_TYPE_STRUCT_RESULT, jsonStruct.toString())); + } else if (JsonUtil.isPrimitiveOrWrapper(object.getClass())) { + if (logger.isDebugEnabled()) { + logger.debug("SelectResults : Adding select primitive : {}", object); + } + list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_PRIMITIVE, object)); + } else { + if (logger.isDebugEnabled()) { + logger.debug("SelectResults : Bean Results class is {}", object.getClass()); + } + String str = toJson(object); + GfJsonObject jsonBean; + try { + jsonBean = new GfJsonObject(str); + } catch (GfJsonException e) { + logger.error(e.getMessage(), e); + jsonBean = new GfJsonObject(); try { - jsonBean = new GfJsonObject(str); - } catch (GfJsonException e) { - e.printStackTrace(); - jsonBean = new GfJsonObject(); - try { - jsonBean.put("msg", e.getMessage()); - } catch (GfJsonException e1) { - } + jsonBean.put("msg", e.getMessage()); + } catch (GfJsonException e1) { + logger.warn("Ignored GfJsonException:", e1); } - if (logger.isDebugEnabled()) - logger.debug("BeanResults : Adding bean json string : {}", jsonBean); - list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString())); } - return DataCommandResult.createSelectResult(queryString, list, queryVerboseMsg, null, null, - true); - - } catch (FunctionDomainException e) { - logger.warn(e.getMessage(), e); - return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, - e.getMessage(), false); - } catch (TypeMismatchException e) { - logger.warn(e.getMessage(), e); - return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, - e.getMessage(), false); - } catch (NameResolutionException e) { - logger.warn(e.getMessage(), e); - return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, - e.getMessage(), false); - } catch (QueryInvocationTargetException e) { - logger.warn(e.getMessage(), e); - return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, - e.getMessage(), false); - } catch (GfJsonException e) { - logger.warn(e.getMessage(), e); - return DataCommandResult.createSelectResult(queryString, null, queryVerboseMsg, e, - e.getMessage(), false); - } finally { - if (queryObserver != null) { - QueryObserverHolder.reset(); + if (logger.isDebugEnabled()) { + logger.debug("SelectResults : Adding bean json string : {}", jsonBean); } + list.add(new SelectResultRow(DataCommandResult.ROW_TYPE_BEAN, jsonBean.toString())); } - } else { - return DataCommandResult.createSelectInfoResult(null, null, -1, null, - CliStrings.QUERY__MSG__QUERY_EMPTY, false); } } private String toJson(Object object) { if (object instanceof Undefined) { return "{\"Value\":\"UNDEFINED\"}"; - } else if (object instanceof PdxInstance) + } else if (object instanceof PdxInstance) { return pdxToJson((PdxInstance) object); - else + } else { return JsonUtil.objectToJsonNestedChkCDep(object, NESTED_JSON_LENGTH); + } } private GfJsonObject getJSONForStruct(StructImpl impl, AtomicInteger ai) throws GfJsonException { @@ -376,14 +371,13 @@ public DataCommandResult remove(String key, String keyClass, String regionName, InternalCache cache = getCache(); - if (regionName == null || regionName.isEmpty()) { + if (StringUtils.isEmpty(regionName)) { return DataCommandResult.createRemoveResult(key, null, null, CliStrings.REMOVE__MSG__REGIONNAME_EMPTY, false); } - boolean allKeysFlag = (removeAllKeys == null || removeAllKeys.isEmpty()); - if (allKeysFlag && (key == null)) { - return DataCommandResult.createRemoveResult(key, null, null, + if (StringUtils.isEmpty(removeAllKeys) && (key == null)) { + return DataCommandResult.createRemoveResult(null, null, null, CliStrings.REMOVE__MSG__KEY_EMPTY, false); } @@ -393,7 +387,7 @@ public DataCommandResult remove(String key, String keyClass, String regionName, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionName), false); } else { if (removeAllKeys == null) { - Object keyObject = null; + Object keyObject; try { keyObject = getClassObject(key, keyClass); } catch (ClassNotFoundException e) { @@ -406,14 +400,16 @@ public DataCommandResult remove(String key, String keyClass, String regionName, if (region.containsKey(keyObject)) { Object value = region.remove(keyObject); - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Removed key {} successfully", key); + } // return DataCommandResult.createRemoveResult(key, value, null, null); Object array[] = getJSONForNonPrimitiveObject(value); DataCommandResult result = DataCommandResult.createRemoveResult(key, array[1], null, null, true); - if (array[0] != null) + if (array[0] != null) { result.setValueClass((String) array[0]); + } return result; } else { return DataCommandResult.createRemoveInfoResult(key, null, null, @@ -423,8 +419,9 @@ public DataCommandResult remove(String key, String keyClass, String regionName, DataPolicy policy = region.getAttributes().getDataPolicy(); if (!policy.withPartitioning()) { region.clear(); - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Cleared all keys in the region - {}", regionName); + } return DataCommandResult.createRemoveInfoResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__CLEARED_ALL_CLEARS, regionName), true); } else { @@ -441,12 +438,12 @@ public DataCommandResult get(Object principal, String key, String keyClass, Stri InternalCache cache = getCache(); - if (regionName == null || regionName.isEmpty()) { + if (StringUtils.isEmpty(regionName)) { return DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__REGIONNAME_EMPTY, false); } - if (key == null || key.isEmpty()) { + if (StringUtils.isEmpty(key)) { return DataCommandResult.createGetResult(key, null, null, CliStrings.GET__MSG__KEY_EMPTY, false); } @@ -454,12 +451,13 @@ public DataCommandResult get(Object principal, String key, String keyClass, Stri Region region = cache.getRegion(regionName); if (region == null) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Region Not Found - {}", regionName); + } return DataCommandResult.createGetResult(key, null, null, CliStrings.format(CliStrings.GET__MSG__REGION_NOT_FOUND, regionName), false); } else { - Object keyObject = null; + Object keyObject; try { keyObject = getClassObject(key, keyClass); } catch (ClassNotFoundException e) { @@ -480,24 +478,27 @@ public DataCommandResult get(Object principal, String key, String keyClass, Stri // run it through post processor. region.get will return the deserialized object already, so // we don't need to // deserialize it anymore to pass it to the postProcessor - value = this.securityService.postProcess(principal, regionName, keyObject, value, false); + value = securityService.postProcess(principal, regionName, keyObject, value, false); - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Get for key {} value {}", key, value); + } // return DataCommandResult.createGetResult(key, value, null, null); Object array[] = getJSONForNonPrimitiveObject(value); if (value != null) { DataCommandResult result = DataCommandResult.createGetResult(key, array[1], null, null, true); - if (array[0] != null) + if (array[0] != null) { result.setValueClass((String) array[0]); + } return result; } else { return DataCommandResult.createGetResult(key, array[1], null, null, false); } } else { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Key is not present in the region {}", regionName); + } return DataCommandResult.createGetInfoResult(key, null, null, CliStrings.GET__MSG__KEY_NOT_FOUND_REGION, false); } @@ -510,71 +511,72 @@ public DataCommandResult locateEntry(String key, String keyClass, String valueCl InternalCache cache = getCache(); - if (regionPath == null || regionPath.isEmpty()) { + if (StringUtils.isEmpty(regionPath)) { return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__REGIONNAME_EMPTY, false); } - if (key == null || key.isEmpty()) { + if (StringUtils.isEmpty(key)) { return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_EMPTY, false); } - - List listofRegionStartingwithRegionPath = new ArrayList(); + List listOfRegionsStartingWithRegionPath = new ArrayList<>(); if (recursive) { // Recursively find the keys starting from the specified region path. List regionPaths = getAllRegionPaths(cache, true); - for (int i = 0; i < regionPaths.size(); i++) { - String path = regionPaths.get(i); + for (String path : regionPaths) { if (path.startsWith(regionPath) || path.startsWith(Region.SEPARATOR + regionPath)) { Region targetRegion = cache.getRegion(path); - listofRegionStartingwithRegionPath.add(targetRegion); + listOfRegionsStartingWithRegionPath.add(targetRegion); } } - if (listofRegionStartingwithRegionPath.size() == 0) { - if (logger.isDebugEnabled()) + if (listOfRegionsStartingWithRegionPath.size() == 0) { + if (logger.isDebugEnabled()) { logger.debug("Region Not Found - {}", regionPath); + } return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionPath), false); } } else { Region region = cache.getRegion(regionPath); if (region == null) { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Region Not Found - {}", regionPath); + } return DataCommandResult.createLocateEntryResult(key, null, null, CliStrings.format(CliStrings.REMOVE__MSG__REGION_NOT_FOUND, regionPath), false); - } else - listofRegionStartingwithRegionPath.add(region); + } else { + listOfRegionsStartingWithRegionPath.add(region); + } } - Object keyObject = null; + Object keyObject; try { keyObject = getClassObject(key, keyClass); } catch (ClassNotFoundException e) { - logger.fatal(e.getMessage(), e); + logger.error(e.getMessage(), e); return DataCommandResult.createLocateEntryResult(key, null, null, "ClassNotFoundException " + keyClass, false); } catch (IllegalArgumentException e) { - logger.fatal(e.getMessage(), e); + logger.error(e.getMessage(), e); return DataCommandResult.createLocateEntryResult(key, null, null, "Error in converting JSON " + e.getMessage(), false); } - Object value = null; - DataCommandResult.KeyInfo keyInfo = null; + Object value; + DataCommandResult.KeyInfo keyInfo; keyInfo = new DataCommandResult.KeyInfo(); DistributedMember member = cache.getDistributedSystem().getDistributedMember(); keyInfo.setHost(member.getHost()); keyInfo.setMemberId(member.getId()); keyInfo.setMemberName(member.getName()); - for (Region region : listofRegionStartingwithRegionPath) { + for (Region region : listOfRegionsStartingWithRegionPath) { if (region instanceof PartitionedRegion) { // Following code is adaptation of which.java of old Gfsh PartitionedRegion pr = (PartitionedRegion) region; - Region localRegion = PartitionRegionHelper.getLocalData((PartitionedRegion) region); + Region localRegion = PartitionRegionHelper.getLocalData(region); value = localRegion.get(keyObject); if (value != null) { DistributedMember primaryMember = @@ -584,24 +586,28 @@ public DataCommandResult locateEntry(String key, String keyClass, String valueCl keyInfo.addLocation(new Object[] {region.getFullPath(), true, getJSONForNonPrimitiveObject(value)[1], isPrimary, "" + bucketId}); } else { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Key is not present in the region {}", regionPath); + } return DataCommandResult.createLocateEntryInfoResult(key, null, null, CliStrings.LOCATE_ENTRY__MSG__KEY_NOT_FOUND_REGION, false); } } else { if (region.containsKey(keyObject)) { value = region.get(keyObject); - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Get for key {} value {} in region {}", key, value, region.getFullPath()); - if (value != null) + } + if (value != null) { keyInfo.addLocation(new Object[] {region.getFullPath(), true, getJSONForNonPrimitiveObject(value)[1], false, null}); - else + } else { keyInfo.addLocation(new Object[] {region.getFullPath(), false, null, false, null}); + } } else { - if (logger.isDebugEnabled()) + if (logger.isDebugEnabled()) { logger.debug("Key is not present in the region {}", regionPath); + } keyInfo.addLocation(new Object[] {region.getFullPath(), false, null, false, null}); } } @@ -619,17 +625,17 @@ public DataCommandResult locateEntry(String key, String keyClass, String valueCl public DataCommandResult put(String key, String value, boolean putIfAbsent, String keyClass, String valueClass, String regionName) { - if (regionName == null || regionName.isEmpty()) { + if (StringUtils.isEmpty(regionName)) { return DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__REGIONNAME_EMPTY, false); } - if (key == null || key.isEmpty()) { + if (StringUtils.isEmpty(key)) { return DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__KEY_EMPTY, false); } - if (value == null || value.isEmpty()) { + if (StringUtils.isEmpty(value)) { return DataCommandResult.createPutResult(key, null, null, CliStrings.PUT__MSG__VALUE_EMPTY, false); } @@ -640,8 +646,8 @@ public DataCommandResult put(String key, String value, boolean putIfAbsent, Stri return DataCommandResult.createPutResult(key, null, null, CliStrings.format(CliStrings.PUT__MSG__REGION_NOT_FOUND, regionName), false); } else { - Object keyObject = null; - Object valueObject = null; + Object keyObject; + Object valueObject; try { keyObject = getClassObject(key, keyClass); } catch (ClassNotFoundException e) { @@ -659,14 +665,16 @@ public DataCommandResult put(String key, String value, boolean putIfAbsent, Stri "ClassNotFoundException " + valueClass, false); } Object returnValue; - if (putIfAbsent && region.containsKey(keyObject)) + if (putIfAbsent && region.containsKey(keyObject)) { returnValue = region.get(keyObject); - else + } else { returnValue = region.put(keyObject, valueObject); + } Object array[] = getJSONForNonPrimitiveObject(returnValue); DataCommandResult result = DataCommandResult.createPutResult(key, array[1], null, null, true); - if (array[0] != null) + if (array[0] != null) { result.setValueClass((String) array[0]); + } return result; } } @@ -674,53 +682,40 @@ public DataCommandResult put(String key, String value, boolean putIfAbsent, Stri @SuppressWarnings({"rawtypes", "unchecked"}) private Object getClassObject(String string, String klassString) throws ClassNotFoundException, IllegalArgumentException { - if (klassString == null || klassString.isEmpty()) + if (StringUtils.isEmpty(klassString)) { return string; - else { - Object o = null; - Class klass = ClassPathLoader.getLatest().forName(klassString); - - if (klass.equals(String.class)) - return string; + } + Class klass = ClassPathLoader.getLatest().forName(klassString); - if (JsonUtil.isPrimitiveOrWrapper(klass)) { - try { - if (klass.equals(Byte.class)) { - o = Byte.parseByte(string); - return o; - } else if (klass.equals(Short.class)) { - o = Short.parseShort(string); - return o; - } else if (klass.equals(Integer.class)) { - o = Integer.parseInt(string); - return o; - } else if (klass.equals(Long.class)) { - o = Long.parseLong(string); - return o; - } else if (klass.equals(Double.class)) { - o = Double.parseDouble(string); - return o; - } else if (klass.equals(Boolean.class)) { - o = Boolean.parseBoolean(string); - return o; - } else if (klass.equals(Float.class)) { - o = Float.parseFloat(string); - return o; - } - return o; - } catch (NumberFormatException e) { - throw new IllegalArgumentException( - "Failed to convert input key to " + klassString + " Msg : " + e.getMessage()); - } - } + if (klass.equals(String.class)) { + return string; + } + if (JsonUtil.isPrimitiveOrWrapper(klass)) { try { - o = getObjectFromJson(string, klass); - return o; - } catch (IllegalArgumentException e) { - throw e; + if (klass.equals(Byte.class)) { + return Byte.parseByte(string); + } else if (klass.equals(Short.class)) { + return Short.parseShort(string); + } else if (klass.equals(Integer.class)) { + return Integer.parseInt(string); + } else if (klass.equals(Long.class)) { + return Long.parseLong(string); + } else if (klass.equals(Double.class)) { + return Double.parseDouble(string); + } else if (klass.equals(Boolean.class)) { + return Boolean.parseBoolean(string); + } else if (klass.equals(Float.class)) { + return Float.parseFloat(string); + } + return null; + } catch (NumberFormatException e) { + throw new IllegalArgumentException( + "Failed to convert input key to " + klassString + " Msg : " + e.getMessage()); } } + + return getObjectFromJson(string, klass); } @SuppressWarnings({"rawtypes"}) @@ -803,10 +798,11 @@ public static V getObjectFromJson(String json, Class klass) { sb.append("{").append(newString.substring(1, len - 1)).append("}"); newString = sb.toString(); } - V v = JsonUtil.jsonToObject(newString, klass); - return v; + return JsonUtil.jsonToObject(newString, klass); } + + /** * Returns a sorted list of all region full paths found in the specified cache. * @@ -822,18 +818,17 @@ public static List getAllRegionPaths(InternalCache cache, boolean recursive) { } // get a list of all root regions - Set regions = cache.rootRegions(); - Iterator itor = regions.iterator(); + Set> regions = cache.rootRegions(); - while (itor.hasNext()) { - String regionPath = ((Region) itor.next()).getFullPath(); + for (Region rootRegion : regions) { + String regionPath = rootRegion.getFullPath(); Region region = cache.getRegion(regionPath); list.add(regionPath); - Set subregionSet = region.subregions(true); + Set subregionSet = region.subregions(true); if (recursive) { - for (Iterator subIter = subregionSet.iterator(); subIter.hasNext();) { - list.add(((Region) subIter.next()).getFullPath()); + for (Region aSubregionSet : subregionSet) { + list.add(aSubregionSet.getFullPath()); } } } @@ -856,7 +851,7 @@ public Result exec() { int startCount = args.getInt(DataCommandResult.QUERY_PAGE_START); int endCount = args.getInt(DataCommandResult.QUERY_PAGE_END); int rows = args.getInt(DataCommandResult.NUM_ROWS); // returns Zero if no rows added so it - // works. + // works. boolean flag = args.getBoolean(DataCommandResult.RESULT_FLAG); CommandResult commandResult = CLIMultiStepHelper.getDisplayResultFromArgs(args); Gfsh.println(); @@ -865,7 +860,7 @@ public Result exec() { } if (flag) { - boolean paginationNeeded = (startCount < rows) && (endCount < rows) && interactive && flag; + boolean paginationNeeded = startCount < rows && endCount < rows && interactive; if (paginationNeeded) { while (true) { String message = ("Press n to move to next page, q to quit and p to previous page : "); @@ -879,17 +874,19 @@ public Result exec() { new Object[] {nextStart, (nextStart + getPageSize())}, SELECT_STEP_MOVE); } else if ("p".equals(step)) { int nextStart = startCount - getPageSize(); - if (nextStart < 0) + if (nextStart < 0) { nextStart = 0; + } return CLIMultiStepHelper.createBannerResult( new String[] {DataCommandResult.QUERY_PAGE_START, DataCommandResult.QUERY_PAGE_END}, new Object[] {nextStart, (nextStart + getPageSize())}, SELECT_STEP_MOVE); - } else if ("q".equals(step)) + } else if ("q".equals(step)) { return CLIMultiStepHelper.createBannerResult(new String[] {}, new Object[] {}, SELECT_STEP_END); - else + } else { Gfsh.println("Unknown option "); + } } catch (IOException e) { throw new RuntimeException(e); } @@ -916,7 +913,7 @@ public Result exec() { int endCount = args.getInt(DataCommandResult.QUERY_PAGE_END); return cachedResult.pageResult(startCount, endCount, SELECT_STEP_DISPLAY); } - }; + } public static class SelectExecStep extends CLIMultiStepHelper.RemoteStep { @@ -938,21 +935,23 @@ public Result exec() { if (interactive) { endCount = getPageSize(); } else { - if (result.getSelectResult() != null) + if (result.getSelectResult() != null) { endCount = result.getSelectResult().size(); + } } - if (interactive) + if (interactive) { return result.pageResult(0, endCount, SELECT_STEP_DISPLAY); - else + } else { return CLIMultiStepHelper.createBannerResult(new String[] {}, new Object[] {}, SELECT_STEP_END); + } } public DataCommandResult _select(String query) { InternalCache cache = (InternalCache) CacheFactory.getAnyInstance(); - DataCommandResult dataResult = null; + DataCommandResult dataResult; - if (query == null || query.isEmpty()) { + if (StringUtils.isEmpty(query)) { dataResult = DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.QUERY__MSG__QUERY_EMPTY, false); return dataResult; @@ -964,15 +963,15 @@ public DataCommandResult _select(String query) { @SuppressWarnings("deprecation") QCompiler compiler = new QCompiler(); - Set regionsInQuery = null; + Set regionsInQuery; try { CompiledValue compiledQuery = compiler.compileQuery(query); - Set regions = new HashSet(); + Set regions = new HashSet<>(); compiledQuery.getRegionsInQuery(regions, null); // authorize data read on these regions for (String region : regions) { - this.securityService.authorizeRegionRead(region); + securityService.authorizeRegionRead(region); } regionsInQuery = Collections.unmodifiableSet(regions); @@ -984,38 +983,38 @@ public DataCommandResult _select(String query) { DataCommandRequest request = new DataCommandRequest(); request.setCommand(CliStrings.QUERY); request.setQuery(query); - Subject subject = this.securityService.getSubject(); + Subject subject = securityService.getSubject(); if (subject != null) { - request.setPrincipal((Serializable) subject.getPrincipal()); + request.setPrincipal(subject.getPrincipal()); } dataResult = DataCommands.callFunctionForRegion(request, function, members); dataResult.setInputQuery(query); - return (dataResult); + return dataResult; } else { - return (dataResult = - DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.format( - CliStrings.QUERY__MSG__REGIONS_NOT_FOUND, regionsInQuery.toString()), false)); + return DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.format( + CliStrings.QUERY__MSG__REGIONS_NOT_FOUND, regionsInQuery.toString()), false); } } else { - return (dataResult = DataCommandResult.createSelectInfoResult(null, null, -1, null, + return DataCommandResult.createSelectInfoResult(null, null, -1, null, CliStrings.format(CliStrings.QUERY__MSG__INVALID_QUERY, "Region mentioned in query probably missing /"), - false)); + false); } } catch (QueryInvalidException qe) { logger.error("{} Failed Error {}", query, qe.getMessage(), qe); - return (dataResult = DataCommandResult.createSelectInfoResult(null, null, -1, null, - CliStrings.format(CliStrings.QUERY__MSG__INVALID_QUERY, qe.getMessage()), false)); + return DataCommandResult.createSelectInfoResult(null, null, -1, null, + CliStrings.format(CliStrings.QUERY__MSG__INVALID_QUERY, qe.getMessage()), false); } } private String addLimit(String query) { if (StringUtils.containsIgnoreCase(query, " limit") - || StringUtils.containsIgnoreCase(query, " count(")) + || StringUtils.containsIgnoreCase(query, " count(")) { return query; + } return query + " limit " + getFetchSize(); } - }; + } public static class SelectQuitStep extends CLIMultiStepHelper.RemoteStep { @@ -1031,20 +1030,20 @@ public Result exec() { GfJsonObject args = CLIMultiStepHelper.getStepArgs(); DataCommandResult dataResult = cachedResult; cachedResult = null; - if (interactive) + if (interactive) { return CLIMultiStepHelper.createEmptyResult("END"); - else { + } else { CompositeResultData rd = dataResult.toSelectCommandResult(); SectionResultData section = rd.addSection(CLIMultiStepHelper.STEP_SECTION); section.addData(CLIMultiStepHelper.NEXT_STEP_NAME, "END"); return ResultBuilder.buildResult(rd); } } - }; + } public static int getPageSize() { int pageSize = -1; - Map session = null; + Map session; if (CliUtil.isGfshVM()) { session = Gfsh.getCurrentInstance().getEnv(); } else { @@ -1052,13 +1051,15 @@ public static int getPageSize() { } if (session != null) { String size = session.get(Gfsh.ENV_APP_COLLECTION_LIMIT); - if (size == null || size.isEmpty()) + if (StringUtils.isEmpty(size)) { pageSize = Gfsh.DEFAULT_APP_COLLECTION_LIMIT; - else + } else { pageSize = Integer.parseInt(size); + } } - if (pageSize == -1) + if (pageSize == -1) { pageSize = Gfsh.DEFAULT_APP_COLLECTION_LIMIT; + } return pageSize; } @@ -1068,7 +1069,6 @@ private static int getFetchSize() { public static String getLogMessage(QueryObserver observer, long startTime, String query) { String usedIndexesString = null; - String rowCountString = null; float time = 0.0f; if (startTime > 0L) { @@ -1087,7 +1087,7 @@ public static String getLogMessage(QueryObserver observer, long startTime, Strin buf.append(":"); for (Iterator itr = usedIndexes.entrySet().iterator(); itr.hasNext();) { Map.Entry entry = (Map.Entry) itr.next(); - buf.append(entry.getKey().toString() + entry.getValue()); + buf.append(entry.getKey().toString()).append(entry.getValue()); if (itr.hasNext()) { buf.append(","); } @@ -1099,9 +1099,8 @@ public static String getLogMessage(QueryObserver observer, long startTime, Strin + observer.getClass().getName() + ")"; } - return "Query Executed" + (startTime > 0L ? " in " + time + " ms;" : ";") - + (rowCountString != null ? rowCountString : "") - + (usedIndexesString != null ? usedIndexesString : ""); + return String.format("Query Executed%s%s", startTime > 0L ? " in " + time + " ms;" : ";", + usedIndexesString != null ? usedIndexesString : ""); } } diff --git a/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/TabularResultData.java b/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/TabularResultData.java index e72654ed4aa1..fa1e4a55902b 100644 --- a/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/TabularResultData.java +++ b/geode-core/src/main/java/org/apache/geode/management/internal/cli/result/TabularResultData.java @@ -14,20 +14,14 @@ */ package org.apache.geode.management.internal.cli.result; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - import org.apache.geode.management.internal.cli.json.GfJsonArray; import org.apache.geode.management.internal.cli.json.GfJsonException; import org.apache.geode.management.internal.cli.json.GfJsonObject; +import java.util.ArrayList; +import java.util.List; + /** - * - * * @since GemFire 7.0 */ public class TabularResultData extends AbstractResultData { @@ -70,8 +64,7 @@ public String getType() { } /** - * - * @param headerText + * @param headerText Text to set to header. * @return this TabularResultData * @throws ResultDataException If the value is non-finite number or if the key is null. */ @@ -80,8 +73,7 @@ public TabularResultData setHeader(String headerText) { } /** - * - * @param footerText + * @param footerText Text to set to footer. * @return this TabularResultData * @throws ResultDataException If the value is non-finite number or if the key is null. */ @@ -99,62 +91,8 @@ public String getFooter() { return gfJsonObject.getString(RESULT_FOOTER); } - public Map retrieveDataByValueInColumn(String columnName, String valueToSearch) { - Map foundValues = Collections.emptyMap(); - try { - GfJsonArray jsonArray = contentObject.getJSONArray(columnName); - int size = jsonArray.size(); - int foundIndex = -1; - for (int i = 0; i < size; i++) { - Object object = jsonArray.get(i); - if (object != null && object.equals(valueToSearch)) { - foundIndex = i; - break; - } - } - - if (foundIndex != -1) { - foundValues = new LinkedHashMap(); - for (Iterator iterator = contentObject.keys(); iterator.hasNext();) { - String storedColumnNames = (String) iterator.next(); - GfJsonArray storedColumnValues = contentObject.getJSONArray(storedColumnNames); - foundValues.put(storedColumnNames, String.valueOf(storedColumnValues.get(foundIndex))); - } - } - } catch (GfJsonException e) { - throw new ResultDataException(e.getMessage()); - } - return foundValues; - } - - public List> retrieveAllDataByValueInColumn(String columnName, - String valueToSearch) { - List> foundValuesList = new ArrayList>(); - try { - GfJsonArray jsonArray = contentObject.getJSONArray(columnName); - int size = jsonArray.size(); - for (int i = 0; i < size; i++) { - Object object = jsonArray.get(i); - if (object != null && object.equals(valueToSearch)) { - Map foundValues = new LinkedHashMap(); - - for (Iterator iterator = contentObject.keys(); iterator.hasNext();) { - String storedColumnNames = (String) iterator.next(); - GfJsonArray storedColumnValues = contentObject.getJSONArray(storedColumnNames); - foundValues.put(storedColumnNames, String.valueOf(storedColumnValues.get(i))); - } - - foundValuesList.add(foundValues); - } - } - } catch (GfJsonException e) { - throw new ResultDataException(e.getMessage()); - } - return foundValuesList; - } - public List retrieveAllValues(String columnName) { - List values = new ArrayList(); + List values = new ArrayList<>(); try { GfJsonArray jsonArray = contentObject.getJSONArray(columnName); diff --git a/geode-core/src/test/java/org/apache/geode/cache/query/dunit/QueryDataInconsistencyDUnitTest.java b/geode-core/src/test/java/org/apache/geode/cache/query/dunit/QueryDataInconsistencyDUnitTest.java index 1af6261613f5..eff4d295cbdb 100644 --- a/geode-core/src/test/java/org/apache/geode/cache/query/dunit/QueryDataInconsistencyDUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/cache/query/dunit/QueryDataInconsistencyDUnitTest.java @@ -14,19 +14,17 @@ */ package org.apache.geode.cache.query.dunit; +import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase; import org.junit.experimental.categories.Category; import org.junit.Test; import static org.junit.Assert.*; import org.apache.geode.test.dunit.cache.internal.JUnit4CacheTestCase; -import org.apache.geode.test.dunit.internal.JUnit4DistributedTestCase; import org.apache.geode.test.junit.categories.DistributedTest; import java.util.Properties; -import org.junit.experimental.categories.Category; - import org.apache.geode.cache.Cache; import org.apache.geode.cache.CacheException; import org.apache.geode.cache.CacheFactory; @@ -38,16 +36,13 @@ import org.apache.geode.cache.client.ClientCacheFactory; import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.query.Index; -import org.apache.geode.cache.query.IndexType; import org.apache.geode.cache.query.QueryService; import org.apache.geode.cache.query.SelectResults; import org.apache.geode.cache.query.data.Portfolio; import org.apache.geode.cache.query.data.Position; import org.apache.geode.cache.query.internal.QueryObserverHolder; import org.apache.geode.cache.query.internal.index.IndexManager; -import org.apache.geode.cache.query.partitioned.PRQueryDUnitHelper; import org.apache.geode.cache30.CacheSerializableRunnable; -import org.apache.geode.cache30.CacheTestCase; import org.apache.geode.internal.cache.execute.PRClientServerTestBase; import org.apache.geode.test.dunit.Assert; import org.apache.geode.test.dunit.AsyncInvocation; @@ -92,17 +87,14 @@ public class QueryDataInconsistencyDUnitTest extends JUnit4CacheTestCase { public static volatile boolean hooked = false; - /** - * @param name - */ public QueryDataInconsistencyDUnitTest() { super(); } @Override public final void postTearDownCacheTestCase() throws Exception { - Invoke.invokeInEveryVM(() -> disconnectFromDS()); - Invoke.invokeInEveryVM(() -> QueryObserverHolder.reset()); + Invoke.invokeInEveryVM(JUnit4DistributedTestCase::disconnectFromDS); + Invoke.invokeInEveryVM(QueryObserverHolder::reset); } @Override @@ -544,8 +536,8 @@ public static void createCacheClientWithoutRegion(String host, Integer port1) { } private void createCacheClientWithoutReg(String host, Integer port1) { - this.disconnectFromDS(); - ClientCache cache = new ClientCacheFactory().addPoolServer(host, port1).create(); + disconnectFromDS(); + new ClientCacheFactory().addPoolServer(host, port1).create(); } /** diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/GemfireDataCommandsDUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/GemfireDataCommandsDUnitTest.java index 2af3feaa6646..14d4d3f534ad 100644 --- a/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/GemfireDataCommandsDUnitTest.java +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/commands/GemfireDataCommandsDUnitTest.java @@ -218,8 +218,8 @@ public void run() { } }); - final String vm1MemberId = (String) vm1.invoke(() -> getMemberId()); - final String vm2MemberId = (String) vm2.invoke(() -> getMemberId()); + final String vm1MemberId = vm1.invoke(() -> getMemberId()); + final String vm2MemberId = vm2.invoke(() -> getMemberId()); getLogWriter().info("Vm1 ID : " + vm1MemberId); getLogWriter().info("Vm2 ID : " + vm2MemberId); @@ -457,9 +457,13 @@ public void run() { doQueryRegionsAssociatedMembers(queryTemplate1, -1, false, new String[] {DATA_PAR_REGION_NAME_VM2_PATH, "/jfgkdfjgkd"}); // one wrong region doQueryRegionsAssociatedMembers(queryTemplate1, -1, true, - new String[] {"/dhgfdhgf", "/dhgddhd"}); // both regions wrong + new String[] {"/dhgfdhgf", "/dhgddhd"}); // both + // regions + // wrong doQueryRegionsAssociatedMembers(queryTemplate1, -1, false, - new String[] {"/dhgfdhgf", "/dhgddhd"}); // both regions wrong + new String[] {"/dhgfdhgf", "/dhgddhd"}); // both + // regions + // wrong } }); } @@ -1961,13 +1965,9 @@ public boolean done() { return false; } else { // verify that bean is proper before executing tests - if (bean.getMembers() != null && bean.getMembers().length > 1 + return bean.getMembers() != null && bean.getMembers().length > 1 && bean.getMemberCount() > 0 - && service.getDistributedSystemMXBean().listRegions().length >= 2) { - return true; - } else { - return false; - } + && service.getDistributedSystemMXBean().listRegions().length >= 2; } } @@ -2037,7 +2037,7 @@ public void testRebalanceCommandForSimulate() { final VM manager = Host.getHost(0).getVM(0); manager.invoke(checkRegionMBeans); - getLogWriter().info("testRebalanceCommandForSimulate verified Mbean and executin command"); + getLogWriter().info("testRebalanceCommandForSimulate verified Mbean and executing command"); String command = "rebalance --simulate=true --include-region=" + "/" + REBALANCE_REGION_NAME; CommandResult cmdResult = executeCommand(command); getLogWriter().info("testRebalanceCommandForSimulate just after executing " + cmdResult); @@ -2298,11 +2298,7 @@ public boolean done() { getLogWriter().info( "waitForListClientMbean Still probing for DistributedRegionMXBean with separator Not null " + bean2.getMembers().length); - if (bean2.getMembers().length > 1) { - return true; - } else { - return false; - } + return bean2.getMembers().length > 1; } } } diff --git a/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java new file mode 100644 index 000000000000..a9a29a0493a2 --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/management/internal/cli/functions/DataCommandFunctionWithPDXJUnitTest.java @@ -0,0 +1,220 @@ +/* + * 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.cli.functions; + +import static org.apache.geode.management.internal.cli.domain.DataCommandResult.MISSING_VALUE; +import static org.assertj.core.api.Assertions.assertThat; + +import org.apache.geode.cache.Region; +import org.apache.geode.cache.RegionShortcut; +import org.apache.geode.management.internal.cli.domain.DataCommandRequest; +import org.apache.geode.management.internal.cli.domain.DataCommandResult; +import org.apache.geode.management.internal.cli.json.GfJsonArray; +import org.apache.geode.management.internal.cli.json.GfJsonException; +import org.apache.geode.management.internal.cli.json.GfJsonObject; +import org.apache.geode.management.internal.cli.result.CompositeResultData; +import org.apache.geode.management.internal.cli.result.TabularResultData; +import org.apache.geode.pdx.PdxReader; +import org.apache.geode.pdx.PdxSerializable; +import org.apache.geode.pdx.PdxWriter; +import org.apache.geode.test.dunit.rules.ServerStarterRule; +import org.apache.geode.test.junit.categories.IntegrationTest; +import org.assertj.core.api.SoftAssertions; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(IntegrationTest.class) +public class DataCommandFunctionWithPDXJUnitTest { + private static final String PARTITIONED_REGION = "part_region"; + + @Rule + public ServerStarterRule server = new ServerStarterRule().withPDXPersistent() + .withRegion(RegionShortcut.PARTITION, PARTITIONED_REGION); + + private Customer alice; + private Customer bob; + private CustomerWithPhone charlie; + private CustomerWithPhone dan; + + @Before + public void setup() { + alice = new Customer("0", "Alice", "Anderson"); + bob = new Customer("1", "Bob", "Bailey"); + charlie = new CustomerWithPhone("2", "Charlie", "Chaplin", "(222) 222-2222"); + dan = new CustomerWithPhone("3", "Dan", "Dickinson", "(333) 333-3333"); + + Region region = server.getCache().getRegion(PARTITIONED_REGION); + region.put(0, alice); + region.put(1, bob); + region.put(2, charlie); + region.put(3, dan); + } + + // GEODE-2662: building a table where early values are missing keys causes the data to shift + // upward during reporting. + @Test + public void testTableIsRectangular() throws GfJsonException { + TabularResultData rawTable = getTableFromQuery("select * from /" + PARTITIONED_REGION); + // Verify any table built + assertThat(rawTable.getGfJsonObject()).isNotNull(); + assertThat(rawTable.getGfJsonObject().getJSONObject("content")).isNotNull(); + GfJsonObject tableJson = rawTable.getGfJsonObject().getJSONObject("content"); + // Verify table is rectangular + SoftAssertions softly = new SoftAssertions(); + for (String k : new String[] {"id", "phone", "firstName", "lastName"}) { + softly.assertThat(tableJson.getJSONArray(k).size()).isEqualTo(4); + } + softly.assertAll(); + } + + @Test + public void testVerifyDataDoesNotShift() throws Exception { + TabularResultData rawTable = + getTableFromQuery("select * from /" + PARTITIONED_REGION + " order by id"); + // Verify any table built + assertThat(rawTable.getGfJsonObject()).isNotNull(); + assertThat(rawTable.getGfJsonObject().getJSONObject("content")).isNotNull(); + GfJsonObject tableJson = rawTable.getGfJsonObject().getJSONObject("content"); + // Table only contains correct keys + assertThat(tableJson.getJSONArray("missingKey")).isNull(); + + // Table contains correct data + assertThatRowIsBuiltCorrectly(tableJson, 0, alice); + assertThatRowIsBuiltCorrectly(tableJson, 1, bob); + assertThatRowIsBuiltCorrectly(tableJson, 2, charlie); + assertThatRowIsBuiltCorrectly(tableJson, 3, dan); + } + + @Test + public void testFilteredQueryWithPhone() throws Exception { + TabularResultData rawTable = getTableFromQuery( + "select * from /" + PARTITIONED_REGION + " c where IS_DEFINED ( c.phone ) order by id"); + assertThat(rawTable.getGfJsonObject()).isNotNull(); + assertThat(rawTable.getGfJsonObject().getJSONObject("content")).isNotNull(); + GfJsonObject tableJson = rawTable.getGfJsonObject().getJSONObject("content"); + for (String k : new String[] {"id", "phone", "firstName", "lastName"}) { + assertThat(tableJson.getJSONArray(k).size()).isEqualTo(2); + } + assertThatRowIsBuiltCorrectly(tableJson, 0, charlie); + assertThatRowIsBuiltCorrectly(tableJson, 1, dan); + } + + + @Test + public void testFilteredQueryWithoutPhone() throws Exception { + TabularResultData rawTable = getTableFromQuery( + "select * from /" + PARTITIONED_REGION + " c where IS_UNDEFINED ( c.phone ) order by id"); + assertThat(rawTable.getGfJsonObject()).isNotNull(); + assertThat(rawTable.getGfJsonObject().getJSONObject("content")).isNotNull(); + GfJsonObject tableJson = rawTable.getGfJsonObject().getJSONObject("content"); + for (String k : new String[] {"id", "firstName", "lastName"}) { + assertThat(tableJson.getJSONArray(k).size()).isEqualTo(2); + } + assertThatRowIsBuiltCorrectly(tableJson, 0, alice); + assertThatRowIsBuiltCorrectly(tableJson, 1, bob); + } + + private TabularResultData getTableFromQuery(String query) { + DataCommandRequest request = new DataCommandRequest(); + request.setQuery(query); + DataCommandResult result = new DataCommandFunction().select(request); + CompositeResultData r = result.toSelectCommandResult(); + return r.retrieveSectionByIndex(0).retrieveTableByIndex(0); + } + + + private void assertThatRowIsBuiltCorrectly(GfJsonObject table, int rowNum, Customer customer) + throws Exception { + SoftAssertions softly = new SoftAssertions(); + String id = (String) table.getJSONArray("id").get(rowNum); + String firstName = (String) table.getJSONArray("firstName").get(rowNum); + String lastName = (String) table.getJSONArray("lastName").get(rowNum); + + softly.assertThat(id).describedAs("Customer ID").isEqualTo(customer.id); + softly.assertThat(firstName).describedAs("First name").isEqualTo(customer.firstName); + softly.assertThat(lastName).describedAs("Last name").isEqualTo(customer.lastName); + + GfJsonArray phoneArray = table.getJSONArray("phone"); + + if (phoneArray == null) { + softly.assertThat(customer).describedAs("No phone data") + .isNotInstanceOf(CustomerWithPhone.class); + } else { + String phone = (String) phoneArray.get(rowNum); + + if (customer instanceof CustomerWithPhone) { + softly.assertThat(phone).describedAs("Phone") + .isEqualTo(((CustomerWithPhone) customer).phone); + } else { + softly.assertThat(phone).describedAs("Phone (missing)").isEqualTo(MISSING_VALUE); + } + } + softly.assertAll(); + } + + public static class Customer implements PdxSerializable { + protected String id; + protected String firstName; + protected String lastName; + + Customer() {} + + Customer(String id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + @Override + public void toData(PdxWriter writer) { + writer.writeString("id", id).markIdentityField("id").writeString("firstName", firstName) + .writeString("lastName", lastName); + } + + @Override + public void fromData(PdxReader reader) { + id = reader.readString("id"); + firstName = reader.readString("firstName"); + lastName = reader.readString("lastName"); + } + } + + public static class CustomerWithPhone extends Customer { + private String phone; + + CustomerWithPhone(String id, String firstName, String lastName, String phone) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + this.phone = phone; + } + + @Override + public void toData(PdxWriter writer) { + writer.writeString("id", id).markIdentityField("id").writeString("firstName", firstName) + .writeString("lastName", lastName).writeString("phone", phone); + } + + @Override + public void fromData(PdxReader reader) { + id = reader.readString("id"); + firstName = reader.readString("firstName"); + lastName = reader.readString("lastName"); + phone = reader.readString("phone"); + } + } +} diff --git a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/ServerStarterRule.java b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/ServerStarterRule.java index ead10471cfac..30ae59fd786b 100644 --- a/geode-core/src/test/java/org/apache/geode/test/dunit/rules/ServerStarterRule.java +++ b/geode-core/src/test/java/org/apache/geode/test/dunit/rules/ServerStarterRule.java @@ -54,6 +54,7 @@ public class ServerStarterRule extends MemberStarterRule impl private transient Cache cache; private transient CacheServer server; private int embeddedLocatorPort = -1; + private boolean pdxPersistent = false; private Map regions = new HashMap<>(); @@ -107,6 +108,13 @@ public void stopMember() { } } + public ServerStarterRule withPDXPersistent() { + pdxPersistent = true; + return this; + } + + + public ServerStarterRule withEmbeddedLocator() { embeddedLocatorPort = AvailablePortHelper.getRandomAvailableTCPPort(); properties.setProperty("start-locator", "localhost[" + embeddedLocatorPort + "]"); @@ -127,9 +135,6 @@ public ServerStarterRule withRestService(boolean useDefaultPort) { return this; } - public void startServer() { - startServer(false); - } public ServerStarterRule withRegion(RegionShortcut type, String name) { this.autoStart = true; @@ -141,7 +146,7 @@ public void startServer(Properties properties, int locatorPort) { withProperties(properties).withConnectionToLocator(locatorPort).startServer(); } - public void startServer(boolean pdxPersistent) { + public void startServer() { CacheFactory cf = new CacheFactory(this.properties); cf.setPdxReadSerialized(pdxPersistent); cf.setPdxPersistent(pdxPersistent);