Skip to content

Commit 3daf161

Browse files
committed
fix: multitenancy must be handled when testing KafkaSqlUpgraderManager
1 parent ac8a0c4 commit 3daf161

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

storage/kafkasql/src/main/java/io/apicurio/registry/storage/impl/kafkasql/upgrade/KafkaSqlUpgrader.java

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
/**
88
* Implementors of this interface provide upgrade functionality for KafkaSQL storage.
99
* Please read the javadoc of {@link KafkaSqlUpgraderManager} as well.
10+
* <p>
11+
* Multitenancy considerations:
12+
* Methods on this class are not called with request context active.
13+
* Therefore, upgraders MUST NOT rely on multitenancy features, unless handled manually
14+
* (e.g. use @ActivateRequestContext and load a tenant manually).
15+
* As a result, they MUST be tenant agnostic.
1016
*/
1117
public interface KafkaSqlUpgrader extends AutoCloseable {
1218

storage/kafkasql/src/main/java/io/apicurio/registry/storage/impl/kafkasql/upgrade/KafkaSqlUpgraderManager.java

+25-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package io.apicurio.registry.storage.impl.kafkasql.upgrade;
1818

1919
import io.apicurio.common.apps.config.Info;
20+
import io.apicurio.common.apps.multitenancy.TenantContext;
21+
import io.apicurio.common.apps.multitenancy.TenantContextLoader;
2022
import io.apicurio.registry.exception.RuntimeAssertionFailedException;
2123
import io.apicurio.registry.exception.UnreachableCodeException;
2224
import io.apicurio.registry.storage.impl.kafkasql.KafkaSqlSubmitter;
@@ -26,7 +28,9 @@
2628
import io.apicurio.registry.storage.impl.kafkasql.values.ActionType;
2729
import io.apicurio.registry.storage.impl.kafkasql.values.MessageValue;
2830
import io.apicurio.registry.storage.impl.kafkasql.values.UpgraderValue;
31+
import io.apicurio.registry.utils.impexp.ContentEntity;
2932
import jakarta.enterprise.context.ApplicationScoped;
33+
import jakarta.enterprise.context.control.ActivateRequestContext;
3034
import jakarta.enterprise.inject.Instance;
3135
import jakarta.inject.Inject;
3236
import lombok.Getter;
@@ -103,6 +107,12 @@ public class KafkaSqlUpgraderManager {
103107
@Inject
104108
ThreadContext threadContext;
105109

110+
@Inject
111+
TenantContextLoader tcl;
112+
113+
@Inject
114+
TenantContext tctx;
115+
106116
/**
107117
* Unique ID of this upgrader, generated on each Registry start.
108118
*/
@@ -336,10 +346,11 @@ public synchronized void read(Instant currentTimestamp, MessageKey key, MessageV
336346
lockHeartbeat.heartbeat(); // Initialize heartbeat
337347
if (testMode) {
338348
try {
339-
var c = sqlStore.getContentEntityByContentId(2);
349+
var c = getContentEntityByContentIdForTest(2);
340350
log.debug("Content hash before: {}", c.contentHash);
341351
log.debug("Canonical content hash before: {}", c.canonicalHash);
342-
} catch (Exception ignored) {
352+
} catch (Exception ex) {
353+
log.error("Suppressing exception in TEST MODE", ex);
343354
}
344355
}
345356
for (KafkaSqlUpgrader u : activeUpgraders) {
@@ -357,10 +368,11 @@ public synchronized void read(Instant currentTimestamp, MessageKey key, MessageV
357368
}
358369
if (testMode) {
359370
try {
360-
var c = sqlStore.getContentEntityByContentId(2);
371+
var c = getContentEntityByContentIdForTest(2);
361372
log.debug("Content hash after: {}", c.contentHash);
362373
log.debug("Canonical content hash after: {}", c.canonicalHash);
363-
} catch (Exception ignored) {
374+
} catch (Exception ex) {
375+
log.error("Suppressing exception in TEST MODE", ex);
364376
}
365377
}
366378
}
@@ -527,6 +539,15 @@ private void updateLockMap(Instant timestamp, UpgraderValue value) {
527539
}
528540

529541

542+
@ActivateRequestContext
543+
synchronized ContentEntity getContentEntityByContentIdForTest(int contentId) {
544+
tctx.setContext(tcl.loadBatchJobContext(TenantContext.DEFAULT_TENANT_ID));
545+
var res = sqlStore.getContentEntityByContentId(contentId);
546+
tctx.clearContext();
547+
return res;
548+
}
549+
550+
530551
private enum State {
531552
WAIT_FOR_BOOTSTRAP,
532553
WAIT,

0 commit comments

Comments
 (0)