|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.ozone.om.ratis; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.UUID; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | + |
| 25 | +import org.junit.After; |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.rules.TemporaryFolder; |
| 31 | + |
| 32 | + |
| 33 | +import org.apache.hadoop.hdds.conf.OzoneConfiguration; |
| 34 | +import org.apache.hadoop.ozone.om.OMMetadataManager; |
| 35 | +import org.apache.hadoop.ozone.om.OmMetadataManagerImpl; |
| 36 | +import org.apache.hadoop.ozone.om.helpers.OmBucketInfo; |
| 37 | +import org.apache.hadoop.ozone.om.response.OMClientResponse; |
| 38 | +import org.apache.hadoop.test.GenericTestUtils; |
| 39 | +import org.apache.hadoop.util.Time; |
| 40 | +import org.apache.hadoop.utils.db.BatchOperation; |
| 41 | + |
| 42 | +import static org.apache.hadoop.hdds.HddsConfigKeys.OZONE_METADATA_DIRS; |
| 43 | + |
| 44 | +/** |
| 45 | + * This class tests OzoneManagerDoubleBuffer implementation with |
| 46 | + * dummy response class. |
| 47 | + */ |
| 48 | +public class TestOzoneManagerDoubleBufferWithDummyResponse { |
| 49 | + |
| 50 | + private OMMetadataManager omMetadataManager; |
| 51 | + private OzoneManagerDoubleBuffer doubleBuffer; |
| 52 | + private AtomicLong trxId = new AtomicLong(0); |
| 53 | + |
| 54 | + @Rule |
| 55 | + public TemporaryFolder folder = new TemporaryFolder(); |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setup() throws IOException { |
| 59 | + OzoneConfiguration configuration = new OzoneConfiguration(); |
| 60 | + configuration.set(OZONE_METADATA_DIRS, |
| 61 | + folder.newFolder().getAbsolutePath()); |
| 62 | + omMetadataManager = |
| 63 | + new OmMetadataManagerImpl(configuration); |
| 64 | + doubleBuffer = new OzoneManagerDoubleBuffer(omMetadataManager); |
| 65 | + } |
| 66 | + |
| 67 | + @After |
| 68 | + public void stop() { |
| 69 | + doubleBuffer.stop(); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * This tests add's 100 bucket creation responses to doubleBuffer, and |
| 74 | + * check OM DB bucket table has 100 entries or not. In addition checks |
| 75 | + * flushed transaction count is matching with expected count or not. |
| 76 | + * @throws Exception |
| 77 | + */ |
| 78 | + @Test(timeout = 300_000) |
| 79 | + public void testDoubleBufferWithDummyResponse() throws Exception { |
| 80 | + String volumeName = UUID.randomUUID().toString(); |
| 81 | + int bucketCount = 100; |
| 82 | + for (int i=0; i < bucketCount; i++) { |
| 83 | + doubleBuffer.add(createDummyBucketResponse(volumeName, |
| 84 | + UUID.randomUUID().toString()), trxId.incrementAndGet()); |
| 85 | + } |
| 86 | + GenericTestUtils.waitFor(() -> |
| 87 | + doubleBuffer.getFlushedTransactionCount() == bucketCount, 100, |
| 88 | + 60000); |
| 89 | + Assert.assertTrue(omMetadataManager.countRowsInTable( |
| 90 | + omMetadataManager.getBucketTable()) == (bucketCount)); |
| 91 | + Assert.assertTrue(doubleBuffer.getFlushIterations() > 0); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Create DummyBucketCreate response. |
| 96 | + * @param volumeName |
| 97 | + * @param bucketName |
| 98 | + * @return OMDummyCreateBucketResponse |
| 99 | + */ |
| 100 | + private OMDummyCreateBucketResponse createDummyBucketResponse( |
| 101 | + String volumeName, String bucketName) { |
| 102 | + OmBucketInfo omBucketInfo = |
| 103 | + OmBucketInfo.newBuilder().setVolumeName(volumeName) |
| 104 | + .setBucketName(bucketName).setCreationTime(Time.now()).build(); |
| 105 | + return new OMDummyCreateBucketResponse(omBucketInfo); |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + /** |
| 110 | + * DummyCreatedBucket Response class used in testing. |
| 111 | + */ |
| 112 | + public static class OMDummyCreateBucketResponse implements OMClientResponse { |
| 113 | + private final OmBucketInfo omBucketInfo; |
| 114 | + |
| 115 | + public OMDummyCreateBucketResponse(OmBucketInfo omBucketInfo) { |
| 116 | + this.omBucketInfo = omBucketInfo; |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void addToDBBatch(OMMetadataManager omMetadataManager, |
| 121 | + BatchOperation batchOperation) throws IOException { |
| 122 | + String dbBucketKey = |
| 123 | + omMetadataManager.getBucketKey(omBucketInfo.getVolumeName(), |
| 124 | + omBucketInfo.getBucketName()); |
| 125 | + omMetadataManager.getBucketTable().putWithBatch(batchOperation, |
| 126 | + dbBucketKey, omBucketInfo); |
| 127 | + } |
| 128 | + |
| 129 | + } |
| 130 | +} |
0 commit comments