Skip to content
This repository was archived by the owner on Feb 24, 2026. It is now read-only.
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
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.concurrent.GuardedBy;

/**
Expand Down Expand Up @@ -219,11 +221,29 @@ class ConnectionWorker implements AutoCloseable {
private RuntimeException testOnlyRunTimeExceptionInAppendLoop = null;
private long testOnlyAppendLoopSleepTime = 0;

private static String projectMatching = "projects/[^/]+/";
private static Pattern streamPatternProject = Pattern.compile(projectMatching);

/** The maximum size of one request. Defined by the API. */
public static long getApiMaxRequestBytes() {
return 10L * 1000L * 1000L; // 10 megabytes (https://en.wikipedia.org/wiki/Megabyte)
}

static String extractProjectName(String streamName) {
Matcher streamMatcher = streamPatternProject.matcher(streamName);
if (streamMatcher.find()) {
return streamMatcher.group();
} else {
throw new IllegalStateException(
String.format("The passed in stream name does not match standard format %s", streamName));
}
}

static String getRoutingHeader(String streamName, String location) {
String project = extractProjectName(streamName);
return project + "locations/" + location;
}

public ConnectionWorker(
String streamName,
String location,
Expand Down Expand Up @@ -259,6 +279,10 @@ public ConnectionWorker(
newHeaders.putAll(clientSettings.toBuilder().getHeaderProvider().getHeaders());
if (this.location == null) {
newHeaders.put("x-goog-request-params", "write_stream=" + this.streamName);
} else {
newHeaders.put(
"x-goog-request-params",
"write_location=" + getRoutingHeader(this.streamName, this.location));
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check on whether it's empty

BigQueryWriteSettings stubSettings =
clientSettings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,4 +710,10 @@ public void testLongTimeIdleWontFail() throws Exception {
assertEquals(i, futures.get(i).get().getAppendResult().getOffset().getValue());
}
}

@Test
public void testLocationName() throws Exception {
assertEquals(
"projects/p1/locations/us", ConnectionWorker.getRoutingHeader(TEST_STREAM_1, "us"));
}
}