Add @RestrictedApi to D2ClientBuilder constructor and build()#1155
Draft
singhsanjay12 wants to merge 1 commit intolinkedin:masterfrom
Draft
Add @RestrictedApi to D2ClientBuilder constructor and build()#1155singhsanjay12 wants to merge 1 commit intolinkedin:masterfrom
singhsanjay12 wants to merge 1 commit intolinkedin:masterfrom
Conversation
a46e4cd to
e7ff079
Compare
Annotate D2ClientBuilder's constructor and build() method with Error
Prone's @RestrictedApi to produce compile-time errors when callers
instantiate or invoke the builder directly, outside of blessed paths.
Why:
- D2ClientBuilder is a raw, low-level API. LinkedIn container apps
must use D2ClientFactory instead, which auto-applies INDIS config,
future migrations, and platform features.
- Runtime LOG.warn / LOG.error already exist but are easy to miss.
A compile-time checker closes the feedback loop earlier.
Changes:
- build.gradle: add error_prone_annotations 2.26.1 to externalDependency
- d2/build.gradle: wire in compileOnly errorProneAnnotations
- AllowRawD2ClientBuilder.java: new opt-in marker annotation with a
required 'reason' field, for customers that legitimately need direct
access during migration
- D2ClientBuilder.java:
- import RestrictedApi
- explicit no-arg constructor annotated with @RestrictedApi
- build() annotated with @RestrictedApi
- both annotations allowlist D2ClientFactory.java, test paths,
@AllowRawD2ClientBuilder, and @SuppressWarnings('RestrictedApiChecker')
Allowed callers:
allowedOnPath : .*/factory/D2ClientFactory\.java | .*/test/.*
allowlistAnnotations: @AllowRawD2ClientBuilder (with required reason),
@SuppressWarnings('RestrictedApiChecker')
Migration tracking:
Search cu:AllowRawD2ClientBuilder in Jarvis to audit remaining
exceptions and drive them to zero over time.
e7ff079 to
552dbec
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Annotate
D2ClientBuilder's constructor andbuild()method with Error Prone's@RestrictedApito produce compile-time errors when LinkedIn-internal callers instantiate or invoke the builder directly outside of blessed paths. External open-source callers are unaffected.Motivation
D2ClientBuilderis a raw, low-level API. LinkedIn container apps must useD2ClientFactoryinstead, which automatically applies INDIS configuration, future platform migrations, and framework features.A
LOG.warn/LOG.errorat runtime already exists but:A compile-time Error Prone checker closes the feedback loop at build time.
Changes
build.gradle(root)errorProneAnnotations: 'com.google.errorprone:error_prone_annotations:2.26.1'toexternalDependencyd2/build.gradlecompileOnly externalDependency.errorProneAnnotationsAllowRawD2ClientBuilder.java(new)reason()field to force documentation at every exception@Retention(CLASS)— compile-time only, no runtime cost@Target({TYPE, METHOD, CONSTRUCTOR})— callers can be surgical about scopeD2ClientBuilder.javaimport com.google.errorprone.annotations.RestrictedApi@RestrictedApibuild()with@RestrictedApiAllowed callers
com/linkedin?D2ClientFactory.javatest/fileorg.foo,com.example, etc.)^(?!.*com/linkedin).*$)com.linkedin.container)@AllowRawD2ClientBuilder@SuppressWarnings("RestrictedApiChecker")allowedOnPathbreakdownMigration tracking
Search
cu:AllowRawD2ClientBuilderin Jarvis codesearch to get a live inventory of remaining exceptions and drive them to zero over time.Testing & Verification
@RestrictedApiis enforced by Error Prone'sRestrictedApiChecker— it requires thenet.ltgt.errorproneGradle plugin to be configured as the compiler plugin. Without it, the annotation compiles cleanly but is not enforced. This PR adds the annotation and allowlist infrastructure; wiring in the Gradle plugin is the necessary follow-up to activate enforcement.Local demo — all 6 cases verified
A standalone project with Error Prone configured was used to verify all enforcement scenarios end-to-end:
com/linkedin/demo/BadCaller.java[RestrictedApi]errororg/external/ExternalUser.javacom/linkedin/demo/AllowedInternalCaller.java@AllowRawD2ClientBuildercom/linkedin/demo/SuppressedCaller.java@SuppressWarningscom/linkedin/factory/D2ClientFactory.javacom/linkedin/test/D2ClientBuilderTest.javatest/pathCase 1 — LinkedIn-internal caller blocked:
Case 2 — External open-source caller allowed (no annotation needed):
Case 3 — LinkedIn-internal with
@AllowRawD2ClientBuilderallowed:D2ClientBuilderTest— passes locally (Java 11, Gradle 6.9.4)Related
D2ClientFactoryinpegasus-d2-client-factorynet.ltgt.errorproneGradle plugin to activate compile-time enforcement