Skip to content

Commit db3acbf

Browse files
ml-metadata-teamtfx-copybara
authored andcommitted
Upsert artifacts before execution in a transaction of PutLineageSubgraph
PiperOrigin-RevId: 588505631
1 parent c6dc59d commit db3acbf

3 files changed

Lines changed: 126 additions & 12 deletions

File tree

ml_metadata/metadata_store/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ cc_library(
468468
"//ml_metadata/proto:metadata_store_proto",
469469
"//ml_metadata/proto:metadata_store_service_proto",
470470
"//ml_metadata/simple_types/proto:simple_types_proto",
471+
"//third_party/protobuf:protobuf_lite",
471472
],
472473
)
473474

ml_metadata/metadata_store/metadata_store.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,18 +1385,7 @@ absl::Status MetadataStore::PutLineageSubgraph(
13851385
response->add_context_ids(context_id);
13861386
}
13871387

1388-
// 2. Upsert executions.
1389-
for (const Execution& execution : request.executions()) {
1390-
int64_t execution_id = -1;
1391-
MLMD_RETURN_IF_ERROR(
1392-
UpsertExecution(execution, metadata_access_object_.get(),
1393-
/*skip_type_and_property_validation=*/true,
1394-
/*force_update_time=*/false,
1395-
google::protobuf::FieldMask(), &execution_id));
1396-
response->add_execution_ids(execution_id);
1397-
}
1398-
1399-
// 3. Upsert artifacts.
1388+
// 2. Upsert artifacts.
14001389
// Select the list of external_ids from Artifacts.
14011390
// Search within the db to create a mapping from external_id to id.
14021391
absl::flat_hash_map<std::string, int64_t> external_id_to_id_map;
@@ -1429,6 +1418,17 @@ absl::Status MetadataStore::PutLineageSubgraph(
14291418
response->add_artifact_ids(artifact_id);
14301419
}
14311420

1421+
// 3. Upsert executions.
1422+
for (const Execution& execution : request.executions()) {
1423+
int64_t execution_id = -1;
1424+
MLMD_RETURN_IF_ERROR(
1425+
UpsertExecution(execution, metadata_access_object_.get(),
1426+
/*skip_type_and_property_validation=*/true,
1427+
/*force_update_time=*/false,
1428+
google::protobuf::FieldMask(), &execution_id));
1429+
response->add_execution_ids(execution_id);
1430+
}
1431+
14321432
// 4. Create associations and attributions.
14331433
absl::flat_hash_set<int64_t> artifact_ids(
14341434
response->artifact_ids().begin(), response->artifact_ids().end());

ml_metadata/metadata_store/metadata_store_test_suite.cc

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ limitations under the License.
4040
#include "ml_metadata/proto/metadata_store.pb.h"
4141
#include "ml_metadata/proto/metadata_store_service.pb.h"
4242
#include "ml_metadata/simple_types/proto/simple_types.pb.h"
43+
#include "google/protobuf/repeated_ptr_field.h"
4344

4445
namespace ml_metadata {
4546
namespace testing {
@@ -7263,6 +7264,118 @@ TEST_P(MetadataStoreTestSuite, PutLineageSubgraphAndVerifyLineageGraph) {
72637264
/*ignore_fields=*/{"milliseconds_since_epoch"})));
72647265
}
72657266

