Skip to content

Commit

Permalink
Merge pull request #15629 from apache/revert-15608-dpcollins-cherry-pick
Browse files Browse the repository at this point in the history
  • Loading branch information
udim authored Oct 1, 2021
2 parents 1816cf4 + bb5ecd1 commit d916c1f
Show file tree
Hide file tree
Showing 25 changed files with 297 additions and 936 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class BeamModulePlugin implements Plugin<Project> {
def errorprone_version = "2.3.4"
def google_clients_version = "1.31.0"
def google_cloud_bigdataoss_version = "2.2.2"
def google_cloud_pubsublite_version = "1.0.4"
def google_cloud_pubsublite_version = "0.13.2"
def google_code_gson_version = "2.8.6"
def google_oauth_clients_version = "1.31.0"
// Try to keep grpc_version consistent with gRPC version in google_cloud_platform_libraries_bom
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,25 @@
import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;

/** Common util functions for converting between PubsubMessage proto and {@link PubsubMessage}. */
public final class PubsubMessages {
private PubsubMessages() {}

public static com.google.pubsub.v1.PubsubMessage toProto(PubsubMessage input) {
Map<String, String> attributes = input.getAttributeMap();
com.google.pubsub.v1.PubsubMessage.Builder message =
com.google.pubsub.v1.PubsubMessage.newBuilder()
.setData(ByteString.copyFrom(input.getPayload()));
// TODO(BEAM-8085) this should not be null
if (attributes != null) {
message.putAllAttributes(attributes);
}
String messageId = input.getMessageId();
if (messageId != null) {
message.setMessageId(messageId);
}
return message.build();
}

public static PubsubMessage fromProto(com.google.pubsub.v1.PubsubMessage input) {
return new PubsubMessage(
input.getData().toByteArray(), input.getAttributesMap(), input.getMessageId());
}

public class PubsubMessages {
// Convert the PubsubMessage to a PubsubMessage proto, then return its serialized representation.
public static class ParsePayloadAsPubsubMessageProto
implements SerializableFunction<PubsubMessage, byte[]> {
@Override
public byte[] apply(PubsubMessage input) {
return toProto(input).toByteArray();
Map<String, String> attributes = input.getAttributeMap();
com.google.pubsub.v1.PubsubMessage.Builder message =
com.google.pubsub.v1.PubsubMessage.newBuilder()
.setData(ByteString.copyFrom(input.getPayload()));
// TODO(BEAM-8085) this should not be null
if (attributes != null) {
message.putAllAttributes(attributes);
}
String messageId = input.getMessageId();
if (messageId != null) {
message.setMessageId(messageId);
}
return message.build().toByteArray();
}
}

Expand All @@ -65,7 +54,8 @@ public PubsubMessage apply(byte[] input) {
try {
com.google.pubsub.v1.PubsubMessage message =
com.google.pubsub.v1.PubsubMessage.parseFrom(input);
return fromProto(message);
return new PubsubMessage(
message.getData().toByteArray(), message.getAttributesMap(), message.getMessageId());
} catch (InvalidProtocolBufferException e) {
throw new RuntimeException("Could not decode Pubsub message", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.beam.sdk.io.gcp.pubsublite;

import static com.google.cloud.pubsublite.cloudpubsub.MessageTransforms.toCpsPublishTransformer;

import com.google.cloud.pubsublite.Message;
import com.google.cloud.pubsublite.proto.PubSubMessage;
import org.apache.beam.sdk.transforms.MapElements;
import org.apache.beam.sdk.transforms.PTransform;
import org.apache.beam.sdk.values.PCollection;
import org.apache.beam.sdk.values.TypeDescriptor;

/**
* A class providing a conversion validity check between Cloud Pub/Sub and Pub/Sub Lite message
* types.
*/
public final class CloudPubsubChecks {
private CloudPubsubChecks() {}

/**
* Ensure that all messages that pass through can be converted to Cloud Pub/Sub messages using the
* standard transformation methods in the client library.
*
* <p>Will fail the pipeline if a message has multiple attributes per key.
*/
public static PTransform<PCollection<? extends PubSubMessage>, PCollection<PubSubMessage>>
ensureUsableAsCloudPubsub() {
return MapElements.into(TypeDescriptor.of(PubSubMessage.class))
.via(
message -> {
Object unused = toCpsPublishTransformer().transform(Message.fromProto(message));
return message;
});
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit d916c1f

Please sign in to comment.