|
| 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.kafka.metadata; |
| 19 | + |
| 20 | +import org.apache.kafka.common.metadata.FeatureLevelRecord; |
| 21 | +import org.apache.kafka.image.MetadataDelta; |
| 22 | +import org.apache.kafka.image.MetadataImage; |
| 23 | +import org.apache.kafka.image.MetadataProvenance; |
| 24 | +import org.apache.kafka.image.loader.LogDeltaManifest; |
| 25 | +import org.apache.kafka.raft.LeaderAndEpoch; |
| 26 | +import org.apache.kafka.server.common.MetadataVersion; |
| 27 | +import org.apache.kafka.server.fault.FaultHandler; |
| 28 | + |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +import java.util.function.Supplier; |
| 32 | + |
| 33 | +import static org.mockito.ArgumentMatchers.any; |
| 34 | +import static org.mockito.ArgumentMatchers.eq; |
| 35 | +import static org.mockito.Mockito.mock; |
| 36 | +import static org.mockito.Mockito.times; |
| 37 | +import static org.mockito.Mockito.verify; |
| 38 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +@SuppressWarnings({"unchecked", "ThrowableNotThrown"}) |
| 42 | +public class MetadataVersionConfigValidatorTest { |
| 43 | + |
| 44 | + private static final LogDeltaManifest TEST_MANIFEST = LogDeltaManifest.newBuilder() |
| 45 | + .provenance(MetadataProvenance.EMPTY) |
| 46 | + .leaderAndEpoch(LeaderAndEpoch.UNKNOWN) |
| 47 | + .numBatches(1) |
| 48 | + .elapsedNs(90) |
| 49 | + .numBytes(88) |
| 50 | + .build(); |
| 51 | + public static final MetadataProvenance TEST_PROVENANCE = |
| 52 | + new MetadataProvenance(50, 3, 8000, true); |
| 53 | + |
| 54 | + void executeMetadataUpdate( |
| 55 | + MetadataVersion metadataVersion, |
| 56 | + Supplier<Boolean> multiLogDirSupplier, |
| 57 | + FaultHandler faultHandler |
| 58 | + ) throws Exception { |
| 59 | + try (MetadataVersionConfigValidator validator = new MetadataVersionConfigValidator(0, multiLogDirSupplier, faultHandler)) { |
| 60 | + MetadataDelta delta = new MetadataDelta.Builder() |
| 61 | + .setImage(MetadataImage.EMPTY) |
| 62 | + .build(); |
| 63 | + if (metadataVersion != null) { |
| 64 | + delta.replay(new FeatureLevelRecord(). |
| 65 | + setName(MetadataVersion.FEATURE_NAME). |
| 66 | + setFeatureLevel(metadataVersion.featureLevel())); |
| 67 | + } |
| 68 | + MetadataImage image = delta.apply(TEST_PROVENANCE); |
| 69 | + |
| 70 | + validator.onMetadataUpdate(delta, image, TEST_MANIFEST); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void testValidatesConfigOnMetadataChange() throws Exception { |
| 76 | + MetadataVersion metadataVersion = MetadataVersion.IBP_3_7_IV2; |
| 77 | + FaultHandler faultHandler = mock(FaultHandler.class); |
| 78 | + Supplier<Boolean> multiLogDirSupplier = mock(Supplier.class); |
| 79 | + when(multiLogDirSupplier.get()).thenReturn(false); |
| 80 | + |
| 81 | + executeMetadataUpdate(metadataVersion, multiLogDirSupplier, faultHandler); |
| 82 | + |
| 83 | + verify(multiLogDirSupplier, times(1)).get(); |
| 84 | + verifyNoMoreInteractions(faultHandler); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void testInvokesFaultHandlerOnException() throws Exception { |
| 89 | + MetadataVersion metadataVersion = MetadataVersion.IBP_3_7_IV1; |
| 90 | + Supplier<Boolean> multiLogDirSupplier = mock(Supplier.class); |
| 91 | + FaultHandler faultHandler = mock(FaultHandler.class); |
| 92 | + |
| 93 | + when(multiLogDirSupplier.get()).thenReturn(true); |
| 94 | + |
| 95 | + executeMetadataUpdate(metadataVersion, multiLogDirSupplier, faultHandler); |
| 96 | + |
| 97 | + verify(multiLogDirSupplier, times(1)).get(); |
| 98 | + verify(faultHandler, times(1)).handleFault( |
| 99 | + eq("Broker configuration does not support the cluster MetadataVersion"), |
| 100 | + any(IllegalArgumentException.class)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void testValidateWithMetadataVersionJbodSupport() throws Exception { |
| 105 | + FaultHandler faultHandler = mock(FaultHandler.class); |
| 106 | + validate(MetadataVersion.IBP_3_6_IV2, false, faultHandler); |
| 107 | + verifyNoMoreInteractions(faultHandler); |
| 108 | + |
| 109 | + faultHandler = mock(FaultHandler.class); |
| 110 | + validate(MetadataVersion.IBP_3_7_IV0, false, faultHandler); |
| 111 | + verifyNoMoreInteractions(faultHandler); |
| 112 | + |
| 113 | + faultHandler = mock(FaultHandler.class); |
| 114 | + validate(MetadataVersion.IBP_3_7_IV2, false, faultHandler); |
| 115 | + verifyNoMoreInteractions(faultHandler); |
| 116 | + |
| 117 | + faultHandler = mock(FaultHandler.class); |
| 118 | + validate(MetadataVersion.IBP_3_6_IV2, true, faultHandler); |
| 119 | + verify(faultHandler, times(1)).handleFault( |
| 120 | + eq("Broker configuration does not support the cluster MetadataVersion"), |
| 121 | + any(IllegalArgumentException.class)); |
| 122 | + |
| 123 | + faultHandler = mock(FaultHandler.class); |
| 124 | + validate(MetadataVersion.IBP_3_7_IV0, true, faultHandler); |
| 125 | + verify(faultHandler, times(1)).handleFault( |
| 126 | + eq("Broker configuration does not support the cluster MetadataVersion"), |
| 127 | + any(IllegalArgumentException.class)); |
| 128 | + |
| 129 | + faultHandler = mock(FaultHandler.class); |
| 130 | + validate(MetadataVersion.IBP_3_7_IV2, true, faultHandler); |
| 131 | + verifyNoMoreInteractions(faultHandler); |
| 132 | + } |
| 133 | + |
| 134 | + private void validate(MetadataVersion metadataVersion, boolean jbodConfig, FaultHandler faultHandler) |
| 135 | + throws Exception { |
| 136 | + Supplier<Boolean> multiLogDirSupplier = () -> jbodConfig; |
| 137 | + |
| 138 | + executeMetadataUpdate(metadataVersion, multiLogDirSupplier, faultHandler); |
| 139 | + } |
| 140 | +} |
0 commit comments