Skip to content

Commit 8101bf1

Browse files
added logs for key bucket count
1 parent 1a95fdf commit 8101bf1

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/main/java/com/uid2/admin/salt/SaltRotation.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public Result rotateSalts(
6262
logSaltAges("refreshable-salts", targetDate, refreshableSalts);
6363
logSaltAges("rotated-salts", targetDate, saltsToRotate);
6464
logSaltAges("total-salts", targetDate, Arrays.asList(postRotationSalts));
65+
logKeyBucketCount(targetDate, preRotationSalts, postRotationSalts);
6566

6667
var nextSnapshot = new SaltSnapshot(
6768
nextEffective,
@@ -246,6 +247,20 @@ private void logSaltAges(String saltCountType, TargetDate targetDate, Collection
246247
}
247248
}
248249

250+
private void logKeyBucketCount(TargetDate targetDate, SaltEntry[] preRotationSalts, SaltEntry[] postRotationSalts) {
251+
int newKeyBucketCounter = 0;
252+
int totalKeyBucketCounter = 0;
253+
for (int i = 0; i < preRotationSalts.length && i < postRotationSalts.length; i++) {
254+
var oldSalt = preRotationSalts[i];
255+
var updatedSalt = postRotationSalts[i];
256+
257+
if (updatedSalt.currentKey() != null) totalKeyBucketCounter++;
258+
if (updatedSalt.currentKey() != null && oldSalt.currentSalt() != null) newKeyBucketCounter++;
259+
}
260+
LOGGER.info("salt_bucket_count_type={} target_date={} bucket_count={}", "new-key-buckets", targetDate, newKeyBucketCounter);
261+
LOGGER.info("salt_bucket_count_type={} target_date={} bucket_count={}", "total-key-buckets", targetDate, totalKeyBucketCounter);
262+
}
263+
249264
@Getter
250265
public static final class Result {
251266
private final SaltSnapshot snapshot; // can be null if new snapshot is not needed

src/test/java/com/uid2/admin/salt/SaltRotationTest.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ void testLogFewSaltAgesOnRotation() throws Exception {
389389
var minAges = new Duration[]{Duration.ofDays(30), Duration.ofDays(60)};
390390
saltRotation.rotateSalts(lastSnapshot, minAges, 0.4, targetDate());
391391

392-
var actual = appender.list.stream().map(Object::toString).collect(Collectors.toSet());
392+
var actual = appender.list.stream().map(Object::toString).filter(s -> s.contains("salt_count_type") || s.contains("Salt rotation complete")).collect(Collectors.toSet());
393393
assertThat(actual).isEqualTo(expected);
394394
}
395395

@@ -429,7 +429,7 @@ void testLogManySaltAgesOnRotation() throws Exception {
429429
var minAges = new Duration[]{Duration.ofDays(30), Duration.ofDays(60)};
430430
saltRotation.rotateSalts(lastSnapshot, minAges, 0.2, targetDate());
431431

432-
var actual = appender.list.stream().map(Object::toString).collect(Collectors.toSet());
432+
var actual = appender.list.stream().map(Object::toString).filter(s -> s.contains("salt_count_type") || s.contains("Salt rotation complete")).collect(Collectors.toSet());
433433
assertThat(actual).isEqualTo(expected);
434434
}
435435

@@ -726,4 +726,35 @@ void testKeyRotationSaltToKeyRotation() throws Exception {
726726
assertThat(salts[0].currentSalt()).isNotNull();
727727
assertThat(salts[0].previousSalt()).isNull();
728728
}
729+
730+
@ParameterizedTest
731+
@CsvSource({
732+
"true, 1, 3",
733+
"false, 0, 1"
734+
})
735+
void testKeyRotationLogKeyBuckets(boolean v4Enabled, int expectedNewKeyBuckets, int expectedTotalKeyBuckets) throws Exception {
736+
saltRotation = new SaltRotation(keyGenerator, JsonObject.of(AdminConst.ENABLE_V4_RAW_UID, v4Enabled));
737+
738+
final Duration[] minAges = {
739+
Duration.ofDays(30)
740+
};
741+
742+
var willRefresh = targetDate();
743+
var willNotRefresh = targetDate().plusDays(30);
744+
var lastSnapshot = SaltSnapshotBuilder.start()
745+
.entries(SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willRefresh).currentSalt(),
746+
SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willNotRefresh).currentSalt(),
747+
SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willRefresh).currentKey(1),
748+
SaltBuilder.start().lastUpdated(targetDate().minusDays(60)).refreshFrom(willNotRefresh).currentKey(2))
749+
.build();
750+
751+
saltRotation.rotateSalts(lastSnapshot, minAges, 1, targetDate());
752+
753+
var expected = Set.of(
754+
"[INFO] salt_bucket_count_type=new-key-buckets target_date=2025-01-01 bucket_count=" + expectedNewKeyBuckets,
755+
"[INFO] salt_bucket_count_type=total-key-buckets target_date=2025-01-01 bucket_count=" + expectedTotalKeyBuckets
756+
);
757+
var actual = appender.list.stream().map(Object::toString).filter(s -> s.contains("salt_bucket_count_type")).collect(Collectors.toSet());
758+
assertThat(actual).isEqualTo(expected);
759+
}
729760
}

0 commit comments

Comments
 (0)