Skip to content

Commit 4f63d60

Browse files
NIFI-15227 Replaced Framework use of DigestUtils with MessageDigestUtils
- Removed direct dependency on commons-codec from framework-cluster and web-api modules
1 parent 8c876da commit 4f63d60

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

nifi-framework-bundle/nifi-framework/nifi-framework-cluster/pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@
100100
<groupId>org.apache.commons</groupId>
101101
<artifactId>commons-lang3</artifactId>
102102
</dependency>
103-
<dependency>
104-
<groupId>commons-codec</groupId>
105-
<artifactId>commons-codec</artifactId>
106-
</dependency>
107103
<dependency>
108104
<groupId>org.apache.nifi</groupId>
109105
<artifactId>nifi-web-security</artifactId>

nifi-framework-bundle/nifi-framework/nifi-framework-cluster/src/main/java/org/apache/nifi/cluster/coordination/flow/PopularVoteFlowElection.java

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717

1818
package org.apache.nifi.cluster.coordination.flow;
1919

20-
import org.apache.commons.codec.digest.DigestUtils;
2120
import org.apache.nifi.cluster.protocol.DataFlow;
2221
import org.apache.nifi.cluster.protocol.NodeIdentifier;
2322
import org.apache.nifi.controller.serialization.VersionedFlowSynchronizer;
23+
import org.apache.nifi.util.security.MessageDigestUtils;
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

2727
import java.nio.charset.StandardCharsets;
28+
import java.time.Duration;
2829
import java.util.Collections;
2930
import java.util.HashMap;
3031
import java.util.HashSet;
32+
import java.util.HexFormat;
3133
import java.util.List;
3234
import java.util.Map;
3335
import java.util.Set;
3436
import java.util.concurrent.TimeUnit;
3537
import java.util.concurrent.atomic.AtomicInteger;
36-
import java.util.stream.Collectors;
3738

