|
| 1 | +/* |
| 2 | + * Copyright 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * SPDX-FileCopyrightText: 2021-present Arcade Data Ltd (info@arcadedata.com) |
| 17 | + * SPDX-License-Identifier: Apache-2.0 |
| 18 | + */ |
| 19 | +package com.arcadedb.test.load; |
| 20 | + |
| 21 | +import com.arcadedb.test.support.ContainersTestTemplate; |
| 22 | +import com.arcadedb.test.support.DatabaseWrapper; |
| 23 | +import com.arcadedb.test.support.ServerWrapper; |
| 24 | +import io.micrometer.core.instrument.Metrics; |
| 25 | +import org.junit.jupiter.api.AfterEach; |
| 26 | +import org.junit.jupiter.api.DisplayName; |
| 27 | +import org.junit.jupiter.params.ParameterizedTest; |
| 28 | +import org.junit.jupiter.params.provider.EnumSource; |
| 29 | + |
| 30 | +import java.time.Duration; |
| 31 | +import java.time.LocalDateTime; |
| 32 | +import java.time.format.DateTimeFormatter; |
| 33 | +import java.util.List; |
| 34 | +import java.util.concurrent.ExecutorService; |
| 35 | +import java.util.concurrent.Executors; |
| 36 | +import java.util.concurrent.TimeUnit; |
| 37 | + |
| 38 | +class ThreeINodesLoadtTestIT extends ContainersTestTemplate { |
| 39 | + |
| 40 | + private static final String SERVER_LIST = "arcadedb-0:2434:2480,arcadedb-1:2434:2480,arcadedb-2:2434:2480"; |
| 41 | + |
| 42 | + @AfterEach |
| 43 | + @Override |
| 44 | + public void tearDown() { |
| 45 | + // Skip compareAllDatabases(): with non-persistent containers, database files are not |
| 46 | + // on the host after stop. The test body already verifies convergence via Awaitility. |
| 47 | + super.tearDown(); |
| 48 | + } |
| 49 | + |
| 50 | + @ParameterizedTest(name = "Three-node Raft HA Load test with {0} protocol") |
| 51 | + @EnumSource(DatabaseWrapper.Protocol.class) |
| 52 | + @DisplayName("Three-node Raft HA: replication across all nodes with consistency check") |
| 53 | + void threeNodeReplication(DatabaseWrapper.Protocol protocol) { |
| 54 | + createArcadeContainer("arcadedb-0", SERVER_LIST, "majority", network); |
| 55 | + createArcadeContainer("arcadedb-1", SERVER_LIST, "majority", network); |
| 56 | + createArcadeContainer("arcadedb-2", SERVER_LIST, "majority", network); |
| 57 | + |
| 58 | + logger.info("Starting all containers"); |
| 59 | + final List<ServerWrapper> servers = startCluster(); |
| 60 | + |
| 61 | + final DatabaseWrapper db1 = new DatabaseWrapper(servers.get(0), idSupplier, wordSupplier); |
| 62 | + final DatabaseWrapper db2 = new DatabaseWrapper(servers.get(1), idSupplier, wordSupplier); |
| 63 | + final DatabaseWrapper db3 = new DatabaseWrapper(servers.get(2), idSupplier, wordSupplier); |
| 64 | + |
| 65 | + logger.info("Creating database and schema"); |
| 66 | + db1.createDatabase(); |
| 67 | + db1.createSchema(); |
| 68 | + |
| 69 | + logger.info("Checking schema replicated to all nodes"); |
| 70 | + db1.checkSchema(); |
| 71 | + db2.checkSchema(); |
| 72 | + db3.checkSchema(); |
| 73 | + |
| 74 | + final int numOfThreads = 3; //number of threads to use to insert users and photos |
| 75 | + final int numOfUsers = 1000; // Each thread will create 200000 users |
| 76 | + final int numOfPhotos = 10; // Each user will have 5 photos |
| 77 | + final int numOfFriendship = 0; // Each thread will create 100000 friendships |
| 78 | + final int numOfLike = 0; // Each thread will create 100000 likes |
| 79 | + |
| 80 | + int expectedUsersCount = numOfUsers * numOfThreads; |
| 81 | + int expectedPhotoCount = expectedUsersCount * numOfPhotos; |
| 82 | + int expectedFriendshipCount = numOfFriendship; |
| 83 | + int expectedLikeCount = numOfLike; |
| 84 | + LocalDateTime startedAt = LocalDateTime.now(); |
| 85 | + logger.info("Starting load test on protocol {}", protocol.name()); |
| 86 | + logger.info("Creating {} users using {} threads", expectedUsersCount, numOfThreads); |
| 87 | + logger.info("Expected users: {} - photos: {} - friendships: {} - likes: {}", expectedUsersCount, expectedPhotoCount, |
| 88 | + expectedFriendshipCount, expectedLikeCount); |
| 89 | + logger.info("Starting at {}", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(startedAt)); |
| 90 | + |
| 91 | + ExecutorService executor = Executors.newFixedThreadPool(10); |
| 92 | + for (int i = 0; i < numOfThreads; i++) { |
| 93 | + // Each thread will create users and photos |
| 94 | + executor.submit(() -> { |
| 95 | + DatabaseWrapper db = new DatabaseWrapper(servers.getFirst(), idSupplier, wordSupplier, protocol); |
| 96 | + db.addUserAndPhotos(numOfUsers, numOfPhotos); |
| 97 | + db.close(); |
| 98 | + }); |
| 99 | + } |
| 100 | + // Each thread will create friendships |
| 101 | + executor.submit(() -> { |
| 102 | + DatabaseWrapper db = new DatabaseWrapper(servers.getFirst(), idSupplier, wordSupplier, protocol); |
| 103 | + db.createFriendships(numOfFriendship); |
| 104 | + db.close(); |
| 105 | + }); |
| 106 | + // Each thread will create friendships |
| 107 | + executor.submit(() -> { |
| 108 | + DatabaseWrapper db = new DatabaseWrapper(servers.getFirst(), idSupplier, wordSupplier, protocol); |
| 109 | + db.createLike(numOfLike); |
| 110 | + db.close(); |
| 111 | + }); |
| 112 | + |
| 113 | + executor.shutdown(); |
| 114 | + |
| 115 | + while (!executor.isTerminated()) { |
| 116 | + try { |
| 117 | + final long users1 = db1.countUsers(); |
| 118 | + final long photos1 = db1.countPhotos(); |
| 119 | + final long users2 = db2.countUsers(); |
| 120 | + final long photos2 = db2.countPhotos(); |
| 121 | + final long users3 = db3.countUsers(); |
| 122 | + final long photos3 = db3.countPhotos(); |
| 123 | + logger.info("Users: {} / {} / {} | Photos: {} / {} / {}", users1, users2, users3, photos1, photos2, photos3); |
| 124 | + |
| 125 | + } catch (Exception e) { |
| 126 | + logger.error(e.getMessage(), e); |
| 127 | + } |
| 128 | + try { |
| 129 | + // Wait for 2 seconds before checking again |
| 130 | + TimeUnit.SECONDS.sleep(5); |
| 131 | + } catch (InterruptedException e) { |
| 132 | + Thread.currentThread().interrupt(); |
| 133 | + } |
| 134 | + } |
| 135 | + LocalDateTime finishedAt = LocalDateTime.now(); |
| 136 | + logger.info("Finishing at {}", DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(finishedAt)); |
| 137 | + logger.info("Total time: {} minutes", Duration.between(startedAt, finishedAt).toMinutes()); |
| 138 | + |
| 139 | + Metrics.globalRegistry.getMeters().forEach(meter -> { |
| 140 | + logger.info("Meter: {} - {}", meter.getId().getName(), meter.measure()); |
| 141 | + }); |
| 142 | + |
| 143 | + db1.assertThatUserCountIs(expectedUsersCount); |
| 144 | + db1.assertThatPhotoCountIs(expectedPhotoCount); |
| 145 | + db1.assertThatFriendshipCountIs(expectedFriendshipCount); |
| 146 | + db1.assertThatLikesCountIs(expectedLikeCount); |
| 147 | + |
| 148 | + db2.assertThatUserCountIs(expectedUsersCount); |
| 149 | + db2.assertThatPhotoCountIs(expectedPhotoCount); |
| 150 | + db2.assertThatFriendshipCountIs(expectedFriendshipCount); |
| 151 | + db2.assertThatLikesCountIs(expectedLikeCount); |
| 152 | + |
| 153 | + db3.assertThatUserCountIs(expectedUsersCount); |
| 154 | + db3.assertThatPhotoCountIs(expectedPhotoCount); |
| 155 | + db3.assertThatFriendshipCountIs(expectedFriendshipCount); |
| 156 | + db3.assertThatLikesCountIs(expectedLikeCount); |
| 157 | + |
| 158 | + db1.close(); |
| 159 | + db2.close(); |
| 160 | + db3.close(); |
| 161 | + } |
| 162 | +} |
0 commit comments