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

[googledatastore] Google Datastore to use default credentials. #984

Merged
merged 2 commits into from
Sep 16, 2017
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
11 changes: 7 additions & 4 deletions googledatastore/conf/googledatastore.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# LICENSE file.

#
# Sample property file for Google Cloud Datastore DB client
# Sample property file for Google Cloud Datastore DB client

## Mandatory parameters
#
Expand All @@ -39,12 +39,12 @@ readallfields = true
#
# googledatastore.readConsistency=STRONG

# Decides how we group entities into entity groups.
# Decides how we group entities into entity groups.
# (See the details section in README.md for documentation)
#
# googledatastore.entityGroupingMode=ONE_ENTITY_PER_GROUP

# If you set the googledatastore.entityGroupingMode property to
# If you set the googledatastore.entityGroupingMode property to
# MULTI_ENTITY_PER_GROUP, you can optionally specify the name of the root entity
#
# googledatastore.rootEntityName="YCSB_ROOT_ENTITY"
Expand All @@ -53,4 +53,7 @@ readallfields = true
# requestdistribution = uniform

# Enable/disable debug message, default is false.
# googledatastore.debug = false
# googledatastore.debug = false

# Skip indexes, default is true.
# googledatastore.skipIndex = true
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.yahoo.ycsb.db;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.datastore.v1.*;
import com.google.datastore.v1.CommitRequest.Mode;
import com.google.datastore.v1.ReadOptions.ReadConsistency;
Expand Down Expand Up @@ -82,6 +83,8 @@ private enum EntityGroupingMode {

private Datastore datastore = null;

private static boolean skipIndex = true;

/**
* Initialize any state for this DB. Called once per DB instance; there is
* one DB instance per client thread.
Expand All @@ -93,6 +96,12 @@ public void init() throws DBException {
logger.setLevel(Level.DEBUG);
}

String skipIndexString = getProperties().getProperty(
"googledatastore.skipIndex", null);
if (null != skipIndexString && "false".equalsIgnoreCase(skipIndexString)) {
skipIndex = false;
}

// We need the following 3 essential properties to initialize datastore:
//
// - DatasetId,
Expand All @@ -107,17 +116,8 @@ public void init() throws DBException {

String privateKeyFile = getProperties().getProperty(
"googledatastore.privateKeyFile", null);
if (privateKeyFile == null) {
throw new DBException(
"Required property \"privateKeyFile\" missing.");
}

String serviceAccountEmail = getProperties().getProperty(
"googledatastore.serviceAccountEmail", null);
if (serviceAccountEmail == null) {
throw new DBException(
"Required property \"serviceAccountEmail\" missing.");
}

// Below are properties related to benchmarking.

Expand Down Expand Up @@ -157,11 +157,18 @@ public void init() throws DBException {
// Setup the connection to Google Cloud Datastore with the credentials
// obtained from the configure.
DatastoreOptions.Builder options = new DatastoreOptions.Builder();
Credential credential = DatastoreHelper.getServiceAccountCredential(
serviceAccountEmail, privateKeyFile);
logger.info("Using JWT Service Account credential.");
logger.info("DatasetID: " + datasetId + ", Service Account Email: " +
serviceAccountEmail + ", Private Key File Path: " + privateKeyFile);
Credential credential = GoogleCredential.getApplicationDefault();
if (serviceAccountEmail != null && privateKeyFile != null) {
credential = DatastoreHelper.getServiceAccountCredential(
serviceAccountEmail, privateKeyFile);
logger.info("Using JWT Service Account credential.");
logger.info("DatasetID: " + datasetId + ", Service Account Email: " +
serviceAccountEmail + ", Private Key File Path: " + privateKeyFile);
} else {
logger.info("Using default gcloud credential.");
logger.info("DatasetID: " + datasetId
+ ", Service Account Email: " + ((GoogleCredential) credential).getServiceAccountId());
}

datastore = DatastoreFactory.get().create(
options.credential(credential).projectId(datasetId).build());
Expand Down Expand Up @@ -298,7 +305,8 @@ private Status doSingleItemMutation(String table, String key,
entityBuilder.getMutableProperties()
.put(val.getKey(),
Value.newBuilder()
.setStringValue(val.getValue().toString()).build());
.setStringValue(val.getValue().toString())
.setExcludeFromIndexes(skipIndex).build());
}
Entity entity = entityBuilder.build();
logger.debug("entity built as: " + entity.toString());
Expand Down