You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by "EndzeitBegins (via GitHub)" <gi...@apache.org> on 2024/03/01 23:37:44 UTC
[PR] NIFI-12857 Add test for and refactor implementation of QueuePrioritizer [nifi]
EndzeitBegins opened a new pull request, #8466:
URL: https://github.com/apache/nifi/pull/8466
<!-- 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. -->
# Summary
[NIFI-12857](https://issues.apache.org/jira/browse/NIFI-12857)
Additionally, removed some dead code from `TestStandardFlowFileQueue`.
# Tracking
Please complete the following tracking steps prior to pull request creation.
### Issue Tracking
- [x] [Apache NiFi Jira](https://issues.apache.org/jira/browse/NIFI) issue created
### Pull Request Tracking
- [x] Pull Request title starts with Apache NiFi Jira issue number, such as `NIFI-00000`
- [x] Pull Request commit message starts with Apache NiFi Jira issue number, as such `NIFI-00000`
### Pull Request Formatting
- [x] Pull Request based on current revision of the `main` branch
- [x] Pull Request refers to a feature branch with one commit containing changes
# Verification
Please indicate the verification steps performed prior to pull request creation.
### Build
- [x] Build completed using `mvn clean install -P contrib-check`
- [x] JDK 21
### Licensing
- [x] New dependencies are compatible with the [Apache License 2.0](https://apache.org/licenses/LICENSE-2.0) according to the [License Policy](https://www.apache.org/legal/resolved.html)
- [x] New dependencies are documented in applicable `LICENSE` and `NOTICE` files
### Documentation
- [x] Documentation formatting appears as expected in rendered files
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12857 Add test for and refactor implementation of QueuePrioritizer [nifi]
Posted by "EndzeitBegins (via GitHub)" <gi...@apache.org>.
EndzeitBegins commented on PR #8466:
URL: https://github.com/apache/nifi/pull/8466#issuecomment-2102726045
Thank you for the review @exceptionfactory.
I've just rebased the PR on the last commit on `main`, added the missing line break to the test, and added braces to conditional blocks where they were missing.
I would appreciate another look.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12857 Add test for and refactor implementation of QueuePrioritizer [nifi]
Posted by "EndzeitBegins (via GitHub)" <gi...@apache.org>.
EndzeitBegins commented on PR #8466:
URL: https://github.com/apache/nifi/pull/8466#issuecomment-2020987603
Of course, just rebased on the last commit of `main`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12857 Add test for and refactor implementation of QueuePrioritizer [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory commented on code in PR #8466:
URL: https://github.com/apache/nifi/pull/8466#discussion_r1593114760
##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/queue/QueuePrioritizer.java:
##########
@@ -18,74 +18,41 @@
package org.apache.nifi.controller.queue;
import org.apache.nifi.controller.repository.FlowFileRecord;
-import org.apache.nifi.controller.repository.claim.ContentClaim;
import org.apache.nifi.flowfile.FlowFilePrioritizer;
import java.io.Serializable;
-import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class QueuePrioritizer implements Comparator<FlowFileRecord>, Serializable {
private static final long serialVersionUID = 1L;
- private final transient List<FlowFilePrioritizer> prioritizers = new ArrayList<>();
+ private static final Comparator<FlowFileRecord> penaltyComparator = Comparator
+ .comparing(FlowFileRecord::isPenalized)
+ .thenComparingLong(record -> record.isPenalized() ? record.getPenaltyExpirationMillis() : 0);
+ private static final Comparator<FlowFileRecord> claimComparator = Comparator
+ .comparing(FlowFileRecord::getContentClaim, Comparator.nullsFirst(Comparator.naturalOrder()))
+ .thenComparingLong(FlowFileRecord::getContentClaimOffset);
+ private static final Comparator<FlowFileRecord> idComparator = Comparator.comparingLong(FlowFileRecord::getId);
+
+ private final transient List<FlowFilePrioritizer> prioritizers;
public QueuePrioritizer(final List<FlowFilePrioritizer> priorities) {
- if (null != priorities) {
- prioritizers.addAll(priorities);
- }
+ prioritizers = priorities == null ? List.of() : List.copyOf(priorities);
}
@Override
public int compare(final FlowFileRecord f1, final FlowFileRecord f2) {
- int returnVal = 0;
- final boolean f1Penalized = f1.isPenalized();
- final boolean f2Penalized = f2.isPenalized();
-
- if (f1Penalized && !f2Penalized) {
- return 1;
- } else if (!f1Penalized && f2Penalized) {
- return -1;
- }
-
- if (f1Penalized && f2Penalized) {
- if (f1.getPenaltyExpirationMillis() < f2.getPenaltyExpirationMillis()) {
- return -1;
- } else if (f1.getPenaltyExpirationMillis() > f2.getPenaltyExpirationMillis()) {
- return 1;
- }
- }
+ final int penaltyComparisonResult = penaltyComparator.compare(f1, f2);
+ if (penaltyComparisonResult != 0) return penaltyComparisonResult;
- if (!prioritizers.isEmpty()) {
- for (final FlowFilePrioritizer prioritizer : prioritizers) {
- returnVal = prioritizer.compare(f1, f2);
- if (returnVal != 0) {
- return returnVal;
- }
- }
+ for (FlowFilePrioritizer comparator : prioritizers) {
+ final int prioritizerComparisonResult = comparator.compare(f1, f2);
+ if (prioritizerComparisonResult != 0) return prioritizerComparisonResult;
Review Comment:
```suggestion
if (prioritizerComparisonResult != 0) {
return prioritizerComparisonResult;
}
```
##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/test/java/org/apache/nifi/controller/queue/QueuePrioritizerTest.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.queue;
+
+import org.apache.nifi.controller.MockFlowFileRecord;
+import org.apache.nifi.controller.repository.claim.ResourceClaim;
+import org.apache.nifi.controller.repository.claim.ResourceClaimManager;
+import org.apache.nifi.controller.repository.claim.StandardContentClaim;
+import org.apache.nifi.controller.repository.claim.StandardResourceClaimManager;
+import org.apache.nifi.flowfile.FlowFile;
+import org.apache.nifi.flowfile.FlowFilePrioritizer;
+import org.junit.jupiter.api.Test;
+
+import java.util.Comparator;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+@SuppressWarnings("EqualsWithItself")
+class QueuePrioritizerTest {
+
+ private static final String SORT_ATTRIBUTE = "my-sort-attribute";
+
+ private final FlowFilePrioritizer flowFilePrioritizer = (o1, o2) -> {
+ Comparator<FlowFile> comparing = Comparator.comparing(
+ (flowFile) -> flowFile.getAttribute(SORT_ATTRIBUTE),
+ Comparator.nullsFirst(Comparator.naturalOrder())
+ );
+ return comparing.compare(o1, o2);
+ };
+ private final ResourceClaimManager claimManager = new StandardResourceClaimManager();
+
+ private final QueuePrioritizer prioritizer = new QueuePrioritizer(List.of(flowFilePrioritizer));
+
+ @Test
+ void deprioritizesFlowFilesWithPenalty() {
+ MockFlowFileRecord nonPenalizedFlowFile = new MockFlowFileRecord(Map.of("penalized", "no", SORT_ATTRIBUTE, "later"), 0);
+ MockFlowFileRecord expiredPenaltyFlowFile = new MockFlowFileRecord(Map.of("penalized", "no longer"), 0);
+ expiredPenaltyFlowFile.setPenaltyExpiration(System.currentTimeMillis() - 9_001);
+ MockFlowFileRecord penalizedFlowFile = new MockFlowFileRecord(Map.of("penalized", "short"), 0);
+ penalizedFlowFile.setPenaltyExpiration(System.currentTimeMillis() + 123_456);
+ MockFlowFileRecord longerPenalizedFlowFile = new MockFlowFileRecord(Map.of("penalized", "long"), 0);
+ longerPenalizedFlowFile.setPenaltyExpiration(System.currentTimeMillis() + 456_789);
+
+ assertEquals(0, prioritizer.compare(nonPenalizedFlowFile, nonPenalizedFlowFile));
+ assertEquals(1, prioritizer.compare(nonPenalizedFlowFile, expiredPenaltyFlowFile));
+ assertEquals(-1, prioritizer.compare(nonPenalizedFlowFile, penalizedFlowFile));
+ assertEquals(-1, prioritizer.compare(nonPenalizedFlowFile, longerPenalizedFlowFile)); assertEquals(1, prioritizer.compare(longerPenalizedFlowFile, nonPenalizedFlowFile));
Review Comment:
```suggestion
assertEquals(-1, prioritizer.compare(nonPenalizedFlowFile, longerPenalizedFlowFile));
assertEquals(1, prioritizer.compare(longerPenalizedFlowFile, nonPenalizedFlowFile));
```
##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/queue/QueuePrioritizer.java:
##########
@@ -18,74 +18,41 @@
package org.apache.nifi.controller.queue;
import org.apache.nifi.controller.repository.FlowFileRecord;
-import org.apache.nifi.controller.repository.claim.ContentClaim;
import org.apache.nifi.flowfile.FlowFilePrioritizer;
import java.io.Serializable;
-import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class QueuePrioritizer implements Comparator<FlowFileRecord>, Serializable {
private static final long serialVersionUID = 1L;
- private final transient List<FlowFilePrioritizer> prioritizers = new ArrayList<>();
+ private static final Comparator<FlowFileRecord> penaltyComparator = Comparator
+ .comparing(FlowFileRecord::isPenalized)
+ .thenComparingLong(record -> record.isPenalized() ? record.getPenaltyExpirationMillis() : 0);
+ private static final Comparator<FlowFileRecord> claimComparator = Comparator
+ .comparing(FlowFileRecord::getContentClaim, Comparator.nullsFirst(Comparator.naturalOrder()))
+ .thenComparingLong(FlowFileRecord::getContentClaimOffset);
+ private static final Comparator<FlowFileRecord> idComparator = Comparator.comparingLong(FlowFileRecord::getId);
+
+ private final transient List<FlowFilePrioritizer> prioritizers;
public QueuePrioritizer(final List<FlowFilePrioritizer> priorities) {
- if (null != priorities) {
- prioritizers.addAll(priorities);
- }
+ prioritizers = priorities == null ? List.of() : List.copyOf(priorities);
}
@Override
public int compare(final FlowFileRecord f1, final FlowFileRecord f2) {
- int returnVal = 0;
- final boolean f1Penalized = f1.isPenalized();
- final boolean f2Penalized = f2.isPenalized();
-
- if (f1Penalized && !f2Penalized) {
- return 1;
- } else if (!f1Penalized && f2Penalized) {
- return -1;
- }
-
- if (f1Penalized && f2Penalized) {
- if (f1.getPenaltyExpirationMillis() < f2.getPenaltyExpirationMillis()) {
- return -1;
- } else if (f1.getPenaltyExpirationMillis() > f2.getPenaltyExpirationMillis()) {
- return 1;
- }
- }
+ final int penaltyComparisonResult = penaltyComparator.compare(f1, f2);
+ if (penaltyComparisonResult != 0) return penaltyComparisonResult;
Review Comment:
```suggestion
if (penaltyComparisonResult != 0) {
return penaltyComparisonResult;
}
```
##########
nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/queue/QueuePrioritizer.java:
##########
@@ -18,74 +18,41 @@
package org.apache.nifi.controller.queue;
import org.apache.nifi.controller.repository.FlowFileRecord;
-import org.apache.nifi.controller.repository.claim.ContentClaim;
import org.apache.nifi.flowfile.FlowFilePrioritizer;
import java.io.Serializable;
-import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class QueuePrioritizer implements Comparator<FlowFileRecord>, Serializable {
private static final long serialVersionUID = 1L;
- private final transient List<FlowFilePrioritizer> prioritizers = new ArrayList<>();
+ private static final Comparator<FlowFileRecord> penaltyComparator = Comparator
+ .comparing(FlowFileRecord::isPenalized)
+ .thenComparingLong(record -> record.isPenalized() ? record.getPenaltyExpirationMillis() : 0);
+ private static final Comparator<FlowFileRecord> claimComparator = Comparator
+ .comparing(FlowFileRecord::getContentClaim, Comparator.nullsFirst(Comparator.naturalOrder()))
+ .thenComparingLong(FlowFileRecord::getContentClaimOffset);
+ private static final Comparator<FlowFileRecord> idComparator = Comparator.comparingLong(FlowFileRecord::getId);
+
+ private final transient List<FlowFilePrioritizer> prioritizers;
public QueuePrioritizer(final List<FlowFilePrioritizer> priorities) {
- if (null != priorities) {
- prioritizers.addAll(priorities);
- }
+ prioritizers = priorities == null ? List.of() : List.copyOf(priorities);
}
@Override
public int compare(final FlowFileRecord f1, final FlowFileRecord f2) {
- int returnVal = 0;
- final boolean f1Penalized = f1.isPenalized();
- final boolean f2Penalized = f2.isPenalized();
-
- if (f1Penalized && !f2Penalized) {
- return 1;
- } else if (!f1Penalized && f2Penalized) {
- return -1;
- }
-
- if (f1Penalized && f2Penalized) {
- if (f1.getPenaltyExpirationMillis() < f2.getPenaltyExpirationMillis()) {
- return -1;
- } else if (f1.getPenaltyExpirationMillis() > f2.getPenaltyExpirationMillis()) {
- return 1;
- }
- }
+ final int penaltyComparisonResult = penaltyComparator.compare(f1, f2);
+ if (penaltyComparisonResult != 0) return penaltyComparisonResult;
- if (!prioritizers.isEmpty()) {
- for (final FlowFilePrioritizer prioritizer : prioritizers) {
- returnVal = prioritizer.compare(f1, f2);
- if (returnVal != 0) {
- return returnVal;
- }
- }
+ for (FlowFilePrioritizer comparator : prioritizers) {
+ final int prioritizerComparisonResult = comparator.compare(f1, f2);
+ if (prioritizerComparisonResult != 0) return prioritizerComparisonResult;
}
- final ContentClaim claim1 = f1.getContentClaim();
- final ContentClaim claim2 = f2.getContentClaim();
-
-
- // put the one without a claim first
- if (claim1 == null && claim2 != null) {
- return -1;
- } else if (claim1 != null && claim2 == null) {
- return 1;
- } else if (claim1 != null && claim2 != null) {
- final int claimComparison = claim1.compareTo(claim2);
- if (claimComparison != 0) {
- return claimComparison;
- }
-
- final int claimOffsetComparison = Long.compare(f1.getContentClaimOffset(), f2.getContentClaimOffset());
- if (claimOffsetComparison != 0) {
- return claimOffsetComparison;
- }
- }
+ final int claimComparisionResult = claimComparator.compare(f1, f2);
+ if (claimComparisionResult != 0) return claimComparisionResult;
Review Comment:
As a general rule, braces should be used for conditional blocks:
```suggestion
if (claimComparisionResult != 0) {
return claimComparisionResult;
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] NIFI-12857 Add test for and refactor implementation of QueuePrioritizer [nifi]
Posted by "exceptionfactory (via GitHub)" <gi...@apache.org>.
exceptionfactory closed pull request #8466: NIFI-12857 Add test for and refactor implementation of QueuePrioritizer
URL: https://github.com/apache/nifi/pull/8466
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org