1717package com .google .gcloud .datastore .testing ;
1818
1919import static com .google .common .base .MoreObjects .firstNonNull ;
20+ import static com .google .common .base .Preconditions .checkArgument ;
2021import static java .nio .charset .StandardCharsets .UTF_8 ;
2122
2223import com .google .common .base .Strings ;
@@ -85,6 +86,7 @@ public class LocalGcdHelper {
8586 private static final String GCLOUD = "gcloud" ;
8687 private static final Path INSTALLED_GCD_PATH ;
8788 private static final String GCD_VERSION_PREFIX = "gcd-emulator " ;
89+ private static final double DEFAULT_CONSISTENCY = 0.9 ;
8890
8991 static {
9092 INSTALLED_GCD_PATH = installedGcdPath ();
@@ -398,9 +400,15 @@ public LocalGcdHelper(String projectId, int port) {
398400 * All content is written to a temporary directory that will be deleted when
399401 * {@link #stop()} is called or when the program terminates) to make sure that no left-over
400402 * data from prior runs is used.
403+ *
404+ * @param consistency the fraction of job application attempts that will succeed, with 0.0
405+ * resulting in no attempts succeeding, and 1.0 resulting in all attempts succeeding. Defaults
406+ * to 0.9. Note that setting this to 1.0 may mask incorrect assumptions about the consistency
407+ * of non-ancestor queries; non-ancestor queries are eventually consistent.
401408 */
402- public void start () throws IOException , InterruptedException {
409+ public void start (double consistency ) throws IOException , InterruptedException {
403410 // send a quick request in case we have a hanging process from a previous run
411+ checkArgument (consistency >= 0.0 && consistency <= 1.0 , "Consistency must be between 0 and 1" );
404412 sendQuitRequest (port );
405413 // Each run is associated with its own folder that is deleted once test completes.
406414 gcdPath = Files .createTempDirectory ("gcd" );
@@ -415,7 +423,7 @@ public void start() throws IOException, InterruptedException {
415423 } else {
416424 gcdExecutablePath = INSTALLED_GCD_PATH ;
417425 }
418- startGcd (gcdExecutablePath );
426+ startGcd (gcdExecutablePath , consistency );
419427 }
420428
421429 private void downloadGcd () throws IOException {
@@ -453,7 +461,8 @@ private void downloadGcd() throws IOException {
453461 }
454462 }
455463
456- private void startGcd (Path executablePath ) throws IOException , InterruptedException {
464+ private void startGcd (Path executablePath , double consistency )
465+ throws IOException , InterruptedException {
457466 // cleanup any possible data for the same project
458467 File datasetFolder = new File (gcdPath .toFile (), projectId );
459468 deleteRecurse (datasetFolder .toPath ());
@@ -486,7 +495,8 @@ private void startGcd(Path executablePath) throws IOException, InterruptedExcept
486495 startProcess =
487496 CommandWrapper .create ()
488497 .command (gcdAbsolutePath .toString (), "start" , "--testing" , "--allow_remote_shutdown" ,
489- "--port=" + Integer .toString (port ), projectId )
498+ "--port=" + Integer .toString (port ), "--consistency=" + Double .toString (consistency ),
499+ projectId )
490500 .directory (gcdPath )
491501 .start ();
492502 processReader = ProcessStreamReader .start (startProcess .getInputStream ());
@@ -578,10 +588,10 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IO
578588 });
579589 }
580590
581- public static LocalGcdHelper start (String projectId , int port )
591+ public static LocalGcdHelper start (String projectId , int port , double consistency )
582592 throws IOException , InterruptedException {
583593 LocalGcdHelper helper = new LocalGcdHelper (projectId , port );
584- helper .start ();
594+ helper .start (consistency );
585595 return helper ;
586596 }
587597
@@ -590,10 +600,13 @@ public static void main(String... args) throws IOException, InterruptedException
590600 String action = parsedArgs .get ("action" );
591601 int port =
592602 (parsedArgs .get ("port" ) == null ) ? DEFAULT_PORT : Integer .parseInt (parsedArgs .get ("port" ));
603+ double consistency =
604+ parsedArgs .get ("consistency" ) == null
605+ ? DEFAULT_CONSISTENCY : Double .parseDouble (parsedArgs .get ("consistency" ));
593606 switch (action ) {
594607 case "START" :
595608 if (!isActive (DEFAULT_PROJECT_ID , port )) {
596- LocalGcdHelper helper = start (DEFAULT_PROJECT_ID , port );
609+ LocalGcdHelper helper = start (DEFAULT_PROJECT_ID , port , consistency );
597610 try (FileWriter writer = new FileWriter (".local_gcd_helper" )) {
598611 writer .write (helper .gcdPath .toAbsolutePath ().toString () + System .lineSeparator ());
599612 writer .write (Integer .toString (port ));
0 commit comments