Search in sources :

Example 6 with TimestampBound

use of com.google.cloud.spanner.TimestampBound in project beam by apache.

the class SpannerIOReadTest method readPipeline.

@Test
public void readPipeline() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    PCollection<Struct> one = pipeline.apply("read q", SpannerIO.read().withSpannerConfig(spannerConfig).withQuery("SELECT * FROM users").withTimestampBound(timestampBound));
    FakeBatchTransactionId txId = new FakeBatchTransactionId("readPipelineTest");
    when(mockBatchTx.getBatchTransactionId()).thenReturn(txId);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound)).thenReturn(mockBatchTx);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class))).thenReturn(mockBatchTx);
    Partition fakePartition = FakePartitionFactory.createFakeQueryPartition(ByteString.copyFromUtf8("one"));
    when(mockBatchTx.partitionQuery(any(PartitionOptions.class), eq(Statement.of("SELECT * FROM users")), any(ReadQueryUpdateTransactionOption.class))).thenReturn(Arrays.asList(fakePartition, fakePartition));
    when(mockBatchTx.execute(any(Partition.class))).thenReturn(ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)), ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 6)));
    PAssert.that(one).containsInAnyOrder(FAKE_ROWS);
    pipeline.run();
}
Also used : Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 7 with TimestampBound

use of com.google.cloud.spanner.TimestampBound in project beam by apache.

the class SpannerIOReadTest method runQuery.

@Test
public void runQuery() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    PCollection<Struct> one = pipeline.apply("read q", SpannerIO.read().withSpannerConfig(spannerConfig).withQuery("SELECT * FROM users").withTimestampBound(timestampBound));
    FakeBatchTransactionId id = new FakeBatchTransactionId("runQueryTest");
    when(mockBatchTx.getBatchTransactionId()).thenReturn(id);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound)).thenReturn(mockBatchTx);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class))).thenReturn(mockBatchTx);
    Partition fakePartition = FakePartitionFactory.createFakeQueryPartition(ByteString.copyFromUtf8("one"));
    when(mockBatchTx.partitionQuery(any(PartitionOptions.class), eq(Statement.of("SELECT * FROM users")), any(ReadQueryUpdateTransactionOption.class))).thenReturn(Arrays.asList(fakePartition, fakePartition));
    when(mockBatchTx.execute(any(Partition.class))).thenReturn(ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)), ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 6)));
    PAssert.that(one).containsInAnyOrder(FAKE_ROWS);
    pipeline.run();
}
Also used : Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) Test(org.junit.Test)

Example 8 with TimestampBound

use of com.google.cloud.spanner.TimestampBound in project beam by apache.

the class SpannerIOReadTest method testReadMetricsSucceed.

@Test
public void testReadMetricsSucceed() throws Exception {
    Timestamp timestamp = Timestamp.ofTimeMicroseconds(12345);
    TimestampBound timestampBound = TimestampBound.ofReadTimestamp(timestamp);
    SpannerConfig spannerConfig = getSpannerConfig();
    pipeline.apply("read q", SpannerIO.read().withSpannerConfig(spannerConfig).withTable("users").withColumns("id", "name").withTimestampBound(timestampBound));
    FakeBatchTransactionId id = new FakeBatchTransactionId("runReadTest");
    when(mockBatchTx.getBatchTransactionId()).thenReturn(id);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(timestampBound)).thenReturn(mockBatchTx);
    when(serviceFactory.mockBatchClient().batchReadOnlyTransaction(any(BatchTransactionId.class))).thenReturn(mockBatchTx);
    Partition fakePartition = FakePartitionFactory.createFakeReadPartition(ByteString.copyFromUtf8("one"));
    when(mockBatchTx.partitionRead(any(PartitionOptions.class), eq("users"), eq(KeySet.all()), eq(Arrays.asList("id", "name")), any(ReadQueryUpdateTransactionOption.class))).thenReturn(Arrays.asList(fakePartition, fakePartition, fakePartition));
    when(mockBatchTx.execute(any(Partition.class))).thenReturn(ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(0, 2)), ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(2, 4)), ResultSets.forRows(FAKE_TYPE, FAKE_ROWS.subList(4, 6)));
    pipeline.run();
    verifyMetricWasSet("test", "aaa", "123", "ok", null, 3);
}
Also used : Partition(com.google.cloud.spanner.Partition) PartitionOptions(com.google.cloud.spanner.PartitionOptions) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) BatchTransactionId(com.google.cloud.spanner.BatchTransactionId) ReadQueryUpdateTransactionOption(com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption) TimestampBound(com.google.cloud.spanner.TimestampBound) FakeBatchTransactionId(com.google.cloud.spanner.FakeBatchTransactionId) Timestamp(com.google.cloud.Timestamp) Test(org.junit.Test)

