@@ -255,6 +255,52 @@ bool ParsePostgreSQLFlagsBasedServerConfigOrDie(
255
255
return true ;
256
256
}
257
257
258
+ // Converts flag sqlite_connection_mode value to actual enum
259
+ // ConnectionMode. Returns error if conversion is not successful.
260
+ absl::StatusOr<ml_metadata::SqliteMetadataSourceConfig::ConnectionMode> ConvertToConnectionMode (
261
+ std::string sqlite_connection_mode) {
262
+ if (sqlite_connection_mode == " READONLY" ) {
263
+ return ml_metadata::SqliteMetadataSourceConfig::READONLY;
264
+ } else if (sqlite_connection_mode == " READWRITE" ) {
265
+ return ml_metadata::SqliteMetadataSourceConfig::READWRITE;
266
+ } else if (sqlite_connection_mode == " READWRITE_OPENCREATE" ) {
267
+ return ml_metadata::SqliteMetadataSourceConfig::READWRITE_OPENCREATE;
268
+ }
269
+
270
+ return absl::InvalidArgumentError (
271
+ " sqlite_connection_mode is not valid, provide value in one of the "
272
+ " followings: READONLY, READWRITE, READWRITE_OPENCREATE." );
273
+ }
274
+
275
+ // Returns true if passed parameters were used to construct sqlite connection
276
+ // config and set it to service_config. If connection config
277
+ // construction is not successful, return false.
278
+ bool ParseSQLiteFlagsBasedServerConfigOrDie (
279
+ const std::string& filename_uri,
280
+ const std::string& connection_mode,
281
+ ml_metadata::MetadataStoreServerConfig* server_config) {
282
+ CHECK (!filename_uri.empty ())
283
+ << " To use sqlite store, all of --filename_uri, "
284
+ " needs to be provided" ;
285
+
286
+ ml_metadata::ConnectionConfig* connection_config =
287
+ server_config->mutable_connection_config ();
288
+ ml_metadata::SqliteMetadataSourceConfig* config = connection_config->mutable_sqlite ();
289
+ config->set_filename_uri (filename_uri);
290
+
291
+ if (!connection_mode.empty ()) {
292
+ absl::StatusOr<ml_metadata::SqliteMetadataSourceConfig::ConnectionMode> sqlite_connection_mode =
293
+ ConvertToConnectionMode (connection_mode);
294
+ CHECK (sqlite_connection_mode.ok ())
295
+ << " sqlite_connection_mode is invalid: "
296
+ << sqlite_connection_mode.status ().message ();
297
+
298
+ config->set_connection_mode (sqlite_connection_mode.value ());
299
+ }
300
+
301
+ return true ;
302
+ }
303
+
258
304
} // namespace
259
305
260
306
// gRPC server options
@@ -270,6 +316,7 @@ enum class SourceConfigType {
270
316
kConfigFile ,
271
317
kMySql ,
272
318
kPostgreSql ,
319
+ kSqlite
273
320
};
274
321
275
322
// Converts flag metadata_source_config_type value to actual enum
@@ -284,6 +331,8 @@ absl::StatusOr<SourceConfigType> ConvertToSourceConfig(
284
331
return SourceConfigType::kMySql ;
285
332
} else if (metadata_source_config_type == " postgresql" ) {
286
333
return SourceConfigType::kPostgreSql ;
334
+ } else if (metadata_source_config_type == " sqlite" ) {
335
+ return SourceConfigType::kSqlite ;
287
336
}
288
337
289
338
return absl::InvalidArgumentError (
@@ -383,6 +432,14 @@ DEFINE_int64(downgrade_db_schema_version, -1,
383
432
" schema version is downgraded to the set value during "
384
433
" initialization(Optional Parameter)" );
385
434
435
+ // SQLite config command line options
436
+ DEFINE_string (sqlite_config_filename_uri, " " ,
437
+ " SQLite database filename uri to be used." );
438
+ DEFINE_string (sqlite_config_connection_mode, " " ,
439
+ " SQLite connection mode. Possible values are READONLY,"
440
+ " READWRITE and READWRITE_OPENCREATE, if not specifified "
441
+ " the default is READWRITE_OPENCREATE." );
442
+
386
443
// Default connection option for metadata source. It will check for
387
444
// the existence of config file first, and check for mysql flags if
388
445
// config file doesn't exist. Otherwise, it will create fake database.
@@ -494,6 +551,35 @@ BuildPostgreSQLConnectionConfig() {
494
551
}
495
552
}
496
553
554
+ // Constructs Connection Config for SQLite database. Requires to
555
+ // set metadata_source_config_type as "sqlite", then provide necessary
556
+ // information in flags that have prefix of `sqlite_`.
557
+ // Example run:
558
+ // sudo docker run --name "${MLMD_GRPC_CONTAINER}" \
559
+ // -p ${MLMD_GRPC_PORT}:${MLMD_GRPC_PORT} \
560
+ // --network="${GRPC_E2E_BRIDGE_NETWORK}"\
561
+ // --entrypoint /bin/metadata_store_server -d "${MLMD_DOCKER_IMAGE}" \
562
+ // --grpc_port=${MLMD_GRPC_PORT} \
563
+ // --metadata_source_config_type="sqlite" \
564
+ // --sqlite_config_filename_uri=${MLMD_SQLITE_DB_URI} \
565
+ // --sqlite_config_connection_mode=READWRITE_OPENCREATE
566
+ // @return server configuration that contains connection config set by
567
+ // sqlite flags. Or error status if failed.
568
+ absl::StatusOr<ml_metadata::MetadataStoreServerConfig>
569
+ BuildSQLiteConnectionConfig () {
570
+ ml_metadata::MetadataStoreServerConfig server_config;
571
+ if (ParseSQLiteFlagsBasedServerConfigOrDie (
572
+ (FLAGS_sqlite_config_filename_uri),
573
+ (FLAGS_sqlite_config_connection_mode),
574
+ &server_config)) {
575
+ return server_config;
576
+ } else {
577
+ LOG (ERROR) << " Unable to construct server config using sqlite flags." ;
578
+ return absl::InvalidArgumentError (
579
+ " Unable to construct server config using sqlite flags." );
580
+ }
581
+ }
582
+
497
583
int main (int argc, char ** argv) {
498
584
gflags::ParseCommandLineFlags (&argc, &argv, true );
499
585
@@ -521,6 +607,8 @@ int main(int argc, char** argv) {
521
607
server_config_status = BuildMySQLConnectionConfig ();
522
608
} else if (source_config.value () == SourceConfigType::kPostgreSql ) {
523
609
server_config_status = BuildPostgreSQLConnectionConfig ();
610
+ } else if (source_config.value () == SourceConfigType::kSqlite ) {
611
+ server_config_status = BuildSQLiteConnectionConfig ();
524
612
} else {
525
613
LOG (ERROR) << " metadata_source_config_type is invalid: "
526
614
<< metadata_source_config_type;
0 commit comments