Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added RPC invocation for PreUpdate #443

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions dao-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
compile externalDependency.reflections
compile externalDependency.commonsLang
implementation 'com.google.protobuf:protobuf-java:3.21.1'
implementation 'io.grpc:grpc-stub:1.57.0'
dataModel project(':core-models')
dataModel project(':validators')

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.linkedin.metadata.dao;

import com.google.common.annotations.VisibleForTesting;
import com.google.protobuf.Descriptors;
import com.google.protobuf.Message;
import com.linkedin.common.AuditStamp;
import com.linkedin.common.urn.Urn;
Expand Down Expand Up @@ -47,6 +48,9 @@
import com.linkedin.metadata.query.IndexFilter;
import com.linkedin.metadata.query.IndexGroupByCriterion;
import com.linkedin.metadata.query.IndexSortCriterion;
import io.grpc.stub.AbstractStub;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Timestamp;
import java.time.Clock;
import java.util.Collections;
Expand Down Expand Up @@ -215,6 +219,8 @@ public static class AspectUpdateLambda<ASPECT extends RecordTemplate> {

private Clock _clock = Clock.systemUTC();

private Map<Class<? extends RecordTemplate>, Object[]> _grpcPreUpdateRoutingMap;

/**
* Constructor for BaseLocalDAO.
*
Expand Down Expand Up @@ -414,6 +420,9 @@ public RestliPreUpdateAspectRegistry getRestliPreUpdateAspectRegistry() {
return _restliPreUpdateAspectRegistry;
}

public void setGrpcPreUpdateRoutingMap(Map<Class<? extends RecordTemplate>, Object[]> grpcPreUpdateRoutingMap) {
_grpcPreUpdateRoutingMap = grpcPreUpdateRoutingMap;
}

/**
* Enables or disables atomic updates of multiple aspects.
Expand Down Expand Up @@ -854,6 +863,9 @@ public <ASPECT extends RecordTemplate> ASPECT add(@Nonnull URN urn, @Nonnull ASP
@Nonnull AuditStamp auditStamp, @Nullable IngestionTrackingContext trackingContext,
@Nullable IngestionParams ingestionParams) {
ASPECT updatedAspect = preUpdateRouting(urn, newValue);
rakhiagr marked this conversation as resolved.
Show resolved Hide resolved
if (_grpcPreUpdateRoutingMap != null && _grpcPreUpdateRoutingMap.containsKey(newValue.getClass())) {
updatedAspect = grpcPreUpdateRouting(urn, updatedAspect);
}
return rawAdd(urn, updatedAspect, auditStamp, trackingContext, ingestionParams);
}

Expand Down Expand Up @@ -1675,4 +1687,40 @@ protected <ASPECT extends RecordTemplate> ASPECT preUpdateRouting(URN urn, ASPEC
}
return newValue;
}
}

rakhiagr marked this conversation as resolved.
Show resolved Hide resolved
protected <ASPECT extends RecordTemplate> ASPECT grpcPreUpdateRouting(URN urn, ASPECT aspect) {
rakhiagr marked this conversation as resolved.
Show resolved Hide resolved
Object[] routingData = _grpcPreUpdateRoutingMap.get(aspect.getClass());
rakhiagr marked this conversation as resolved.
Show resolved Hide resolved
// Extract stored information
AbstractStub<?> stub = (AbstractStub<?>) routingData[0];
Method newBuilderMethod = (Method) routingData[1]; // Method for newBuilder()
Method preUpdateMethod = (Method) routingData[2]; // Method for gRPC call

// Dynamically create the request using newBuilder method
try {
Message.Builder requestBuilder = (Message.Builder) newBuilderMethod.invoke(null);
rakhiagr marked this conversation as resolved.
Show resolved Hide resolved

// Get the descriptor for the request type
Descriptors.Descriptor requestDescriptor = requestBuilder.getDescriptorForType();

// Find the "urn" field descriptor and set its value
Descriptors.FieldDescriptor urnField = requestDescriptor.findFieldByName("urn");
requestBuilder.setField(urnField, urn);

// Find the "value" field descriptor and set its value (this is the aspect in your case)
Descriptors.FieldDescriptor valueField = requestDescriptor.findFieldByName("value");
requestBuilder.setField(valueField, aspect);

// Build the request object
Message request = requestBuilder.build();

// Invoke the gRPC preUpdate method
Object response = preUpdateMethod.invoke(stub, request);
// Extract and return the updated aspect from the response
Descriptors.Descriptor responseDescriptor = ((Message) response).getDescriptorForType();
log.info("PreUpdateRouting completed in BaseLocalDao, urn: {}, updated aspect: {}", urn, response);
rakhiagr marked this conversation as resolved.
Show resolved Hide resolved
return (ASPECT) ((Message) response).getField(responseDescriptor.findFieldByName("value"));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
rakhiagr marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Loading