|
| 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 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.confignode.manager.pipe.coordinator.runtime.heartbeat; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.conf.CommonDescriptor; |
| 23 | +import org.apache.iotdb.confignode.manager.ConfigManager; |
| 24 | +import org.apache.iotdb.confignode.manager.ProcedureManager; |
| 25 | +import org.apache.iotdb.confignode.manager.node.NodeManager; |
| 26 | +import org.apache.iotdb.confignode.manager.pipe.coordinator.PipeManager; |
| 27 | +import org.apache.iotdb.confignode.manager.pipe.coordinator.runtime.PipeRuntimeCoordinator; |
| 28 | +import org.apache.iotdb.confignode.manager.pipe.coordinator.task.PipeTaskCoordinator; |
| 29 | +import org.apache.iotdb.confignode.persistence.pipe.PipeTaskInfo; |
| 30 | + |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Before; |
| 33 | +import org.junit.Test; |
| 34 | +import org.mockito.Mockito; |
| 35 | + |
| 36 | +import java.lang.reflect.Field; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.concurrent.CompletableFuture; |
| 39 | +import java.util.concurrent.ExecutorService; |
| 40 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 41 | +import java.util.concurrent.atomic.AtomicReference; |
| 42 | + |
| 43 | +import static org.mockito.ArgumentMatchers.any; |
| 44 | +import static org.mockito.ArgumentMatchers.anyBoolean; |
| 45 | +import static org.mockito.Mockito.never; |
| 46 | +import static org.mockito.Mockito.times; |
| 47 | +import static org.mockito.Mockito.verify; |
| 48 | +import static org.mockito.Mockito.when; |
| 49 | + |
| 50 | +public class PipeHeartbeatParserTest { |
| 51 | + |
| 52 | + private boolean originalSeparatedPipeHeartbeatEnabled; |
| 53 | + |
| 54 | + @Before |
| 55 | + public void setUp() { |
| 56 | + originalSeparatedPipeHeartbeatEnabled = |
| 57 | + CommonDescriptor.getInstance().getConfig().isSeperatedPipeHeartbeatEnabled(); |
| 58 | + } |
| 59 | + |
| 60 | + @After |
| 61 | + public void tearDown() { |
| 62 | + CommonDescriptor.getInstance() |
| 63 | + .getConfig() |
| 64 | + .setSeperatedPipeHeartbeatEnabled(originalSeparatedPipeHeartbeatEnabled); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void testParseHeartbeatCountsOnlyDataNodesWhenSeparatedHeartbeatDisabled() |
| 69 | + throws Exception { |
| 70 | + CommonDescriptor.getInstance().getConfig().setSeperatedPipeHeartbeatEnabled(false); |
| 71 | + |
| 72 | + final ParserTestContext context = createParserTestContext(2); |
| 73 | + setMetaChangeFlags(context.parser, true, false); |
| 74 | + |
| 75 | + context.parser.parseHeartbeat(1, emptyHeartbeat()); |
| 76 | + verify(context.procedureManager, never()).pipeHandleMetaChange(anyBoolean(), anyBoolean()); |
| 77 | + |
| 78 | + context.parser.parseHeartbeat(2, emptyHeartbeat()); |
| 79 | + verify(context.procedureManager, times(1)).pipeHandleMetaChange(true, false); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testParseHeartbeatCountsLocalConfigNodeWhenSeparatedHeartbeatEnabled() |
| 84 | + throws Exception { |
| 85 | + CommonDescriptor.getInstance().getConfig().setSeperatedPipeHeartbeatEnabled(true); |
| 86 | + |
| 87 | + final ParserTestContext context = createParserTestContext(2); |
| 88 | + setMetaChangeFlags(context.parser, true, false); |
| 89 | + |
| 90 | + context.parser.parseHeartbeat(1, emptyHeartbeat()); |
| 91 | + context.parser.parseHeartbeat(2, emptyHeartbeat()); |
| 92 | + verify(context.procedureManager, never()).pipeHandleMetaChange(anyBoolean(), anyBoolean()); |
| 93 | + |
| 94 | + context.parser.parseHeartbeat(3, emptyHeartbeat()); |
| 95 | + verify(context.procedureManager, times(1)).pipeHandleMetaChange(true, false); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void testParseHeartbeatKeepsPendingFlagsWhenProcedureSubmissionFails() throws Exception { |
| 100 | + CommonDescriptor.getInstance().getConfig().setSeperatedPipeHeartbeatEnabled(false); |
| 101 | + |
| 102 | + final ParserTestContext context = createParserTestContext(2); |
| 103 | + when(context.procedureManager.pipeHandleMetaChange(anyBoolean(), anyBoolean())) |
| 104 | + .thenReturn(false, true); |
| 105 | + setMetaChangeFlags(context.parser, true, false); |
| 106 | + |
| 107 | + context.parser.parseHeartbeat(1, emptyHeartbeat()); |
| 108 | + verify(context.procedureManager, never()).pipeHandleMetaChange(anyBoolean(), anyBoolean()); |
| 109 | + |
| 110 | + context.parser.parseHeartbeat(2, emptyHeartbeat()); |
| 111 | + verify(context.procedureManager, times(1)).pipeHandleMetaChange(true, false); |
| 112 | + |
| 113 | + context.parser.parseHeartbeat(3, emptyHeartbeat()); |
| 114 | + verify(context.procedureManager, times(1)).pipeHandleMetaChange(true, false); |
| 115 | + |
| 116 | + context.parser.parseHeartbeat(4, emptyHeartbeat()); |
| 117 | + verify(context.procedureManager, times(2)).pipeHandleMetaChange(true, false); |
| 118 | + } |
| 119 | + |
| 120 | + private ParserTestContext createParserTestContext(final int registeredDataNodeCount) { |
| 121 | + final ConfigManager configManager = Mockito.mock(ConfigManager.class); |
| 122 | + final NodeManager nodeManager = Mockito.mock(NodeManager.class); |
| 123 | + final ProcedureManager procedureManager = Mockito.mock(ProcedureManager.class); |
| 124 | + final PipeManager pipeManager = Mockito.mock(PipeManager.class); |
| 125 | + final PipeRuntimeCoordinator pipeRuntimeCoordinator = |
| 126 | + Mockito.mock(PipeRuntimeCoordinator.class); |
| 127 | + final PipeTaskCoordinator pipeTaskCoordinator = Mockito.mock(PipeTaskCoordinator.class); |
| 128 | + final ExecutorService procedureSubmitter = Mockito.mock(ExecutorService.class); |
| 129 | + |
| 130 | + when(configManager.getNodeManager()).thenReturn(nodeManager); |
| 131 | + when(configManager.getProcedureManager()).thenReturn(procedureManager); |
| 132 | + when(configManager.getPipeManager()).thenReturn(pipeManager); |
| 133 | + when(nodeManager.getRegisteredDataNodeCount()).thenReturn(registeredDataNodeCount); |
| 134 | + when(pipeManager.getPipeRuntimeCoordinator()).thenReturn(pipeRuntimeCoordinator); |
| 135 | + when(pipeManager.getPipeTaskCoordinator()).thenReturn(pipeTaskCoordinator); |
| 136 | + when(pipeRuntimeCoordinator.getProcedureSubmitter()).thenReturn(procedureSubmitter); |
| 137 | + when(pipeTaskCoordinator.tryLock()).thenReturn(new AtomicReference<>(new PipeTaskInfo())); |
| 138 | + when(procedureManager.pipeHandleMetaChange(anyBoolean(), anyBoolean())).thenReturn(true); |
| 139 | + Mockito.doAnswer( |
| 140 | + invocation -> { |
| 141 | + ((Runnable) invocation.getArgument(0)).run(); |
| 142 | + return CompletableFuture.completedFuture(null); |
| 143 | + }) |
| 144 | + .when(procedureSubmitter) |
| 145 | + .submit(any(Runnable.class)); |
| 146 | + |
| 147 | + return new ParserTestContext(new PipeHeartbeatParser(configManager), procedureManager); |
| 148 | + } |
| 149 | + |
| 150 | + private void setMetaChangeFlags( |
| 151 | + final PipeHeartbeatParser parser, |
| 152 | + final boolean needWriteConsensusOnConfigNodes, |
| 153 | + final boolean needPushPipeMetaToDataNodes) |
| 154 | + throws Exception { |
| 155 | + setAtomicBooleanField( |
| 156 | + parser, "needWriteConsensusOnConfigNodes", needWriteConsensusOnConfigNodes); |
| 157 | + setAtomicBooleanField(parser, "needPushPipeMetaToDataNodes", needPushPipeMetaToDataNodes); |
| 158 | + } |
| 159 | + |
| 160 | + private void setAtomicBooleanField( |
| 161 | + final PipeHeartbeatParser parser, final String fieldName, final boolean value) |
| 162 | + throws Exception { |
| 163 | + final Field field = PipeHeartbeatParser.class.getDeclaredField(fieldName); |
| 164 | + field.setAccessible(true); |
| 165 | + ((AtomicBoolean) field.get(parser)).set(value); |
| 166 | + } |
| 167 | + |
| 168 | + private PipeHeartbeat emptyHeartbeat() { |
| 169 | + return new PipeHeartbeat(Collections.emptyList(), null, null, null); |
| 170 | + } |
| 171 | + |
| 172 | + private static class ParserTestContext { |
| 173 | + private final PipeHeartbeatParser parser; |
| 174 | + private final ProcedureManager procedureManager; |
| 175 | + |
| 176 | + private ParserTestContext( |
| 177 | + final PipeHeartbeatParser parser, final ProcedureManager procedureManager) { |
| 178 | + this.parser = parser; |
| 179 | + this.procedureManager = procedureManager; |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments