Skip to content

Commit 1e5b6bb

Browse files
cleaned up code
1 parent 1a95fdf commit 1e5b6bb

File tree

3 files changed

+104
-158
lines changed

3 files changed

+104
-158
lines changed

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

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,42 @@
55
import java.util.Arrays;
66
import java.util.concurrent.atomic.AtomicInteger;
77

8+
/**
9+
* Intended outcomes of KeyIdGenerator:
10+
* - Key ids are always consecutive, starting from 0
11+
* - When the last allocated key id reaches 16777215, the next key id will wrap around to 0
12+
* - Continuing to increment from the highest key id will result in monotonic incrementation of key ids for all newly rotated buckets
13+
*
14+
* Assumptions:
15+
* - The latest assigned key ids are from the latest updated buckets
16+
* - Key ids from these buckets will always be consecutive (apart from wraparound) as they have not rotated again after last assignment
17+
*/
818
public class KeyIdGenerator {
919
private static final int MAX_KEY_ID = 16777215; // 3 bytes
1020
private final AtomicInteger lastActiveKeyId;
1121

12-
public KeyIdGenerator(SaltEntry[] salts) {
13-
this.lastActiveKeyId = new AtomicInteger(getLastActiveKeyId(salts));
22+
public KeyIdGenerator(SaltEntry[] buckets) {
23+
this.lastActiveKeyId = new AtomicInteger(getLastActiveKeyId(buckets));
1424
}
1525

16-
private int getLastActiveKeyId(SaltEntry[] salts) {
17-
long maxLastUpdated = Arrays.stream(salts).mapToLong(SaltEntry::lastUpdated).max().orElse(0);
18-
int[] lastActiveKeyIds = Arrays.stream(salts)
26+
private static int getLastActiveKeyId(SaltEntry[] buckets) {
27+
long maxLastUpdated = Arrays.stream(buckets).mapToLong(SaltEntry::lastUpdated).max().orElse(0);
28+
int[] lastActiveKeyIdsSorted = Arrays.stream(buckets)
1929
.filter(s -> s.lastUpdated() == maxLastUpdated && s.currentKey() != null)
2030
.mapToInt(s -> s.currentKey().id())
2131
.sorted()
2232
.toArray();
2333

24-
if (lastActiveKeyIds.length == 0) return MAX_KEY_ID; // so that next ID will start at 0
34+
if (lastActiveKeyIdsSorted.length == 0) return MAX_KEY_ID; // so that next ID will start at 0
2535

26-
int highestId = lastActiveKeyIds[lastActiveKeyIds.length - 1];
36+
int highestId = lastActiveKeyIdsSorted[lastActiveKeyIdsSorted.length - 1];
2737

2838
if (highestId < MAX_KEY_ID) return highestId;
2939

30-
// Wrapped case - find the last consecutive ID
31-
for (int i = 0; i < lastActiveKeyIds.length - 1; i++) {
32-
if (lastActiveKeyIds[i + 1] - lastActiveKeyIds[i] > 1) {
33-
return lastActiveKeyIds[i];
40+
// Wrapped case - find the last consecutive ID from 0
41+
for (int i = 0; i < lastActiveKeyIdsSorted.length - 1; i++) {
42+
if (lastActiveKeyIdsSorted[i + 1] - lastActiveKeyIdsSorted[i] > 1) {
43+
return lastActiveKeyIdsSorted[i];
3444
}
3545
}
3646

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

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,23 @@ private SaltEntry[] rotateSalts(SaltEntry[] oldSalts, List<SaltEntry> saltsToRot
115115
return updatedSalts;
116116
}
117117

118-
private SaltEntry updateSalt(SaltEntry oldSalt, TargetDate targetDate, boolean shouldRotate, KeyIdGenerator keyIdGenerator) throws Exception {
119-
var lastUpdated = shouldRotate ? targetDate.asEpochMs() : oldSalt.lastUpdated();
120-
var refreshFrom = calculateRefreshFrom(oldSalt, targetDate);
121-
var currentSalt = calculateCurrentSalt(oldSalt, shouldRotate);
122-
var previousSalt = calculatePreviousSalt(oldSalt, shouldRotate, targetDate);
123-
var currentKey = calculateCurrentKey(oldSalt, shouldRotate, keyIdGenerator);
124-
var previousKey = calculatePreviousKey(oldSalt,shouldRotate, targetDate);
118+
private SaltEntry updateSalt(SaltEntry oldBucket, TargetDate targetDate, boolean shouldRotate, KeyIdGenerator keyIdGenerator) throws Exception {
119+
var lastUpdated = shouldRotate ? targetDate.asEpochMs() : oldBucket.lastUpdated();
120+
var refreshFrom = calculateRefreshFrom(oldBucket, targetDate);
121+
var currentSalt = calculateCurrentSalt(oldBucket, shouldRotate);
122+
var previousSalt = calculatePreviousSalt(oldBucket, shouldRotate, targetDate);
123+
var currentKeySalt = calculateCurrentKeySalt(oldBucket, shouldRotate, keyIdGenerator);
124+
var previousKeySalt = calculatePreviousKeySalt(oldBucket,shouldRotate, targetDate);
125125

126126
return new SaltEntry(
127-
oldSalt.id(),
128-
oldSalt.hashedId(),
127+
oldBucket.id(),
128+
oldBucket.hashedId(),
129129
lastUpdated,
130130
currentSalt,
131131
refreshFrom,
132132
previousSalt,
133-
currentKey,
134-
previousKey
133+
currentKeySalt,
134+
previousKeySalt
135135
);
136136
}
137137

@@ -141,9 +141,15 @@ private long calculateRefreshFrom(SaltEntry salt, TargetDate targetDate) {
141141
}
142142

143143
private String calculateCurrentSalt(SaltEntry salt, boolean shouldRotate) throws Exception {
144-
return shouldRotate ?
145-
ENABLE_V4_RAW_UID ? null : this.keyGenerator.generateRandomKeyString(32)
146-
: salt.currentSalt();
144+
if (shouldRotate) {
145+
if (ENABLE_V4_RAW_UID) {
146+
return null;
147+
}
148+
else {
149+
return this.keyGenerator.generateRandomKeyString(32);
150+
}
151+
}
152+
return salt.currentSalt();
147153
}
148154

149155
private String calculatePreviousSalt(SaltEntry salt, boolean shouldRotate, TargetDate targetDate) {
@@ -156,7 +162,7 @@ private String calculatePreviousSalt(SaltEntry salt, boolean shouldRotate, Targe
156162
return null;
157163
}
158164

159-
private SaltEntry.KeyMaterial calculateCurrentKey(SaltEntry salt, boolean shouldRotate, KeyIdGenerator keyIdGenerator) throws Exception {
165+
private SaltEntry.KeyMaterial calculateCurrentKeySalt(SaltEntry bucket, boolean shouldRotate, KeyIdGenerator keyIdGenerator) throws Exception {
160166
if (shouldRotate) {
161167
if (ENABLE_V4_RAW_UID) {
162168
return new SaltEntry.KeyMaterial(
@@ -168,15 +174,15 @@ private SaltEntry.KeyMaterial calculateCurrentKey(SaltEntry salt, boolean should
168174
return null;
169175
}
170176
}
171-
return salt.currentKey();
177+
return bucket.currentKey();
172178
}
173179

174-
private SaltEntry.KeyMaterial calculatePreviousKey(SaltEntry salt, boolean shouldRotate, TargetDate targetDate) {
180+
private SaltEntry.KeyMaterial calculatePreviousKeySalt(SaltEntry bucket, boolean shouldRotate, TargetDate targetDate) {
175181
if (shouldRotate) {
176-
return salt.currentKey();
182+
return bucket.currentKey();
177183
}
178-
if (targetDate.saltAgeInDays(salt) < 90) {
179-
return salt.previousKey();
184+
if (targetDate.saltAgeInDays(bucket) < 90) {
185+
return bucket.previousKey();
180186
}
181187
return null;
182188
}

0 commit comments

Comments
 (0)