|
| 1 | +package io.weaviate.integration.client.cluster; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | +import java.util.concurrent.ExecutionException; |
| 8 | +import java.util.concurrent.TimeUnit; |
| 9 | +import java.util.concurrent.TimeoutException; |
| 10 | +import java.util.function.Supplier; |
| 11 | + |
| 12 | +import org.assertj.core.api.Assertions; |
| 13 | +import org.assertj.core.util.Arrays; |
| 14 | +import org.junit.After; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.ClassRule; |
| 17 | +import org.junit.Test; |
| 18 | + |
| 19 | +import io.weaviate.client.Config; |
| 20 | +import io.weaviate.client.WeaviateClient; |
| 21 | +import io.weaviate.client.base.Result; |
| 22 | +import io.weaviate.client.v1.cluster.api.replication.Replication; |
| 23 | +import io.weaviate.client.v1.cluster.api.replication.model.ReplicateOperation; |
| 24 | +import io.weaviate.client.v1.cluster.api.replication.model.ReplicateOperationState; |
| 25 | +import io.weaviate.client.v1.cluster.model.NodeStatusOutput; |
| 26 | +import io.weaviate.client.v1.cluster.model.NodesStatusResponse; |
| 27 | +import io.weaviate.client.v1.cluster.model.ReplicationType; |
| 28 | +import io.weaviate.client.v1.cluster.model.ShardReplicas; |
| 29 | +import io.weaviate.client.v1.cluster.model.ShardingState; |
| 30 | +import io.weaviate.client.v1.schema.model.WeaviateClass; |
| 31 | +import io.weaviate.integration.client.WeaviateDockerComposeCluster; |
| 32 | + |
| 33 | +public class ClientReplicateTest { |
| 34 | + @ClassRule |
| 35 | + public static WeaviateDockerComposeCluster cluster = new WeaviateDockerComposeCluster(); |
| 36 | + |
| 37 | + private static WeaviateClient client; |
| 38 | + |
| 39 | + @Before |
| 40 | + public void before() { |
| 41 | + Config config = new Config("http", cluster.getHttpHost0Address()); |
| 42 | + client = new WeaviateClient(config); |
| 43 | + } |
| 44 | + |
| 45 | + private static final String CLASSNAME = "ShardDweller"; |
| 46 | + |
| 47 | + @After |
| 48 | + public void afterEach() { |
| 49 | + client.schema().classDeleter().withClassName(CLASSNAME).run(); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void testQueryShardingState() { |
| 54 | + // Arrange |
| 55 | + Boolean created = client.schema().classCreator() |
| 56 | + .withClass(WeaviateClass.builder().className(CLASSNAME).build()) |
| 57 | + .run().getResult(); |
| 58 | + assumeTrue(created, "created test collection"); |
| 59 | + |
| 60 | + NodesStatusResponse nodes = client.cluster().nodesStatusGetter() |
| 61 | + .withClassName(CLASSNAME) |
| 62 | + .withOutput(NodeStatusOutput.VERBOSE) |
| 63 | + .run().getResult(); |
| 64 | + |
| 65 | + assumeTrue(nodes != null, "nodes status result is not null"); |
| 66 | + assumeTrue(!Arrays.isArrayEmpty(nodes.getNodes()), "there're 1+ nodes in the cluster"); |
| 67 | + String wantShard = nodes.getNodes()[0].getShards()[0].getName(); |
| 68 | + |
| 69 | + ShardingState shardingState; |
| 70 | + |
| 71 | + // Act: query by collection name |
| 72 | + shardingState = client.cluster().shardingStateQuerier() |
| 73 | + .withClassName(CLASSNAME) |
| 74 | + .run().getResult(); |
| 75 | + Assertions.assertThat(shardingState.getShards()) |
| 76 | + .as("shard present in the sharding state output (by collection)") |
| 77 | + .extracting(ShardReplicas::getName).contains(wantShard); |
| 78 | + |
| 79 | + // Act: query by collection + shard name |
| 80 | + shardingState = client.cluster().shardingStateQuerier() |
| 81 | + .withClassName(CLASSNAME) |
| 82 | + .withShard(wantShard) |
| 83 | + .run().getResult(); |
| 84 | + Assertions.assertThat(shardingState.getShards()) |
| 85 | + .as("shard present in the sharding state output (by collection+shard)") |
| 86 | + .extracting(ShardReplicas::getName).contains(wantShard); |
| 87 | + |
| 88 | + ShardingState inexistent; |
| 89 | + // Act: query inexistent |
| 90 | + inexistent = client.cluster().shardingStateQuerier() |
| 91 | + .withClassName("Unknown") |
| 92 | + .run().getResult(); |
| 93 | + Assertions.assertThat(inexistent).isNull(); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + /** |
| 98 | + * This test starts a replication operation between two nodes, |
| 99 | + * queries for its status, then cancels the replication and eventually deletes |
| 100 | + * it. |
| 101 | + * |
| 102 | + * Note that assertions that use {@link #eventually} helper may be flaky. |
| 103 | + */ |
| 104 | + public void testReplicateLifecycle() { |
| 105 | + // Arrange |
| 106 | + Boolean created = client.schema().classCreator() |
| 107 | + .withClass(WeaviateClass.builder().className(CLASSNAME).build()) |
| 108 | + .run().getResult(); |
| 109 | + assumeTrue(created, "created test collection"); |
| 110 | + |
| 111 | + NodesStatusResponse nodes = client.cluster().nodesStatusGetter() |
| 112 | + .withClassName(CLASSNAME) |
| 113 | + .withOutput(NodeStatusOutput.VERBOSE) |
| 114 | + .run().getResult(); |
| 115 | + |
| 116 | + assumeTrue(nodes != null, "nodes status result is not null"); |
| 117 | + assumeTrue(nodes.getNodes().length >= 2, "there're 2+ nodes in the cluster"); |
| 118 | + |
| 119 | + String srcNode = nodes.getNodes()[0].getName(); |
| 120 | + String tgtNode = nodes.getNodes()[1].getName(); |
| 121 | + String wantShard = nodes.getNodes()[0].getShards()[0].getName(); |
| 122 | + |
| 123 | + deleteAllReplications(5); |
| 124 | + |
| 125 | + // Act: kick-off replication |
| 126 | + String uuid = client.cluster().replicator() |
| 127 | + .withClassName(CLASSNAME) |
| 128 | + .withShard(wantShard) |
| 129 | + .withSourceNode(srcNode) |
| 130 | + .withTargetNode(tgtNode) |
| 131 | + .run().getResult(); |
| 132 | + assumeTrue(uuid != null, "replication started with valid uuid"); |
| 133 | + |
| 134 | + // Act: get status |
| 135 | + ReplicateOperation status_1 = client.cluster().replication().getter() |
| 136 | + .withUuid(uuid).run().getResult(); |
| 137 | + |
| 138 | + Assertions.assertThat(status_1).isNotNull() |
| 139 | + .as("expected replication status") |
| 140 | + .returns(CLASSNAME, ReplicateOperation::getClassName) |
| 141 | + .returns(wantShard, ReplicateOperation::getShard) |
| 142 | + .returns(srcNode, ReplicateOperation::getSourceNode) |
| 143 | + .returns(tgtNode, ReplicateOperation::getTargetNode) |
| 144 | + .returns(ReplicationType.COPY, ReplicateOperation::getTransferType) |
| 145 | + .returns(null, ReplicateOperation::getStatusHistory) |
| 146 | + .extracting(ReplicateOperation::getStatus).isNotNull(); |
| 147 | + |
| 148 | + // Act: get status with history |
| 149 | + ReplicateOperation status_2 = client.cluster().replication().getter() |
| 150 | + .withUuid(uuid).includeHistory(true) |
| 151 | + .run().getResult(); |
| 152 | + |
| 153 | + Assertions.assertThat(status_2).isNotNull() |
| 154 | + .as("includes replication status history") |
| 155 | + .extracting(ReplicateOperation::getStatusHistory).isNotNull(); |
| 156 | + |
| 157 | + // Act: query status |
| 158 | + List<ReplicateOperation> operations = client.cluster().replication().querier() |
| 159 | + .withClassName(CLASSNAME).withShard(wantShard).withTargetNode(tgtNode) |
| 160 | + .run().getResult(); |
| 161 | + |
| 162 | + Assertions.assertThat(operations).as("no. replications").hasSize(1); |
| 163 | + |
| 164 | + // Act: cancel |
| 165 | + Result<Boolean> cancel = client.cluster().replication().canceler().withUuid(uuid).run(); |
| 166 | + Assertions.assertThat(cancel).as("cancel error").returns(null, Result::getError); |
| 167 | + |
| 168 | + eventually(() -> client.cluster().replication().getter().withUuid(uuid).run().getResult() |
| 169 | + .getStatus().getState() == ReplicateOperationState.CANCELLED, |
| 170 | + 25, "replication was not cancelled"); |
| 171 | + |
| 172 | + // Act: delete |
| 173 | + Result<Boolean> delete = client.cluster().replication().deleter().withUuid(uuid).run(); |
| 174 | + Assertions.assertThat(delete).as("delete error").returns(null, Result::getError); |
| 175 | + |
| 176 | + eventually(() -> client.cluster().replication().allGetter().run().getResult().isEmpty(), |
| 177 | + 15, "replication was not deleted"); |
| 178 | + } |
| 179 | + |
| 180 | + private static void deleteAllReplications(int timeoutSeconds) { |
| 181 | + Replication replication = client.cluster().replication(); |
| 182 | + replication.allDeleter().run(); |
| 183 | + eventually(() -> replication.allGetter().run().getResult().isEmpty(), |
| 184 | + timeoutSeconds, |
| 185 | + "did not delete existing replications"); |
| 186 | + } |
| 187 | + |
| 188 | + private static void eventually(Supplier<Boolean> cond, int timeoutSeconds, String... message) { |
| 189 | + CompletableFuture<?> check = CompletableFuture.runAsync(() -> { |
| 190 | + while (!Thread.currentThread().isInterrupted() && !cond.get()) { |
| 191 | + try { |
| 192 | + Thread.sleep(500); |
| 193 | + } catch (InterruptedException ex) { |
| 194 | + Thread.currentThread().interrupt(); |
| 195 | + } |
| 196 | + } |
| 197 | + }); |
| 198 | + |
| 199 | + try { |
| 200 | + check.get(timeoutSeconds, TimeUnit.SECONDS); |
| 201 | + } catch (TimeoutException ex) { |
| 202 | + check.cancel(true); |
| 203 | + Assertions.fail(message.length >= 0 ? message[0] : null, ex); |
| 204 | + } catch (InterruptedException ex) { |
| 205 | + Thread.currentThread().interrupt(); |
| 206 | + Assertions.fail(ex); |
| 207 | + } catch (ExecutionException ex) { |
| 208 | + throw new RuntimeException(ex); |
| 209 | + } |
| 210 | + } |
| 211 | +} |
0 commit comments