Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

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 @@ -17,7 +17,9 @@
package com.google.cloud.pubsublite;

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode.Code;
import com.google.auto.value.AutoOneOf;
import com.google.cloud.pubsublite.internal.CheckedApiException;
import java.io.Serializable;

@AutoOneOf(CloudRegionOrZone.Kind.class)
Expand Down Expand Up @@ -54,12 +56,16 @@ public static CloudRegionOrZone of(CloudZone zone) {
}

public static CloudRegionOrZone parse(String value) throws ApiException {
try {
return of(CloudZone.parse(value));
} catch (ApiException e) {
// pass
String[] splits = value.split("-", -1);
switch (splits.length) {
case 2:
return of(CloudRegion.of(value));
case 3:
return of(CloudZone.parse(value));
default:
throw new CheckedApiException("Invalid location: " + value, Code.INVALID_ARGUMENT)
.underlying;
}
return of(CloudRegion.of(value));
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,20 @@ public ApiFuture<Offset> publish(Message message) {
PubSubMessage proto = message.toProto();
try (CloseableMonitor.Hold h = batcherMonitor.enter()) {
ApiService.State currentState = state();
checkState(
currentState == ApiService.State.RUNNING,
"Cannot publish when Publisher state is %s.",
currentState.name());
return batcher.add(proto);
switch (currentState) {
case FAILED:
throw new CheckedApiException(
"Cannot publish when publisher has failed.",
failureCause(),
Code.FAILED_PRECONDITION);
case STARTING:
case RUNNING:
return batcher.add(proto);
default:
throw new CheckedApiException(
"Cannot publish when Publisher state is " + currentState.name(),
Code.FAILED_PRECONDITION);
}
} catch (CheckedApiException e) {
onPermanentError(e);
return ApiFutures.immediateFailedFuture(e);
Expand Down