Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,12 @@
import java.util.List;
import java.util.Map;
import org.apache.beam.sdk.options.PipelineOptions;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Construct an oauth credential to be used by the SDK and the SDK workers. Always returns a null
* Credential object.
*/
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public class NoopCredentialFactory implements CredentialFactory {
private static final NoopCredentialFactory INSTANCE = new NoopCredentialFactory();
private static final NoopCredentials NOOP_CREDENTIALS = new NoopCredentials();
Expand All @@ -45,13 +43,18 @@ public Credentials getCredential() throws IOException {
}

private static class NoopCredentials extends Credentials {
// google-auth Credentials is not annotated for nullness; these overrides intentionally
// return null (this is a no-op credential), which its API permits.
@Override
public String getAuthenticationType() {
@SuppressWarnings("nullness")
public @Nullable String getAuthenticationType() {
return null;
}

@Override
public Map<String, List<String>> getRequestMetadata(URI uri) throws IOException {
@SuppressWarnings("nullness")
public @Nullable Map<String, List<String>> getRequestMetadata(@Nullable URI uri)
throws IOException {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.beam.sdk.extensions.gcp.options;

import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Strings.isNullOrEmpty;

Expand All @@ -42,7 +43,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.beam.sdk.extensions.gcp.auth.CredentialFactory;
Expand Down Expand Up @@ -80,9 +80,6 @@
* mechanisms for creating credentials.
*/
@Description("Options used to configure Google Cloud Platform project and credentials.")
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public interface GcpOptions extends GoogleApiDebugOptions, PipelineOptions {
/** Project id to use when launching jobs. */
@Description(
Expand Down Expand Up @@ -248,11 +245,11 @@ public List<String> create(PipelineOptions options) {
* Attempts to infer the default project based upon the environment this application is executing
* within. Currently this only supports getting the active project from gcloud.
*/
class DefaultProjectFactory implements DefaultValueFactory<String> {
class DefaultProjectFactory implements DefaultValueFactory<@Nullable String> {
private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectFactory.class);

@Override
public String create(PipelineOptions options) {
public @Nullable String create(PipelineOptions options) {
try {
File configFile;
if (getEnvironment().containsKey("CLOUDSDK_CONFIG")) {
Expand Down Expand Up @@ -293,7 +290,7 @@ public String create(PipelineOptions options) {
} else if (section == null || "core".equals(section)) {
matcher = projectPattern.matcher(line);
if (matcher.matches()) {
String project = matcher.group(1).trim();
String project = checkArgumentNotNull(matcher.group(1)).trim();
LOG.info(
"Inferred default GCP project '{}' from gcloud. If this is the incorrect "
+ "project, please cancel this Pipeline and specify the command-line "
Expand Down Expand Up @@ -326,9 +323,9 @@ Map<String, String> getEnvironment() {
* Attempts to load the GCP credentials. See {@link CredentialFactory#getCredential()} for more
* details.
*/
class GcpUserCredentialsFactory implements DefaultValueFactory<Credentials> {
class GcpUserCredentialsFactory implements DefaultValueFactory<@Nullable Credentials> {
@Override
public Credentials create(PipelineOptions options) {
public @Nullable Credentials create(PipelineOptions options) {
GcpOptions gcpOptions = options.as(GcpOptions.class);
try {
CredentialFactory factory =
Expand Down Expand Up @@ -365,7 +362,7 @@ public Boolean create(PipelineOptions options) {
void setGcpTempLocation(String value);

/** Returns {@link PipelineOptions#getTempLocation} as the default GCP temp location. */
class GcpTempLocationFactory implements DefaultValueFactory<String> {
class GcpTempLocationFactory implements DefaultValueFactory<@Nullable String> {
private static final FluentBackoff BACKOFF_FACTORY =
FluentBackoff.DEFAULT.withMaxRetries(3).withInitialBackoff(Duration.millis(200));
static final String DEFAULT_REGION = "us-central1";
Expand Down Expand Up @@ -415,7 +412,7 @@ static boolean isSoftDeletePolicyEnabled(PipelineOptions options, String tempLoc
GcsUtil gcsUtil = gcsOptions.getGcsUtil();
try {
SoftDeletePolicy policy =
Objects.requireNonNull(gcsUtil.getBucket(GcsPath.fromUri(tempLocation)))
checkArgumentNotNull(gcsUtil.getBucket(GcsPath.fromUri(tempLocation)))
.getSoftDeletePolicy();
if (policy != null && policy.getRetentionDurationSeconds() > 0) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*/
package org.apache.beam.sdk.extensions.gcp.options;

import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;

import com.google.api.client.googleapis.services.AbstractGoogleClient;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
Expand All @@ -25,13 +27,11 @@
import java.util.Map;
import org.apache.beam.sdk.options.Description;
import org.apache.beam.sdk.options.PipelineOptions;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* These options configure debug settings for Google API clients created within the Apache Beam SDK.
*/
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public interface GoogleApiDebugOptions extends PipelineOptions {
/**
* This option enables tracing of API calls to Google services used within the Apache Beam SDK.
Expand Down Expand Up @@ -67,7 +67,7 @@ class GoogleApiTracer extends HashMap<String, String> implements GoogleClientReq
* given client type.
*/
public GoogleApiTracer addTraceFor(AbstractGoogleClient client, String traceDestination) {
put(client.getClass().getCanonicalName(), traceDestination);
put(checkArgumentNotNull(client.getClass().getCanonicalName()), traceDestination);
return this;
}

Expand All @@ -77,14 +77,15 @@ public GoogleApiTracer addTraceFor(AbstractGoogleClient client, String traceDest
*/
public GoogleApiTracer addTraceFor(
AbstractGoogleClientRequest<?> request, String traceDestination) {
put(request.getClass().getCanonicalName(), traceDestination);
put(checkArgumentNotNull(request.getClass().getCanonicalName()), traceDestination);
return this;
}

@Override
public void initialize(AbstractGoogleClientRequest<?> request) throws IOException {
@Nullable String requestClassName = request.getClass().getCanonicalName();
for (Map.Entry<String, String> entry : this.entrySet()) {
if (request.getClass().getCanonicalName().contains(entry.getKey())) {
if (requestClassName != null && requestClassName.contains(entry.getKey())) {
request.set("$trace", entry.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package org.apache.beam.sdk.extensions.gcp.storage;

import static org.apache.beam.sdk.io.FileSystemUtils.wildcardToRegexp;
import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.MoreObjects.firstNonNull;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;
Expand Down Expand Up @@ -69,9 +71,6 @@
* href="https://github.com/apache/beam/blob/master/sdks/java/extensions/google-cloud-platform-core/OWNERS">
* here</a>.
*/
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
class GcsFileSystem extends FileSystem<GcsResourceId> {
private static final Logger LOG = LoggerFactory.getLogger(GcsFileSystem.class);

Expand Down Expand Up @@ -183,7 +182,7 @@ protected WritableByteChannel create(GcsResourceId resourceId, CreateOptions cre
if (createOptions instanceof GcsCreateOptions) {
builder =
builder.setUploadBufferSizeBytes(
((GcsCreateOptions) createOptions).gcsUploadBufferSizeBytes());
checkArgumentNotNull(((GcsCreateOptions) createOptions).gcsUploadBufferSizeBytes()));
}
Stopwatch stopwatch = Stopwatch.createStarted();
try {
Expand Down Expand Up @@ -376,8 +375,9 @@ private MatchResult toMatchResult(StorageObjectOrIOException objectOrException)
} else if (exception != null) {
return MatchResult.create(Status.ERROR, exception);
} else {
StorageObject object = objectOrException.storageObject();
assert object != null; // fix a warning; guaranteed by StorageObjectOrIOException semantics.
// Guaranteed non-null by StorageObjectOrIOException semantics: exactly one of
// storageObject/ioException is set, and ioException was null above.
StorageObject object = checkStateNotNull(objectOrException.storageObject());
return MatchResult.create(Status.OK, ImmutableList.of(toMetadata(object)));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.beam.sdk.extensions.gcp.storage;

import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkNotNull;
import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState;
Expand Down Expand Up @@ -72,10 +73,9 @@ public GcsResourceId getCurrentDirectory() {
if (isDirectory()) {
return this;
} else {
GcsPath parent = gcsPath.getParent();
checkState(
parent != null,
String.format("Failed to get the current directory for path: [%s].", gcsPath));
GcsPath parent =
checkStateNotNull(
gcsPath.getParent(), "Failed to get the current directory for path: [%s].", gcsPath);
return fromGcsPath(parent);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* An optional component to use with the {@code RetryHttpRequestInitializer} in order to provide
Expand Down Expand Up @@ -63,9 +64,6 @@
*
* <p>
*/
@SuppressWarnings({
"nullness" // TODO(https://github.com/apache/beam/issues/20497)
})
public class CustomHttpErrors {

/**
Expand Down Expand Up @@ -133,7 +131,7 @@ private CustomHttpErrors(List<MatcherAndError> matchersAndLogs) {
}

/** Returns the first custom error for the failing request and response to match, or null. */
public String getCustomError(HttpRequestWrapper req, HttpResponseWrapper res) {
public @Nullable String getCustomError(HttpRequestWrapper req, HttpResponseWrapper res) {
for (MatcherAndError m : matchersAndLogs) {
if (m.getMatcher().matchResponse(req, res)) {
return m.getCustomError().customError(req, res);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ static String resolveGcpProjectId(@Nullable String projectId, @Nullable String c
try {
Class<?> clazz = Class.forName("com.google.cloud.ServiceOptions");
java.lang.reflect.Method method = clazz.getMethod("getDefaultProjectId");
// getDefaultProjectId is static, so passing a null receiver to Method.invoke is correct;
// the checker's stub for Method.invoke conservatively requires a non-null receiver.
@SuppressWarnings("nullness")
Object result = method.invoke(null);
if (result != null && !Strings.isNullOrEmpty(result.toString())) {
Expand Down
Loading
Loading