Example 9 with TimestampBound

use of com.google.cloud.spanner.TimestampBound in project google-cloud-java by GoogleCloudPlatform.

the class ITReadOnlyTxnTest method singleExactStaleness.

@Test
public void singleExactStaleness() {
    // TODO(user): Use a shorter deadline (when supported) and pass on the call to Cloud Spanner.
    long deadlineNanoTime = System.nanoTime() + TimeUnit.MINUTES.toNanos(1);
    // The only exact staleness values that can be tested reliably are before the first item or
    // later than the last item: we choose the former.
    //
    // Pick a staleness that is "guaranteed" not to observe the first write.  Note that this
    // guarantee doesn't strictly hold in the absence of enforced read deadlines, but we use a
    // deadline large enough to make it practically true.
    long stalenessNanos = 1 + deadlineNanoTime - history.get(0).minCommitNanoTime;
    TimestampBound bound = TimestampBound.ofExactStaleness(stalenessNanos, TimeUnit.NANOSECONDS);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound);
    Struct row = readRow(readContext);
    assertThat(row).isNull();
    assertThat(readContext.getReadTimestamp().toSqlTimestamp()).isLessThan(history.get(0).timestamp.toSqlTimestamp());
    row = readRow(client.singleUse(bound));
    assertThat(row).isNull();
}
Also used : TimestampBound(com.google.cloud.spanner.TimestampBound) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Example 10 with TimestampBound

use of com.google.cloud.spanner.TimestampBound in project google-cloud-java by GoogleCloudPlatform.

the class ITReadOnlyTxnTest method singleMaxStaleness.

@Test
public void singleMaxStaleness() {
    History minimum = history.get(2);
    NavigableMap<Timestamp, String> possibleValues = new TreeMap<>();
    for (History item : history.subList(2, history.size())) {
        possibleValues.put(item.timestamp, item.value);
    }
    // Pick a staleness that cannot precede the second write (which, in practice, is the staleness
    // that exceeds the minimum commit time for the subsequent write).
    long stalenessNanos = System.nanoTime() - history.get(3).minCommitNanoTime;
    TimestampBound bound = TimestampBound.ofMaxStaleness(stalenessNanos, TimeUnit.NANOSECONDS);
    ReadOnlyTransaction readContext = client.singleUseReadOnlyTransaction(bound);
    Struct row = readRow(readContext);
    assertThat(row).isNotNull();
    assertThat(readContext.getReadTimestamp()).isAtLeast(minimum.timestamp);
    assertThat(row.getString(0)).isEqualTo(possibleValues.floorEntry(readContext.getReadTimestamp()).getValue());
    row = readRow(client.singleUse(bound));
    assertThat(row).isNotNull();
    assertThat(row.getString(0)).isIn(possibleValues.values());
}
Also used : TimestampBound(com.google.cloud.spanner.TimestampBound) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) TreeMap(java.util.TreeMap) Timestamp(com.google.cloud.Timestamp) Struct(com.google.cloud.spanner.Struct) IntegrationTest(com.google.cloud.spanner.IntegrationTest) Test(org.junit.Test)

Aggregations

TimestampBound (com.google.cloud.spanner.TimestampBound)16 Test (org.junit.Test)15 Timestamp (com.google.cloud.Timestamp)13 Struct (com.google.cloud.spanner.Struct)12 BatchTransactionId (com.google.cloud.spanner.BatchTransactionId)11 FakeBatchTransactionId (com.google.cloud.spanner.FakeBatchTransactionId)11 ReadQueryUpdateTransactionOption (com.google.cloud.spanner.Options.ReadQueryUpdateTransactionOption)11 Partition (com.google.cloud.spanner.Partition)11 PartitionOptions (com.google.cloud.spanner.PartitionOptions)11 IntegrationTest (com.google.cloud.spanner.IntegrationTest)5 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)5 SpannerException (com.google.cloud.spanner.SpannerException)2 TreeMap (java.util.TreeMap)2 PipelineExecutionException (org.apache.beam.sdk.Pipeline.PipelineExecutionException)2 Read (org.apache.beam.sdk.io.gcp.spanner.SpannerIO.Read)2 BatchReadOnlyTransaction (com.google.cloud.spanner.BatchReadOnlyTransaction)1