3839
/**
3940
* <p>
@@ -82,16 +83,16 @@ public synchronized boolean isElectionComplete() {
8283
final long nanosSinceStart = System.nanoTime() - startNanos;
8384
if (nanosSinceStart > maxWaitNanos) {
8485
final FlowCandidate elected = performElection();
85-
logger.info("Election is complete because the maximum allowed time has elapsed. "
86-
+ "The elected dataflow is held by the following nodes: {}", elected.getNodes());
86+
final Set<NodeIdentifier> nodes = elected == null ? Set.of() : elected.getNodes();
87+
logger.info("Election completed after maximum duration [{} seconds] with Nodes {}", Duration.ofNanos(maxWaitNanos).toSeconds(), nodes);
8788

8889
return true;
8990
} else if (maxNodes != null) {
9091
final int numVotes = getVoteCount();
9192
if (numVotes >= maxNodes) {
9293
final FlowCandidate elected = performElection();
93-
logger.info("Election is complete because the required number of nodes ({}) have voted. "
94-
+ "The elected dataflow is held by the following nodes: {}", maxNodes, elected.getNodes());
94+
final Set<NodeIdentifier> nodes = elected == null ? Set.of() : elected.getNodes();
95+
logger.info("Election completed after maximum votes [{}] with Nodes {}", maxNodes, nodes);
9596

9697
return true;
9798
}
@@ -107,7 +108,7 @@ public boolean isVoteCounted(final NodeIdentifier nodeIdentifier) {
107108
}
108109

109110
private synchronized int getVoteCount() {
110-
return candidateByFingerprint.values().stream().mapToInt(candidate -> candidate.getVotes()).sum();
111+
return candidateByFingerprint.values().stream().mapToInt(FlowCandidate::getVotes).sum();
111112
}
112113

113114
@Override
@@ -136,11 +137,11 @@ public synchronized DataFlow castVote(final DataFlow candidate, final NodeIdenti
136137
}
137138

138139
private String fingerprint(final DataFlow dataFlow) {
139-
final String flowFingerprint = DigestUtils.sha256Hex(dataFlow.getFlow());
140+
final byte[] flowDigest = MessageDigestUtils.getDigest(dataFlow.getFlow());
141+
final String flowFingerprint = HexFormat.of().formatHex(flowDigest);
140142
final String authFingerprint = dataFlow.getAuthorizerFingerprint() == null ? "" : new String(dataFlow.getAuthorizerFingerprint(), StandardCharsets.UTF_8);
141-
final String candidateFingerprint = flowFingerprint + authFingerprint;
142143

143-
return candidateFingerprint;
144+
return flowFingerprint + authFingerprint;
144145
}
145146

146147
@Override
@@ -155,7 +156,7 @@ private FlowCandidate performElection() {
155156

156157
final List<FlowCandidate> nonEmptyCandidates = candidateByFingerprint.values().stream()
157158
.filter(candidate -> !candidate.isFlowEmpty())
158-
.collect(Collectors.toList());
159+
.toList();
159160

160161
if (nonEmptyCandidates.isEmpty()) {
161162
// All flow candidates are empty flows. Just use one of them.
@@ -167,7 +168,7 @@ private FlowCandidate performElection() {
167168
final FlowCandidate elected;
168169
if (nonEmptyCandidates.size() == 1) {
169170
// Only one flow is non-empty. Use that one.
170-
elected = nonEmptyCandidates.iterator().next();
171+
elected = nonEmptyCandidates.getFirst();
171172
} else {
172173
// Choose the non-empty flow that got the most votes.
173174
elected = nonEmptyCandidates.stream()
@@ -196,7 +197,7 @@ public synchronized String getStatusDescription() {
196197
}
197198

198199
if (maxNodes != null) {
199-
final int votesNeeded = maxNodes.intValue() - getVoteCount();
200+
final int votesNeeded = maxNodes - getVoteCount();
200201
descriptionBuilder.append(" or after ").append(votesNeeded).append(" more vote");
201202
descriptionBuilder.append(votesNeeded == 1 ? " is " : "s are ");
202203
descriptionBuilder.append("cast, whichever occurs first.");

nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -317,11 +317,6 @@
317317
<artifactId>spring-security-oauth2-jose</artifactId>
318318
<scope>provided</scope> <!-- expected to be provided by parent classloader -->
319319
</dependency>
320-
<dependency>
321-
<groupId>commons-codec</groupId>
322-
<artifactId>commons-codec</artifactId>
323-
<scope>provided</scope> <!-- expected to be provided by parent classloader -->
324-
</dependency>
325320
<dependency>
326321
<groupId>commons-io</groupId>
327322
<artifactId>commons-io</artifactId>

nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/dto/DtoFactory.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.apache.nifi.web.api.dto;
1818

1919
import jakarta.ws.rs.WebApplicationException;
20-
import org.apache.commons.codec.digest.DigestUtils;
2120
import org.apache.commons.lang3.ClassUtils;
2221
import org.apache.commons.lang3.StringUtils;
2322
import org.apache.nifi.action.Action;
@@ -178,6 +177,7 @@
178177
import org.apache.nifi.scheduling.SchedulingStrategy;
179178
import org.apache.nifi.util.FlowDifferenceFilters;
180179
import org.apache.nifi.util.FormatUtils;
180+
import org.apache.nifi.util.security.MessageDigestUtils;
181181
import org.apache.nifi.web.FlowModification;
182182
import org.apache.nifi.web.Revision;
183183
import org.apache.nifi.web.api.dto.SystemDiagnosticsSnapshotDTO.ResourceClaimDetailsDTO;
@@ -260,6 +260,7 @@
260260
import org.slf4j.LoggerFactory;
261261

262262
import java.io.File;
263+
import java.nio.charset.StandardCharsets;
263264
import java.text.Collator;
264265
import java.text.NumberFormat;
265266
import java.util.ArrayList;
@@ -270,6 +271,7 @@
270271
import java.util.Date;
271272
import java.util.HashMap;
272273
import java.util.HashSet;
274+
import java.util.HexFormat;
273275
import java.util.Iterator;
274276
import java.util.LinkedHashMap;
275277
import java.util.LinkedHashSet;
@@ -4117,7 +4119,7 @@ private JVMDiagnosticsSnapshotDTO createJvmDiagnosticsSnapshotDto(final FlowCont
41174119
final RepositoryUsageDTO usageDto = new RepositoryUsageDTO();
41184120
usageDto.setName(repoName);
41194121

4120-
usageDto.setFileStoreHash(DigestUtils.sha256Hex(flowController.getContentRepoFileStoreName(repoName)));
4122+
usageDto.setFileStoreHash(getDigest(flowController.getContentRepoFileStoreName(repoName)));
41214123
usageDto.setFreeSpace(FormatUtils.formatDataSize(usage.getFreeSpace()));
41224124
usageDto.setFreeSpaceBytes(usage.getFreeSpace());
41234125
usageDto.setTotalSpace(FormatUtils.formatDataSize(usage.getTotalSpace()));
@@ -4137,7 +4139,7 @@ private JVMDiagnosticsSnapshotDTO createJvmDiagnosticsSnapshotDto(final FlowCont
41374139
final RepositoryUsageDTO usageDto = new RepositoryUsageDTO();
41384140
usageDto.setName(repoName);
41394141

4140-
usageDto.setFileStoreHash(DigestUtils.sha256Hex(flowController.getProvenanceRepoFileStoreName(repoName)));
4142+
usageDto.setFileStoreHash(getDigest(flowController.getProvenanceRepoFileStoreName(repoName)));
41414143
usageDto.setFreeSpace(FormatUtils.formatDataSize(usage.getFreeSpace()));
41424144
usageDto.setFreeSpaceBytes(usage.getFreeSpace());
41434145
usageDto.setTotalSpace(FormatUtils.formatDataSize(usage.getTotalSpace()));
@@ -4156,7 +4158,7 @@ private JVMDiagnosticsSnapshotDTO createJvmDiagnosticsSnapshotDto(final FlowCont
41564158

41574159
flowFileRepoUsage.setName(repoName);
41584160

4159-
flowFileRepoUsage.setFileStoreHash(DigestUtils.sha256Hex(flowController.getFlowRepoFileStoreName()));
4161+
flowFileRepoUsage.setFileStoreHash(getDigest(flowController.getFlowRepoFileStoreName()));
41604162
flowFileRepoUsage.setFreeSpace(FormatUtils.formatDataSize(usage.getFreeSpace()));
41614163
flowFileRepoUsage.setFreeSpaceBytes(usage.getFreeSpace());
41624164
flowFileRepoUsage.setTotalSpace(FormatUtils.formatDataSize(usage.getTotalSpace()));
@@ -5209,4 +5211,10 @@ private ProcessingPerformanceStatusDTO createProcessingPerformanceStatusDTO(fina
52095211

52105212
return performanceStatusDTO;
52115213
}
5214+
5215+
private String getDigest(final String name) {
5216+
final byte[] bytes = name.getBytes(StandardCharsets.UTF_8);
5217+
final byte[] digest = MessageDigestUtils.getDigest(bytes);
5218+
return HexFormat.of().formatHex(digest);
5219+
}
52125220
}

0 commit comments

Comments
 (0)