Skip to content

Commit 33eef37

Browse files
ttypicclaude
andcommitted
Retry object publishes on fallback hosts, as ably-js does
Publish went to the current host only, so during a primary-host outage it failed where ably-js and plain channel.publish fall back. It now goes through the usual fallback path for every batch. No ids are generated (RSO3f), so a batch without ids that the first host applied before failing can be applied twice; the HttpObject.publish docs say to set ids when that matters, as the server de-duplicates by id. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent df4db77 commit 33eef37

3 files changed

Lines changed: 31 additions & 26 deletions

File tree

‎lib/src/main/java/io/ably/pubsub/liveobjects/HttpObject.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ public interface HttpObject {
184184
* validate targeting or the number of operations; the server rejects invalid batches (RSO3g, RSO3h).
185185
* Invalid input (for example a non-finite number or a null value) fails before any request is sent.
186186
*
187-
* <p>The request goes to the current host only and is not retried on a fallback host, because
188-
* without operation ids a batch the first host applied before failing would be applied twice.
189-
* If the request fails with a server or connection error, it may or may not have been applied;
190-
* set operation ids if you need to retry safely.
187+
* <p>A request that fails with a server or connection error is retried on fallback hosts, like
188+
* other REST requests. The server de-duplicates operations by id, so set ids if a batch must
189+
* not be applied twice: without them, a batch the first host applied before failing is applied
190+
* again by the retry.
191191
*
192192
* <p>Spec: RSO3
193193
*

‎lib/src/main/java/io/ably/pubsub/liveobjects/http/DefaultHttpObject.java‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.ably.pubsub.liveobjects.http;
22

33
import com.google.gson.JsonElement;
4-
import io.ably.pubsub.http.HttpConstants;
54
import io.ably.pubsub.http.HttpCore;
65
import io.ably.pubsub.http.HttpRequester;
76
import io.ably.pubsub.http.HttpUtils;
@@ -171,12 +170,10 @@ private HttpRequester.Request<HttpObjectPublishResult> publishImpl(final List<Ht
171170
http.execute(() -> callback.onError(e.errorInfo));
172171
return;
173172
}
174-
// RSO3c, RSO3e - one POST carrying all operations as an array.
175-
// Sent to the current host only, never retried on a fallback host: operations don't
176-
// carry generated ids (RSO3f), so a batch the first host applied before failing would
177-
// be applied twice.
178-
http.ablyHttpExecuteWithRetry(client.httpCore.getPreferredHost(), basePath, HttpConstants.Methods.POST,
179-
HttpUtils.defaultAcceptHeaders(binary()), query, body,
173+
// RSO3c, RSO3e - one POST carrying all operations as an array, retried on fallback hosts
174+
// like other REST requests and as ably-js does. No ids are generated (RSO3f), so a batch
175+
// without ids that the first host applied before failing can be applied twice.
176+
http.post(basePath, HttpUtils.defaultAcceptHeaders(binary()), query, body,
180177
responseHandler(HttpObjectCodec::decodePublishResult), true, callback);
181178
});
182179
}

‎lib/src/test/kotlin/io/ably/pubsub/liveobjects/http/HttpObjectEdgeCasesTest.kt‎

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,24 +160,32 @@ class HttpObjectEdgeCasesTest {
160160
//region Fallback hosts
161161

162162
@Test
163-
fun `publish is not retried on a fallback host after a server error`() {
163+
fun `publish is retried on a fallback host after a server error, with or without ids`() {
164164
val serverError = mapOf("error" to mapOf("code" to 50000, "statusCode" to 500, "message" to "boom"))
165-
val setup = setupRestObject("edge-no-fallback") { it.respondWith(500, serverError) }
166-
167-
// Control: a GET to the same failing mock is retried on fallback hosts.
168-
assertFailsWith<AblyException> { setup.channel.`object`.getCompact() }
169-
val getRequests = setup.capturedRequests.size
170-
assertTrue(getRequests > 1, "expected the GET to be retried on fallback hosts, saw $getRequests request(s)")
165+
var posts = 0
166+
val setup = setupRestObject("edge-fallback") { req ->
167+
// the first attempt of each publish fails; the retry on a fallback host succeeds
168+
if (++posts % 2 == 1) req.respondWith(500, serverError)
169+
else req.respondWithBody(201, mapOf("messageId" to "m", "channel" to "c", "objectIds" to listOf("root")), false)
170+
}
171+
val batches = listOf(
172+
listOf(HttpObjectOperation.counterInc(5).withPath("c")),
173+
listOf(HttpObjectOperation.counterInc(5).withPath("c").withId("op-1"), HttpObjectOperation.counterInc(1).withPath("d")),
174+
listOf(HttpObjectOperation.counterInc(5).withPath("c").withId("op-1"), HttpObjectOperation.counterInc(1).withPath("d").withId("op-2")),
175+
)
171176

172-
setup.capturedRequests.clear()
173-
val error = assertFailsWith<AblyException> { setup.channel.`object`.publish(HttpObjectOperation.counterInc(5).withPath("c")) }
174-
assertEquals(500, error.errorInfo.statusCode)
175-
assertEquals(1, setup.capturedRequests.size)
177+
for (operations in batches) {
178+
setup.capturedRequests.clear()
179+
assertEquals("m", setup.channel.`object`.publish(operations).messageId)
180+
assertEquals(2, setup.capturedRequests.size)
181+
assertTrue(setup.capturedRequests[0].url.host != setup.capturedRequests[1].url.host, "expected the retry to go to a fallback host")
182+
// the retry sends the same body, so any ids are the same and the server can de-duplicate
183+
assertEquals(String(setup.capturedRequests[0].body), String(setup.capturedRequests[1].body))
176184

177-
setup.capturedRequests.clear()
178-
val asyncError = awaitCallback<HttpObjectPublishResult> { setup.channel.`object`.publishAsync(HttpObjectOperation.counterInc(5).withPath("c"), it) }
179-
assertEquals(500, asyncError.errorInfo().statusCode)
180-
assertEquals(1, setup.capturedRequests.size)
185+
setup.capturedRequests.clear()
186+
assertEquals("m", awaitCallback<HttpObjectPublishResult> { setup.channel.`object`.publishAsync(operations, it) }.result!!.messageId)
187+
assertEquals(2, setup.capturedRequests.size)
188+
}
181189
}
182190

183191
//endregion

0 commit comments

Comments
 (0)