Skip to content

Commit 274988b

Browse files
committed
Use subspace(...).unpack(..)
1 parent 85db65a commit 274988b

File tree

3 files changed

+24
-33
lines changed

3 files changed

+24
-33
lines changed

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/IndexingBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ private CompletableFuture<Void> setIndexingTypeOrThrow(FDBRecordStore store, boo
364364
heartbeat = new IndexingHeartbeat(common.getIndexerId(), indexingTypeStamp.getMethod(), common.config.getLeaseLengthMillis());
365365

366366
return forEachTargetIndex(index -> setIndexingTypeOrThrow(store, continuedBuild, index, indexingTypeStamp)
367-
.thenApply(ignore -> updateHeartbeat(true, store, index)));
367+
.thenCompose(ignore -> updateHeartbeat(true, store, index)));
368368
}
369369

370370
@Nonnull

fdb-record-layer-core/src/main/java/com/apple/foundationdb/record/provider/foundationdb/IndexingHeartbeat.java

Lines changed: 23 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import com.apple.foundationdb.record.logging.LogMessageKeys;
2828
import com.apple.foundationdb.record.metadata.Index;
2929
import com.apple.foundationdb.synchronizedsession.SynchronizedSessionLockedException;
30-
import com.apple.foundationdb.tuple.Tuple;
3130
import com.google.protobuf.InvalidProtocolBufferException;
3231

3332
import javax.annotation.Nonnull;
@@ -73,12 +72,29 @@ public CompletableFuture<Void> checkAndUpdateHeartbeat(@Nonnull FDBRecordStore s
7372
case MULTI_TARGET_BY_RECORDS:
7473
case BY_INDEX:
7574
final AsyncIterator<KeyValue> iterator = heartbeatsIterator(store, index);
75+
final long now = nowMilliseconds();
7676
return AsyncUtil.whileTrue(() -> iterator.onHasNext()
7777
.thenApply(hasNext -> {
7878
if (!hasNext) {
7979
return false;
8080
}
81-
validateNonCompetingHeartbeat(iterator.next(), nowMilliseconds());
81+
final KeyValue kv = iterator.next();
82+
try {
83+
final UUID otherIndexerId = heartbeatKeyToIndexerId(store, index, kv.getKey());
84+
if (!otherIndexerId.equals(this.indexerId)) {
85+
final IndexBuildProto.IndexBuildHeartbeat otherHeartbeat = IndexBuildProto.IndexBuildHeartbeat.parseFrom(kv.getValue());
86+
final long age = now - otherHeartbeat.getHeartbeatTimeMilliseconds();
87+
if (age > 0 && age < leaseLength) {
88+
throw new SynchronizedSessionLockedException("Failed to initialize the session because of an existing session in progress")
89+
.addLogInfo(LogMessageKeys.INDEXER_ID, indexerId)
90+
.addLogInfo(LogMessageKeys.EXISTING_INDEXER_ID, otherIndexerId)
91+
.addLogInfo(LogMessageKeys.AGE_MILLISECONDS, age)
92+
.addLogInfo(LogMessageKeys.TIME_LIMIT_MILLIS, leaseLength);
93+
}
94+
}
95+
} catch (InvalidProtocolBufferException e) {
96+
throw new RuntimeException(e);
97+
}
8298
return true;
8399
}))
84100
.thenApply(ignore -> {
@@ -92,29 +108,6 @@ public CompletableFuture<Void> checkAndUpdateHeartbeat(@Nonnull FDBRecordStore s
92108
}
93109
}
94110

95-
private void validateNonCompetingHeartbeat(KeyValue kv, long now) {
96-
final Tuple keyTuple = Tuple.fromBytes(kv.getKey());
97-
if (keyTuple.size() < 2) { // expecting 8
98-
return;
99-
}
100-
final UUID otherIndexerId = keyTuple.getUUID(keyTuple.size() - 1);
101-
if (!otherIndexerId.equals(this.indexerId)) {
102-
try {
103-
final IndexBuildProto.IndexBuildHeartbeat otherHeartbeat = IndexBuildProto.IndexBuildHeartbeat.parseFrom(kv.getValue());
104-
final long age = now - otherHeartbeat.getHeartbeatTimeMilliseconds();
105-
if (age > 0 && age < leaseLength) {
106-
throw new SynchronizedSessionLockedException("Failed to initialize the session because of an existing session in progress")
107-
.addLogInfo(LogMessageKeys.INDEXER_ID, indexerId)
108-
.addLogInfo(LogMessageKeys.EXISTING_INDEXER_ID, otherIndexerId)
109-
.addLogInfo(LogMessageKeys.AGE_MILLISECONDS, age)
110-
.addLogInfo(LogMessageKeys.TIME_LIMIT_MILLIS, leaseLength);
111-
}
112-
} catch (InvalidProtocolBufferException e) {
113-
throw new RuntimeException(e);
114-
}
115-
}
116-
}
117-
118111
public void clearHeartbeat(@Nonnull FDBRecordStore store, @Nonnull Index index) {
119112
store.ensureContextActive().clear(IndexingSubspaces.indexheartbeatSubspace(store, index, indexerId).pack());
120113
}
@@ -132,11 +125,7 @@ public static CompletableFuture<Map<UUID, IndexBuildProto.IndexBuildHeartbeat>>
132125
return false;
133126
}
134127
final KeyValue kv = iterator.next();
135-
final Tuple keyTuple = Tuple.fromBytes(kv.getKey());
136-
if (keyTuple.size() < 2) { // expecting 8
137-
return true; // ignore, next
138-
}
139-
final UUID otherIndexerId = keyTuple.getUUID(keyTuple.size() - 1);
128+
final UUID otherIndexerId = heartbeatKeyToIndexerId(store, index, kv.getKey());
140129
try {
141130
final IndexBuildProto.IndexBuildHeartbeat otherHeartbeat = IndexBuildProto.IndexBuildHeartbeat.parseFrom(kv.getValue());
142131
ret.put(otherIndexerId, otherHeartbeat);
@@ -183,10 +172,13 @@ public static CompletableFuture<Integer> clearIndexingHeartbeats(@Nonnull FDBRec
183172
.thenApply(ignore -> deleteCount.get());
184173
}
185174

186-
public static AsyncIterator<KeyValue> heartbeatsIterator(FDBRecordStore store, Index index) {
175+
private static AsyncIterator<KeyValue> heartbeatsIterator(FDBRecordStore store, Index index) {
187176
return store.getContext().ensureActive().snapshot().getRange(IndexingSubspaces.indexheartbeatSubspace(store, index).range()).iterator();
188177
}
189178

179+
private static UUID heartbeatKeyToIndexerId(FDBRecordStore store, Index index, byte[] key) {
180+
return IndexingSubspaces.indexheartbeatSubspace(store, index).unpack(key).getUUID(0);
181+
}
190182

191183
private static long nowMilliseconds() {
192184
return System.currentTimeMillis();

fdb-record-layer-core/src/test/java/com/apple/foundationdb/record/provider/foundationdb/OnlineIndexingHeartbeatTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
*/
4949
class OnlineIndexingHeartbeatTest extends OnlineIndexerTest {
5050

51-
5251
@Test
5352
void testHeartbeatLowLevel() {
5453
List<Index> indexes = new ArrayList<>();

0 commit comments

Comments
 (0)