@@ -1592,7 +1592,8 @@ public List<Session> batchCreateSessions(
1592
1592
requestBuilder .setSessionTemplate (sessionBuilder );
1593
1593
BatchCreateSessionsRequest request = requestBuilder .build ();
1594
1594
GrpcCallContext context =
1595
- newCallContext (options , databaseName , request , SpannerGrpc .getBatchCreateSessionsMethod ());
1595
+ newCallContext (
1596
+ options , databaseName , request , SpannerGrpc .getBatchCreateSessionsMethod (), true );
1596
1597
return get (spannerStub .batchCreateSessionsCallable ().futureCall (request , context ))
1597
1598
.getSessionList ();
1598
1599
}
@@ -1616,7 +1617,7 @@ public Session createSession(
1616
1617
requestBuilder .setSession (sessionBuilder );
1617
1618
CreateSessionRequest request = requestBuilder .build ();
1618
1619
GrpcCallContext context =
1619
- newCallContext (options , databaseName , request , SpannerGrpc .getCreateSessionMethod ());
1620
+ newCallContext (options , databaseName , request , SpannerGrpc .getCreateSessionMethod (), true );
1620
1621
return get (spannerStub .createSessionCallable ().futureCall (request , context ));
1621
1622
}
1622
1623
@@ -1630,15 +1631,19 @@ public void deleteSession(String sessionName, @Nullable Map<Option, ?> options)
1630
1631
public ApiFuture <Empty > asyncDeleteSession (String sessionName , @ Nullable Map <Option , ?> options ) {
1631
1632
DeleteSessionRequest request = DeleteSessionRequest .newBuilder ().setName (sessionName ).build ();
1632
1633
GrpcCallContext context =
1633
- newCallContext (options , sessionName , request , SpannerGrpc .getDeleteSessionMethod ());
1634
+ newCallContext (options , sessionName , request , SpannerGrpc .getDeleteSessionMethod (), false );
1634
1635
return spannerStub .deleteSessionCallable ().futureCall (request , context );
1635
1636
}
1636
1637
1637
1638
@ Override
1638
1639
public StreamingCall read (
1639
- ReadRequest request , ResultStreamConsumer consumer , @ Nullable Map <Option , ?> options ) {
1640
+ ReadRequest request ,
1641
+ ResultStreamConsumer consumer ,
1642
+ @ Nullable Map <Option , ?> options ,
1643
+ boolean routeToLeader ) {
1640
1644
GrpcCallContext context =
1641
- newCallContext (options , request .getSession (), request , SpannerGrpc .getReadMethod ());
1645
+ newCallContext (
1646
+ options , request .getSession (), request , SpannerGrpc .getReadMethod (), routeToLeader );
1642
1647
SpannerResponseObserver responseObserver = new SpannerResponseObserver (consumer );
1643
1648
spannerStub .streamingReadCallable ().call (request , responseObserver , context );
1644
1649
final StreamController controller = responseObserver .getController ();
@@ -1658,13 +1663,14 @@ public void cancel(String message) {
1658
1663
}
1659
1664
1660
1665
@ Override
1661
- public ResultSet executeQuery (ExecuteSqlRequest request , @ Nullable Map <Option , ?> options ) {
1662
- return get (executeQueryAsync (request , options ));
1666
+ public ResultSet executeQuery (
1667
+ ExecuteSqlRequest request , @ Nullable Map <Option , ?> options , boolean routeToLeader ) {
1668
+ return get (executeQueryAsync (request , options , routeToLeader ));
1663
1669
}
1664
1670
1665
1671
@ Override
1666
1672
public ApiFuture <ResultSet > executeQueryAsync (
1667
- ExecuteSqlRequest request , @ Nullable Map <Option , ?> options ) {
1673
+ ExecuteSqlRequest request , @ Nullable Map <Option , ?> options , boolean routeToLeader ) {
1668
1674
GrpcCallContext context =
1669
1675
newCallContext (options , request .getSession (), request , SpannerGrpc .getExecuteSqlMethod ());
1670
1676
return spannerStub .executeSqlCallable ().futureCall (request , context );
@@ -1674,7 +1680,8 @@ public ApiFuture<ResultSet> executeQueryAsync(
1674
1680
public ResultSet executePartitionedDml (
1675
1681
ExecuteSqlRequest request , @ Nullable Map <Option , ?> options ) {
1676
1682
GrpcCallContext context =
1677
- newCallContext (options , request .getSession (), request , SpannerGrpc .getExecuteSqlMethod ());
1683
+ newCallContext (
1684
+ options , request .getSession (), request , SpannerGrpc .getExecuteSqlMethod (), true );
1678
1685
return get (partitionedDmlStub .executeSqlCallable ().futureCall (request , context ));
1679
1686
}
1680
1687
@@ -1688,18 +1695,29 @@ public ServerStream<PartialResultSet> executeStreamingPartitionedDml(
1688
1695
ExecuteSqlRequest request , Map <Option , ?> options , Duration timeout ) {
1689
1696
GrpcCallContext context =
1690
1697
newCallContext (
1691
- options , request .getSession (), request , SpannerGrpc .getExecuteStreamingSqlMethod ());
1698
+ options ,
1699
+ request .getSession (),
1700
+ request ,
1701
+ SpannerGrpc .getExecuteStreamingSqlMethod (),
1702
+ true );
1692
1703
// Override any timeout settings that might have been set on the call context.
1693
1704
context = context .withTimeout (timeout ).withStreamWaitTimeout (timeout );
1694
1705
return partitionedDmlStub .executeStreamingSqlCallable ().call (request , context );
1695
1706
}
1696
1707
1697
1708
@ Override
1698
1709
public StreamingCall executeQuery (
1699
- ExecuteSqlRequest request , ResultStreamConsumer consumer , @ Nullable Map <Option , ?> options ) {
1710
+ ExecuteSqlRequest request ,
1711
+ ResultStreamConsumer consumer ,
1712
+ @ Nullable Map <Option , ?> options ,
1713
+ boolean routeToLeader ) {
1700
1714
GrpcCallContext context =
1701
1715
newCallContext (
1702
- options , request .getSession (), request , SpannerGrpc .getExecuteStreamingSqlMethod ());
1716
+ options ,
1717
+ request .getSession (),
1718
+ request ,
1719
+ SpannerGrpc .getExecuteStreamingSqlMethod (),
1720
+ routeToLeader );
1703
1721
SpannerResponseObserver responseObserver = new SpannerResponseObserver (consumer );
1704
1722
spannerStub .executeStreamingSqlCallable ().call (request , responseObserver , context );
1705
1723
final StreamController controller = responseObserver .getController ();
@@ -1729,30 +1747,35 @@ public ApiFuture<ExecuteBatchDmlResponse> executeBatchDmlAsync(
1729
1747
ExecuteBatchDmlRequest request , @ Nullable Map <Option , ?> options ) {
1730
1748
GrpcCallContext context =
1731
1749
newCallContext (
1732
- options , request .getSession (), request , SpannerGrpc .getExecuteBatchDmlMethod ());
1750
+ options , request .getSession (), request , SpannerGrpc .getExecuteBatchDmlMethod (), true );
1733
1751
return spannerStub .executeBatchDmlCallable ().futureCall (request , context );
1734
1752
}
1735
1753
1736
1754
@ Override
1737
1755
public ApiFuture <Transaction > beginTransactionAsync (
1738
- BeginTransactionRequest request , @ Nullable Map <Option , ?> options ) {
1756
+ BeginTransactionRequest request , @ Nullable Map <Option , ?> options , boolean routeToLeader ) {
1739
1757
GrpcCallContext context =
1740
1758
newCallContext (
1741
- options , request .getSession (), request , SpannerGrpc .getBeginTransactionMethod ());
1759
+ options ,
1760
+ request .getSession (),
1761
+ request ,
1762
+ SpannerGrpc .getBeginTransactionMethod (),
1763
+ routeToLeader );
1742
1764
return spannerStub .beginTransactionCallable ().futureCall (request , context );
1743
1765
}
1744
1766
1745
1767
@ Override
1746
1768
public Transaction beginTransaction (
1747
- BeginTransactionRequest request , @ Nullable Map <Option , ?> options ) throws SpannerException {
1748
- return get (beginTransactionAsync (request , options ));
1769
+ BeginTransactionRequest request , @ Nullable Map <Option , ?> options , boolean routeToLeader )
1770
+ throws SpannerException {
1771
+ return get (beginTransactionAsync (request , options , routeToLeader ));
1749
1772
}
1750
1773
1751
1774
@ Override
1752
1775
public ApiFuture <CommitResponse > commitAsync (
1753
1776
CommitRequest request , @ Nullable Map <Option , ?> options ) {
1754
1777
GrpcCallContext context =
1755
- newCallContext (options , request .getSession (), request , SpannerGrpc .getCommitMethod ());
1778
+ newCallContext (options , request .getSession (), request , SpannerGrpc .getCommitMethod (), true );
1756
1779
return spannerStub .commitCallable ().futureCall (request , context );
1757
1780
}
1758
1781
@@ -1765,7 +1788,8 @@ public CommitResponse commit(CommitRequest commitRequest, @Nullable Map<Option,
1765
1788
@ Override
1766
1789
public ApiFuture <Empty > rollbackAsync (RollbackRequest request , @ Nullable Map <Option , ?> options ) {
1767
1790
GrpcCallContext context =
1768
- newCallContext (options , request .getSession (), request , SpannerGrpc .getRollbackMethod ());
1791
+ newCallContext (
1792
+ options , request .getSession (), request , SpannerGrpc .getRollbackMethod (), true );
1769
1793
return spannerStub .rollbackCallable ().futureCall (request , context );
1770
1794
}
1771
1795
@@ -1780,7 +1804,7 @@ public PartitionResponse partitionQuery(
1780
1804
PartitionQueryRequest request , @ Nullable Map <Option , ?> options ) throws SpannerException {
1781
1805
GrpcCallContext context =
1782
1806
newCallContext (
1783
- options , request .getSession (), request , SpannerGrpc .getPartitionQueryMethod ());
1807
+ options , request .getSession (), request , SpannerGrpc .getPartitionQueryMethod (), true );
1784
1808
return get (spannerStub .partitionQueryCallable ().futureCall (request , context ));
1785
1809
}
1786
1810
@@ -1789,7 +1813,7 @@ public PartitionResponse partitionRead(
1789
1813
PartitionReadRequest request , @ Nullable Map <Option , ?> options ) throws SpannerException {
1790
1814
GrpcCallContext context =
1791
1815
newCallContext (
1792
- options , request .getSession (), request , SpannerGrpc .getPartitionReadMethod ());
1816
+ options , request .getSession (), request , SpannerGrpc .getPartitionReadMethod (), true );
1793
1817
return get (spannerStub .partitionReadCallable ().futureCall (request , context ));
1794
1818
}
1795
1819
@@ -1898,6 +1922,16 @@ <ReqT, RespT> GrpcCallContext newCallContext(
1898
1922
String resource ,
1899
1923
ReqT request ,
1900
1924
MethodDescriptor <ReqT , RespT > method ) {
1925
+ return newCallContext (options , resource , request , method , false );
1926
+ }
1927
+
1928
+ @ VisibleForTesting
1929
+ <ReqT , RespT > GrpcCallContext newCallContext (
1930
+ @ Nullable Map <Option , ?> options ,
1931
+ String resource ,
1932
+ ReqT request ,
1933
+ MethodDescriptor <ReqT , RespT > method ,
1934
+ boolean routeToLeader ) {
1901
1935
GrpcCallContext context = GrpcCallContext .createDefault ();
1902
1936
if (options != null ) {
1903
1937
context = context .withChannelAffinity (Option .CHANNEL_HINT .getLong (options ).intValue ());
@@ -1907,6 +1941,9 @@ <ReqT, RespT> GrpcCallContext newCallContext(
1907
1941
context = context .withCallOptions (context .getCallOptions ().withCompression (compressorName ));
1908
1942
}
1909
1943
context = context .withExtraHeaders (metadataProvider .newExtraHeaders (resource , projectName ));
1944
+ if (routeToLeader ) {
1945
+ context = context .withExtraHeaders (metadataProvider .newRouteToLeaderHeader ());
1946
+ }
1910
1947
if (callCredentialsProvider != null ) {
1911
1948
CallCredentials callCredentials = callCredentialsProvider .getCallCredentials ();
1912
1949
if (callCredentials != null ) {
0 commit comments