7267+
// Test that PutLineageSubgraph Upsert artifacts before execution.
7268+
TEST_P(MetadataStoreTestSuite, PutLineageSubgraphWithNewArtifactsExecutions) {
7269+
// Prepare types and write them to MLMD.
7270+
PutTypesRequest put_types_request = ParseTextProtoOrDie<PutTypesRequest>(R"pb(
7271+
context_types: {
7272+
name: 'context_type'
7273+
properties { key: 'property_1' value: INT }
7274+
}
7275+
7276+
execution_types: {
7277+
name: 'execution_type'
7278+
properties { key: 'property_1' value: DOUBLE }
7279+
}
7280+
artifact_types: {
7281+
name: 'artifact_type'
7282+
properties { key: 'property_1' value: STRING }
7283+
}
7284+
)pb");
7285+
PutTypesResponse put_types_response;
7286+
ASSERT_EQ(absl::OkStatus(),
7287+
metadata_store_->PutTypes(put_types_request, &put_types_response));
7288+
7289+
// Prepare a context and write it to MLMD.
7290+
Context context;
7291+
context.set_type_id(put_types_response.context_type_ids(0));
7292+
context.set_name("context");
7293+
(*context.mutable_properties())["property_1"].set_int_value(1);
7294+
PutContextsRequest put_contexts_request;
7295+
*put_contexts_request.add_contexts() = context;
7296+
PutContextsResponse put_contexts_response;
7297+
ASSERT_EQ(absl::OkStatus(),
7298+
metadata_store_->PutContexts(put_contexts_request,
7299+
&put_contexts_response));
7300+
7301+
// Prepare a new execution and write it to MLMD.
7302+
Execution execution;
7303+
execution.set_type_id(put_types_response.execution_type_ids(0));
7304+
(*execution.mutable_properties())["property_1"].set_double_value(1.0);
7305+
PutExecutionRequest put_execution_request;
7306+
*put_execution_request.mutable_execution() = execution;
7307+
PutExecutionResponse put_execution_response;
7308+
ASSERT_EQ(absl::OkStatus(),
7309+
metadata_store_->PutExecution(put_execution_request,
7310+
&put_execution_response));
7311+
execution.set_id(put_execution_response.execution_id());
7312+
7313+
// Prepare a new artifact, but don't write it to MLMD.
7314+
Artifact artifact;
7315+
artifact.set_type_id(put_types_response.artifact_type_ids(0));
7316+
artifact.set_uri("testuri");
7317+
(*artifact.mutable_properties())["property_1"].set_string_value("1");
7318+
7319+
// Prepare the PutLineageSubgraph request
7320+
PutLineageSubgraphRequest put_lineage_subgraph_request;
7321+
*put_lineage_subgraph_request.add_executions() = execution;
7322+
*put_lineage_subgraph_request.add_artifacts() = artifact;
7323+
*put_lineage_subgraph_request.add_contexts() = context;
7324+
put_lineage_subgraph_request.mutable_options()
7325+
->set_reuse_context_if_already_exist(true);
7326+
7327+
// Prepare event_edge with execution_index and artifact_index but no
7328+
// execution_id and no artifact_id
7329+
Event event;
7330+
event.set_type(Event::OUTPUT);
7331+
PutLineageSubgraphRequest::EventEdge* event_edge =
7332+
put_lineage_subgraph_request.add_event_edges();
7333+
event_edge->set_execution_index(0);
7334+
event_edge->set_artifact_index(0);
7335+
*event_edge->mutable_event() = event;
7336+
7337+
PutLineageSubgraphResponse put_lineage_subgraph_response;
7338+
ASSERT_EQ(absl::OkStatus(),
7339+
metadata_store_->PutLineageSubgraph(
7340+
put_lineage_subgraph_request, &put_lineage_subgraph_response));
7341+
7342+
// Verify that new artifact and execution are correctly written.
7343+
GetExecutionsByContextRequest get_executions_by_context_request;
7344+
get_executions_by_context_request.set_context_id(
7345+
put_lineage_subgraph_response.context_ids(0));
7346+
GetExecutionsByContextResponse get_executions_by_context_response;
7347+
ASSERT_EQ(absl::OkStatus(), metadata_store_->GetExecutionsByContext(
7348+
get_executions_by_context_request,
7349+
&get_executions_by_context_response));
7350+
ASSERT_THAT(get_executions_by_context_response.executions(), SizeIs(1));
7351+
EXPECT_THAT(get_executions_by_context_response.executions(),
7352+
ElementsAre(EqualsProto(
7353+
execution,
7354+
/*ignore_fields=*/{"id", "type", "create_time_since_epoch",
7355+
"last_update_time_since_epoch"})));
7356+
7357+
GetArtifactsByContextRequest get_artifacts_by_context_request;
7358+
get_artifacts_by_context_request.set_context_id(
7359+
put_lineage_subgraph_response.context_ids(0));
7360+
GetArtifactsByContextResponse get_artifacts_by_context_response;
7361+
ASSERT_EQ(absl::OkStatus(), metadata_store_->GetArtifactsByContext(
7362+
get_artifacts_by_context_request,
7363+
&get_artifacts_by_context_response));
7364+
ASSERT_THAT(get_artifacts_by_context_response.artifacts(), SizeIs(1));
7365+
EXPECT_THAT(get_artifacts_by_context_response.artifacts(),
7366+
ElementsAre(EqualsProto(
7367+
artifact,
7368+
/*ignore_fields=*/{"id", "type", "create_time_since_epoch",
7369+
"last_update_time_since_epoch"})));
7370+
7371+
// Verify that the update time of execution is larger than the creation time
7372+
// of the artifact.
7373+
EXPECT_GE(
7374+
get_executions_by_context_response.executions(0)
7375+
.last_update_time_since_epoch(),
7376+
get_artifacts_by_context_response.artifacts(0).create_time_since_epoch());
7377+
}
7378+
72667379
TEST_P(MetadataStoreTestSuite,
72677380
PutLineageSubgraphUpdatesArtifactsAndExecutions) {
72687381
// Prepare the metadata store with types

0 commit comments

Comments
 (0)