From f4487dd5f6338c9b5fa384e36e3e2ee62aeab987 Mon Sep 17 00:00:00 2001 From: Matt Gilman Date: Tue, 9 Feb 2016 00:06:48 -0500 Subject: [PATCH] NIFI-1492: - Limiting the amount of state entries returned to a client. - Code clean up. Signed-off-by: joewitt --- .../apache/nifi/web/api/dto/StateMapDTO.java | 15 +++++ .../manager/impl/WebClusterManager.java | 15 ++++- .../controller/state/SortedStateUtils.java | 62 +++++++++++++++++++ .../apache/nifi/web/api/dto/DtoFactory.java | 14 ++++- .../canvas/component-state-dialog.jsp | 16 +++-- .../src/main/webapp/css/component-state.css | 9 +++ .../webapp/js/nf/canvas/nf-component-state.js | 21 ++++++- .../nf/canvas/nf-processor-configuration.js | 4 +- 8 files changed, 144 insertions(+), 12 deletions(-) create mode 100644 nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/state/SortedStateUtils.java diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/StateMapDTO.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/StateMapDTO.java index be5e80868d..6f67287975 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/StateMapDTO.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-client-dto/src/main/java/org/apache/nifi/web/api/dto/StateMapDTO.java @@ -28,6 +28,7 @@ import java.util.List; public class StateMapDTO { private String scope; + private int totalEntryCount; private List state; /** @@ -44,6 +45,20 @@ public class StateMapDTO { this.scope = scope; } + /** + * @return The total number of state entries. When the state map is lengthy, only of portion of the entries are returned. + */ + @ApiModelProperty( + value = "The total number of state entries. When the state map is lengthy, only of portion of the entries are returned." + ) + public int getTotalEntryCount() { + return totalEntryCount; + } + + public void setTotalEntryCount(int totalEntryCount) { + this.totalEntryCount = totalEntryCount; + } + /** * @return The state */ diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/manager/impl/WebClusterManager.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/manager/impl/WebClusterManager.java index a8f6118e1d..e98e8e7e72 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/manager/impl/WebClusterManager.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/manager/impl/WebClusterManager.java @@ -151,6 +151,7 @@ import org.apache.nifi.controller.service.ControllerServiceNode; import org.apache.nifi.controller.service.ControllerServiceProvider; import org.apache.nifi.controller.service.ControllerServiceState; import org.apache.nifi.controller.service.StandardControllerServiceProvider; +import org.apache.nifi.controller.state.SortedStateUtils; import org.apache.nifi.controller.state.manager.StandardStateManagerProvider; import org.apache.nifi.controller.status.ProcessGroupStatus; import org.apache.nifi.controller.status.RemoteProcessGroupStatus; @@ -2574,8 +2575,9 @@ public class WebClusterManager implements HttpClusterManager, ProtocolHandler, C } private void mergeComponentState(final ComponentStateDTO componentState, Map componentStateMap) { - final List localStateEntries = new ArrayList<>(); + List localStateEntries = new ArrayList<>(); + int totalStateEntries = 0; for (final Map.Entry nodeEntry : componentStateMap.entrySet()) { final ComponentStateDTO nodeComponentState = nodeEntry.getValue(); final NodeIdentifier nodeId = nodeEntry.getKey(); @@ -2583,6 +2585,8 @@ public class WebClusterManager implements HttpClusterManager, ProtocolHandler, C final StateMapDTO nodeLocalStateMap = nodeComponentState.getLocalState(); if (nodeLocalStateMap.getState() != null) { + totalStateEntries += nodeLocalStateMap.getTotalEntryCount(); + for (final StateEntryDTO nodeStateEntry : nodeLocalStateMap.getState()) { nodeStateEntry.setClusterNodeId(nodeId.getId()); nodeStateEntry.setClusterNodeAddress(nodeAddress); @@ -2591,7 +2595,16 @@ public class WebClusterManager implements HttpClusterManager, ProtocolHandler, C } } + // ensure appropriate sort + Collections.sort(localStateEntries, SortedStateUtils.getEntryDtoComparator()); + + // sublist if necessary + if (localStateEntries.size() > SortedStateUtils.MAX_COMPONENT_STATE_ENTRIES) { + localStateEntries = localStateEntries.subList(0, SortedStateUtils.MAX_COMPONENT_STATE_ENTRIES); + } + // add all the local state entries + componentState.getLocalState().setTotalEntryCount(totalStateEntries); componentState.getLocalState().setState(localStateEntries); } diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/state/SortedStateUtils.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/state/SortedStateUtils.java new file mode 100644 index 0000000000..2eb8f222da --- /dev/null +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/state/SortedStateUtils.java @@ -0,0 +1,62 @@ +/* + * 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.nifi.controller.state; + +import org.apache.nifi.web.api.dto.StateEntryDTO; + +import java.text.Collator; +import java.util.Comparator; +import java.util.Locale; + +public class SortedStateUtils { + + /** + * The maximum number of state entries to return to a client + */ + public static final int MAX_COMPONENT_STATE_ENTRIES = 500; + + /** + * Gets a comparator for comparing state entry keys. + * + * @return comparator for comparing state entry keys + */ + public static Comparator getKeyComparator() { + final Collator collator = Collator.getInstance(Locale.US); + return new Comparator() { + @Override + public int compare(String s1, String s2) { + return collator.compare(s1, s2); + } + }; + } + + /** + * Gets a comparator for comparing state entry keys. + * + * @return comparator for comparing state entry keys + */ + public static Comparator getEntryDtoComparator() { + final Collator collator = Collator.getInstance(Locale.US); + return new Comparator() { + @Override + public int compare(StateEntryDTO o1, StateEntryDTO o2) { + return collator.compare(o1.getKey(), o2.getKey()); + } + }; + } +} diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/dto/DtoFactory.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/dto/DtoFactory.java index 339b725692..652fd04362 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/dto/DtoFactory.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/dto/DtoFactory.java @@ -71,6 +71,7 @@ import org.apache.nifi.controller.repository.claim.ContentClaim; import org.apache.nifi.controller.repository.claim.ResourceClaim; import org.apache.nifi.controller.service.ControllerServiceNode; import org.apache.nifi.controller.service.ControllerServiceReference; +import org.apache.nifi.controller.state.SortedStateUtils; import org.apache.nifi.controller.status.ConnectionStatus; import org.apache.nifi.controller.status.PortStatus; import org.apache.nifi.controller.status.ProcessGroupStatus; @@ -137,11 +138,13 @@ import java.util.Comparator; import java.util.Date; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; @@ -319,14 +322,21 @@ public final class DtoFactory { final StateMapDTO dto = new StateMapDTO(); dto.setScope(scope.toString()); - final List stateEntries = new ArrayList<>(); + final TreeMap sortedState = new TreeMap(SortedStateUtils.getKeyComparator()); final Map state = stateMap.toMap(); - for (final Map.Entry entry : state.entrySet()) { + sortedState.putAll(state); + + int count = 0; + final List stateEntries = new ArrayList<>(); + final Set> entrySet = sortedState.entrySet(); + for (final Iterator> iter = entrySet.iterator(); iter.hasNext() && count++ < SortedStateUtils.MAX_COMPONENT_STATE_ENTRIES;) { + final Map.Entry entry = iter.next(); final StateEntryDTO entryDTO = new StateEntryDTO(); entryDTO.setKey(entry.getKey()); entryDTO.setValue(entry.getValue()); stateEntries.add(entryDTO); } + dto.setTotalEntryCount(state.size()); dto.setState(stateEntries); return dto; diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src/main/webapp/WEB-INF/partials/canvas/component-state-dialog.jsp b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src/main/webapp/WEB-INF/partials/canvas/component-state-dialog.jsp index abc1140fb6..53387566f1 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src/main/webapp/WEB-INF/partials/canvas/component-state-dialog.jsp +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-ui/src/main/webapp/WEB-INF/partials/canvas/component-state-dialog.jsp @@ -29,13 +29,19 @@
-
-
- +
+ -
- Displaying  of  +
+
+ +
+
+ Displaying  of  +
+