1919import static com .google .common .truth .TruthJUnit .assume ;
2020import static org .junit .Assert .assertThrows ;
2121
22+ import com .google .api .gax .rpc .InvalidArgumentException ;
2223import com .google .cloud .Date ;
2324import com .google .cloud .bigtable .admin .v2 .models .CreateSchemaBundleRequest ;
2425import com .google .cloud .bigtable .data .v2 .BigtableDataClient ;
4445import java .util .List ;
4546import java .util .Map ;
4647import java .util .UUID ;
48+ import java .util .logging .Level ;
49+ import java .util .logging .Logger ;
4750import org .junit .BeforeClass ;
4851import org .junit .ClassRule ;
4952import org .junit .Test ;
@@ -59,6 +62,7 @@ public class ExecuteQueryIT {
5962 private static String schemaBundleId ;
6063 private static String cf ;
6164 private static String uniquePrefix ;
65+ private static final Logger logger = Logger .getLogger (ExecuteQueryIT .class .getName ());
6266
6367 @ BeforeClass
6468 public static void setUpAll () throws IOException {
@@ -164,24 +168,43 @@ public void withHistoryQuery() {
164168 public void allTypes () throws Exception {
165169 createTestSchemaBundle ();
166170 Album album = Album .newBuilder ().setTitle ("Lover" ).build ();
167- PreparedStatement preparedStatement =
168- dataClient .prepareStatement (
169- "SELECT 'stringVal' AS strCol, b'foo' as bytesCol, 1 AS intCol, CAST(1.2 AS FLOAT32) as"
170- + " f32Col, CAST(1.3 AS FLOAT64) as f64Col, true as boolCol,"
171- + " TIMESTAMP_FROM_UNIX_MILLIS(1000) AS tsCol, DATE(2024, 06, 01) as dateCol,"
172- + " STRUCT(1 as a, \" foo\" as b) AS structCol, [1,2,3] AS arrCol, "
173- + cf
174- + " as mapCol, "
175- + " CAST(b'\022 \005 Lover' AS `"
176- + schemaBundleId
177- + ".com.google.cloud.bigtable.data.v2.test.Album`) as protoCol, CAST('JAZZ' AS `"
178- + schemaBundleId
179- + ".com.google.cloud.bigtable.data.v2.test.Genre`) as enumCol FROM `"
180- + tableId
181- + "` WHERE _key='"
182- + uniquePrefix
183- + "a' LIMIT 1" ,
184- new HashMap <>());
171+
172+ // For some reason the ExecuteQuery data path sometimes cannot resolve a newly-created schema
173+ // bundle immediately after its creation. To avoid test flakiness, we wrap query preparation
174+ // with a retry loop.
175+ PreparedStatement preparedStatement ;
176+ int retryCount = 0 ;
177+ int maxRetries = 10 ;
178+ while (true ) {
179+ try {
180+ preparedStatement =
181+ dataClient .prepareStatement (
182+ "SELECT 'stringVal' AS strCol, b'foo' as bytesCol, 1 AS intCol, CAST(1.2 AS FLOAT32) as"
183+ + " f32Col, CAST(1.3 AS FLOAT64) as f64Col, true as boolCol,"
184+ + " TIMESTAMP_FROM_UNIX_MILLIS(1000) AS tsCol, DATE(2024, 06, 01) as dateCol,"
185+ + " STRUCT(1 as a, \" foo\" as b) AS structCol, [1,2,3] AS arrCol, "
186+ + cf
187+ + " as mapCol, "
188+ + " CAST(b'\022 \005 Lover' AS `"
189+ + schemaBundleId
190+ + ".com.google.cloud.bigtable.data.v2.test.Album`) as protoCol, CAST('JAZZ' AS `"
191+ + schemaBundleId
192+ + ".com.google.cloud.bigtable.data.v2.test.Genre`) as enumCol FROM `"
193+ + tableId
194+ + "` WHERE _key='"
195+ + uniquePrefix
196+ + "a' LIMIT 1" ,
197+ new HashMap <>());
198+ break ;
199+ } catch (InvalidArgumentException e ) {
200+ if (++retryCount == maxRetries ) {
201+ throw e ;
202+ }
203+ logger .log (Level .INFO , "Retrying prepareStatement, retryCount: " + retryCount );
204+ Thread .sleep (5000 );
205+ }
206+ }
207+
185208 BoundStatement statement = preparedStatement .bind ().build ();
186209 try (ResultSet rs = dataClient .executeQuery (statement )) {
187210 assertThat (rs .next ()).isTrue ();
@@ -416,10 +439,5 @@ private static void createTestSchemaBundle() throws Exception {
416439 CreateSchemaBundleRequest .of (tableId , schemaBundleId )
417440 .setProtoSchema (fileDescriptorSet .toByteString ());
418441 testEnvRule .env ().getTableAdminClient ().createSchemaBundle (request );
419-
420- // For some reason the ExecuteQuery data path sometimes cannot resolve a newly-created schema
421- // bundle immediately after its creation. Adding a manual sleep to avoid test flakiness until
422- // the underlying issue is resolved.
423- Thread .sleep (5000 );
424442 }
425443}
0 commit comments