Skip to content

GH-2400: ReplyingKT Improve CorrelationId Logging #2401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2018-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,23 +16,25 @@

package org.springframework.kafka.requestreply;

import java.math.BigInteger;
import java.util.Arrays;

import org.springframework.util.Assert;

/**
* Wrapper for byte[] that can be used as a hash key. We could have used BigInteger
* instead but this wrapper is much less expensive. We do use a BigInteger in
* {@link #toString()} though.
* instead but this wrapper is much less expensive.
*
* @author Gary Russell
* @since 2.1.3
*/
public final class CorrelationKey {

private static final char[] HEX_ARRAY = "0123456789abcdef".toCharArray();

private final byte[] correlationId;

private String asString;

private volatile Integer hashCode;

public CorrelationKey(byte[] correlationId) { // NOSONAR array reference
Expand Down Expand Up @@ -74,9 +76,27 @@ public boolean equals(Object obj) {
return true;
}

private static String bytesToHex(byte[] bytes) {
boolean uuid = bytes.length == 16;
char[] hexChars = new char[bytes.length * 2 + (uuid ? 4 : 0)];
int i = 0;
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[i++] = HEX_ARRAY[v >>> 4];
hexChars[i++] = HEX_ARRAY[v & 0x0F];
if (uuid && (j == 3 || j == 5 || j == 7 || j == 9)) {
hexChars[i++] = '-';
}
}
return new String(hexChars);
}

@Override
public String toString() {
return "[" + new BigInteger(this.correlationId) + "]";
if (this.asString == null) {
this.asString = bytesToHex(this.correlationId);
}
return this.asString;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed 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
*
* https://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.springframework.kafka.requestreply;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;

/**
* @author Gary Russell
* @since 2.8.10
*
*/
public class CorrelationKeyTests {

@Test
void asString() {
CorrelationKey key = new CorrelationKey(new byte[16]);
assertThat(key.toString()).isEqualTo("00000000-0000-0000-0000-000000000000");
key = new CorrelationKey(new byte[10]);
assertThat(key.toString()).isEqualTo("00000000000000000000");
}

}