|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.hadoop.ozone.local; |
| 19 | + |
| 20 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 23 | + |
| 24 | +import java.io.IOException; |
| 25 | +import java.net.InetSocketAddress; |
| 26 | +import java.net.Socket; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.time.Duration; |
| 29 | +import java.util.UUID; |
| 30 | +import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
| 31 | +import org.apache.hadoop.ozone.client.OzoneBucket; |
| 32 | +import org.apache.hadoop.ozone.client.OzoneClient; |
| 33 | +import org.apache.hadoop.ozone.client.OzoneClientFactory; |
| 34 | +import org.apache.hadoop.ozone.client.OzoneVolume; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | +import org.junit.jupiter.api.io.TempDir; |
| 37 | + |
| 38 | +/** |
| 39 | + * Integration tests for the SCM and OM portion of {@link LocalOzoneCluster}. |
| 40 | + */ |
| 41 | +class TestLocalOzoneClusterRuntime { |
| 42 | + |
| 43 | + @TempDir |
| 44 | + private Path tempDir; |
| 45 | + |
| 46 | + @Test |
| 47 | + void scmAndOmStartAndReuseExistingMetadata() throws Exception { |
| 48 | + String volumeName = uniqueName("vol"); |
| 49 | + String bucketName = uniqueName("bucket"); |
| 50 | + Path dataDir = tempDir.resolve("local-ozone-runtime"); |
| 51 | + LocalOzoneClusterConfig config = LocalOzoneClusterConfig.builder(dataDir) |
| 52 | + .setS3gEnabled(false) |
| 53 | + .setStartupTimeout(Duration.ofMinutes(2)) |
| 54 | + .build(); |
| 55 | + |
| 56 | + startRuntimeAndCreateBucket(config, volumeName, bucketName); |
| 57 | + restartRuntimeAndVerifyBucket(config, volumeName, bucketName); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void formatNeverRejectsUninitializedScmOmStorage() throws Exception { |
| 62 | + Path dataDir = tempDir.resolve("local-ozone-runtime"); |
| 63 | + LocalOzoneClusterConfig initialConfig = |
| 64 | + LocalOzoneClusterConfig.builder(dataDir).build(); |
| 65 | + try (LocalOzoneCluster cluster = |
| 66 | + new LocalOzoneCluster(initialConfig, new OzoneConfiguration())) { |
| 67 | + cluster.prepareConfiguration(); |
| 68 | + } |
| 69 | + |
| 70 | + LocalOzoneClusterConfig neverFormatConfig = |
| 71 | + LocalOzoneClusterConfig.builder(dataDir) |
| 72 | + .setFormatMode(LocalOzoneClusterConfig.FormatMode.NEVER) |
| 73 | + .setS3gEnabled(false) |
| 74 | + .build(); |
| 75 | + |
| 76 | + IOException error = assertThrows(IOException.class, () -> { |
| 77 | + try (LocalOzoneCluster cluster = |
| 78 | + new LocalOzoneCluster(neverFormatConfig, |
| 79 | + new OzoneConfiguration())) { |
| 80 | + cluster.start(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + assertTrue(error.getMessage().contains("storage is not initialized"), |
| 85 | + error.getMessage()); |
| 86 | + } |
| 87 | + |
| 88 | + private void startRuntimeAndCreateBucket(LocalOzoneClusterConfig config, |
| 89 | + String volumeName, String bucketName) throws Exception { |
| 90 | + try (LocalOzoneCluster cluster = |
| 91 | + new LocalOzoneCluster(config, new OzoneConfiguration())) { |
| 92 | + OzoneConfiguration clientConf = |
| 93 | + cluster.prepareConfiguration().getConfiguration(); |
| 94 | + cluster.start(); |
| 95 | + |
| 96 | + assertServicePortsReachable(cluster); |
| 97 | + |
| 98 | + try (OzoneClient client = OzoneClientFactory.getRpcClient(clientConf)) { |
| 99 | + OzoneVolume volume = createVolume(client, volumeName); |
| 100 | + createBucket(volume, bucketName); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private void restartRuntimeAndVerifyBucket(LocalOzoneClusterConfig config, |
| 106 | + String volumeName, String bucketName) throws Exception { |
| 107 | + try (LocalOzoneCluster cluster = |
| 108 | + new LocalOzoneCluster(config, new OzoneConfiguration())) { |
| 109 | + OzoneConfiguration clientConf = |
| 110 | + cluster.prepareConfiguration().getConfiguration(); |
| 111 | + cluster.start(); |
| 112 | + |
| 113 | + assertServicePortsReachable(cluster); |
| 114 | + |
| 115 | + try (OzoneClient client = OzoneClientFactory.getRpcClient(clientConf)) { |
| 116 | + OzoneVolume volume = client.getObjectStore().getVolume(volumeName); |
| 117 | + OzoneBucket bucket = volume.getBucket(bucketName); |
| 118 | + assertEquals(bucketName, bucket.getName()); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static void assertServicePortsReachable(LocalOzoneCluster cluster) |
| 124 | + throws IOException { |
| 125 | + assertTrue(cluster.getScmPort() > 0); |
| 126 | + assertTrue(cluster.getOmPort() > 0); |
| 127 | + assertPortReachable(cluster.getDisplayHost(), cluster.getScmPort()); |
| 128 | + assertPortReachable(cluster.getDisplayHost(), cluster.getOmPort()); |
| 129 | + } |
| 130 | + |
| 131 | + private static void assertPortReachable(String host, int port) |
| 132 | + throws IOException { |
| 133 | + try (Socket socket = new Socket()) { |
| 134 | + socket.connect(new InetSocketAddress(host, port), 1_000); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + private static OzoneVolume createVolume(OzoneClient client, String volumeName) |
| 139 | + throws IOException { |
| 140 | + client.getObjectStore().createVolume(volumeName); |
| 141 | + return client.getObjectStore().getVolume(volumeName); |
| 142 | + } |
| 143 | + |
| 144 | + private static OzoneBucket createBucket(OzoneVolume volume, String bucketName) |
| 145 | + throws IOException { |
| 146 | + volume.createBucket(bucketName); |
| 147 | + return volume.getBucket(bucketName); |
| 148 | + } |
| 149 | + |
| 150 | + private static String uniqueName(String prefix) { |
| 151 | + return prefix + UUID.randomUUID().toString().replace("-", ""); |
| 152 | + } |
| 153 | +} |
0 commit comments