|
| 1 | +package io.weaviate.client6.v1.api.collections.tenants; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.ArrayList; |
| 5 | +import java.util.List; |
| 6 | +import java.util.stream.IntStream; |
| 7 | + |
| 8 | +import org.assertj.core.api.Assertions; |
| 9 | +import org.junit.Test; |
| 10 | + |
| 11 | +import io.weaviate.client6.v1.internal.orm.CollectionDescriptor; |
| 12 | +import io.weaviate.testutil.transport.MockRestTransport; |
| 13 | + |
| 14 | +/** |
| 15 | + * The server rejects an update of more than 100 tenants with HTTP 422, so the |
| 16 | + * client splits longer lists. Adding tenants is not capped and has to keep going |
| 17 | + * out as a single request. |
| 18 | + */ |
| 19 | +public class UpdateTenantsBatchingTest { |
| 20 | + private static final CollectionDescriptor<?> COLLECTION = CollectionDescriptor.ofMap("Things"); |
| 21 | + |
| 22 | + private static List<Tenant> tenants(int n) { |
| 23 | + return IntStream.rangeClosed(1, n).mapToObj(i -> Tenant.active("tenant-" + i)).toList(); |
| 24 | + } |
| 25 | + |
| 26 | + private static List<String> names(int n) { |
| 27 | + return IntStream.rangeClosed(1, n).mapToObj(i -> "tenant-" + i).toList(); |
| 28 | + } |
| 29 | + |
| 30 | + /** Collect the body of every request the client sent, in order. */ |
| 31 | + private static List<String> bodies(MockRestTransport transport, int expectAtMost) { |
| 32 | + var seen = new ArrayList<String>(); |
| 33 | + var assertions = new MockRestTransport.AssertFunction[expectAtMost]; |
| 34 | + for (var i = 0; i < expectAtMost; i++) { |
| 35 | + assertions[i] = (method, url, body, query) -> seen.add(body); |
| 36 | + } |
| 37 | + transport.assertNext(assertions); |
| 38 | + return seen; |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void test_batchesOfAtMost100() { |
| 43 | + Assertions.assertThat(UpdateTenantsRequest.batches(tenants(100))) |
| 44 | + .as("exactly at the limit is one request").hasSize(1); |
| 45 | + Assertions.assertThat(UpdateTenantsRequest.batches(tenants(101))) |
| 46 | + .as("one over the limit splits") |
| 47 | + .extracting(List::size).containsExactly(100, 1); |
| 48 | + Assertions.assertThat(UpdateTenantsRequest.batches(tenants(250))) |
| 49 | + .extracting(List::size).containsExactly(100, 100, 50); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void test_batchesCoverEveryTenantInOrder() { |
| 54 | + var all = tenants(250); |
| 55 | + |
| 56 | + var flattened = new ArrayList<Tenant>(); |
| 57 | + UpdateTenantsRequest.batches(all).forEach(flattened::addAll); |
| 58 | + |
| 59 | + Assertions.assertThat(flattened).isEqualTo(all); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void test_emptyListIsASingleEmptyBatch() { |
| 64 | + Assertions.assertThat(UpdateTenantsRequest.batches(List.of())).containsExactly(List.of()); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void test_updateSplitsIntoSeveralRequests() throws IOException { |
| 69 | + var transport = new MockRestTransport(); |
| 70 | + |
| 71 | + new WeaviateTenantsClient(COLLECTION, transport, null).update(tenants(101)); |
| 72 | + |
| 73 | + var bodies = bodies(transport, 3); |
| 74 | + Assertions.assertThat(bodies).as("101 tenants -> 2 requests").hasSize(2); |
| 75 | + Assertions.assertThat(bodies.get(0)).contains("tenant-100").doesNotContain("tenant-101"); |
| 76 | + Assertions.assertThat(bodies.get(1)).contains("tenant-101").doesNotContain("tenant-100"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void test_updateAtTheLimitIsASingleRequest() throws IOException { |
| 81 | + var transport = new MockRestTransport(); |
| 82 | + |
| 83 | + new WeaviateTenantsClient(COLLECTION, transport, null).update(tenants(100)); |
| 84 | + |
| 85 | + Assertions.assertThat(bodies(transport, 2)).hasSize(1); |
| 86 | + } |
| 87 | + |
| 88 | + /** activate/deactivate/offload all delegate to update, so they split too. */ |
| 89 | + @Test |
| 90 | + public void test_deactivateSplitsIntoSeveralRequests() throws IOException { |
| 91 | + var transport = new MockRestTransport(); |
| 92 | + |
| 93 | + new WeaviateTenantsClient(COLLECTION, transport, null).deactivate(names(201)); |
| 94 | + |
| 95 | + var bodies = bodies(transport, 4); |
| 96 | + Assertions.assertThat(bodies).as("201 tenants -> 3 requests").hasSize(3); |
| 97 | + Assertions.assertThat(bodies).allSatisfy(body -> Assertions.assertThat(body).contains("INACTIVE")); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void test_createIsNotSplit() throws IOException { |
| 102 | + var transport = new MockRestTransport(); |
| 103 | + |
| 104 | + new WeaviateTenantsClient(COLLECTION, transport, null).create(tenants(250)); |
| 105 | + |
| 106 | + var bodies = bodies(transport, 2); |
| 107 | + Assertions.assertThat(bodies).as("adding tenants is not capped by the server").hasSize(1); |
| 108 | + Assertions.assertThat(bodies.get(0)).contains("tenant-1").contains("tenant-250"); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void test_asyncUpdateSplitsIntoSeveralRequests() throws Exception { |
| 113 | + var transport = new MockRestTransport(); |
| 114 | + |
| 115 | + new WeaviateTenantsClientAsync(COLLECTION, transport, null).update(tenants(101)).get(); |
| 116 | + |
| 117 | + var bodies = bodies(transport, 3); |
| 118 | + Assertions.assertThat(bodies).hasSize(2); |
| 119 | + Assertions.assertThat(bodies.get(1)).contains("tenant-101"); |
| 120 | + } |
| 121 | +} |
0 commit comments